public static byte[] compress(byte[] bytes) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(bytes); DeflaterOutputStream out = new DeflaterOutputStream(baos, new Deflater(), BUF_SIZE); write(in, out, BUF_SIZE); return baos.toByteArray(); }
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; }
@Override public byte[] doCompress(byte[] payload) throws IOException { Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); ByteArrayOutputStream outputStream = null; DeflaterOutputStream deflaterOutputStream = null; try { outputStream = new ByteArrayOutputStream(); deflaterOutputStream = new DeflaterOutputStream(outputStream, deflater, true); deflaterOutputStream.write(payload, 0, payload.length); deflaterOutputStream.flush(); return outputStream.toByteArray(); } finally { Objects.nullSafeClose(outputStream, deflaterOutputStream); } }
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 void main(String[] args) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); Throwable baosEx = null; try { final GZIPOutputStream gzip = new GZIPOutputStream(baos); Throwable gzipEx = null; try { gzip.write("TEST".getBytes("UTF-8")); } catch (Throwable t) { gzipEx = t; if (gzipEx != null) { try { gzip.close(); } catch (Throwable t) { gzipEx.addSuppressed(t); gzip.close(); if (baosEx != null) { try { baos.close(); } catch (Throwable t) { baosEx.addSuppressed(t); baos.close();
public static byte[] compressBytes(byte[] ori, int offset, int len, boolean compress) { try { ByteArrayInputStream inb = new ByteArrayInputStream(ori, offset, len); InputStream in = compress ? inb : new InflaterInputStream(inb); ByteArrayOutputStream outb = new ByteArrayOutputStream(); OutputStream out = compress ? new DeflaterOutputStream(outb) : outb; shovelInToOut(in, out); in.close(); out.close(); return outb.toByteArray(); } catch (Exception e) { throw new PngjException(e); } }
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(); }
enum StringCompressor { ; public static byte[] compress(String text) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { OutputStream out = new DeflaterOutputStream(baos); out.write(text.getBytes("UTF-8")); out.close(); } catch (IOException e) { throw new AssertionError(e); } return baos.toByteArray(); } public static String decompress(byte[] bytes) { InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { byte[] buffer = new byte[8192]; int len; while((len = in.read(buffer))>0) baos.write(buffer, 0, len); return new String(baos.toByteArray(), "UTF-8"); } catch (IOException e) { throw new AssertionError(e); } } }
public final byte[] pngEncode(final int compressionLevel) throws IOException { if (this.frame == null) return exportImage(this.getImage(), "png").toByteArray(); final int width = image.getWidth(null); final int height = image.getHeight(null); final Deflater scrunch = new Deflater(compressionLevel); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024); final OutputStream compBytes = new BufferedOutputStream(new DeflaterOutputStream(outBytes, scrunch)); int i = 0; for (int row = 0; row < height; row++) { compBytes.write(0); compBytes.write(frame, i, 3 * width); i += 3 * width; compBytes.close(); scrunch.finish(); final int nCompressed = outBytes.size(); final byte[] pngBytes = new byte[nCompressed + 57]; // yes thats the exact size, not too less, not too much. No resizing needed. int bytePos = writeBytes(pngBytes, new byte[]{-119, 80, 78, 71, 13, 10, 26, 10}, 0);
Socket s = serverSocket.accept(); ByteArrayOutputStream compressed = new ByteArrayOutputStream(); GZIPOutputStream compressor = new GZIPOutputStream(compressed); Msg m = new Msg("123", "123", karte); m.writeObject (compressor); compressor.close(); OutputStream fout = s.getOutputStream(); fout.write (compressed.toByteArray() ); fout.close ();
private synchronized byte[] compress(byte[] data, int length) throws IOException { if(!compress ) return data; if(deflater == null){ deflater = new Deflater(); }else{ deflater.reset(); } ByteArrayOutputStream b = new ByteArrayOutputStream(0); OutputStream d = new DeflaterOutputStream(b,deflater); d.write(data,0,length); d.close(); return b.toByteArray(); }
private ByteBuffer compress(ByteBuffer source) throws IOException { if (source.remaining() == 0) { return source; } ByteBufferInputStream sourceStream = new ByteBufferInputStream(source); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(source.remaining() / 2); OutputStream compressor = null; if (compressionType == CompressionType.GZIP) { compressor = new GZIPOutputStream(outputStream); } if (compressionType == CompressionType.DEFLATE) { compressor = new DeflaterOutputStream(outputStream); } try { copy(sourceStream, compressor); } finally { compressor.close(); } return ByteBuffer.wrap(outputStream.toByteArray()); }
public String compressAndEncode(byte[] data) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gz = new GZIPOutputStream(out); ByteArrayInputStream in = new ByteArrayInputStream(data); int c; while ((c = in.read()) != -1) gz.write(c); gz.finish(); Base64 b = new Base64(0); return b.encodeToString(out.toByteArray()); }
@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 baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos, true); for (char ch = 'a'; ch <= 'z'; ch++) { dos.write(("Hello world").getBytes()); baos2.write(("Hello world").getBytes()); dos.flush(); System.out.println("compressed size = " + baos.toByteArray().length); } dos.close(); System.out.println("After close, compressed size = " + baos.toByteArray().length); System.out.println("uncompressed size = " + baos2.toByteArray().length);
public InputStream toInputStream() { try { os.flush(); return new InflaterInputStream(new ByteArrayInputStream(baos.toByteArray())); } catch (IOException e) { throw new RuntimeException(e); } }
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(); }
private static byte[] writeJsonAsCompressedBytes(ObjectWriter objectWriter, Object o) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream out = new DeflaterOutputStream(baos)) { objectWriter.writeValue(out, o); } catch (Exception e) { log.error("Error compressing data.", e); return null; } 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(); }
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DeflaterOutputStream outputStream = new DeflaterOutputStream(byteArrayOutputStream, new Deflater(Deflater.BEST_COMPRESSION)); DataVersion dataVersion = topicConfigSerializeWrapper.getDataVersion(); ConcurrentMap<String, TopicConfig> topicConfigTable = cloneTopicConfigTable(topicConfigSerializeWrapper.getTopicConfigTable()); outputStream.write(convertIntToByteArray(buffer.length)); outputStream.write(buffer); outputStream.write(convertIntToByteArray(topicNumber)); outputStream.write(convertIntToByteArray(buffer.length)); outputStream.write(buffer); outputStream.write(convertIntToByteArray(buffer.length)); outputStream.write(buffer); outputStream.finish(); long interval = System.currentTimeMillis() - start; if (interval > 50) { LOGGER.info("Compressing takes {}ms", interval); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { LOGGER.error("Failed to compress RegisterBrokerBody object", e);