@Override public ByteBuffer decompress(ByteBuffer compressedData) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.array(), computeOffset(compressedData), compressedData.remaining()); try(BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais)) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int readCount = -1; while ((readCount = inputStream.read(buffer, compressedData.position(), buffer.length)) > 0) { baos.write(buffer, 0, readCount); } return ByteBuffer.wrap(baos.toByteArray()); } }
FileInputStream in = new FileInputStream("archive.tar.bz2"); FileOutputStream out = new FileOutputStream("archive.tar"); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); final byte[] buffer = new byte[buffersize]; int n = 0; while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); bzIn.close();
@Override public ByteBuffer decompress(ByteBuffer compressedData) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.array()); BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int readCount = -1; while ( (readCount = inputStream.read(buffer, compressedData.position(), buffer.length))> 0) { baos.write(buffer, 0, readCount); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; } finally { inputStream.close(); } }
@Override ByteBuffer decompress(ByteBuffer compressedData) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.array()); BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int readCount = -1; while ( (readCount = inputStream.read(buffer, compressedData.position(), buffer.length))> 0) { baos.write(buffer, 0, readCount); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; } finally { inputStream.close(); } }
public static void main(String[] args) throws IOException { if (args.length == 0) { throw new IllegalArgumentException("You need to specify the bz2 file!"); } String fromFile = args[0]; if (!fromFile.endsWith(".bz2")) { throw new IllegalArgumentException("You need to specify a bz2 file! But was:" + fromFile); } String toFile = Helper.pruneFileEnd(fromFile); FileInputStream in = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); try { final byte[] buffer = new byte[1024 * 8]; int n = 0; while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } } finally { out.close(); bzIn.close(); } } }
final byte[] buf = new byte[1024]; int n = 0; while ((n = bzin.read(buf, 0, buf.length)) > -1) { sb.append(new String(buf, 0, n));
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip) throws IOException { int MAX_DATA_SIZE = 1024 * 1024; byte[] data = new byte[MAX_DATA_SIZE]; FileInputStream fis = new FileInputStream(testFileUrl.getFile()); int count; if (bzip) { BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis); count = bzIn.read(data); bzIn.close(); } else { count = fis.read(data); } fis.close(); assertTrue("Test file data too big for buffer", count < data.length); return new String(data, 0, count, "UTF-8").split("\n"); }
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip) throws IOException { int MAX_DATA_SIZE = 1024 * 1024; byte[] data = new byte[MAX_DATA_SIZE]; FileInputStream fis = new FileInputStream(testFileUrl.getFile()); int count; if (bzip) { BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis); count = bzIn.read(data); bzIn.close(); } else { count = fis.read(data); } fis.close(); assertTrue("Test file data too big for buffer", count < data.length); return new String(data, 0, count, "UTF-8").split("\n"); }
@Override public ByteBuffer decompress(ByteBuffer compressedData) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.array()); BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int readCount = -1; while ( (readCount = inputStream.read(buffer, compressedData.position(), buffer.length))> 0) { baos.write(buffer, 0, readCount); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; } finally { inputStream.close(); } }
public void extract(String input_f, String output_f){ int buffersize = 1024; FileInputStream in; try { in = new FileInputStream(input_f); FileOutputStream out = new FileOutputStream(output_f); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); final byte[] buffer = new byte[buffersize]; int n = 0; int total = 0; while (-1 != (n = bzIn.read(buffer))) { total += n; final int percentage=(int)(total*100/lenghtOfFile); YourActivity.this.runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub progressBar.setProgress(percentage); } }); out.write(buffer, 0, n); } out.close(); bzIn.close(); } catch (Exception e) { throw new Error(e.getMessage()); } }
int bytesRead = bz2is.read(buffer, 0, buffer.length); if(expectedBytesRead < bytesRead) { Logger.normal(this, "expectedBytesRead="+expectedBytesRead+", bytesRead="+bytesRead+", written="+written+", maxLength="+maxLength+" throwing a CompressionOutputSizeException"); while(true) { expectedBytesRead = (int) Math.min(buffer.length, maxLength + maxCheckSizeBytes - written); bytesRead = bz2is.read(buffer, 0, expectedBytesRead); if(bytesRead <= -1) throw new CompressionOutputSizeException(written); if(bytesRead == 0) throw new IOException("Returned zero from read()");
BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream); byte[] uncompressedPacketData = new byte[uncompressedSize]; bzip2.read(uncompressedPacketData, 0, uncompressedSize);
byte[] uncompress(byte[] bdata) { ByteArrayOutputStream out = new ByteArrayOutputStream(20 * bdata.length); ByteArrayInputStream in = new ByteArrayInputStream(bdata); try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in, false)) { int bytesRead; byte[] decoded = new byte [524288]; while ((bytesRead = bzIn.read (decoded)) != -1) { out.write (decoded, 0, bytesRead) ; } out.close(); } catch (Exception e) { e.printStackTrace(); } return out.toByteArray(); } }
@Override public int decompress(byte[] src, int srcOff, int srcLen, byte[] dst, int dstOff) throws InsufficientBufferException, DataCorruptionException { try { if (srcStream == null) { srcStream = new ReusableByteArrayInputStream(src, srcOff, srcLen); } else { srcStream.reuse(src, srcOff, srcLen); } BZip2CompressorInputStream decompressStream = new BZip2CompressorInputStream(srcStream); int decompressedLen = decompressStream.read(dst, dstOff, dst.length - dstOff); srcStream.close(); decompressStream.close(); if (decompressedLen < 0) { throw new DataCorruptionException("Fail to decompress, decompressedLen: " + decompressedLen); } return decompressedLen; } catch (IOException e) { throw new DataCorruptionException(e); } } }