KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jrmp > ejb > CompressionOutputStream


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.test.jrmp.ejb;
24
25 import java.io.*;
26  
27 class CompressionOutputStream extends FilterOutputStream
28     implements CompressionConstants
29 {
30
31     /*
32      * Constructor calls constructor of superclass.
33      */

34     public CompressionOutputStream(OutputStream out) {
35         super(out);
36     }
37  
38     /*
39      * Buffer of 6-bit codes to pack into next 32-bit word
40      * Five 6-bit codes fit into 4 words.
41      */

42     int buf[] = new int[5];
43  
44     /*
45      * Index of valid codes waiting in buf.
46      */

47     int bufPos = 0;
48  
49
50     /*
51      * This method writes one byte to the socket stream.
52      */

53     public void write(int b) throws IOException {
54         // force argument to one byte
55
b &= 0xFF;
56
57         // Look up pos in codeTable to get its encoding.
58
int pos = codeTable.indexOf((char)b);
59
60         if (pos != -1){
61             // If pos is in the codeTable, write BASE + pos into buf.
62
// By adding BASE to pos, we know that the characters in
63
// the codeTable will always have a code between 2 and 63
64
// inclusive. This allows us to use RAW (RAW is equal to
65
// 1) to signify that the next two groups of 6-bits are
66
// necessary for decompression of the next character.
67

68             writeCode(BASE + pos);
69         } else {
70             // Otherwise, write RAW into buf to signify that the
71
// Character is being sent in 12 bits.
72
writeCode(RAW);
73
74             // Write the last 4 bits of b into the buf.
75
writeCode(b >> 4);
76
77             // Truncate b to contain data in only the first 4 bits,
78
// and write the first 4 bits of b into buf.
79
writeCode(b & 0xF);
80         }
81     }
82  
83     /*
84      * This method writes up to len bytes to the socket stream.
85      */

86     public void write(byte b[], int off, int len) throws IOException {
87         /*
88          * This implementation is quite inefficient because it has to
89          * call the other write method for every byte in the array. It
90          * could be optimized for performance by doing all the processing
91          * in this method.
92          */

93         for (int i = 0; i < len; i++)
94             write(b[off + i]);
95     }
96  
97
98    /*
99     * Clears buffer of all data (zeroes it out).
100     */

101   public void flush() throws IOException {
102     while (bufPos > 0)
103       writeCode(NOP);
104   }
105  
106     /*
107      * This method actually puts the data into the output stream after
108      * packing the data from all 5 bytes in buf into one word.
109      * Remember, each byte has, at most, 6 significant bits.
110      */

111     private void writeCode(int c) throws IOException {
112         buf[bufPos++] = c;
113         if (bufPos == 5) { // write next word when we have 5 codes
114
int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) |
115                        (buf[3] << 6) | buf[4];
116             out.write((pack >>> 24) & 0xFF);
117             out.write((pack >>> 16) & 0xFF);
118             out.write((pack >>> 8) & 0xFF);
119             out.write((pack >>> 0) & 0xFF);
120             bufPos = 0;
121         }
122     }
123 }
124
Popular Tags