private void closeEncoder() { if (encodingStream == null) { return; } try { encodingStream.close(); } catch (IOException e) { throw new IllegalStateException( "Error closing encodingStream, this should not happen with byte arrays.", e); } }
@Override public void close() throws IOException { try { out.close(); } finally { deflater.end(); } } }
@Override public void close() throws IOException { try { deflaterOutputStream.close(); } finally { deflater.end(); } } }
@Override public void close() throws IOException { super.close(); def.end(); } };
public static byte[] compress(final byte[] src, final int level) throws IOException { byte[] result = src; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length); java.util.zip.Deflater defeater = new java.util.zip.Deflater(level); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater); try { deflaterOutputStream.write(src); deflaterOutputStream.finish(); deflaterOutputStream.close(); result = byteArrayOutputStream.toByteArray(); } catch (IOException e) { defeater.end(); throw e; } finally { try { byteArrayOutputStream.close(); } catch (IOException ignored) { } defeater.end(); } return result; }
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()); } }
@Override public void close() throws IOException { Hessian2Output out = _out; _out = null; if (out != null) { _deflateOut.close(); _bodyOut.close(); out.writeInt(0); out.completeEnvelope(); out.close(); } } }
public static byte[] compress(String string) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(string.length()); GZIPOutputStream gos = new GZIPOutputStream(os); gos.write(string.getBytes()); gos.close(); byte[] compressed = os.toByteArray(); os.close(); return compressed; } public static String decompress(byte[] compressed) throws IOException { final int BUFFER_SIZE = 32; ByteArrayInputStream is = new ByteArrayInputStream(compressed); GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE); StringBuilder string = new StringBuilder(); byte[] data = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = gis.read(data)) != -1) { string.append(new String(data, 0, bytesRead)); } gis.close(); is.close(); return string.toString(); }
GZIPOutputStream gos = new GZIPOutputStream(os); gos.write(string.getBytes()); gos.close(); os.close(); byte[] compressed = new byte[4 + os.toByteArray().length];
public static byte[] compress(byte[] input) throws IOException { // Destination where compressed data will be stored. ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create a compressor. Deflater deflater = createDeflater(); DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater); // Compress the data. // // Some other implementations such as Jetty and Tyrus use // Deflater.deflate(byte[], int, int, int) with Deflate.SYNC_FLUSH, // but this implementation does not do it intentionally because the // method and the constant value are not available before Java 7. dos.write(input, 0, input.length); dos.close(); // Release the resources held by the compressor. deflater.end(); // Retrieve the compressed data. return baos.toByteArray(); }
public static byte[] compress(byte[] data) { Profiler.enter("time cost on [compress]"); ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4); DeflaterOutputStream zipOut = new DeflaterOutputStream(out); try { zipOut.write(data); zipOut.finish(); zipOut.close(); } catch (IOException e) { LOGGER.error("compress ex", e); return Constants.EMPTY_BYTES; } finally { close(zipOut); Profiler.release(); } return out.toByteArray(); }
zipStream.close();
dos.flush(); dos.close(); long time = System.nanoTime() - start; long latency = time / count;
@Override public void setPictureData(byte[] pictureData) { super.setPictureData(pictureData); setUncompressedSize(pictureData.length); // info of chicago project: // "... LZ compression algorithm in the format used by GNU Zip deflate/inflate with a 32k window ..." // not sure what to do, when lookup tables exceed 32k ... try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(bos); dos.write(pictureData); dos.close(); raw_pictureData = bos.toByteArray(); } catch (IOException e) { throw new RuntimeException("Can't compress metafile picture data", e); } setCompressedSize(raw_pictureData.length); setCompressed(true); }
rowsLeft -= nRows; compBytes.close();
@Test public void testDeflateCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException { StreamingCompressor compressor = new DeflateCompressor(_executor); final byte[] origin = new byte[BUF_SIZE]; Arrays.fill(origin, (byte)'c'); ByteArrayOutputStream out = new ByteArrayOutputStream(); DeflaterOutputStream zlib = new DeflaterOutputStream(out); IOUtils.write(origin, zlib); zlib.close(); byte[] compressed = out.toByteArray(); testCompress(compressor, origin, compressed); testDecompress(compressor, origin, compressed); testCompressThenDecompress(compressor, origin); }
@Test public void testDeflateCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException { StreamingCompressor compressor = new DeflateCompressor(_executor); final byte[] origin = new byte[BUF_SIZE]; Arrays.fill(origin, (byte)'c'); ByteArrayOutputStream out = new ByteArrayOutputStream(); DeflaterOutputStream zlib = new DeflaterOutputStream(out); IOUtils.write(origin, zlib); zlib.close(); byte[] compressed = out.toByteArray(); testCompress(compressor, origin, compressed); testDecompress(compressor, origin, compressed); testCompressThenDecompress(compressor, origin); }
ByteArrayOutputStream buf = new ByteArrayOutputStream(); GZIPOutputStream gz = new GZIPOutputStream(buf) { { def.setLevel(Deflater.NO_COMPRESSION); } }; gz.write("helloworld".getBytes("UTF-8")); gz.close(); for (byte b : buf.toByteArray()) System.out.print((b & 0xff) + " ");