use of java.util.zip.Inflater in project robovm by robovm.
the class SpdyReader method newNameValueBlockStream.
private DataInputStream newNameValueBlockStream() {
// Limit the inflater input stream to only those bytes in the Name/Value block.
final InputStream throttleStream = new InputStream() {
@Override
public int read() throws IOException {
return Util.readSingleByte(this);
}
@Override
public int read(byte[] buffer, int offset, int byteCount) throws IOException {
byteCount = Math.min(byteCount, compressedLimit);
int consumed = in.read(buffer, offset, byteCount);
compressedLimit -= consumed;
return consumed;
}
@Override
public void close() throws IOException {
in.close();
}
};
// Subclass inflater to install a dictionary when it's needed.
Inflater inflater = new Inflater() {
@Override
public int inflate(byte[] buffer, int offset, int count) throws DataFormatException {
int result = super.inflate(buffer, offset, count);
if (result == 0 && needsDictionary()) {
setDictionary(DICTIONARY);
result = super.inflate(buffer, offset, count);
}
return result;
}
};
return new DataInputStream(new InflaterInputStream(throttleStream, inflater));
}
use of java.util.zip.Inflater in project robovm by robovm.
the class FilterInputStreamNullSourceTest method testInflaterInputStream.
public void testInflaterInputStream() throws IOException {
try {
new InflaterInputStream(null);
fail();
} catch (NullPointerException expected) {
}
try {
new InflaterInputStream(null, new Inflater());
fail();
} catch (NullPointerException expected) {
}
try {
new InflaterInputStream(null, new Inflater(), 1024);
fail();
} catch (NullPointerException expected) {
}
}
use of java.util.zip.Inflater in project robovm by robovm.
the class DeflaterTest method testDeflate.
public void testDeflate() throws DataFormatException {
deflater.setInput(new byte[] { 1, 2, 3 });
deflateInflate(Deflater.NO_FLUSH);
assertTrue(totalInflated < 3);
// the 3rd byte shouldn't have been flushed yet
assertEquals(0, decompressed[2]);
deflater.setInput(new byte[] { 4, 5, 6 });
deflateInflate(Deflater.SYNC_FLUSH);
assertEquals(6, totalInflated);
assertDecompressed(1, 2, 3, 4, 5, 6);
assertEquals(0, inflater.inflate(decompressed));
deflater.setInput(new byte[] { 7, 8, 9 });
deflateInflate(Deflater.FULL_FLUSH);
assertEquals(9, totalInflated);
assertDecompressed(1, 2, 3, 4, 5, 6, 7, 8, 9);
assertEquals(0, inflater.inflate(decompressed));
// safe because we did a FULL_FLUSH
inflater = new Inflater(true);
deflater.setInput(new byte[] { 10, 11, 12 });
deflateInflate(Deflater.SYNC_FLUSH);
assertEquals(12, totalInflated);
assertDecompressed(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
assertEquals(0, inflater.inflate(decompressed));
}
use of java.util.zip.Inflater in project robovm by robovm.
the class OldAndroidDeflateTest method bigTest.
/*
* "step" determines how compressible the data is.
*
* Note we must set "nowrap" to false, or the Adler-32 doesn't get
* computed.
*/
private void bigTest(int step, int expectedAdler) throws UnsupportedEncodingException, DataFormatException {
byte[] input = new byte[128 * 1024];
byte[] comp = new byte[128 * 1024 + 512];
byte[] output = new byte[128 * 1024 + 512];
Inflater inflater = new Inflater(false);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);
createSample(input, step);
compress(deflater, input, comp);
expand(inflater, comp, (int) deflater.getBytesWritten(), output);
assertEquals(inflater.getBytesWritten(), input.length);
assertEquals(deflater.getAdler(), inflater.getAdler());
assertEquals(deflater.getAdler(), expectedAdler);
}
use of java.util.zip.Inflater in project robovm by robovm.
the class OldAndroidDeflateTest method simpleTest.
/*
* Simple inflate/deflate test, taken from the reference docs for the
* Inflater/Deflater classes.
*/
private void simpleTest() throws UnsupportedEncodingException, DataFormatException {
// Encode a String into bytes
String inputString = "blahblahblah??";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
assertEquals(inputString, outputString);
assertEquals(compresser.getAdler(), decompresser.getAdler());
decompresser.end();
}
Aggregations