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:
029: package org.jboss.test.invokers.ejb;
030:
031: import java.io.*;
032:
033: /**
034: * From some old jdk 1.3 rmi guide
035: */
036: class CompressionOutputStream extends FilterOutputStream implements
037: CompressionConstants {
038:
039: /*
040: * Constructor calls constructor of superclass.
041: */
042: public CompressionOutputStream(OutputStream out) {
043: super (out);
044: }
045:
046: /*
047: * Buffer of 6-bit codes to pack into next 32-bit word
048: * Five 6-bit codes fit into 4 words.
049: */
050: int buf[] = new int[5];
051:
052: /*
053: * Index of valid codes waiting in buf.
054: */
055: int bufPos = 0;
056:
057: /*
058: * This method writes one byte to the socket stream.
059: */
060: public void write(int b) throws IOException {
061: // force argument to one byte
062: b &= 0xFF;
063:
064: // Look up pos in codeTable to get its encoding.
065: int pos = codeTable.indexOf((char) b);
066:
067: if (pos != -1) {
068: // If pos is in the codeTable, write BASE + pos into buf.
069: // By adding BASE to pos, we know that the characters in
070: // the codeTable will always have a code between 2 and 63
071: // inclusive. This allows us to use RAW (RAW is equal to
072: // 1) to signify that the next two groups of 6-bits are
073: // necessary for decompression of the next character.
074:
075: writeCode(BASE + pos);
076: } else {
077: // Otherwise, write RAW into buf to signify that the
078: // Character is being sent in 12 bits.
079: writeCode(RAW);
080:
081: // Write the last 4 bits of b into the buf.
082: writeCode(b >> 4);
083:
084: // Truncate b to contain data in only the first 4 bits,
085: // and write the first 4 bits of b into buf.
086: writeCode(b & 0xF);
087: }
088: }
089:
090: /*
091: * This method writes up to len bytes to the socket stream.
092: */
093: public void write(byte b[], int off, int len) throws IOException {
094: /*
095: * This implementation is quite inefficient because it has to
096: * call the other write method for every byte in the array. It
097: * could be optimized for performance by doing all the processing
098: * in this method.
099: */
100: for (int i = 0; i < len; i++)
101: write(b[off + i]);
102: }
103:
104: /*
105: * Clears buffer of all data (zeroes it out).
106: */
107: public void flush() throws IOException {
108: while (bufPos > 0)
109: writeCode(NOP);
110: }
111:
112: /*
113: * This method actually puts the data into the output stream after
114: * packing the data from all 5 bytes in buf into one word.
115: * Remember, each byte has, at most, 6 significant bits.
116: */
117: private void writeCode(int c) throws IOException {
118: buf[bufPos++] = c;
119: if (bufPos == 5) { // write next word when we have 5 codes
120: int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12)
121: | (buf[3] << 6) | buf[4];
122: out.write((pack >>> 24) & 0xFF);
123: out.write((pack >>> 16) & 0xFF);
124: out.write((pack >>> 8) & 0xFF);
125: out.write((pack >>> 0) & 0xFF);
126: bufPos = 0;
127: }
128: }
129: }
|