@Override protected OutputStream createOutputStream(OutputStream out) throws IOException { return new BZip2CompressorOutputStream(out); } };
@Override ByteBuffer compress(ByteBuffer uncompressedData) throws IOException { ByteArrayOutputStream baos = getOutputBuffer(uncompressedData.remaining()); BZip2CompressorOutputStream outputStream = new BZip2CompressorOutputStream(baos); try { outputStream.write(uncompressedData.array()); } finally { outputStream.close(); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; }
@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 ByteBuffer compress(ByteBuffer uncompressedData) throws IOException { ByteArrayOutputStream baos = getOutputBuffer(uncompressedData.remaining()); try (BZip2CompressorOutputStream outputStream = new BZip2CompressorOutputStream(baos)) { outputStream.write(uncompressedData.array(), computeOffset(uncompressedData), uncompressedData.remaining()); } return ByteBuffer.wrap(baos.toByteArray()); }
@Test public void testBzip2Compressor() throws IOException, InterruptedException, CompressionException, ExecutionException { StreamingCompressor compressor = new Bzip2Compressor(_executor); final byte[] origin = new byte[BUF_SIZE]; Arrays.fill(origin, (byte)'c'); ByteArrayOutputStream out = new ByteArrayOutputStream(); BZip2CompressorOutputStream bzip = new BZip2CompressorOutputStream(out); IOUtils.write(origin, bzip); bzip.close(); byte[] compressed = out.toByteArray(); testCompress(compressor, origin, compressed); testDecompress(compressor, origin, compressed); testCompressThenDecompress(compressor, origin); }
/** * 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; } }
protected ByteArrayOutputStream compress(ByteArrayOutputStream raw) throws TransportException { ByteArrayOutputStream compressed = new ByteArrayOutputStream(); BZip2CompressorOutputStream bcos = null; try { bcos = new BZip2CompressorOutputStream(compressed); } catch (IOException e) { throw new TransportException("unable to open compressed stream", e); } try { raw.writeTo(bcos); bcos.flush(); } catch (IOException e) { throw new TransportException("unable to compress data", e); } finally { try { bcos.close(); } catch (IOException e) { } } return compressed; }
try { CountedOutputStream cos = new CountedOutputStream(os); bz2os = new BZip2CompressorOutputStream(HeaderStreams.dimOutput(BZ_HEADER, cos)); long read = 0; if(x <= -1) break; if(x == 0) throw new IOException("Returned zero from read()"); bz2os.write(buffer, 0, x); read += x; if(cos.written() > maxWriteLength) throw new CompressionOutputSizeException(); bz2os.flush(); cos.flush(); bz2os.close(); bz2os = null; if(cos.written() > maxWriteLength) } finally { if(bz2os != null) { bz2os.flush(); bz2os.close();
@Override public int compress(byte[] src, int srcOff, int srcLen, byte[] dst, int dstOff) throws DataCorruptionException { try { if (dstStream == null) { dstStream = new NoCopyByteArrayOutputStream(dst, dstOff); } else { dstStream.reuse(dst, dstOff); } BZip2CompressorOutputStream compressStream = new BZip2CompressorOutputStream( dstStream, BZip2CompressorOutputStream.chooseBlockSize(srcLen)); compressStream.write(src, srcOff, srcLen); compressStream.close(); int compressedLen = dstStream.getNumBytesWritten(); dstStream.close(); if (compressedLen > (dst.length - dstOff)) { throw new InsufficientBufferException("destination buffer remains " + (dst.length - dstOff) + " bytes, requires " + compressedLen + " bytes."); } return compressedLen; } catch (IOException e) { throw new DataCorruptionException(e); } } }
@Override public void close() { try { if ( this.zOut != null ) { this.zOut.close(); zOut = null; } } catch ( final IOException e ) { throw new ArchiverException( "Failure closing target.", e ); } }
@Override public void close() throws IOException { if (!closed) { final OutputStream outShadow = this.out; try { finish(); } finally { outShadow.close(); } } }
@Test public void testBzip2Compressor() throws IOException, InterruptedException, CompressionException, ExecutionException { StreamingCompressor compressor = new Bzip2Compressor(_executor); final byte[] origin = new byte[BUF_SIZE]; Arrays.fill(origin, (byte)'c'); ByteArrayOutputStream out = new ByteArrayOutputStream(); BZip2CompressorOutputStream bzip = new BZip2CompressorOutputStream(out); IOUtils.write(origin, bzip); bzip.close(); byte[] compressed = out.toByteArray(); testCompress(compressor, origin, compressed); testDecompress(compressor, origin, compressed); testCompressThenDecompress(compressor, origin); }
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(); }
protected ByteArrayOutputStream compress(ByteArrayOutputStream raw) throws TransportException { ByteArrayOutputStream compressed = new ByteArrayOutputStream(); BZip2CompressorOutputStream bcos = null; try { bcos = new BZip2CompressorOutputStream(compressed); } catch (IOException e) { throw new TransportException("unable to open compressed stream", e); } try { raw.writeTo(bcos); bcos.flush(); } catch (IOException e) { throw new TransportException("unable to compress data", e); } finally { try { bcos.close(); } catch (IOException e) { } } return compressed; }
@Override public void close() throws IOException { if (!closed) { final OutputStream outShadow = this.out; finish(); outShadow.close(); } }
@Override public OutputStream wrapOutputStream(OutputStream out) throws IOException { return new BZip2CompressorOutputStream(out); } };
@Override public ByteBuffer compress(ByteBuffer uncompressedData) throws IOException { ByteArrayOutputStream baos = getOutputBuffer(uncompressedData.remaining()); BZip2CompressorOutputStream outputStream = new BZip2CompressorOutputStream(baos); try { outputStream.write(uncompressedData.array(), uncompressedData.position(), uncompressedData.remaining()); } finally { outputStream.close(); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; }
/** * Compress a byte array into BZIP2 blob. * * @param data byte array to compress * @return compressed blob */ public static byte[] bzip2(final byte[] data) throws IOException { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final BZip2CompressorOutputStream bos = new BZip2CompressorOutputStream(byteArrayOutputStream); IOUtil.copyStream(new ByteArrayInputStream(data), bos); bos.close(); return byteArrayOutputStream.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); } }
@Signature public void __construct(Environment env, OutputStream outputStream, int blockSize) throws IOException { this.outputStream = new BZip2CompressorOutputStream(outputStream, blockSize); } }