Source for java.util.zip.GZIPOutputStream

   1: /* GZIPOutputStream.java - Create a file in gzip format
   2:    Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package java.util.zip;
  39: 
  40: import java.io.IOException;
  41: import java.io.OutputStream;
  42: 
  43: /**
  44:  * This filter stream is used to compress a stream into a "GZIP" stream.
  45:  * The "GZIP" format is described in RFC 1952.
  46:  *
  47:  * @author John Leuner
  48:  * @author Tom Tromey
  49:  * @since JDK 1.1
  50:  */
  51: 
  52: /* Written using on-line Java Platform 1.2 API Specification
  53:  * and JCL book.
  54:  * Believed complete and correct.
  55:  */
  56: 
  57: public class GZIPOutputStream extends DeflaterOutputStream
  58: {
  59:   /**
  60:    * CRC-32 value for uncompressed data
  61:    */
  62:   protected CRC32 crc;
  63: 
  64:   /**
  65:    * Creates a GZIPOutputStream with the default buffer size
  66:    *
  67:    * @param out The stream to read data (to be compressed) from
  68:    *
  69:    */
  70:   public GZIPOutputStream(OutputStream out) throws IOException
  71:   {
  72:     this(out, 4096);
  73:   }
  74: 
  75:   /**
  76:    * Creates a GZIPOutputStream with the specified buffer size
  77:    *
  78:    * @param out The stream to read compressed data from
  79:    * @param size Size of the buffer to use
  80:    */
  81:   public GZIPOutputStream(OutputStream out, int size) throws IOException
  82:   {
  83:     super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
  84:     crc = new CRC32();
  85:     int mod_time = (int) (System.currentTimeMillis() / 1000L);
  86:     byte[] gzipHeader =
  87:       {
  88:         /* The two magic bytes */
  89:         (byte) GZIPInputStream.GZIP_MAGIC,
  90:         (byte) (GZIPInputStream.GZIP_MAGIC >> 8),
  91: 
  92:         /* The compression type */
  93:         (byte) Deflater.DEFLATED,
  94: 
  95:         /* The flags (not set) */
  96:         0,
  97: 
  98:         /* The modification time */
  99:         (byte) mod_time, (byte) (mod_time >> 8),
 100:         (byte) (mod_time >> 16), (byte) (mod_time >> 24),
 101: 
 102:         /* The extra flags */
 103:         0,
 104: 
 105:         /* The OS type (unknown) */
 106:         (byte) 255
 107:       };
 108: 
 109:     out.write(gzipHeader);
 110:     //    System.err.println("wrote GZIP header (" + gzipHeader.length + " bytes )");
 111:   }
 112: 
 113:   public synchronized void write(byte[] buf, int off, int len)
 114:     throws IOException
 115:   {
 116:     super.write(buf, off, len);
 117:     crc.update(buf, off, len);
 118:   }
 119: 
 120:   /**
 121:    * Writes remaining compressed output data to the output stream
 122:    * and closes it.
 123:    */
 124:   public void close() throws IOException
 125:   {
 126:     finish();
 127:     out.close();
 128:   }
 129: 
 130:   public void finish() throws IOException
 131:   {
 132:     super.finish();
 133: 
 134:     int totalin = def.getTotalIn();
 135:     int crcval = (int) (crc.getValue() & 0xffffffff);
 136: 
 137:     //    System.err.println("CRC val is " + Integer.toHexString( crcval )                     + " and length " + Integer.toHexString(totalin));
 138: 
 139:     byte[] gzipFooter =
 140:       {
 141:         (byte) crcval, (byte) (crcval >> 8),
 142:         (byte) (crcval >> 16), (byte) (crcval >> 24),
 143: 
 144:         (byte) totalin, (byte) (totalin >> 8),
 145:         (byte) (totalin >> 16), (byte) (totalin >> 24)
 146:       };
 147: 
 148:     out.write(gzipFooter);
 149:   //    System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )");
 150:   }
 151: }