Frames | No Frames |
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: put2(GZIPInputStream.GZIP_MAGIC); 86: out.write(GZIPInputStream.Z_DEFLATED); 87: // No flags for now. 88: out.write(0); 89: // No time either. 90: put2(0); 91: put2(0); 92: // No xflags either. 93: out.write(0); 94: // FIXME: unknown OS. 95: out.write(255); 96: } 97: 98: public synchronized void write(int bval) throws IOException 99: { 100: super.write(bval); 101: crc.update(bval); 102: } 103: 104: public synchronized void write(byte[] buf) throws IOException 105: { 106: write(buf, 0, buf.length); 107: } 108: 109: public synchronized void write(byte[] buf, int off, int len) 110: throws IOException 111: { 112: super.write(buf, off, len); 113: crc.update(buf, off, len); 114: } 115: 116: /** 117: * Writes remaining compressed output data to the output stream 118: * and closes it. 119: */ 120: public void close() throws IOException 121: { 122: finish(); 123: out.close(); 124: } 125: 126: public void finish() throws IOException 127: { 128: super.finish(); 129: put4((int) crc.getValue()); 130: put4(def.getTotalIn()); 131: } 132: 133: private final void put2(int i) throws IOException 134: { 135: out.write(i); 136: out.write(i >> 8); 137: } 138: 139: private final void put4 (int i) throws IOException 140: { 141: out.write(i); 142: out.write(i >> 8); 143: out.write(i >> 16); 144: out.write(i >> 24); 145: } 146: }