@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; }
@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; }
@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); }
@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); }
@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 ); } }
/** * 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(); }
@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; }
/** * Encode bz byte [ ]. * * @param data the data * @return the byte [ ] */ public static byte[] encodeBZ(byte[] data) { try { int blockSize = 4; ByteArrayOutputStream output = new ByteArrayOutputStream(); BZip2CompressorOutputStream compresser = new BZip2CompressorOutputStream(output, blockSize); compresser.write(data); compresser.close(); byte[] bytes = output.toByteArray(); return bytes; } catch (IOException e) { throw new RuntimeException(e); } }
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; }
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; }
baos.flush(); baos.close(); bos.close(); bzipSize += baos.toByteArray().length;
@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); } } }