下面列出了java.util.zip.Inflater#needsDictionary ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* java.util.zip.Inflater#getAdler()
*/
public void test_getAdler() {
// test method of java.util.zip.inflater.getAdler()
byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
Inflater inflateDiction = new Inflater();
inflateDiction.setInput(outPutDiction);
if (inflateDiction.needsDictionary() == true) {
// getting the checkSum value through the Adler32 class
Adler32 adl = new Adler32();
adl.update(dictionaryArray);
long checkSumR = adl.getValue();
assertEquals(
"the checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
inflateDiction.getAdler(), checkSumR);
}
inflateDiction.end();
}
@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());
}
private static void decompress(InputStream in, OutputStream out) throws IOException, DataFormatException
{
byte[] buf = new byte[2048];
int read = in.read(buf);
if (read > 0)
{
Inflater inflater = new Inflater();
inflater.setInput(buf,0,read);
byte[] res = new byte[2048];
while (true)
{
int resRead = inflater.inflate(res);
if (resRead != 0)
{
out.write(res,0,resRead);
continue;
}
if (inflater.finished() || inflater.needsDictionary() || in.available() == 0)
{
break;
}
read = in.read(buf);
inflater.setInput(buf,0,read);
}
}
out.flush();
}
@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;
}
/**
* 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));
}
}