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