FileDocCategorySizeDatePackage
DeflaterOutputStream.javaAPI DocAndroid 1.5 API6301Wed May 06 22:41:02 BST 2009java.util.zip

DeflaterOutputStream

public class DeflaterOutputStream extends FilterOutputStream
This class provides an implementation of {@code FilterOutputStream} that compresses data using the DEFLATE algorithm. Basically it wraps the {@code Deflater} class and takes care of the buffering.
see
Deflater
since
Android 1.0

Fields Summary
static final int
BUF_SIZE
protected byte[]
buf
The buffer for the data to be written to.
protected Deflater
def
The deflater used.
boolean
done
Constructors Summary
public DeflaterOutputStream(OutputStream os, Deflater def)
This constructor lets you pass the {@code Deflater} specifying the compression algorithm.

param
os is the {@code OutputStream} where to write the compressed data to.
param
def is the specific {@code Deflater} that is used to compress data.
since
Android 1.0


                                                                                               
         
        this(os, def, BUF_SIZE);
    
public DeflaterOutputStream(OutputStream os)
This is the most basic constructor. You only need to pass the {@code OutputStream} to which the compressed data shall be written to. The default settings for the {@code Deflater} and internal buffer are used. In particular the {@code Deflater} produces a ZLIB header in the output stream.

param
os is the OutputStream where to write the compressed data to.
since
Android 1.0

        this(os, new Deflater());
    
public DeflaterOutputStream(OutputStream os, Deflater def, int bsize)
This constructor lets you specify both the compression algorithm as well as the internal buffer size to be used.

param
os is the {@code OutputStream} where to write the compressed data to.
param
def is the specific {@code Deflater} that will be used to compress data.
param
bsize is the size to be used for the internal buffer.
since
Android 1.0

        super(os);
        if (os == null || def == null) {
            throw new NullPointerException();
        }
        if (bsize <= 0) {
            throw new IllegalArgumentException();
        }
        this.def = def;
        buf = new byte[bsize];
    
Methods Summary
public voidclose()
Writes any unwritten compressed data to the underlying stream, the closes all underlying streams. This stream can no longer be used after close() has been called.

throws
IOException If an error occurs while closing the data compression process.
since
Android 1.0

        if (!def.finished()) {
            finish();
        }
        def.end();
        out.close();
    
protected voiddeflate()
Compress the data in the input buffer and write it to the underlying stream.

throws
IOException If an error occurs during deflation.
since
Android 1.0

        int x = 0;
        do {
            x = def.deflate(buf);
            out.write(buf, 0, x);
        } while (!def.needsInput());
    
public voidfinish()
Writes any unwritten data to the underlying stream. Does not close the stream.

throws
IOException If an error occurs.
since
Android 1.0

        if (done) {
            return;
        }
        def.finish();
        int x = 0;
        while (!def.finished()) {
            if (def.needsInput()) {
                def.setInput(buf, 0, 0);
            }
            x = def.deflate(buf);
            out.write(buf, 0, x);
        }
        done = true;
    
public voidwrite(int i)

        byte[] b = new byte[1];
        b[0] = (byte) i;
        write(b, 0, 1);
    
public voidwrite(byte[] buffer, int off, int nbytes)
Compresses {@code nbytes} of data from {@code buf} starting at {@code off} and writes it to the underlying stream.

param
buffer the buffer of data to compress.
param
off offset in buffer to extract data from.
param
nbytes the number of bytes of data to read from the buffer.
throws
IOException If an error occurs during writing.
since
Android 1.0

        if (done) {
            throw new IOException(Messages.getString("archive.26")); //$NON-NLS-1$
        }
        // avoid int overflow, check null buf
        if (off <= buffer.length && nbytes >= 0 && off >= 0
                && buffer.length - off >= nbytes) {
            if (!def.needsInput()) {
                throw new IOException();
            }
            def.setInput(buffer, off, nbytes);
            deflate();
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }