Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 1997-2008 Day Management AG
* Barfuesserplatz 6, 4001 Basel, Switzerland
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Day Management AG, ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Day.
*/
package com.day.durbo.io;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipException;
/**
* This class implements a stream filter for uncompressing data in the
* "deflate" compression format. Most of its implementation is copied from the
* {@link InflaterInputStream} but in addition to its original, this one can
* handle several chunks of compressed data on the same underlying stream.
* such streams are for example generated by the
* {@link ChunkedDeflaterOutputStream}.
*/
public class ChunkedInflaterInputStream extends FilterInputStream {
/**
* Decompressor for this stream.
*/
protected Inflater inf;
/**
* Input buffer for decompression.
*/
protected byte[] buf;
/**
* Length of input buffer.
*/
protected int len;
/**
* true if closed
*/
private boolean closed = false;
/**
* this flag is set to true after EOF has reached
*/
private boolean reachEOF = false;
/**
* true if default inflater is used
*/
boolean usesDefaultInflater = false;
/**
* Check to make sure that this stream has not been closed
*
* @throws IOException if not open
*/
private void ensureOpen() throws IOException {
if (closed) {
throw new IOException("Stream closed");
}
}
/**
* Creates a new input stream with the specified decompressor and
* buffer size.
*
* @param in the input stream
* @param inf the decompressor ("inflater")
* @param size the input buffer size
* @throws IllegalArgumentException if size is <= 0
*/
public ChunkedInflaterInputStream(InputStream in, Inflater inf, int size) {
super(in);
if (in == null || inf == null) {
throw new NullPointerException();
} else if (size <= 0) {
throw new IllegalArgumentException("buffer size <= 0");
}
this.inf = inf;
buf = new byte[size];
}
/**
* Creates a new input stream with the specified decompressor and a
* default buffer size.
*
* @param in the input stream
* @param inf the decompressor ("inflater")
*/
public ChunkedInflaterInputStream(InputStream in, Inflater inf) {
this(in, inf, 8192);
}
/**
* Creates a new input stream with a default decompressor and buffer size.
*
* @param in the input stream
*/
public ChunkedInflaterInputStream(InputStream in) {
this(in, new Inflater());
usesDefaultInflater = true;
}
private byte[] singleByteBuf = new byte[1];
/**
* Reads a byte of uncompressed data. This method will block until
* enough input is available for decompression.
*
* @return the byte read, or -1 if end of compressed input is reached
* @throws IOException if an I/O error has occurred
*/
public int read() throws IOException {
ensureOpen();
return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff;
}
/**
* Reads uncompressed data into an array of bytes. This method will
* block until some input can be decompressed.
*
* @param b the buffer into which the data is read
* @param off the start offset of the data
* @param len the maximum number of bytes read
* @return the actual number of bytes read, or -1 if the end of the
* compressed input is reached or a preset dictionary is needed
* @throws ZipException if a ZIP format error has occurred
* @throws IOException if an I/O error has occurred
*/
public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
try {
int n;
while ((n = inf.inflate(b, off, len)) == 0) {
if (inf.finished() || inf.needsDictionary()) {
int r = inf.getRemaining();
// check if underlying stream would have more data
if (r == 0 && in.available() <= 0) {
reachEOF = true;
return -1;
}
inf.reset();
inf.setInput(buf, this.len - r, r);
}
if (inf.needsInput()) {
fill();
}
}
return n;
} catch (DataFormatException e) {
String s = e.getMessage();
throw new ZipException(s != null ? s : "Invalid ZLIB data format");
}
}
/**
* Returns 0 after EOF has reached, otherwise always return 1.
*
* Programs should not count on this method to return the actual number
* of bytes that could be read without blocking.
*
* @return 1 before EOF and 0 after EOF.
* @throws IOException if an I/O error occurs.
*/
public int available() throws IOException {
ensureOpen();
if (reachEOF) {
return 0;
} else {
return 1;
}
}
private byte[] b = new byte[512];
/**
* Skips specified number of bytes of uncompressed data.
*
* @param n the number of bytes to skip
* @return the actual number of bytes skipped.
* @throws IOException if an I/O error has occurred
* @throws IllegalArgumentException if n < 0
*/
public long skip(long n) throws IOException {
if (n < 0) {
throw new IllegalArgumentException("negative skip length");
}
ensureOpen();
int max = (int) Math.min(n, Integer.MAX_VALUE);
int total = 0;
while (total < max) {
int len = max - total;
if (len > b.length) {
len = b.length;
}
len = read(b, 0, len);
if (len == -1) {
reachEOF = true;
break;
}
total += len;
}
return total;
}
/**
* Closes the input stream.
*
* @throws IOException if an I/O error has occurred
*/
public void close() throws IOException {
if (!closed) {
if (usesDefaultInflater)
inf.end();
in.close();
closed = true;
}
}
/**
* Fills input buffer with more data to decompress.
*
* @throws IOException if an I/O error has occurred
*/
protected void fill() throws IOException {
ensureOpen();
len = in.read(buf, 0, buf.length);
if (len == -1) {
throw new EOFException("Unexpected end of ZLIB input stream");
}
inf.setInput(buf, 0, len);
}
}