0N/A/*
2418N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.zip;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class implements a stream filter for writing compressed data in
0N/A * the GZIP file format.
0N/A * @author David Connelly
0N/A *
0N/A */
0N/Apublic
0N/Aclass GZIPOutputStream extends DeflaterOutputStream {
0N/A /**
0N/A * CRC-32 of uncompressed data.
0N/A */
0N/A protected CRC32 crc = new CRC32();
0N/A
0N/A /*
0N/A * GZIP header magic number.
0N/A */
0N/A private final static int GZIP_MAGIC = 0x8b1f;
0N/A
0N/A /*
0N/A * Trailer size in bytes.
0N/A *
0N/A */
0N/A private final static int TRAILER_SIZE = 8;
0N/A
0N/A /**
0N/A * Creates a new output stream with the specified buffer size.
2398N/A *
2398N/A * <p>The new output stream instance is created as if by invoking
2398N/A * the 3-argument constructor GZIPOutputStream(out, size, false).
2398N/A *
0N/A * @param out the output stream
0N/A * @param size the output buffer size
0N/A * @exception IOException If an I/O error has occurred.
0N/A * @exception IllegalArgumentException if size is <= 0
2398N/A
0N/A */
0N/A public GZIPOutputStream(OutputStream out, int size) throws IOException {
2398N/A this(out, size, false);
2398N/A }
2398N/A
2398N/A /**
2398N/A * Creates a new output stream with the specified buffer size and
2398N/A * flush mode.
2398N/A *
2398N/A * @param out the output stream
2398N/A * @param size the output buffer size
2398N/A * @param syncFlush
2398N/A * if {@code true} invocation of the inherited
2398N/A * {@link DeflaterOutputStream#flush() flush()} method of
2398N/A * this instance flushes the compressor with flush mode
2398N/A * {@link Deflater#SYNC_FLUSH} before flushing the output
2398N/A * stream, otherwise only flushes the output stream
2398N/A * @exception IOException If an I/O error has occurred.
2398N/A * @exception IllegalArgumentException if size is <= 0
2398N/A *
2398N/A * @since 1.7
2398N/A */
2398N/A public GZIPOutputStream(OutputStream out, int size, boolean syncFlush)
2398N/A throws IOException
2398N/A {
2398N/A super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true),
2398N/A size,
2398N/A syncFlush);
0N/A usesDefaultDeflater = true;
0N/A writeHeader();
0N/A crc.reset();
0N/A }
0N/A
2398N/A
0N/A /**
0N/A * Creates a new output stream with a default buffer size.
2398N/A *
2398N/A * <p>The new output stream instance is created as if by invoking
2398N/A * the 2-argument constructor GZIPOutputStream(out, false).
2398N/A *
0N/A * @param out the output stream
0N/A * @exception IOException If an I/O error has occurred.
0N/A */
0N/A public GZIPOutputStream(OutputStream out) throws IOException {
2398N/A this(out, 512, false);
2398N/A }
2398N/A
2398N/A /**
2398N/A * Creates a new output stream with a default buffer size and
2398N/A * the specified flush mode.
2398N/A *
2398N/A * @param out the output stream
2398N/A * @param syncFlush
2398N/A * if {@code true} invocation of the inherited
2398N/A * {@link DeflaterOutputStream#flush() flush()} method of
2398N/A * this instance flushes the compressor with flush mode
2398N/A * {@link Deflater#SYNC_FLUSH} before flushing the output
2398N/A * stream, otherwise only flushes the output stream
2398N/A *
2398N/A * @exception IOException If an I/O error has occurred.
2398N/A *
2398N/A * @since 1.7
2398N/A */
2398N/A public GZIPOutputStream(OutputStream out, boolean syncFlush)
2398N/A throws IOException
2398N/A {
2398N/A this(out, 512, syncFlush);
0N/A }
0N/A
0N/A /**
0N/A * Writes array of bytes to the compressed output stream. This method
0N/A * will block until all the bytes are written.
0N/A * @param buf the data to be written
0N/A * @param off the start offset of the data
0N/A * @param len the length of the data
0N/A * @exception IOException If an I/O error has occurred.
0N/A */
0N/A public synchronized void write(byte[] buf, int off, int len)
0N/A throws IOException
0N/A {
0N/A super.write(buf, off, len);
0N/A crc.update(buf, off, len);
0N/A }
0N/A
0N/A /**
0N/A * Finishes writing compressed data to the output stream without closing
0N/A * the underlying stream. Use this method when applying multiple filters
0N/A * in succession to the same output stream.
0N/A * @exception IOException if an I/O error has occurred
0N/A */
0N/A public void finish() throws IOException {
0N/A if (!def.finished()) {
0N/A def.finish();
0N/A while (!def.finished()) {
0N/A int len = def.deflate(buf, 0, buf.length);
0N/A if (def.finished() && len <= buf.length - TRAILER_SIZE) {
0N/A // last deflater buffer. Fit trailer at the end
0N/A writeTrailer(buf, len);
0N/A len = len + TRAILER_SIZE;
0N/A out.write(buf, 0, len);
0N/A return;
0N/A }
0N/A if (len > 0)
0N/A out.write(buf, 0, len);
0N/A }
0N/A // if we can't fit the trailer at the end of the last
0N/A // deflater buffer, we write it separately
0N/A byte[] trailer = new byte[TRAILER_SIZE];
0N/A writeTrailer(trailer, 0);
0N/A out.write(trailer);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Writes GZIP member header.
0N/A */
0N/A private void writeHeader() throws IOException {
2399N/A out.write(new byte[] {
2399N/A (byte) GZIP_MAGIC, // Magic number (short)
2399N/A (byte)(GZIP_MAGIC >> 8), // Magic number (short)
2399N/A Deflater.DEFLATED, // Compression method (CM)
2399N/A 0, // Flags (FLG)
2399N/A 0, // Modification time MTIME (int)
2399N/A 0, // Modification time MTIME (int)
2399N/A 0, // Modification time MTIME (int)
2399N/A 0, // Modification time MTIME (int)
2399N/A 0, // Extra flags (XFLG)
2399N/A 0 // Operating system (OS)
2399N/A });
0N/A }
0N/A
0N/A /*
0N/A * Writes GZIP member trailer to a byte array, starting at a given
0N/A * offset.
0N/A */
0N/A private void writeTrailer(byte[] buf, int offset) throws IOException {
0N/A writeInt((int)crc.getValue(), buf, offset); // CRC-32 of uncompr. data
0N/A writeInt(def.getTotalIn(), buf, offset + 4); // Number of uncompr. bytes
0N/A }
0N/A
0N/A /*
0N/A * Writes integer in Intel byte order to a byte array, starting at a
0N/A * given offset.
0N/A */
0N/A private void writeInt(int i, byte[] buf, int offset) throws IOException {
0N/A writeShort(i & 0xffff, buf, offset);
0N/A writeShort((i >> 16) & 0xffff, buf, offset + 2);
0N/A }
0N/A
0N/A /*
0N/A * Writes short integer in Intel byte order to a byte array, starting
0N/A * at a given offset
0N/A */
0N/A private void writeShort(int s, byte[] buf, int offset) throws IOException {
0N/A buf[offset] = (byte)(s & 0xff);
0N/A buf[offset + 1] = (byte)((s >> 8) & 0xff);
0N/A }
0N/A}