下面列出了java.util.zip.Inflater#needsInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
assert (buffer != this.buffer);
try {
do {
int read = inflater.inflate(buffer, offset, length);
if(read <= 0) {
if(inflater.finished()) {
throw new EOFException();
}
if(inflater.needsInput()) {
refillInflater(inflater);
} else {
throw new IOException("Can't inflate " + length + " bytes");
}
} else {
offset += read;
length -= read;
}
} while(length > 0);
} catch(DataFormatException ex) {
throw (IOException)(new IOException("inflate error").initCause(ex));
}
}
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
try {
do {
int read = inflater.inflate(buffer, offset, length);
if(read <= 0) {
if(inflater.finished()) {
throw new EOFException();
}
if(inflater.needsInput()) {
refillInflater(inflater);
} else {
throw new IOException("Can't inflate " + length + " bytes");
}
} else {
offset += read;
length -= read;
}
} while(length > 0);
} catch (DataFormatException ex) {
throw (IOException)(new IOException("inflate error").initCause(ex));
}
}
public static DeflaterInflaterData uncompressBytes(byte[] output, int compressedDataLength) throws DataFormatException {
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] buffer = new byte[512];
byte[] result = new byte[0];
int bytesRead;
while (!decompresser.needsInput()) {
bytesRead = decompresser.inflate(buffer);
byte[] newResult = new byte[result.length + bytesRead];
System.arraycopy(result, 0, newResult, 0, result.length);
System.arraycopy(buffer, 0, newResult, result.length, bytesRead);
result = newResult;
}
// System.out.println(new String(result));
decompresser.end();
return new DeflaterInflaterData(result.length, result);
}
public static DeflaterInflaterData uncompressBytes(byte[] output, int compressedDataLength) throws DataFormatException {
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] buffer = new byte[512];
byte[] result = new byte[0];
int bytesRead;
while (!decompresser.needsInput()) {
bytesRead = decompresser.inflate(buffer);
byte[] newResult = new byte[result.length + bytesRead];
System.arraycopy(result, 0, newResult, 0, result.length);
System.arraycopy(buffer, 0, newResult, result.length, bytesRead);
result = newResult;
}
// System.out.println(new String(result));
decompresser.end();
return new DeflaterInflaterData(result.length, result);
}
public static byte[] b(byte abyte0[])
{
int i = 0;
if (abyte0 == null || abyte0.length == 0)
{
return null;
}
Inflater inflater = new Inflater();
inflater.setInput(abyte0, 0, abyte0.length);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte abyte1[] = new byte[1024];
do
{
if (inflater.needsInput())
{
inflater.end();
return bytearrayoutputstream.toByteArray();
}
int j = inflater.inflate(abyte1);
bytearrayoutputstream.write(abyte1, i, j);
i += j;
} while (true);
}
public static InputStream inflate(byte[] deflatedToken, boolean nowrap)
throws DataFormatException {
Inflater inflater = new Inflater(true);
inflater.setInput(deflatedToken);
byte[] buffer = new byte[deflatedToken.length];
int inflateLen;
ByteArrayOutputStream inflatedToken = new ByteArrayOutputStream();
while (!inflater.finished()) {
inflateLen = inflater.inflate(buffer, 0, deflatedToken.length);
if (inflateLen == 0 && !inflater.finished()) {
if (inflater.needsInput()) {
throw new DataFormatException("Inflater can not inflate all the token bytes");
}
break;
}
inflatedToken.write(buffer, 0, inflateLen);
}
return new ByteArrayInputStream(inflatedToken.toByteArray());
}
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
try {
do {
int read = inflater.inflate(buffer, offset, length);
if(read <= 0) {
if(inflater.finished()) {
throw new EOFException();
}
if(inflater.needsInput()) {
refillInflater(inflater);
} else {
throw new IOException("Can't inflate " + length + " bytes");
}
} else {
offset += read;
length -= read;
}
} while(length > 0);
} catch (DataFormatException ex) {
throw (IOException)(new IOException("inflate error").initCause(ex));
}
}
@Override
public void decompress(ByteBuffer in, ByteBuffer out) throws IOException {
Inflater inflater = new Inflater(true);
inflater.setInput(in.array(), in.arrayOffset() + in.position(),
in.remaining());
while (!(inflater.finished() || inflater.needsDictionary() ||
inflater.needsInput())) {
try {
int count = inflater.inflate(out.array(),
out.arrayOffset() + out.position(),
out.remaining());
out.position(count + out.position());
} catch (DataFormatException dfe) {
throw new IOException("Bad compression data", dfe);
}
}
out.flip();
inflater.end();
in.position(in.limit());
}
public ByteBuffer getData() {
try {
byte[] input = new byte[1024];
byte[] output = new byte[1024];
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Inflater inflater = new Inflater(true);
while (true) {
int numRead = fin.read(input);
if (numRead != -1) {
inflater.setInput(input, 0, numRead);
}
int numDecompressed;
while ((numDecompressed = inflater.inflate(output, 0, output.length)) != 0) {
bos.write(output, 0, numDecompressed);
}
if (inflater.finished()) {
break;
}
else if (inflater.needsInput()) {
continue;
}
}
inflater.end();
ByteBuffer result = ByteBuffer.wrap(bos.toByteArray(), 0, bos.size());
bos.close();
fin.close();
return result;
} catch (Exception e) {
FileLog.e(e);
}
return null;
}
public ByteBuffer getData() {
try {
byte[] input = new byte[1024];
byte[] output = new byte[1024];
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Inflater inflater = new Inflater(true);
while (true) {
int numRead = fin.read(input);
if (numRead != -1) {
inflater.setInput(input, 0, numRead);
}
int numDecompressed;
while ((numDecompressed = inflater.inflate(output, 0, output.length)) != 0) {
bos.write(output, 0, numDecompressed);
}
if (inflater.finished()) {
break;
} else if (inflater.needsInput()) {
continue;
}
}
inflater.end();
ByteBuffer result = ByteBuffer.wrap(bos.toByteArray(), 0, bos.size());
bos.close();
fin.close();
return result;
} catch (Exception e) {
FileLog.e(e);
}
return null;
}
/**
* java.util.zip.Inflater#Inflater(boolean)
*/
public void test_ConstructorZ() {
// test method of java.util.zip.inflater.Inflater(boolean)
// note does not throw exception if deflater has a header, but inflater
// doesn't or vice versa.
byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
Inflater inflate = new Inflater(true);
assertNotNull("failed to create the instance of inflater", inflate);
byte outPutInf[] = new byte[500];
int r = 0;
try {
while (!(inflate.finished())) {
if (inflate.needsInput()) {
inflate.setInput(outPutBuff1);
}
inflate.inflate(outPutInf);
}
for (int i = 0; i < byteArray.length; i++) {
assertEquals("the output array from inflate should contain 0 because the"
+ " header of inflate and deflate did not match, but this failed",
0, outPutBuff1[i]);
}
} catch (DataFormatException e) {
r = 1;
}
assertEquals("Error: exception should be thrown because of header inconsistency", 1, r);
inflate.end();
}
private void expand(Inflater inflater, byte[] inBuf, int inCount,
byte[] outBuf) throws DataFormatException {
int inPosn;
int outPosn;
inPosn = outPosn = 0;
//System.out.println("### starting expand, inCount is " + inCount);
while (!inflater.finished()) {
int want = -1, got;
// only read if the input buffer is empty
if (inflater.needsInput() && inCount != 0) {
want = (inCount < LOCAL_BUF_SIZE) ? inCount : LOCAL_BUF_SIZE;
inflater.setInput(inBuf, inPosn, want);
inCount -= want;
inPosn += want;
}
// inflate to current position in output buffer
int compCount;
compCount = inflater.inflate(outBuf, outPosn, LOCAL_BUF_SIZE);
outPosn += compCount;
//System.out.println("Expanded " + want + ", output " + compCount);
}
}
public ByteBuffer getData() {
try {
byte[] input = new byte[1024];
byte[] output = new byte[1024];
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Inflater inflater = new Inflater(true);
while (true) {
int numRead = fin.read(input);
if (numRead != -1) {
inflater.setInput(input, 0, numRead);
}
int numDecompressed;
while ((numDecompressed = inflater.inflate(output, 0, output.length)) != 0) {
bos.write(output, 0, numDecompressed);
}
if (inflater.finished()) {
break;
} else if (inflater.needsInput()) {
continue;
}
}
inflater.end();
ByteBuffer result = ByteBuffer.wrap(bos.toByteArray(), 0, bos.size());
bos.close();
fin.close();
return result;
} catch (Exception e) {
FileLog.e(e);
}
return null;
}
/**
* zlib decompress 2 byte
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int bufferSizeInBytes = numberOfBytesToDecompress;
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();
try {
while (inflater.needsInput() == false) {
byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
/**
* Returns the non-null InputStream that should be returned to by all
* requests to {@link #getContent()}.
*
* @return a non-null InputStream
* @throws IOException
* if there was a problem
*/
@Override
InputStream getDecompressingInputStream(final InputStream wrapped)
throws IOException {
/*
* A zlib stream will have a header.
*
* CMF | FLG [| DICTID ] | ...compressed data | ADLER32 |
*
* * CMF is one byte.
*
* * FLG is one byte.
*
* * DICTID is four bytes, and only present if FLG.FDICT is set.
*
* Sniff the content. Does it look like a zlib stream, with a CMF, etc?
* c.f. RFC1950, section 2.2. http://tools.ietf.org/html/rfc1950#page-4
*
* We need to see if it looks like a proper zlib stream, or whether it
* is just a deflate stream. RFC2616 calls zlib streams deflate.
* Confusing, isn't it? That's why some servers implement deflate
* Content-Encoding using deflate streams, rather than zlib streams.
*
* We could start looking at the bytes, but to be honest, someone else
* has already read the RFCs and implemented that for us. So we'll just
* use the JDK libraries and exception handling to do this. If that
* proves slow, then we could potentially change this to check the first
* byte - does it look like a CMF? What about the second byte - does it
* look like a FLG, etc.
*/
/* We read a small buffer to sniff the content. */
final byte[] peeked = new byte[6];
final PushbackInputStream pushback = new PushbackInputStream(wrapped,
peeked.length);
final int headerLength = pushback.read(peeked);
if (headerLength == -1) {
throw new IOException("Unable to read the response");
}
/* We try to read the first uncompressed byte. */
final byte[] dummy = new byte[1];
final Inflater inf = new Inflater();
try {
int n;
while ((n = inf.inflate(dummy)) == 0) {
if (inf.finished()) {
/* Not expecting this, so fail loudly. */
throw new IOException("Unable to read the response");
}
if (inf.needsDictionary()) {
/*
* Need dictionary - then it must be zlib stream with DICTID
* part?
*/
break;
}
if (inf.needsInput()) {
inf.setInput(peeked);
}
}
if (n == -1) {
throw new IOException("Unable to read the response");
}
/*
* We read something without a problem, so it's a valid zlib stream.
* Just need to reset and return an unused InputStream now.
*/
pushback.unread(peeked, 0, headerLength);
return new InflaterInputStream(pushback);
} catch (final DataFormatException e) {
/*
* Presume that it's an RFC1951 deflate stream rather than RFC1950
* zlib stream and try again.
*/
pushback.unread(peeked, 0, headerLength);
return new InflaterInputStream(pushback, new Inflater(true));
}
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress
* @return
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int bufferSizeInBytes = numberOfBytesToDecompress;
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();
try {
while (inflater.needsInput() == false) {
byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress
* @return
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<>();
try {
while (!inflater.needsInput()) {
byte[] bytesDecompressedBuffer = new byte[numberOfBytesToDecompress];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = bytesDecompressedSoFar.get(b);
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress
* @return
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int bufferSizeInBytes = numberOfBytesToDecompress;
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();
try {
while (inflater.needsInput() == false) {
byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
@Override
public int inflate(byte[] b, int off, int len) throws DataFormatException {
if (finished) {
if (in.hasRemaining()) throw new DataFormatException("zlib stream ended unexpectedly");
return 0;
}
if (!in.hasRemaining()) return 0;
if (determineWrapper) {
// First two bytes are needed to decide if it's a ZLIB stream.
if (accumulator.remaining() + len < 2) {
buffer();
return 0;
}
byte[] cmf_flg = new byte[2];
cmf_flg[0] = readByte();
cmf_flg[1] = readByte();
boolean nowrap = !looksLikeZlib(cmf_flg[0], cmf_flg[1]);
inflater = new Inflater(nowrap);
inflater.inflate(cmf_flg, 0, 2);
determineWrapper = false;
}
if (crc != null) {
switch (gzipState) {
case FOOTER_START:
if (readGZIPFooter()) {
finished = true;
}
return 0;
default:
if (gzipState != GzipState.HEADER_END) {
if (!readGZIPHeader()) {
return 0;
}
}
}
// Some bytes may have been consumed, and so we must re-set the number of readable bytes.
// readableBytes = in.readableBytes();
}
inflater.setInput(in.array(), in.arrayOffset() + in.position(), in.remaining());
boolean readFooter = false;
int totalWritten = 0;
while (off < len && !inflater.needsInput()) {
int bytesWritten = inflater.inflate(b, off, len);
if (bytesWritten > 0) {
totalWritten += bytesWritten;
if (crc != null) {
crc.update(b, off, bytesWritten);
}
off += bytesWritten;
} else {
if (inflater.needsDictionary()) {
inflater.setDictionary(dictionary);
}
}
if (inflater.finished()) {
if (crc == null) {
finished = true; // Do not decode anymore.
} else {
readFooter = true;
}
break;
}
}
in.position(in.position() + in.remaining() - inflater.getRemaining());
if (readFooter) {
gzipState = GzipState.FOOTER_START;
if (readGZIPFooter()) {
finished = true;
}
}
return totalWritten;
}
private static void assertRoundTrip(byte[] dictionary) throws Exception {
// Construct a nice long input byte sequence.
String expected = makeString();
byte[] expectedBytes = expected.getBytes("UTF-8");
// Compress the bytes, using the passed-in dictionary (or no dictionary).
byte[] deflatedBytes = deflate(expectedBytes, dictionary);
// Get ready to decompress deflatedBytes back to the original bytes ...
Inflater inflater = new Inflater();
// We'll only supply the input a little bit at a time, so that zlib has to ask for more.
final int CHUNK_SIZE = 16;
int offset = 0;
inflater.setInput(deflatedBytes, offset, CHUNK_SIZE);
offset += CHUNK_SIZE;
// If we used a dictionary to compress, check that we're asked for that same dictionary.
if (dictionary != null) {
// 1. there's no data available immediately...
assertEquals(0, inflater.inflate(new byte[8]));
// 2. ...because you need a dictionary.
assertTrue(inflater.needsDictionary());
// 3. ...and that dictionary has the same Adler32 as the dictionary we used.
assertEquals(adler32(dictionary), inflater.getAdler());
inflater.setDictionary(dictionary);
}
// Do the actual decompression, now the dictionary's set up appropriately...
// We use a tiny output buffer to ensure that we call inflate multiple times, and
// a tiny input buffer to ensure that zlib has to ask for more input.
ByteArrayOutputStream inflatedBytes = new ByteArrayOutputStream();
byte[] buf = new byte[8];
while (inflatedBytes.size() != expectedBytes.length) {
if (inflater.needsInput()) {
int nextChunkByteCount = Math.min(CHUNK_SIZE, deflatedBytes.length - offset);
inflater.setInput(deflatedBytes, offset, nextChunkByteCount);
offset += nextChunkByteCount;
} else {
int inflatedByteCount = inflater.inflate(buf);
if (inflatedByteCount > 0) {
inflatedBytes.write(buf, 0, inflatedByteCount);
}
}
}
inflater.end();
assertEquals(expected, new String(inflatedBytes.toByteArray(), "UTF-8"));
}