java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.util.zip.InflaterOutputStream
net.i2p.i2ptunnel.util.GunzipOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Gunzip implementation per RFC 1952, reusing java's standard CRC32 and Inflater and InflaterOutputStream implementations.
Note that the underlying InflaterOutputStream cannot be reused after close(), so we don't have a Reusable version of this.
Sets up GunzipOutputStream -- InflaterOutputStream -- CRC32OutputStream -- uncompressedStream
Not a public API, subject to change, not for external use.
Modified from net.i2p.util.ResettableGZIPInputStream to use Java 6 InflaterOutputstream.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCalculate CRC32 along the way.private static enumState of the gzip header parse.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]One-byte read buffer.private longTotal bytes fed to the stream.private longByte count when the stream completed.private final LimitOutputStream.DoneCallbackCallback run when the stream completes.private booleanTrue once the gzip stream is complete.private intBytes left of the extra header field.private intThe gzip flags read from the header.private final byte[]Footer bytes read so far.private final LogLogger for the gunzip stream.private GunzipOutputStream.HeaderStateCurrent state of the header parse.private booleanTrue once the footer checks passed.private static final intSize of the gzip footer, in bytes.Fields inherited from class InflaterOutputStream
buf, infFields inherited from class FilterOutputStream
outConstructor Summary
ConstructorsConstructorDescriptionGunzipOutputStream(OutputStream uncompressedStream) Build a new Gunzip stream.GunzipOutputStream(OutputStream uncompressedStream, LimitOutputStream.DoneCallback cb) With a callback when done.Method Summary
Modifier and TypeMethodDescriptionvoidclose()booleanInflater statistic.longInflater statistic.longInflater statistic.longInflater statistic.toString()private voidCheck the gzip CRC and size trailer.private voidverifyHeader(byte b) Make sure the header is valid, throwing an IOException if it is bad.voidwrite(byte[] buf, int off, int len) voidwrite(int b) Methods inherited from class InflaterOutputStream
finish, flushMethods inherited from class FilterOutputStream
writeMethods inherited from class OutputStream
nullOutputStream
Field Details
FOOTER_SIZE
private static final int FOOTER_SIZESize of the gzip footer, in bytes.- See Also:
_buf1
private final byte[] _buf1One-byte read buffer._complete
private boolean _completeTrue once the gzip stream is complete._validated
private boolean _validatedTrue once the footer checks passed._bytesReceived
private long _bytesReceivedTotal bytes fed to the stream._bytesReceivedAtCompletion
private long _bytesReceivedAtCompletionByte count when the stream completed._state
Current state of the header parse._flags
private int _flagsThe gzip flags read from the header._extHdrToRead
private int _extHdrToReadBytes left of the extra header field._callback
Callback run when the stream completes._log
Logger for the gunzip stream.
Constructor Details
GunzipOutputStream
Build a new Gunzip stream.- Parameters:
uncompressedStream- the sink the decompressed bytes are written to- Throws:
IOException- if the underlying CRC32/Deflater stream cannot be created
GunzipOutputStream
With a callback when done.- Parameters:
uncompressedStream- the sink the decompressed bytes are written tocb- may be null
Method Details
write
- Overrides:
writein classInflaterOutputStream- Throws:
IOException
write
- Overrides:
writein classInflaterOutputStream- Throws:
IOException
getTotalRead
public long getTotalRead()Inflater statistic.- Returns:
- the total read
getTotalExpanded
public long getTotalExpanded()Inflater statistic.- Returns:
- the total expanded
getRemaining
public long getRemaining()Inflater statistic.- Returns:
- the number of inflated bytes still buffered inside the inflater, or 0 if the inflater is unavailable
getFinished
public boolean getFinished()Inflater statistic.- Returns:
- true if the inflater has finished decompressing the compressed input; also true if the inflater is unavailable
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInflaterOutputStream- Throws:
IOException
toString
verifyHeader
Make sure the header is valid, throwing an IOException if it is bad. Pushes through the state machine, checking as we go. Call for each byte until HeaderState is DONE.- Parameters:
b- the next header byte, read as unsigned- Throws:
IOException- if the byte does not match what the current header state expects