Lines 5-13
Link Here
|
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Lines 29-36
import org.apache.catalina.tribes.Member
Link Here
|
29 |
import org.apache.catalina.tribes.group.ChannelInterceptorBase; |
29 |
import org.apache.catalina.tribes.group.ChannelInterceptorBase; |
30 |
import org.apache.catalina.tribes.group.InterceptorPayload; |
30 |
import org.apache.catalina.tribes.group.InterceptorPayload; |
31 |
|
31 |
|
32 |
|
|
|
33 |
|
34 |
/** |
32 |
/** |
35 |
* |
33 |
* |
36 |
* |
34 |
* |
Lines 38-100
import org.apache.catalina.tribes.group.
Link Here
|
38 |
* @version 1.0 |
36 |
* @version 1.0 |
39 |
*/ |
37 |
*/ |
40 |
public class GzipInterceptor extends ChannelInterceptorBase { |
38 |
public class GzipInterceptor extends ChannelInterceptorBase { |
41 |
public static final int DEFAULT_BUFFER_SIZE = 2048; |
39 |
|
42 |
|
40 |
private static final int DEFAULT_BUFFER_SIZE = 2048; |
43 |
public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException { |
41 |
|
44 |
try { |
42 |
@Override |
45 |
byte[] data = compress(msg.getMessage().getBytes()); |
43 |
public void sendMessage(Member[] destination, ChannelMessage msg, |
46 |
msg.getMessage().trim(msg.getMessage().getLength()); |
44 |
InterceptorPayload payload) throws ChannelException { |
47 |
msg.getMessage().append(data,0,data.length); |
45 |
try { |
48 |
getNext().sendMessage(destination, msg, payload); |
46 |
byte[] data = compress(msg.getMessage().getBytes()); |
49 |
} catch ( IOException x ) { |
47 |
msg.getMessage().trim(msg.getMessage().getLength()); |
50 |
log.error("Unable to compress byte contents"); |
48 |
msg.getMessage().append(data, 0, data.length); |
51 |
throw new ChannelException(x); |
49 |
getNext().sendMessage(destination, msg, payload); |
52 |
} |
50 |
} catch (IOException x) { |
|
|
51 |
log.error("Unable to compress byte contents"); |
52 |
throw new ChannelException(x); |
53 |
} |
53 |
} |
54 |
} |
54 |
|
55 |
|
|
|
56 |
@Override |
55 |
public void messageReceived(ChannelMessage msg) { |
57 |
public void messageReceived(ChannelMessage msg) { |
56 |
try { |
58 |
try { |
57 |
byte[] data = decompress(msg.getMessage().getBytes()); |
59 |
byte[] data = decompress(msg.getMessage().getBytes()); |
58 |
msg.getMessage().trim(msg.getMessage().getLength()); |
60 |
msg.getMessage().trim(msg.getMessage().getLength()); |
59 |
msg.getMessage().append(data,0,data.length); |
61 |
msg.getMessage().append(data, 0, data.length); |
60 |
getPrevious().messageReceived(msg); |
62 |
getPrevious().messageReceived(msg); |
61 |
} catch ( IOException x ) { |
63 |
} catch (IOException x) { |
62 |
log.error("Unable to decompress byte contents",x); |
64 |
log.error("Unable to decompress byte contents", x); |
63 |
} |
65 |
} |
64 |
} |
66 |
} |
65 |
|
67 |
|
66 |
public static byte[] compress(byte[] data) throws IOException { |
68 |
/** |
67 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
69 |
* @param data |
68 |
GZIPOutputStream gout = new GZIPOutputStream(bout); |
70 |
* @return |
69 |
gout.write(data); |
71 |
* @throws IOException |
70 |
gout.flush(); |
72 |
*/ |
71 |
gout.close(); |
73 |
private static byte[] compress(byte[] data) throws IOException { |
72 |
return bout.toByteArray(); |
74 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
|
75 |
GZIPOutputStream gout = new GZIPOutputStream(bout); |
76 |
gout.write(data); |
77 |
gout.flush(); |
78 |
gout.close(); |
79 |
return bout.toByteArray(); |
73 |
} |
80 |
} |
74 |
|
81 |
|
75 |
/** |
82 |
/** |
76 |
* @todo Fix to create an automatically growing buffer. |
83 |
* @param data |
77 |
* @param data byte[] |
84 |
* @return |
78 |
* @return byte[] |
|
|
79 |
* @throws IOException |
85 |
* @throws IOException |
80 |
*/ |
86 |
*/ |
81 |
public static byte[] decompress(byte[] data) throws IOException { |
87 |
private static byte[] decompress(byte[] data) throws IOException { |
82 |
ByteArrayInputStream bin = new ByteArrayInputStream(data); |
88 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
83 |
GZIPInputStream gin = new GZIPInputStream(bin); |
89 |
ByteArrayInputStream bin = new ByteArrayInputStream(data); |
84 |
byte[] tmp = new byte[DEFAULT_BUFFER_SIZE]; |
90 |
GZIPInputStream gin = new GZIPInputStream(bin); |
85 |
int length = gin.read(tmp); |
91 |
|
86 |
byte[] result = new byte[length]; |
92 |
byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; |
87 |
System.arraycopy(tmp,0,result,0,length); |
93 |
while (true) { |
88 |
return result; |
94 |
int length = gin.read(buf); |
|
|
95 |
if (length == -1) { |
96 |
break; |
97 |
} |
98 |
bout.write(buf, 0, length); |
99 |
} |
100 |
|
101 |
return bout.toByteArray(); |
89 |
} |
102 |
} |
90 |
|
103 |
|
|
|
104 |
/** |
105 |
* @param arg |
106 |
* @throws Exception |
107 |
*/ |
91 |
public static void main(String[] arg) throws Exception { |
108 |
public static void main(String[] arg) throws Exception { |
92 |
byte[] data = new byte[1024]; |
109 |
test(1024); |
93 |
Arrays.fill(data,(byte)1); |
110 |
test(2048); |
94 |
byte[] compress = compress(data); |
111 |
test(4096); |
95 |
byte[] decompress = decompress(compress); |
112 |
test(8192); |
96 |
System.out.println("Debug test"); |
113 |
} |
97 |
|
114 |
|
|
|
115 |
private static void test(int size) throws IOException { |
116 |
byte[] data = new byte[size]; |
117 |
Arrays.fill(data, (byte) 1); |
118 |
|
119 |
byte[] compressed = compress(data); |
120 |
byte[] decompressed = decompress(compressed); |
121 |
|
122 |
if (Arrays.equals(data, decompressed)) { |
123 |
System.out.println("Yes!"); |
124 |
} else { |
125 |
System.out.println("No!"); |
126 |
} |
98 |
} |
127 |
} |
99 |
|
|
|
100 |
} |
128 |
} |