2
* Copyright 1999,2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
16
package org.apache.catalina.tribes.group.interceptors;
18
import java.io.ByteArrayInputStream;
19
import java.io.ByteArrayOutputStream;
20
import java.io.IOException;
21
import java.util.Arrays;
22
import java.util.zip.GZIPInputStream;
23
import java.util.zip.GZIPOutputStream;
25
import org.apache.catalina.tribes.ChannelException;
26
import org.apache.catalina.tribes.ChannelMessage;
27
import org.apache.catalina.tribes.Member;
28
import org.apache.catalina.tribes.group.ChannelInterceptorBase;
29
import org.apache.catalina.tribes.group.InterceptorPayload;
39
public class GzipInterceptor extends ChannelInterceptorBase {
40
public static final int DEFAULT_BUFFER_SIZE = 2048;
42
public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException {
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);
54
public void messageReceived(ChannelMessage msg) {
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);
65
public static byte[] compress(byte[] data) throws IOException {
66
ByteArrayOutputStream bout = new ByteArrayOutputStream();
67
GZIPOutputStream gout = new GZIPOutputStream(bout);
71
return bout.toByteArray();
75
* @todo Fix to create an automatically growing buffer.
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);
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");