home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / rmi / code / CompressionOutputStream.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  4.5 KB  |  130 lines

  1. /*
  2.  * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  5.  * modify and redistribute this software in source and binary code form,
  6.  * provided that i) this copyright notice and license appear on all copies of
  7.  * the software; and ii) Licensee does not utilize the software in a manner
  8.  * which is disparaging to Sun.
  9.  *
  10.  * This software is provided "AS IS," without a warranty of any kind. ALL
  11.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  12.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  13.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  14.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  15.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  16.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  17.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  18.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  19.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  20.  * POSSIBILITY OF SUCH DAMAGES.
  21.  *
  22.  * This software is not designed or intended for use in on-line control of
  23.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  24.  * the design, construction, operation or maintenance of any nuclear
  25.  * facility. Licensee represents and warrants that it will not use or
  26.  * redistribute the Software for such purposes.
  27.  */
  28.  
  29. package examples.rmisocfac;
  30.  
  31. import java.io.*;
  32.  
  33. class CompressionOutputStream extends FilterOutputStream
  34.     implements CompressionConstants
  35. {
  36.  
  37.     /*
  38.      * Constructor calls constructor of superclass.
  39.      */
  40.     public CompressionOutputStream(OutputStream out) {
  41.         super(out);
  42.     }
  43.  
  44.     /* 
  45.      * Buffer of 6-bit codes to pack into next 32-bit word
  46.      * Five 6-bit codes fit into 4 words. 
  47.      */
  48.     int buf[] = new int[5];
  49.  
  50.     /*
  51.      * Index of valid codes waiting in buf. 
  52.      */
  53.     int bufPos = 0;
  54.  
  55.  
  56.     /*  
  57.      * This method writes one byte to the socket stream. 
  58.      */ 
  59.     public void write(int b) throws IOException {
  60.         // force argument to one byte
  61.         b &= 0xFF;  
  62.  
  63.         // Look up pos in codeTable to get its encoding. 
  64.         int pos = codeTable.indexOf((char)b);
  65.  
  66.         if (pos != -1){
  67.             // If pos is in the codeTable, write BASE + pos into buf. 
  68.             // By adding BASE to pos, we know that the characters in
  69.             // the codeTable will always have a code between 2 and 63
  70.             // inclusive. This allows us to use RAW (RAW is equal to
  71.             // 1) to signify that the next two groups of 6-bits are 
  72.             // necessary for decompression of the next character.
  73.         
  74.             writeCode(BASE + pos);
  75.         } else {
  76.             // Otherwise, write RAW into buf to signify that the
  77.             // Character is being sent in 12 bits.
  78.             writeCode(RAW);
  79.  
  80.             // Write the last 4 bits of b into the buf.
  81.             writeCode(b >> 4);
  82.  
  83.             // Truncate b to contain data in only the first 4 bits, 
  84.             // and write the first 4 bits of b into buf.
  85.             writeCode(b & 0xF);
  86.         }
  87.     }
  88.  
  89.     /* 
  90.      * This method writes up to len bytes to the socket stream. 
  91.      */
  92.     public void write(byte b[], int off, int len) throws IOException {
  93.         /*
  94.          * This implementation is quite inefficient because it has to
  95.          * call the other write method for every byte in the array.  It
  96.          * could be optimized for performance by doing all the processing
  97.          * in this method.
  98.          */
  99.         for (int i = 0; i < len; i++)
  100.             write(b[off + i]);
  101.     }
  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. }
  130.