/** Sets the deflate compression level. Default is {@link Deflater#DEFAULT_COMPRESSION}. */ public void setCompression (int level) { deflater.setLevel(level); }
/** Sets the deflate compression level. Default is {@link Deflater#DEFAULT_COMPRESSION}. */ public void setCompression (int level) { deflater.setLevel(level); }
public void setLevel( int level ) { def.setLevel(level); } }
/** * Level is 1-9 -- 1 being best speed, and 9 being best compression */ public GZIPOutputStreamEx(OutputStream out, int level) throws IOException { super(out); def.setLevel(level); } }
public ZipOutputStream(final OutputStream out, final int compressionLevel) { super(out); def.setLevel(compressionLevel); } }
public GZIPOutputStream(final OutputStream out, final int compressionLevel) throws IOException { super(out); def.setLevel(compressionLevel); } }
public static byte[] gzip(byte[] b) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(b.length); GZIPOutputStream out = new GZIPOutputStream(baos, Math.min(65536, b.length)){{def.setLevel(Deflater.BEST_COMPRESSION);}}; out.write(b, 0, b.length); out.finish(); out.close(); return baos.toByteArray(); } catch (IOException e) {} return null; }
public static void gzip(File source, File dest, boolean deleteSource) throws IOException { byte[] buffer = new byte[2^20]; GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest), 65536)){{def.setLevel(Deflater.BEST_COMPRESSION);}}; FileInputStream in = new FileInputStream(source); int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l); in.close(); out.finish(); out.close(); if (deleteSource && dest.exists()) source.delete(); }
public byte[] compress(byte[] in) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Deflater deflater = new Deflater(Deflater.HUFFMAN_ONLY); deflater.setLevel(9); final DeflaterOutputStream gz = new DeflaterOutputStream(baos, deflater); try { gz.write(in); gz.close(); baos.close(); return baos.toByteArray(); } catch (IOException e) { throw new IllegalStateException(e.toString()); } }
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(); 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(); }
/** * Compresses the specified byte range using the specified compressionLevel (constants are * defined in java.util.zip.Deflater). */ public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { /* Create an expandable byte array to hold the compressed data. * You cannot use an array that's the same size as the orginal because * there is no guarantee that the compressed data will be smaller than * the uncompressed data. */ ByteArrayOutputStream bos = new ByteArrayOutputStream(length); Deflater compressor = new Deflater(); try { compressor.setLevel(compressionLevel); compressor.setInput(value, offset, length); compressor.finish(); final byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
private JsonCapsuleFactory(JSONObject jo) { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); GZIPOutputStream out = null; try {out = new GZIPOutputStream(baos, 1024){{def.setLevel(Deflater.BEST_COMPRESSION);}};} catch (IOException e) {} OutputStreamWriter osw = new OutputStreamWriter(out); jo.write(osw); try {osw.close();} catch (IOException e) {} //byte[] b = new ObjectMapper().writer().writeValueAsBytes(json); //byte[] c = Compression.gzip(b); byte[] c = baos.toByteArray(); //if (b.length <= c.length) { // this.capsule = new byte[b.length + 1]; // this.capsule[0] = 0; // System.arraycopy(b, 0, this.capsule, 1, b.length); //} else { this.capsule = new byte[c.length + 1]; this.capsule[0] = 1; System.arraycopy(c, 0, this.capsule, 1, c.length); //} //System.out.print("DEBUG " + this.getRawJson()); }
private static byte[] deflate(byte[] input) throws IOException { Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input); compressor.finish(); try (ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length)) { byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } return bos.toByteArray(); } } }
@Test public void multipleSegmentsWithoutCompression() throws IOException { Buffer buffer = new Buffer(); Deflater deflater = new Deflater(); deflater.setLevel(Deflater.NO_COMPRESSION); DeflaterSink deflaterSink = new DeflaterSink(buffer, deflater); int byteCount = SEGMENT_SIZE * 4; deflaterSink.write(new Buffer().writeUtf8(repeat('a', byteCount)), byteCount); deflaterSink.close(); assertEquals(repeat('a', byteCount), inflate(buffer).readUtf8(byteCount)); }
/** * 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()"); }
@Test public void gzipSink() throws Exception { Pipe pipe = new Pipe(1024 * 1024); GzipSink gzipSink = new GzipSink(pipe.sink()); // Disable compression to speed up a slow test. Improved from 141s to 35s on one machine. gzipSink.deflater().setLevel(Deflater.NO_COMPRESSION); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), gzipSink); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GZIPInputStream gzipIn = new GZIPInputStream(Okio.buffer(pipe.source()).inputStream()); readAllAndClose(Okio.source(gzipIn), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
def.setLevel(level); hasCompressionLevelChanged = false;
def.setLevel(level); hasCompressionLevelChanged = false;
@Test public void gzipSource() throws Exception { Pipe pipe = new Pipe(1024 * 1024); OutputStream gzipOut = new GZIPOutputStream(Okio.buffer(pipe.sink()).outputStream()) { { // Disable compression to speed up a slow test. Improved from 141s to 33s on one machine. def.setLevel(Deflater.NO_COMPRESSION); } }; Future<Long> future = readAllAndCloseAsync( randomSource(FOUR_GIB_PLUS_ONE), Okio.sink(gzipOut)); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GzipSource gzipSource = new GzipSource(pipe.source()); readAllAndClose(gzipSource, hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
def.setLevel(level); hasCompressionLevelChanged = false;