01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: */
16:
17: package org.apache.catalina.tribes.group.interceptors;
18:
19: import java.io.ByteArrayInputStream;
20: import java.io.ByteArrayOutputStream;
21: import java.io.IOException;
22: import java.util.Arrays;
23: import java.util.zip.GZIPInputStream;
24: import java.util.zip.GZIPOutputStream;
25:
26: import org.apache.catalina.tribes.ChannelException;
27: import org.apache.catalina.tribes.ChannelMessage;
28: import org.apache.catalina.tribes.Member;
29: import org.apache.catalina.tribes.group.ChannelInterceptorBase;
30: import org.apache.catalina.tribes.group.InterceptorPayload;
31:
32: /**
33: *
34: *
35: * @author Filip Hanik
36: * @version 1.0
37: */
38: public class GzipInterceptor extends ChannelInterceptorBase {
39: public static final int DEFAULT_BUFFER_SIZE = 2048;
40:
41: public void sendMessage(Member[] destination, ChannelMessage msg,
42: InterceptorPayload payload) throws ChannelException {
43: try {
44: byte[] data = compress(msg.getMessage().getBytes());
45: msg.getMessage().trim(msg.getMessage().getLength());
46: msg.getMessage().append(data, 0, data.length);
47: getNext().sendMessage(destination, msg, payload);
48: } catch (IOException x) {
49: log.error("Unable to compress byte contents");
50: throw new ChannelException(x);
51: }
52: }
53:
54: public void messageReceived(ChannelMessage msg) {
55: try {
56: byte[] data = decompress(msg.getMessage().getBytes());
57: msg.getMessage().trim(msg.getMessage().getLength());
58: msg.getMessage().append(data, 0, data.length);
59: getPrevious().messageReceived(msg);
60: } catch (IOException x) {
61: log.error("Unable to decompress byte contents", x);
62: }
63: }
64:
65: public static byte[] compress(byte[] data) throws IOException {
66: ByteArrayOutputStream bout = new ByteArrayOutputStream();
67: GZIPOutputStream gout = new GZIPOutputStream(bout);
68: gout.write(data);
69: gout.flush();
70: gout.close();
71: return bout.toByteArray();
72: }
73:
74: /**
75: * @todo Fix to create an automatically growing buffer.
76: * @param data byte[]
77: * @return byte[]
78: * @throws IOException
79: */
80: public static byte[] decompress(byte[] data) throws IOException {
81: ByteArrayInputStream bin = new ByteArrayInputStream(data);
82: GZIPInputStream gin = new GZIPInputStream(bin);
83: byte[] tmp = new byte[DEFAULT_BUFFER_SIZE];
84: int length = gin.read(tmp);
85: byte[] result = new byte[length];
86: System.arraycopy(tmp, 0, result, 0, length);
87: return result;
88: }
89:
90: public static void main(String[] arg) throws Exception {
91: byte[] data = new byte[1024];
92: Arrays.fill(data, (byte) 1);
93: byte[] compress = compress(data);
94: byte[] decompress = decompress(compress);
95: System.out.println("Debug test");
96:
97: }
98:
99: }
|