@Override OutputStream getOutputStream(final OutputStream source) { return new DeflaterOutputStream(source, new Deflater(level)); } }
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[] data) throws IOException { long startTime = System.currentTimeMillis(); Deflater deflater = new Deflater(1); deflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); logger.debug("Original: " + data.length + " bytes. " + "Compressed: " + output.length + " byte. Time: " + (System.currentTimeMillis() - startTime)); return output; }
/** * Serialize this object, compressing the partitions which can exceed the * allowed jobConf size. * Partitions will be compressed and put into a byte array which then we have to write into the * ObjectOutputStream this method is given. * @see <a href="https://issues.apache.org/jira/browse/HCATALOG-453">HCATALOG-453</a> */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); ByteArrayOutputStream serialObj = new ByteArrayOutputStream(); ObjectOutputStream objStream = new ObjectOutputStream(serialObj); Deflater def = new Deflater(Deflater.BEST_COMPRESSION); ObjectOutputStream partInfoWriter = new ObjectOutputStream(new DeflaterOutputStream(objStream, def)); partInfoWriter.writeObject(partitions); //Closing only the writer used for compression byte stream partInfoWriter.close(); //Appending the compressed partition information oos.writeObject(serialObj.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(); }
@Override public OutputStream encode(String contentEncoding, OutputStream entityStream) throws IOException { // some implementations don't support the correct deflate // so we have a property to configure the incorrect deflate (no zlib wrapper) should be used // let's check that Object value = config.getProperty(MessageProperties.DEFLATE_WITHOUT_ZLIB); boolean deflateWithoutZLib; if (value instanceof String) { deflateWithoutZLib = Boolean.valueOf((String) value); } else if (value instanceof Boolean) { deflateWithoutZLib = (Boolean) value; } else { deflateWithoutZLib = false; } return deflateWithoutZLib ? new DeflaterOutputStream(entityStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true)) : new DeflaterOutputStream(entityStream); } }
@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); } }
/** * 压缩. * * @param inputByte * 需要解压缩的byte[]数组 * @return 压缩后的数据 * @throws IOException */ public static byte[] deflater(final byte[] inputByte) throws IOException { int compressedDataLength = 0; Deflater compresser = new Deflater(); compresser.setInput(inputByte); compresser.finish(); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while (!compresser.finished()) { compressedDataLength = compresser.deflate(result); o.write(result, 0, compressedDataLength); } } finally { o.close(); } compresser.end(); return o.toByteArray(); }
@Override public OutputStream encode(String contentEncoding, OutputStream entityStream) throws IOException { // some implementations don't support the correct deflate // so we have a property to configure the incorrect deflate (no zlib wrapper) should be used // let's check that Object value = config.getProperty(MessageProperties.DEFLATE_WITHOUT_ZLIB); boolean deflateWithoutZLib; if (value instanceof String) { deflateWithoutZLib = Boolean.valueOf((String) value); } else if (value instanceof Boolean) { deflateWithoutZLib = (Boolean) value; } else { deflateWithoutZLib = false; } return deflateWithoutZLib ? new DeflaterOutputStream(entityStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true)) : new DeflaterOutputStream(entityStream); } }
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; }
/** * 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(); }
/** * Compresses a file into zlib archive. */ public static File zlib(File file) throws IOException { if (file.isDirectory()) { throw new IOException("Can't zlib folder"); } FileInputStream fis = new FileInputStream(file); Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); String zlibFileName = file.getAbsolutePath() + ZLIB_EXT; DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater); try { StreamUtil.copy(fis, dos); } finally { StreamUtil.close(dos); StreamUtil.close(fis); } return new File(zlibFileName); }
@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 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(); } } }
/** * 将普通数据流压缩成zlib到out中 * * @param in zlib数据流 * @param out 输出 * @param level 压缩级别,0~9 */ private static void deflater(InputStream in, OutputStream out, int level) { final DeflaterOutputStream ios = (out instanceof DeflaterOutputStream) ? (DeflaterOutputStream) out : new DeflaterOutputStream(out, new Deflater(level, true)); IoUtil.copy(in, ios); try { ios.finish(); } catch (IOException e) { throw new IORuntimeException(e); } } // ---------------------------------------------------------------------------------------------- Private method end
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()); } }
Deflater zlib = new Deflater(); zlib.setInput(input); zlib.finish(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];
/** * 将普通数据流压缩成zlib到out中 * * @param in zlib数据流 * @param out 输出 * @param level 压缩级别,0~9 */ private static void deflater(InputStream in, OutputStream out, int level) { final DeflaterOutputStream ios = (out instanceof DeflaterOutputStream) ? (DeflaterOutputStream) out : new DeflaterOutputStream(out, new Deflater(level, true)); IoUtil.copy(in, ios); try { ios.finish(); } catch (IOException e) { throw new IORuntimeException(e); } } // ---------------------------------------------------------------------------------------------- Private method end
/** * 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(); }
private String binaryEncode(String tag) { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(); deflater.setInput(tag.getBytes()); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); byte compressed[] = outputStream.toByteArray(); return bytesToHex(compressed); } catch (Exception ex) { logger.warn("Caught exception", ex); return ""; } }