@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()); }
@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; }
@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; }
/** * 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; } }
/** * 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); } }
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(); }
bos.write(raw); baos.flush(); baos.close();
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)
@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); } } }