KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > rmi > socket > zip > CompressionOutputStream


1 /*
2  * $Id: CompressionOutputStream.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
7  * modify and redistribute this software in source and binary code form,
8  * provided that i) this copyright notice and license appear on all copies of
9  * the software; and ii) Licensee does not utilize the software in a manner
10  * which is disparaging to Sun.
11  *
12  * This software is provided "AS IS," without a warranty of any kind. ALL
13  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
14  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
15  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
16  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
17  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
18  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
19  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
20  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
21  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
22  * POSSIBILITY OF SUCH DAMAGES.
23  *
24  * This software is not designed or intended for use in on-line control of
25  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
26  * the design, construction, operation or maintenance of any nuclear
27  * facility. Licensee represents and warrants that it will not use or
28  * redistribute the Software for such purposes.
29  */

30
31 package org.ofbiz.service.rmi.socket.zip;
32
33 import java.io.FilterOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 public class CompressionOutputStream extends FilterOutputStream JavaDoc implements CompressionConstants {
38
39     /*
40      * Constructor calls constructor of superclass.
41      */

42     public CompressionOutputStream(OutputStream JavaDoc out) {
43         super(out);
44     }
45
46     /*
47      * Buffer of 6-bit codes to pack into next 32-bit word
48      * Five 6-bit codes fit into 4 words.
49      */

50     int buf[] = new int[5];
51
52     /*
53      * Index of valid codes waiting in buf.
54      */

55     int bufPos = 0;
56
57
58     /*
59      * This method writes one byte to the socket stream.
60      */

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

76             writeCode(BASE + pos);
77         } else {
78             // Otherwise, write RAW into buf to signify that the
79
// Character is being sent in 12 bits.
80
writeCode(RAW);
81
82             // Write the last 4 bits of b into the buf.
83
writeCode(b >> 4);
84
85             // Truncate b to contain data in only the first 4 bits,
86
// and write the first 4 bits of b into buf.
87
writeCode(b & 0xF);
88         }
89     }
90
91     /*
92      * This method writes up to len bytes to the socket stream.
93      */

94     public void write(byte b[], int off, int len) throws IOException JavaDoc {
95         /*
96          * This implementation is quite inefficient because it has to
97          * call the other write method for every byte in the array. It
98          * could be optimized for performance by doing all the processing
99          * in this method.
100          */

101         for (int i = 0; i < len; i++)
102             write(b[off + i]);
103     }
104
105
106     /*
107      * Clears buffer of all data (zeroes it out).
108      */

109     public void flush() throws IOException JavaDoc {
110         while (bufPos > 0)
111             writeCode(NOP);
112     }
113
114     /*
115      * This method actually puts the data into the output stream after
116      * packing the data from all 5 bytes in buf into one word.
117      * Remember, each byte has, at most, 6 significant bits.
118      */

119     private void writeCode(int c) throws IOException JavaDoc {
120         buf[bufPos++] = c;
121         if (bufPos == 5) { // write next word when we have 5 codes
122
int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) |
123                     (buf[3] << 6) | buf[4];
124             out.write((pack >>> 24) & 0xFF);
125             out.write((pack >>> 16) & 0xFF);
126             out.write((pack >>> 8) & 0xFF);
127             out.write((pack >>> 0) & 0xFF);
128             bufPos = 0;
129         }
130     }
131 }
132
Popular Tags