use of java.util.zip.Deflater in project intellij-community by JetBrains.
the class ConnectionStreams method setGzipped.
// Actions ================================================================
// Actions ================================================================
public void setGzipped() throws IOException {
loggedWriter.flush();
loggedOutputStream.flush();
deflaterOutputStream = new DeflaterOutputStream(connection.getOutputStream(), new Deflater(6));
setOutputStream(deflaterOutputStream);
setInputStream(new InflaterInputStream(connection.getInputStream()));
}
use of java.util.zip.Deflater in project teaTime by ancfdy.
the class AppZLibMgr method compress.
/**
* 压缩
*
* @param data
*
* @return byte[] 压缩后的数据
*/
public static byte[] compress(byte[] data) {
byte[] output = new byte[0];
Deflater compresser = new Deflater();
compresser.reset();
compresser.setInput(data);
compresser.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!compresser.finished()) {
int i = compresser.deflate(buf);
bos.write(buf, 0, i);
}
output = bos.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
compresser.end();
return output;
}
use of java.util.zip.Deflater in project eiger by wlloyd.
the class DeflateCompressor method compress.
public int compress(byte[] input, int inputOffset, int inputLength, ICompressor.WrappedArray output, int outputOffset) throws IOException {
Deflater def = deflater.get();
def.reset();
def.setInput(input, inputOffset, inputLength);
def.finish();
if (def.needsInput())
return 0;
int offs = outputOffset;
while (true) {
offs += def.deflate(output.buffer, offs, output.buffer.length - offs);
if (def.finished()) {
return offs - outputOffset;
} else {
// We're not done, output was too small. Increase it and continue
byte[] newBuffer = new byte[(output.buffer.length * 4) / 3 + 1];
System.arraycopy(output.buffer, 0, newBuffer, 0, offs);
output.buffer = newBuffer;
}
}
}
use of java.util.zip.Deflater in project robovm by robovm.
the class DeflaterOutputStreamTest method testSyncFlushDeflater.
/**
* Confirm that a DeflaterOutputStream constructed with Deflater
* with flushParm == SYNC_FLUSH does not need to to be flushed.
*
* http://b/4005091
*/
public void testSyncFlushDeflater() throws Exception {
Deflater def = new Deflater();
Field f = def.getClass().getDeclaredField("flushParm");
f.setAccessible(true);
f.setInt(def, Deflater.SYNC_FLUSH);
final int deflaterBufferSize = 512;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos, def, deflaterBufferSize);
// make output buffer large enough that even if compressed it
// won't all fit within the deflaterBufferSize.
final int outputBufferSize = 128 * deflaterBufferSize;
byte[] output = new byte[outputBufferSize];
for (int i = 0; i < output.length; i++) {
output[i] = (byte) i;
}
dos.write(output);
byte[] compressed = baos.toByteArray();
// this main reason for this assert is to make sure that the
// compressed byte count is larger than the
// deflaterBufferSize. However, when the original bug exists,
// it will also fail because the compressed length will be
// exactly the length of the deflaterBufferSize.
assertTrue("compressed=" + compressed.length + " but deflaterBufferSize=" + deflaterBufferSize, compressed.length > deflaterBufferSize);
// assert that we returned data matches the input exactly.
ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
InflaterInputStream iis = new InflaterInputStream(bais);
byte[] input = new byte[output.length];
int total = 0;
while (true) {
int n = iis.read(input, total, input.length - total);
if (n == -1) {
break;
}
total += n;
if (total == input.length) {
try {
iis.read();
fail();
} catch (EOFException expected) {
break;
}
}
}
assertEquals(output.length, total);
assertTrue(Arrays.equals(input, output));
// ensure Deflater.finish has not been called at any point
// during the test, since that would lead to the results being
// flushed even without SYNC_FLUSH being used
assertFalse(def.finished());
// Quieten CloseGuard.
def.end();
iis.close();
}
use of java.util.zip.Deflater in project robovm by robovm.
the class InflaterTest method deflate.
private static byte[] deflate(byte[] input, byte[] dictionary) {
Deflater deflater = new Deflater();
if (dictionary != null) {
deflater.setDictionary(dictionary);
}
deflater.setInput(input);
deflater.finish();
ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
byte[] buf = new byte[8];
while (!deflater.finished()) {
int byteCount = deflater.deflate(buf);
deflatedBytes.write(buf, 0, byteCount);
}
deflater.end();
return deflatedBytes.toByteArray();
}
Aggregations