Example usage for org.apache.commons.compress.compressors CompressorException printStackTrace

List of usage examples for org.apache.commons.compress.compressors CompressorException printStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors CompressorException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:org.apache.hama.bsp.message.compress.Bzip2Compressor.java

@Override
public byte[] compress(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    DataInputStream in = new DataInputStream(bis);

    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(outBuffer);

    CompressorOutputStream cos = null;/*ww  w .j  ava2  s.c om*/
    try {
        cos = new CompressorStreamFactory().createCompressorOutputStream("bzip2", out);
        IOUtils.copy(in, cos);
        cos.close();
    } catch (CompressorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return outBuffer.toByteArray();
}

From source file:org.apache.hama.bsp.message.compress.Bzip2Compressor.java

/**
 * Decompresses a BSPCompressedBundle and returns the corresponding
 * BSPMessageBundle./*from  ww  w.j  av  a 2s . c o m*/
 * 
 * @param compressedBytes
 * @return The result after decompressing BSPMessageBundle.
 */
@Override
public byte[] decompress(byte[] compressedBytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(compressedBytes);
    DataInputStream in = new DataInputStream(bis);

    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(outBuffer);
    try {

        final CompressorInputStream cin = new CompressorStreamFactory().createCompressorInputStream("bzip2",
                in);
        IOUtils.copy(cin, out);
        in.close();
    } catch (CompressorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outBuffer.toByteArray();
}

From source file:org.apache.logging.log4j.core.appender.rolling.RollingAppenderSizeTest.java

@Test
public void testAppender() throws Exception {
    final Path path = Paths.get(DIR, "rollingtest.log");
    if (Files.exists(path) && createOnDemand) {
        Assert.fail(String.format("Unexpected file: %s (%s bytes)", path, Files.getAttribute(path, "size")));
    }/*www.j  a v a 2s .  c om*/
    for (int i = 0; i < 500; ++i) {
        logger.debug("This is test message number " + i);
    }
    try {
        Thread.sleep(100);
    } catch (final InterruptedException ie) {
        // Ignore the error.
    }

    final File dir = new File(DIR);
    assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
    final File[] files = dir.listFiles();
    assertNotNull(files);
    assertThat(files, hasItemInArray(that(hasName(that(endsWith(fileExtension))))));

    final FileExtension ext = FileExtension.lookup(fileExtension);
    if (ext == null || FileExtension.ZIP == ext || FileExtension.PACK200 == ext) {
        return; // Apache Commons Compress cannot deflate zip? TODO test decompressing these formats
    }
    // Stop the context to make sure all files are compressed and closed. Trying to remedy failures in CI builds.
    if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) {
        System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
    }
    for (final File file : files) {
        if (file.getName().endsWith(fileExtension)) {
            CompressorInputStream in = null;
            try (FileInputStream fis = new FileInputStream(file)) {
                try {
                    in = new CompressorStreamFactory().createCompressorInputStream(ext.name().toLowerCase(),
                            fis);
                } catch (final CompressorException ce) {
                    ce.printStackTrace();
                    fail("Error creating intput stream from " + file.toString() + ": " + ce.getMessage());
                }
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                assertNotNull("No input stream for " + file.getName(), in);
                try {
                    IOUtils.copy(in, baos);
                } catch (final Exception ex) {
                    ex.printStackTrace();
                    fail("Unable to decompress " + file.getAbsolutePath());
                }
                final String text = new String(baos.toByteArray(), Charset.defaultCharset());
                final String[] lines = text.split("[\\r\\n]+");
                for (final String line : lines) {
                    assertTrue(line.contains(
                            "DEBUG o.a.l.l.c.a.r.RollingAppenderSizeTest [main] This is test message number"));
                }
            } finally {
                Closer.close(in);
            }
        }
    }
}

From source file:org.apache.logging.log4j.core.appender.rolling.RollingAppenderTempCompressedFilePatternTest.java

@Test
public void testAppender() throws Exception {
    final File dirTmp = new File(DIR_TMP);
    dirTmp.mkdirs();//from  ww  w  .j av  a 2s. co  m
    try (final WatchService watcher = FileSystems.getDefault().newWatchService()) {
        WatchKey key = dirTmp.toPath().register(watcher, StandardWatchEventKinds.ENTRY_CREATE);

        final List<String> messages = new ArrayList<>();
        for (int i = 0; i < 500; ++i) {
            final String message = "This is test message number " + i;
            messages.add(message);
            logger.debug(message);
            if (i % 100 == 0) {
                Thread.sleep(500);
            }
        }
        if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) {
            System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
        }
        final File dir = new File(DIR);
        assertTrue("Directory not created", dir.exists());
        final File[] files = dir.listFiles();
        assertNotNull(files);
        int gzippedFiles = 0;
        for (final File file : files) {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream in = null;
            final FileExtension ext = FileExtension.lookupForFile(file.getName());
            try {
                try (FileInputStream fis = new FileInputStream(file)) {
                    if (ext != null) {
                        gzippedFiles++;
                        try {
                            in = new CompressorStreamFactory()
                                    .createCompressorInputStream(ext.name().toLowerCase(), fis);
                        } catch (final CompressorException ce) {
                            ce.printStackTrace();
                            fail("Error creating intput stream from " + file.toString() + ": "
                                    + ce.getMessage());
                        }
                    } else {
                        in = new FileInputStream(file);
                    }
                    assertNotNull("No input stream for " + file.getName(), in);
                    try {
                        IOUtils.copy(in, baos);
                    } catch (final Exception ex) {
                        ex.printStackTrace();
                        fail("Unable to decompress " + file.getAbsolutePath());
                    }
                }
            } finally {
                Closer.close(in);
            }
            final String text = new String(baos.toByteArray(), Charset.defaultCharset());
            final String[] lines = text.split("[\\r\\n]+");
            for (final String line : lines) {
                messages.remove(line);
            }
        }
        assertTrue("Log messages lost : " + messages.size(), messages.isEmpty());
        assertTrue("Files not rolled : " + files.length, files.length > 2);
        assertTrue("Files gzipped not rolled : " + gzippedFiles, gzippedFiles > 0);

        int temporaryFilesCreated = 0;
        key = watcher.take();

        for (final WatchEvent<?> event : key.pollEvents()) {
            final WatchEvent<Path> ev = (WatchEvent<Path>) event;
            final Path filename = ev.context();
            if (filename.toString().endsWith(".tmp")) {
                temporaryFilesCreated++;
            }
        }
        assertTrue("No temporary file created during compression", temporaryFilesCreated > 0);
        assertTrue("Temporarys file created not equals to compressed files " + temporaryFilesCreated + "/"
                + gzippedFiles, gzippedFiles == temporaryFilesCreated);
    }
}