home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / rmi / code / CompressionInputStream.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  4.8 KB  |  153 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. import java.net.*;
  33.  
  34. class CompressionInputStream extends FilterInputStream
  35.     implements CompressionConstants
  36. {
  37.     /*
  38.      * Constructor calls constructor of superclass
  39.      */
  40.     public CompressionInputStream(InputStream in) {
  41.         super(in);
  42.     }
  43.  
  44.     /* 
  45.      * Buffer of unpacked 6-bit codes 
  46.      * from last 32 bits read.
  47.      */
  48.     int buf[] = new int[5];
  49.  
  50.     /*
  51.      * Position of next code to read in buffer (5 signifies end). 
  52.      */ 
  53.     int bufPos = 5;
  54.  
  55.     /*
  56.      * Reads in format code and decompresses character accordingly.
  57.      */
  58.  
  59.     public int read() throws IOException {
  60.         try {
  61.             int code;
  62.  
  63.             // Read in and ignore empty bytes (NOP's) as long as they
  64.             // arrive. 
  65.             do {
  66.           code = readCode();
  67.         } while (code == NOP);      
  68.  
  69.             if (code >= BASE) {
  70.                 // Retrieve index of character in codeTable if the
  71.                 // code is in the correct range.
  72.                 return codeTable.charAt(code - BASE);
  73.             } else if (code == RAW) {
  74.                 // read in the lower 4 bits and the higher 4 bits,
  75.                 // and return the reconstructed character
  76.                 int high = readCode();
  77.                 int low = readCode();
  78.                 return (high << 4) | low;
  79.             } else 
  80.                 throw new IOException("unknown compression code: " + code);
  81.         } catch (EOFException e) {
  82.             // Return the end of file code
  83.             return -1;
  84.         }
  85.     }
  86.  
  87.     /* 
  88.      * This method reads up to len bytes from the input stream. 
  89.      * Returns if read blocks before len bytes are read.
  90.      */ 
  91.     public int read(byte b[], int off, int len) throws IOException {
  92.  
  93.     if (len <= 0) {
  94.         return 0;
  95.     }
  96.  
  97.     int c = read();
  98.     if (c == -1) {
  99.         return -1;
  100.     }
  101.     b[off] = (byte)c;
  102.  
  103.     int i = 1;
  104.         // Try to read up to len bytes or until no
  105.         // more bytes can be read without blocking.
  106.            try {
  107.         for (; (i < len) && (in.available() > 0); i++) {
  108.         c = read();
  109.         if (c == -1) {
  110.             break;
  111.         }
  112.         if (b != null) {
  113.             b[off + i] = (byte)c;
  114.         }
  115.         }
  116.     } catch (IOException ee) {
  117.     }
  118.     return i;
  119.     }
  120.  
  121.     /*
  122.      * If there is no more data to decode left in buf, read the
  123.      * next four bytes from the wire. Then store each group of 6
  124.      * bits in an element of buf.  Return one element of buf.
  125.      */
  126.     private int readCode() throws IOException {
  127.         // As soon as all the data in buf has been read
  128.         // (when bufPos == 5) read in another four bytes.
  129.         if (bufPos == 5) {
  130.             int b1 = in.read();
  131.             int b2 = in.read();
  132.             int b3 = in.read();
  133.             int b4 = in.read();
  134.  
  135.             // make sure none of the bytes signify the
  136.             // end of the data in the stream
  137.             if ((b1 | b2 | b3 | b4) < 0) {
  138.                 throw new EOFException();
  139.             }
  140.             // Assign each group of 6 bits to an element of
  141.             // buf
  142.             int pack = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
  143.             buf[0] = (pack >>> 24) & 0x3F;
  144.             buf[1] = (pack >>> 18) & 0x3F;
  145.             buf[2] = (pack >>> 12) & 0x3F;
  146.             buf[3] = (pack >>>  6) & 0x3F;
  147.             buf[4] = (pack >>>  0) & 0x3F;
  148.             bufPos = 0;
  149.         }
  150.         return buf[bufPos++];
  151.     }
  152. }
  153.