001: /*
002: * $Id: CompressionOutputStream.java,v 1.1 2003/12/02 06:39:32 ajzeneski Exp $
003: *
004: * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
007: * modify and redistribute this software in source and binary code form,
008: * provided that i) this copyright notice and license appear on all copies of
009: * the software; and ii) Licensee does not utilize the software in a manner
010: * which is disparaging to Sun.
011: *
012: * This software is provided "AS IS," without a warranty of any kind. ALL
013: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
014: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
015: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
016: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
018: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
019: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
020: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
021: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
022: * POSSIBILITY OF SUCH DAMAGES.
023: *
024: * This software is not designed or intended for use in on-line control of
025: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
026: * the design, construction, operation or maintenance of any nuclear
027: * facility. Licensee represents and warrants that it will not use or
028: * redistribute the Software for such purposes.
029: */
030:
031: package org.ofbiz.service.rmi.socket;
032:
033: import java.io.FilterOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: class CompressionOutputStream extends FilterOutputStream implements
038: CompressionConstants {
039:
040: /*
041: * Constructor calls constructor of superclass.
042: */
043: public CompressionOutputStream(OutputStream out) {
044: super (out);
045: }
046:
047: /*
048: * Buffer of 6-bit codes to pack into next 32-bit word
049: * Five 6-bit codes fit into 4 words.
050: */
051: int buf[] = new int[5];
052:
053: /*
054: * Index of valid codes waiting in buf.
055: */
056: int bufPos = 0;
057:
058: /*
059: * This method writes one byte to the socket stream.
060: */
061: public void write(int b) throws IOException {
062: // force argument to one byte
063: b &= 0xFF;
064:
065: // Look up pos in codeTable to get its encoding.
066: int pos = codeTable.indexOf((char) b);
067:
068: if (pos != -1) {
069: // If pos is in the codeTable, write BASE + pos into buf.
070: // By adding BASE to pos, we know that the characters in
071: // the codeTable will always have a code between 2 and 63
072: // inclusive. This allows us to use RAW (RAW is equal to
073: // 1) to signify that the next two groups of 6-bits are
074: // necessary for decompression of the next character.
075:
076: writeCode(BASE + pos);
077: } else {
078: // Otherwise, write RAW into buf to signify that the
079: // Character is being sent in 12 bits.
080: writeCode(RAW);
081:
082: // Write the last 4 bits of b into the buf.
083: writeCode(b >> 4);
084:
085: // Truncate b to contain data in only the first 4 bits,
086: // and write the first 4 bits of b into buf.
087: writeCode(b & 0xF);
088: }
089: }
090:
091: /*
092: * This method writes up to len bytes to the socket stream.
093: */
094: public void write(byte b[], int off, int len) throws IOException {
095: /*
096: * This implementation is quite inefficient because it has to
097: * call the other write method for every byte in the array. It
098: * could be optimized for performance by doing all the processing
099: * in this method.
100: */
101: for (int i = 0; i < len; i++)
102: write(b[off + i]);
103: }
104:
105: /*
106: * Clears buffer of all data (zeroes it out).
107: */
108: public void flush() throws IOException {
109: while (bufPos > 0)
110: writeCode(NOP);
111: }
112:
113: /*
114: * This method actually puts the data into the output stream after
115: * packing the data from all 5 bytes in buf into one word.
116: * Remember, each byte has, at most, 6 significant bits.
117: */
118: private void writeCode(int c) throws IOException {
119: buf[bufPos++] = c;
120: if (bufPos == 5) { // write next word when we have 5 codes
121: int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12)
122: | (buf[3] << 6) | buf[4];
123: out.write((pack >>> 24) & 0xFF);
124: out.write((pack >>> 16) & 0xFF);
125: out.write((pack >>> 8) & 0xFF);
126: out.write((pack >>> 0) & 0xFF);
127: bufPos = 0;
128: }
129: }
130: }
|