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(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 currently buffered chunk of data and sends it to the output stream * * @throws IOException if issues occur writing to stream */ protected void compressAndWrite() throws IOException { if (bufferIndex <= 0) { return; } deflater.setInput(buffer, 0, bufferIndex); deflater.finish(); final int compressedBytes = deflater.deflate(compressed); writeChunkHeader(compressedBytes); out.write(compressed, 0, compressedBytes); bufferIndex = 0; deflater.reset(); }
@Override public int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) { int maxCompressedLength = maxCompressedLength(inputLength); if (maxOutputLength < maxCompressedLength) { throw new IllegalArgumentException("Output buffer must be at least " + maxCompressedLength + " bytes"); } Deflater deflater = new Deflater(COMPRESSION_LEVEL, true); try { deflater.setInput(input, inputOffset, inputLength); deflater.finish(); int compressedDataLength = deflater.deflate(output, outputOffset, maxOutputLength, FULL_FLUSH); if (!deflater.finished()) { throw new IllegalStateException("maxCompressedLength formula is incorrect, because deflate produced more data"); } return compressedDataLength; } finally { deflater.end(); } }
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); bytePos = writeInt4(pngBytes, height, bytePos); bytePos = writeBytes(pngBytes, new byte[]{8, 2, 0, 0, 0}, bytePos); final CRC32 crc = new CRC32(); crc.reset(); crc.update(pngBytes, startPos, bytePos - startPos); bytePos = writeInt4(pngBytes, (int) crc.getValue(), bytePos); crc.reset();
@Override public final void write(DataOutput out) throws IOException { if (compressed == null) { ByteArrayOutputStream deflated = new ByteArrayOutputStream(); Deflater deflater = new Deflater(Deflater.BEST_SPEED); DataOutputStream dout = new DataOutputStream(new DeflaterOutputStream(deflated, deflater)); writeCompressed(dout); dout.close(); deflater.end(); compressed = deflated.toByteArray(); } out.writeInt(compressed.length); out.write(compressed); }
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(); }
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 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 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 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(); }
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); LOGGER.info("Compressing takes {}ms", interval); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { LOGGER.error("Failed to compress RegisterBrokerBody object", e);
/** * Encodes the specified {@code message} into a deflated base64 string. */ static String toDeflatedBase64(SAMLObject message) { requireNonNull(message, "message"); final String messageStr; try { messageStr = nodeToString(XMLObjectSupport.marshall(message)); } catch (MarshallingException e) { throw new SamlException("failed to serialize a SAML message", e); } final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); try (DeflaterOutputStream deflaterStream = new DeflaterOutputStream(Base64.getEncoder().wrap(bytesOut), new Deflater(Deflater.DEFLATED, true))) { deflaterStream.write(messageStr.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new SamlException("failed to deflate a SAML message", e); } return bytesOut.toString(); }
/** * Flushes remaining buffer and calls {@link Deflater#end()} to free acquired memory to prevent OutOfMemory error. * @throws IOException for any issues closing underlying stream */ @Override public void close() throws IOException { compressAndWrite(); out.write(0); // indicate that the stream is finished. out.flush(); deflater.end(); } }
DeflaterOutputStream deflaterOutput = new DeflaterOutputStream(buffer, deflater); DataOutputStream dataOutput = new DataOutputStream(output); dataOutput.write(SIGNATURE); deflater.reset(); deflaterOutput.write(PAETH); deflaterOutput.write(lineOut, 0, lineLen); buffer.endChunk(dataOutput); output.flush();
/** * Constructs a new {@code GZIPOutputStream} to write data in GZIP format to * the given stream with the given internal buffer size and * flushing behavior (see {@link DeflaterOutputStream#flush}). * @since 1.7 */ public GZIPOutputStream(OutputStream os, int bufferSize, boolean syncFlush) throws IOException { super(os, new Deflater(Deflater.DEFAULT_COMPRESSION, true), bufferSize, syncFlush); writeShort(GZIPInputStream.GZIP_MAGIC); out.write(Deflater.DEFLATED); out.write(0); // flags writeLong(0); // mod time out.write(0); // extra flags out.write(0); // operating system }