home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / DeflaterOutputStream.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  148 lines

  1. /*
  2.  * @(#)DeflaterOutputStream.java    1.17 97/01/30
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. import java.io.FilterOutputStream;
  26. import java.io.OutputStream;
  27. import java.io.InputStream;
  28. import java.io.IOException;
  29.  
  30. /**
  31.  * This class implements an output stream filter for compressing data in
  32.  * the "deflate" compression format. It is also used as the basis for other
  33.  * types of compression filters, such as GZIPOutputStream.
  34.  *
  35.  * @see        Deflater
  36.  * @version     1.17, 01/30/97
  37.  * @author     David Connelly
  38.  */
  39. public
  40. class DeflaterOutputStream extends FilterOutputStream {
  41.     /**
  42.      * Compressor for this stream.
  43.      */
  44.     protected Deflater def;
  45.  
  46.     /**
  47.      * Output buffer for writing compressed data.
  48.      */
  49.     protected byte[] buf;
  50.    
  51.     /**
  52.      * Creates a new output stream with the specified compressor and
  53.      * buffer size.
  54.      * @param out the output stream
  55.      * @param def the compressor ("deflater")
  56.      * @param len the output buffer size
  57.      */
  58.     public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
  59.         super(out);
  60.         this.def = def;
  61.         buf = new byte[size];
  62.     }
  63.  
  64.     /**
  65.      * Creates a new output stream with the specified compressor and
  66.      * a default buffer size.
  67.      * @param out the output stream
  68.      * @param def the compressor ("deflater")
  69.      */
  70.     public DeflaterOutputStream(OutputStream out, Deflater def) {
  71.     this(out, def, 512);
  72.     }
  73.  
  74.     /**
  75.      * Creates a new output stream with a defaul compressor and buffer size.
  76.      */
  77.     public DeflaterOutputStream(OutputStream out) {
  78.     this(out, new Deflater());
  79.     }
  80.  
  81.     /**
  82.      * Writes a byte to the compressed output stream. This method will
  83.      * block until the byte can be written.
  84.      * @param b the byte to be written
  85.      * @exception IOException if an I/O error has occurred
  86.      */
  87.     public void write(int b) throws IOException {
  88.     byte[] buf = new byte[1];
  89.     buf[0] = (byte)(b & 0xff);
  90.     write(buf, 0, 1);
  91.     }
  92.  
  93.     /**
  94.      * Writes an array of bytes to the compressed output stream. This
  95.      * method will block until all the bytes are written.
  96.      * @param buf the data to be written
  97.      * @param off the start offset of the data
  98.      * @param len the length of the data
  99.      * @exception IOException if an I/O error has occurred
  100.      */
  101.     public void write(byte[] b, int off, int len) throws IOException {
  102.     if (def.finished()) {
  103.         throw new IOException("write beyond end of stream");
  104.     }
  105.     if (!def.finished()) {
  106.         def.setInput(b, off, len);
  107.         while (!def.needsInput()) {
  108.         deflate();
  109.         }
  110.     }
  111.     }
  112.  
  113.     /**
  114.      * Finishes writing compressed data to the output stream without closing
  115.      * the underlying stream. Use this method when applying multiple filters
  116.      * in succession to the same output stream.
  117.      * @exception IOException if an I/O error has occurred
  118.      */
  119.     public void finish() throws IOException {
  120.     if (!def.finished()) {
  121.         def.finish();
  122.         while (!def.finished()) {
  123.         deflate();
  124.         }
  125.     }
  126.     }
  127.  
  128.     /**
  129.      * Writes remaining compressed data to the output stream and closes the
  130.      * underlying stream.
  131.      * @exception IOException if an I/O error has occurred
  132.      */
  133.     public void close() throws IOException {
  134.     finish();
  135.     out.close();
  136.     }
  137.  
  138.     /**
  139.      * Writes next block of compressed data to the output stream.
  140.      */
  141.     protected void deflate() throws IOException {
  142.     int len = def.deflate(buf, 0, buf.length);
  143.     if (len > 0) {
  144.         out.write(buf, 0, len);
  145.     }
  146.     }
  147. }
  148.