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 / GZIPOutputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.7 KB  |  137 lines

  1. /*
  2.  * @(#)GZIPOutputStream.java    1.13 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.OutputStream;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * This class implements a stream filter for writing compressed data in
  22.  * the GZIP file format.
  23.  * @version     1.13, 03/18/98
  24.  * @author     David Connelly
  25.  *
  26.  */
  27. public
  28. class GZIPOutputStream extends DeflaterOutputStream {
  29.     /**
  30.      * CRC-32 of uncompressed data.
  31.      */
  32.     protected CRC32 crc = new CRC32();
  33.  
  34.     /*
  35.      * GZIP header magic number.
  36.      */
  37.     private final static int GZIP_MAGIC = 0x8b1f;
  38.  
  39.     /**
  40.      * Creates a new output stream with the specified buffer size.
  41.      * @param out the output stream
  42.      * @param size the output buffer size
  43.      * @exception IOException If an I/O error has occurred.
  44.      */
  45.     public GZIPOutputStream(OutputStream out, int size) throws IOException {
  46.     super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
  47.     writeHeader();
  48.     crc.reset();
  49.     }
  50.  
  51.     /**
  52.      * Creates a new output stream with a default buffer size.
  53.      * @param out the output stream
  54.      * @exception IOException If an I/O error has occurred.
  55.      */
  56.     public GZIPOutputStream(OutputStream out) throws IOException {
  57.     this(out, 512);
  58.     }
  59.  
  60.     /**
  61.      * Writes array of bytes to the compressed output stream. This method
  62.      * will block until all the bytes are written.
  63.      * @param buf the data to be written
  64.      * @param off the start offset of the data
  65.      * @param len the length of the data
  66.      * @exception IOException If an I/O error has occurred.
  67.      */
  68.     public synchronized void write(byte[] buf, int off, int len)
  69.     throws IOException
  70.     {
  71.     super.write(buf, off, len);
  72.     crc.update(buf, off, len);
  73.     }
  74.  
  75.     /**
  76.      * Finishes writing compressed data to the output stream without closing
  77.      * the underlying stream. Use this method when applying multiple filters
  78.      * in succession to the same output stream.
  79.      * @exception IOException if an I/O error has occurred
  80.      */
  81.     public void finish() throws IOException {
  82.     if (!def.finished()) {
  83.         def.finish();
  84.         while (!def.finished()) {
  85.         deflate();
  86.         }
  87.         writeTrailer();
  88.     }
  89.     }
  90.  
  91.     /**
  92.      * Writes remaining compressed data to the output stream and closes the
  93.      * underlying stream.
  94.      * @exception IOException if an I/O error has occurred
  95.      */
  96.     public void close() throws IOException {
  97.     finish();
  98.     out.close();
  99.     }
  100.   
  101.     /*
  102.      * Writes GZIP member header.
  103.      */
  104.     private void writeHeader() throws IOException {
  105.     writeShort(GZIP_MAGIC);        // Magic number
  106.     out.write(def.DEFLATED);    // Compression method (CM)
  107.     out.write(0);            // Flags (FLG)
  108.     writeInt(0);            // Modification time (MTIME)
  109.     out.write(0);            // Extra flags (XFL)
  110.     out.write(0);            // Operating system (OS)
  111.     }
  112.  
  113.     /*
  114.      * Writes GZIP member trailer.
  115.      */
  116.     private void writeTrailer() throws IOException {
  117.     writeInt((int)crc.getValue());  // CRC-32 of uncompressed data
  118.     writeInt(def.getTotalIn());    // Number of uncompressed bytes
  119.     }
  120.  
  121.     /*
  122.      * Writes integer in Intel byte order.
  123.      */
  124.     private void writeInt(int i) throws IOException {
  125.     writeShort(i & 0xffff);
  126.     writeShort((i >> 16) & 0xffff);
  127.     }
  128.  
  129.     /*
  130.      * Writes short integer in Intel byte order.
  131.      */
  132.     private void writeShort(int s) throws IOException {
  133.     out.write(s & 0xff);
  134.     out.write((s >> 8) & 0xff);
  135.     }
  136. }
  137.