Search in sources :

Example 1 with XZOutputStream

use of org.tukaani.xz.XZOutputStream 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;
}
Also used : XZOutputStream(org.tukaani.xz.XZOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XZOutputStream(org.tukaani.xz.XZOutputStream) LZMA2Options(org.tukaani.xz.LZMA2Options) IOException(java.io.IOException)

Example 2 with XZOutputStream

use of org.tukaani.xz.XZOutputStream 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);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XZOutputStream(org.tukaani.xz.XZOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XZOutputStream(org.tukaani.xz.XZOutputStream) FilenameFilter(java.io.FilenameFilter) FileOutputStream(java.io.FileOutputStream) LZMA2Options(org.tukaani.xz.LZMA2Options) File(java.io.File)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 LZMA2Options (org.tukaani.xz.LZMA2Options)2 XZOutputStream (org.tukaani.xz.XZOutputStream)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FilenameFilter (java.io.FilenameFilter)1