001: /*
002: * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
003: *
004: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
005: * modify and redistribute this software in source and binary code form,
006: * provided that i) this copyright notice and license appear on all copies of
007: * the software; and ii) Licensee does not utilize the software in a manner
008: * which is disparaging to Sun.
009: *
010: * This software is provided "AS IS," without a warranty of any kind. ALL
011: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
012: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
013: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
014: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
015: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
016: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
017: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
018: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
019: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
020: * POSSIBILITY OF SUCH DAMAGES.
021: *
022: * This software is not designed or intended for use in on-line control of
023: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
024: * the design, construction, operation or maintenance of any nuclear
025: * facility. Licensee represents and warrants that it will not use or
026: * redistribute the Software for such purposes.
027: */
028: package org.jboss.test.invokers.ejb;
029:
030: import java.io.*;
031: import java.net.*;
032:
033: /**
034: * From some old jdk 1.3 rmi guide
035: */
036: class CompressionInputStream extends FilterInputStream implements
037: CompressionConstants {
038: /*
039: * Constructor calls constructor of superclass
040: */
041: public CompressionInputStream(InputStream in) {
042: super (in);
043: }
044:
045: /*
046: * Buffer of unpacked 6-bit codes
047: * from last 32 bits read.
048: */
049: int buf[] = new int[5];
050:
051: /*
052: * Position of next code to read in buffer (5 signifies end).
053: */
054: int bufPos = 5;
055:
056: /*
057: * Reads in format code and decompresses character accordingly.
058: */
059:
060: public int read() throws IOException {
061: try {
062: int code;
063:
064: // Read in and ignore empty bytes (NOP's) as long as they
065: // arrive.
066: do {
067: code = readCode();
068: } while (code == NOP);
069:
070: if (code >= BASE) {
071: // Retrieve index of character in codeTable if the
072: // code is in the correct range.
073: return codeTable.charAt(code - BASE);
074: } else if (code == RAW) {
075: // read in the lower 4 bits and the higher 4 bits,
076: // and return the reconstructed character
077: int high = readCode();
078: int low = readCode();
079: return (high << 4) | low;
080: } else
081: throw new IOException("unknown compression code: "
082: + code);
083: } catch (EOFException e) {
084: // Return the end of file code
085: return -1;
086: }
087: }
088:
089: /*
090: * This method reads up to len bytes from the input stream.
091: * Returns if read blocks before len bytes are read.
092: */
093: public int read(byte b[], int off, int len) throws IOException {
094:
095: if (len <= 0) {
096: return 0;
097: }
098:
099: 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 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 {
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();
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: }
|