KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CompressionInputStream.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.EOFException JavaDoc;
34 import java.io.FilterInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37
38 public class CompressionInputStream extends FilterInputStream JavaDoc implements CompressionConstants {
39     /*
40      * Constructor calls constructor of superclass
41      */

42     public CompressionInputStream(InputStream JavaDoc in) {
43         super(in);
44     }
45
46     /*
47      * Buffer of unpacked 6-bit codes
48      * from last 32 bits read.
49      */

50     int buf[] = new int[5];
51
52     /*
53      * Position of next code to read in buffer (5 signifies end).
54      */

55     int bufPos = 5;
56
57     /*
58      * Reads in format code and decompresses character accordingly.
59      */

60
61     public int read() throws IOException JavaDoc {
62         try {
63             int code;
64
65             // Read in and ignore empty bytes (NOP's) as long as they
66
// arrive.
67
do {
68                 code = readCode();
69             } while (code == NOP);
70
71             if (code >= BASE) {
72                 // Retrieve index of character in codeTable if the
73
// code is in the correct range.
74
return codeTable.charAt(code - BASE);
75             } else if (code == RAW) {
76                 // read in the lower 4 bits and the higher 4 bits,
77
// and return the reconstructed character
78
int high = readCode();
79                 int low = readCode();
80                 return (high << 4) | low;
81             } else
82                 throw new IOException JavaDoc("unknown compression code: " + code);
83         } catch (EOFException JavaDoc e) {
84             // Return the end of file code
85
return -1;
86         }
87     }
88
89     /*
90      * This method reads up to len bytes from the input stream.
91      * Returns if read blocks before len bytes are read.
92      */

93     public int read(byte b[], int off, int len) throws IOException JavaDoc {
94
95         if (len <= 0) {
96             return 0;
97         }
98
99         int c = read();
100         if (c == -1) {
101             return -1;
102         }
103         b[off] = (byte) c;
104
105         int i = 1;
106         // Try to read up to len bytes or until no
107
// more bytes can be read without blocking.
108
try {
109             for (; (i < len) && (in.available() > 0); i++) {
110                 c = read();
111                 if (c == -1) {
112                     break;
113                 }
114                 if (b != null) {
115                     b[off + i] = (byte) c;
116                 }
117             }
118         } catch (IOException JavaDoc ee) {
119         }
120         return i;
121     }
122
123     /*
124      * If there is no more data to decode left in buf, read the
125      * next four bytes from the wire. Then store each group of 6
126      * bits in an element of buf. Return one element of buf.
127      */

128     private int readCode() throws IOException JavaDoc {
129         // As soon as all the data in buf has been read
130
// (when bufPos == 5) read in another four bytes.
131
if (bufPos == 5) {
132             int b1 = in.read();
133             int b2 = in.read();
134             int b3 = in.read();
135             int b4 = in.read();
136
137             // make sure none of the bytes signify the
138
// end of the data in the stream
139
if ((b1 | b2 | b3 | b4) < 0) {
140                 throw new EOFException JavaDoc();
141             }
142             // Assign each group of 6 bits to an element of
143
// buf
144
int pack = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
145             buf[0] = (pack >>> 24) & 0x3F;
146             buf[1] = (pack >>> 18) & 0x3F;
147             buf[2] = (pack >>> 12) & 0x3F;
148             buf[3] = (pack >>> 6) & 0x3F;
149             buf[4] = (pack >>> 0) & 0x3F;
150             bufPos = 0;
151         }
152         return buf[bufPos++];
153     }
154 }
155
Popular Tags