KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jboss.test.jrmp.ejb;
23
24 import java.io.*;
25 import java.net.*;
26
27 class CompressionInputStream extends FilterInputStream
28     implements CompressionConstants
29 {
30     /*
31      * Constructor calls constructor of superclass
32      */

33     public CompressionInputStream(InputStream in) {
34         super(in);
35     }
36  
37     /*
38      * Buffer of unpacked 6-bit codes
39      * from last 32 bits read.
40      */

41     int buf[] = new int[5];
42  
43     /*
44      * Position of next code to read in buffer (5 signifies end).
45      */

46     int bufPos = 5;
47  
48     /*
49      * Reads in format code and decompresses character accordingly.
50      */

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

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

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