use of org.tukaani.xz.LZMA2Options in project buck by facebook.
the class XzStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) {
try (InputStream in = filesystem.newFileInputStream(sourceFile);
OutputStream out = filesystem.newFileOutputStream(destinationFile);
XZOutputStream xzOut = new XZOutputStream(out, new LZMA2Options(compressionLevel), check)) {
XzMemorySemaphore.acquireMemory(compressionLevel);
ByteStreams.copy(in, xzOut);
xzOut.finish();
if (!keep) {
filesystem.deleteFileAtPath(sourceFile);
}
} catch (IOException e) {
LOG.error(e);
return StepExecutionResult.ERROR;
} finally {
XzMemorySemaphore.releaseMemory(compressionLevel);
}
return StepExecutionResult.SUCCESS;
}
use of org.tukaani.xz.LZMA2Options in project netty by netty.
the class TestUtils method compressHeapDumps.
public static void compressHeapDumps() throws IOException {
final File[] files = new File(System.getProperty("user.dir")).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".hprof");
}
});
final byte[] buf = new byte[65536];
final LZMA2Options options = new LZMA2Options(LZMA2Options.PRESET_DEFAULT);
for (File file : files) {
final String filename = file.toString();
final String xzFilename = filename + ".xz";
final long fileLength = file.length();
logger.info("Compressing the heap dump: {}", xzFilename);
long lastLogTime = System.nanoTime();
long counter = 0;
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(filename);
out = new XZOutputStream(new FileOutputStream(xzFilename), options);
for (; ; ) {
int readBytes = in.read(buf);
if (readBytes < 0) {
break;
}
if (readBytes == 0) {
continue;
}
out.write(buf, 0, readBytes);
counter += readBytes;
long currentTime = System.nanoTime();
if (currentTime - lastLogTime > DUMP_PROGRESS_LOGGING_INTERVAL) {
logger.info("Compressing the heap dump: {} ({}%)", xzFilename, counter * 100 / fileLength);
lastLogTime = currentTime;
}
}
out.close();
in.close();
} catch (Throwable t) {
logger.warn("Failed to compress the heap dump: {}", xzFilename, t);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
// Ignore.
}
}
if (out != null) {
try {
out.close();
} catch (IOException ignored) {
// Ignore.
}
}
}
// Delete the uncompressed dump in favor of the compressed one.
if (!file.delete()) {
logger.warn("Failed to delete the uncompressed heap dump: {}", filename);
}
}
}
Aggregations