~ubuntu-branches/ubuntu/intrepid/tomcat5.5/intrepid

« back to all changes in this revision

Viewing changes to container/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-27 11:19:17 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060927111917-wov6fmkz3x6rsl68
Tags: 5.5.17-1ubuntu1
(Build-) depend on libmx4j-java (>= 3.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1999,2004 The Apache Software Foundation.
 
3
 * 
 
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
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
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
 
14
 */
 
15
 
 
16
package org.apache.catalina.tribes.group.interceptors;
 
17
 
 
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;
 
24
 
 
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;
 
30
 
 
31
 
 
32
 
 
33
/**
 
34
 *
 
35
 *
 
36
 * @author Filip Hanik
 
37
 * @version 1.0
 
38
 */
 
39
public class GzipInterceptor extends ChannelInterceptorBase {
 
40
    public static final int DEFAULT_BUFFER_SIZE = 2048;
 
41
    
 
42
    public void sendMessage(Member[] destination, ChannelMessage msg, 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
}