home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / zip / DeflaterOutputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.8 KB  |  140 lines

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