use of java.util.zip.Deflater in project okio by square.
the class DeflaterSinkTest method closeWithExceptionWhenWritingAndClosing.
/**
* This test deflates a single segment of without compression because that's
* the easiest way to force close() to emit a large amount of data to the
* underlying sink.
*/
@Test
public void closeWithExceptionWhenWritingAndClosing() throws IOException {
MockSink mockSink = new MockSink();
mockSink.scheduleThrow(0, new IOException("first"));
mockSink.scheduleThrow(1, new IOException("second"));
Deflater deflater = new Deflater();
deflater.setLevel(Deflater.NO_COMPRESSION);
DeflaterSink deflaterSink = new DeflaterSink(mockSink, deflater);
deflaterSink.write(new Buffer().writeUtf8(repeat('a', Segment.SIZE)), Segment.SIZE);
try {
deflaterSink.close();
fail();
} catch (IOException expected) {
assertEquals("first", expected.getMessage());
}
mockSink.assertLogContains("close()");
}
use of java.util.zip.Deflater in project bilibili-android-client by HotBitmapGG.
the class BiliDanmukuCompressionTools method compress.
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
Deflater compressor = new Deflater();
try {
// 将当前压缩级别设置为指定值。
compressor.setLevel(compressionLevel);
compressor.setInput(value, offset, length);
// 调用时,指示压缩应当以输入缓冲区的当前内容结尾。
compressor.finish();
// Compress the data
final byte[] buf = new byte[1024];
while (!compressor.finished()) {
// 如果已到达压缩数据输出流的结尾,则返回 true。
int count = compressor.deflate(buf);
// 使用压缩数据填充指定缓冲区。
bos.write(buf, 0, count);
}
} finally {
// 关闭解压缩器并放弃所有未处理的输入。
compressor.end();
}
return bos.toByteArray();
}
use of java.util.zip.Deflater in project jodd by oblac.
the class ZipUtil method zlib.
/**
* Compresses a file into zlib archive.
*/
public static File zlib(File file) throws IOException {
if (file.isDirectory()) {
throw new IOException("Can't zlib folder");
}
FileInputStream fis = new FileInputStream(file);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
String zlibFileName = file.getAbsolutePath() + ZLIB_EXT;
DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater);
try {
StreamUtil.copy(fis, dos);
} finally {
StreamUtil.close(dos);
StreamUtil.close(fis);
}
return new File(zlibFileName);
}
use of java.util.zip.Deflater in project robovm by robovm.
the class OldAndroidZipStressTest method testZipDeflateInflateStress.
/**
* Native memory allocated by Deflater in system_server. The fix reduced
* some internal ZLIB buffers in size, so this test is trying to execute a
* lot of deflating to ensure that things are still working properly.
* http://b/1185084
*/
public void testZipDeflateInflateStress() throws Exception {
final int DATA_SIZE = 16384;
// Seed makes test reproducible
Random random = new Random(42);
// Outer loop selects "mode" of test.
for (int j = 1; j <= 2; j++) {
byte[] input = new byte[DATA_SIZE];
if (j == 1) {
// Totally random content
random.nextBytes(input);
} else {
// Random contents with longer repetitions
int pos = 0;
while (pos < input.length) {
byte what = (byte) random.nextInt(256);
int howMany = random.nextInt(32);
if (pos + howMany >= input.length) {
howMany = input.length - pos;
}
Arrays.fill(input, pos, pos + howMany, what);
pos += howMany;
}
}
// Inner loop tries all 9 compression levels.
for (int i = 1; i <= 9; i++) {
System.out.println("ZipDeflateInflateStress test (" + j + "," + i + ")...");
// Just to make sure...
byte[] zipped = new byte[2 * DATA_SIZE];
Deflater deflater = new Deflater(i);
deflater.setInput(input);
deflater.finish();
deflater.deflate(zipped);
deflater.end();
byte[] output = new byte[DATA_SIZE];
Inflater inflater = new Inflater();
inflater.setInput(zipped);
inflater.finished();
inflater.inflate(output);
inflater.end();
assertEquals(input, output);
}
}
}
use of java.util.zip.Deflater in project Dragonet-Legacy by DragonetMC.
the class BatchPacket method encode.
@Override
public void encode() {
try {
//We don't waste our memory
setShouldSendImmidate(true);
//Combine all packets
ByteArrayOutputStream packetCombinerData = new ByteArrayOutputStream();
PEBinaryWriter packetCombiner = new PEBinaryWriter(packetCombinerData);
for (PEPacket pk : packets) {
pk.encode();
packetCombiner.writeInt(pk.getData().length);
packetCombiner.write(pk.getData());
}
Deflater def = new Deflater(7);
def.reset();
def.setInput(packetCombinerData.toByteArray());
def.finish();
byte[] deflateBuffer = new byte[65535];
int size = def.deflate(deflateBuffer);
deflateBuffer = Arrays.copyOfRange(deflateBuffer, 0, size);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PEBinaryWriter writer = new PEBinaryWriter(bos);
writer.writeByte((byte) (this.pid() & 0xFF));
writer.writeInt(deflateBuffer.length);
writer.write(deflateBuffer);
this.setData(bos.toByteArray());
} catch (IOException e) {
}
}
Aggregations