// Stream buffer
final int BUFF_SIZE = 8192;
final byte[] buffer = new byte[BUFF_SIZE];
BZip2CompressorInputStream inputStream = null;
FileOutputStream outStream = null;
try {
FileInputStream is = new FileInputStream(in.getPath());
inputStream = new BZip2CompressorInputStream(is);
outStream = new FileOutputStream(out.getAbsolutePath());
if (isTar) {
// Read Tar header
int remainingBytes = readTarHeader(inputStream);
// Read content
ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE);
byte[] tmpCache = new byte[BUFF_SIZE];
int nRead, nGet;
while ((nRead = inputStream.read(tmpCache)) != -1) {
if (nRead == 0) {
continue;
}
bb.put(tmpCache);
bb.position(0);
bb.limit(nRead);
while (bb.hasRemaining() && remainingBytes > 0) {
nGet = Math.min(bb.remaining(), BUFF_SIZE);
nGet = Math.min(nGet, remainingBytes);
bb.get(buffer, 0, nGet);
outStream.write(buffer, 0, nGet);
remainingBytes -= nGet;
}
bb.clear();
}
} else {
int len;
while ((len = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, len);
}
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outStream != null) {
outStream.close();
}
}