--- java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java 2011-02-02 20:07:32.000000000 +0100 +++ java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java 2011-06-30 16:49:40.481341463 +0200 @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -29,8 +29,6 @@ import org.apache.catalina.tribes.Member import org.apache.catalina.tribes.group.ChannelInterceptorBase; import org.apache.catalina.tribes.group.InterceptorPayload; - - /** * * @@ -38,63 +36,93 @@ import org.apache.catalina.tribes.group. * @version 1.0 */ public class GzipInterceptor extends ChannelInterceptorBase { - public static final int DEFAULT_BUFFER_SIZE = 2048; - - public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException { - try { - byte[] data = compress(msg.getMessage().getBytes()); - msg.getMessage().trim(msg.getMessage().getLength()); - msg.getMessage().append(data,0,data.length); - getNext().sendMessage(destination, msg, payload); - } catch ( IOException x ) { - log.error("Unable to compress byte contents"); - throw new ChannelException(x); - } + + private static final int DEFAULT_BUFFER_SIZE = 2048; + + @Override + public void sendMessage(Member[] destination, ChannelMessage msg, + InterceptorPayload payload) throws ChannelException { + try { + byte[] data = compress(msg.getMessage().getBytes()); + msg.getMessage().trim(msg.getMessage().getLength()); + msg.getMessage().append(data, 0, data.length); + getNext().sendMessage(destination, msg, payload); + } catch (IOException x) { + log.error("Unable to compress byte contents"); + throw new ChannelException(x); + } } + @Override public void messageReceived(ChannelMessage msg) { - try { - byte[] data = decompress(msg.getMessage().getBytes()); - msg.getMessage().trim(msg.getMessage().getLength()); - msg.getMessage().append(data,0,data.length); - getPrevious().messageReceived(msg); - } catch ( IOException x ) { - log.error("Unable to decompress byte contents",x); - } - } - - public static byte[] compress(byte[] data) throws IOException { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - GZIPOutputStream gout = new GZIPOutputStream(bout); - gout.write(data); - gout.flush(); - gout.close(); - return bout.toByteArray(); + try { + byte[] data = decompress(msg.getMessage().getBytes()); + msg.getMessage().trim(msg.getMessage().getLength()); + msg.getMessage().append(data, 0, data.length); + getPrevious().messageReceived(msg); + } catch (IOException x) { + log.error("Unable to decompress byte contents", x); + } + } + + /** + * @param data + * @return + * @throws IOException + */ + private static byte[] compress(byte[] data) throws IOException { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + GZIPOutputStream gout = new GZIPOutputStream(bout); + gout.write(data); + gout.flush(); + gout.close(); + return bout.toByteArray(); } - + /** - * @todo Fix to create an automatically growing buffer. - * @param data byte[] - * @return byte[] + * @param data + * @return * @throws IOException */ - public static byte[] decompress(byte[] data) throws IOException { - ByteArrayInputStream bin = new ByteArrayInputStream(data); - GZIPInputStream gin = new GZIPInputStream(bin); - byte[] tmp = new byte[DEFAULT_BUFFER_SIZE]; - int length = gin.read(tmp); - byte[] result = new byte[length]; - System.arraycopy(tmp,0,result,0,length); - return result; + private static byte[] decompress(byte[] data) throws IOException { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + ByteArrayInputStream bin = new ByteArrayInputStream(data); + GZIPInputStream gin = new GZIPInputStream(bin); + + byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; + while (true) { + int length = gin.read(buf); + if (length == -1) { + break; + } + bout.write(buf, 0, length); + } + + return bout.toByteArray(); } - + + /** + * @param arg + * @throws Exception + */ public static void main(String[] arg) throws Exception { - byte[] data = new byte[1024]; - Arrays.fill(data,(byte)1); - byte[] compress = compress(data); - byte[] decompress = decompress(compress); - System.out.println("Debug test"); - + test(1024); + test(2048); + test(4096); + test(8192); + } + + private static void test(int size) throws IOException { + byte[] data = new byte[size]; + Arrays.fill(data, (byte) 1); + + byte[] compressed = compress(data); + byte[] decompressed = decompress(compressed); + + if (Arrays.equals(data, decompressed)) { + System.out.println("Yes!"); + } else { + System.out.println("No!"); + } } - }