@Override public void close() throws IOException { if (!closed) { final OutputStream outShadow = this.out; try { finish(); } finally { outShadow.close(); } } }
@Override public byte[] deflate(InputStream data) throws CompressionException { ByteArrayOutputStream out = new ByteArrayOutputStream(); BZip2CompressorOutputStream compressor = null; try { out = new ByteArrayOutputStream(); compressor = new BZip2CompressorOutputStream(out); IOUtils.copy(data, compressor); compressor.finish(); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e); } finally { if (compressor != null) { IOUtils.closeQuietly(compressor); } } return out.toByteArray(); } }
@Override public void close() throws IOException { if (!closed) { final OutputStream outShadow = this.out; finish(); outShadow.close(); } }
/** * Bzip2s the specified array, removing the header. * * @param uncompressed The uncompressed array. * @return The compressed array. * @throws IOException If there is an error compressing the array. */ public static byte[] bzip2(byte[] uncompressed) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) { os.write(uncompressed); os.finish(); byte[] compressed = bout.toByteArray(); byte[] newCompressed = new byte[compressed.length - 4]; // Strip the header System.arraycopy(compressed, 4, newCompressed, 0, newCompressed.length); return newCompressed; } }
@Override public byte[] deflate(InputStream data) throws CompressionException { ByteArrayOutputStream out = new ByteArrayOutputStream(); BZip2CompressorOutputStream compressor = null; try { out = new ByteArrayOutputStream(); compressor = new BZip2CompressorOutputStream(out); IOUtils.copy(data, compressor); compressor.finish(); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e); } finally { if (compressor != null) { IOUtils.closeQuietly(compressor); } } return out.toByteArray(); } }
public void compressTo(InputStream in, OutputStream out) throws CompressException { BZip2CompressorOutputStream outputBZStream = null; try { outputBZStream = new BZip2CompressorOutputStream(out); NioUtils.copy(in, outputBZStream); outputBZStream.finish(); } catch (Exception e) { throw new CompressException("bzip_compress_error", e); } }
byte[] compress(byte[] bdata) { ByteArrayOutputStream out = new ByteArrayOutputStream(bdata.length); try (BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(out)) { bzOut.write(bdata, 0, bdata.length); bzOut.finish(); } catch (Exception e) { e.printStackTrace(); } return out.toByteArray(); }