Search in sources :

Example 56 with FilterOutputStream

use of java.io.FilterOutputStream in project druid by druid-io.

the class CompressionUtilsTest method testGoodGzipWithException.

@Test
public void testGoodGzipWithException() throws Exception {
    final AtomicLong flushes = new AtomicLong(0);
    final File tmpDir = temporaryFolder.newFolder("testGoodGzipByteSource");
    final File gzFile = new File(tmpDir, testFile.getName() + ".gz");
    Assert.assertFalse(gzFile.exists());
    CompressionUtils.gzip(Files.asByteSource(testFile), new ByteSink() {

        @Override
        public OutputStream openStream() throws IOException {
            return new FilterOutputStream(new FileOutputStream(gzFile)) {

                @Override
                public void flush() throws IOException {
                    if (flushes.getAndIncrement() > 0) {
                        super.flush();
                    } else {
                        throw new IOException("Haven't flushed enough");
                    }
                }
            };
        }
    }, Predicates.alwaysTrue());
    Assert.assertTrue(gzFile.exists());
    try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(gzFile), "file.gz")) {
        assertGoodDataStream(inputStream);
    }
    if (!testFile.delete()) {
        throw new IOE("Unable to delete file [%s]", testFile.getAbsolutePath());
    }
    Assert.assertFalse(testFile.exists());
    CompressionUtils.gunzip(Files.asByteSource(gzFile), testFile);
    Assert.assertTrue(testFile.exists());
    try (final InputStream inputStream = new FileInputStream(testFile)) {
        assertGoodDataStream(inputStream);
    }
    // 2 for suppressed closes, 2 for manual calls to shake out errors
    Assert.assertEquals(4, flushes.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteSink(com.google.common.io.ByteSink) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) CountingInputStream(com.google.common.io.CountingInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) ZstdCompressorOutputStream(org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream) OutputStream(java.io.OutputStream) FramedSnappyCompressorOutputStream(org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorOutputStream) XZCompressorOutputStream(org.apache.commons.compress.compressors.xz.XZCompressorOutputStream) FileOutputStream(java.io.FileOutputStream) FilterOutputStream(java.io.FilterOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 57 with FilterOutputStream

use of java.io.FilterOutputStream in project incubator-gobblin by apache.

the class FilterStreamUnpacker method unpackFilterOutputStream.

/**
 * Finds the underlying {@link OutputStream} to a {@link FilterOutputStream}. Note this is not always possible due
 * to security restrictions of the JVM.
 * @throws IllegalAccessException If security policies of the JVM prevent unpacking of the {@link FilterOutputStream}.
 */
public static OutputStream unpackFilterOutputStream(FilterOutputStream os) throws IllegalAccessException {
    try {
        Field field = FilterOutputStream.class.getDeclaredField("out");
        field.setAccessible(true);
        return (OutputStream) field.get(os);
    } catch (NoSuchFieldException nsfe) {
        throw new RuntimeException(nsfe);
    }
}
Also used : Field(java.lang.reflect.Field) OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 58 with FilterOutputStream

use of java.io.FilterOutputStream in project Java-Tutorial by gpcodervn.

the class FilterOutputStreamExample method main.

public static void main(String[] args) throws IOException {
    FileOutputStream file = null;
    FilterOutputStream filter = null;
    try {
        file = new FileOutputStream(new File("data/test.txt"));
        filter = new FilterOutputStream(file);
        String s = "gpcoder.com";
        byte[] b = s.getBytes();
        filter.write(b);
        filter.flush();
        System.out.println("Success...");
    } finally {
        filter.close();
        file.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FilterOutputStream(java.io.FilterOutputStream) File(java.io.File)

Example 59 with FilterOutputStream

use of java.io.FilterOutputStream in project Java-Tutorial by gpcodervn.

the class FilterOutputStreamExample method main.

public static void main(String[] args) throws IOException {
    FileOutputStream file = null;
    FilterOutputStream filter = null;
    try {
        file = new FileOutputStream(new File("data/test.txt"));
        filter = new FilterOutputStream(file);
        String s = "gpcoder.com";
        byte[] b = s.getBytes();
        filter.write(b);
        filter.flush();
        System.out.println("Success...");
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        filter.close();
        file.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) File(java.io.File)

Aggregations

FilterOutputStream (java.io.FilterOutputStream)59 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 OutputStream (java.io.OutputStream)19 FileOutputStream (java.io.FileOutputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 File (java.io.File)12 Support_OutputStream (tests.support.Support_OutputStream)12 Test (org.junit.Test)9 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)7 FilterInputStream (java.io.FilterInputStream)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ByteSink (com.google.common.io.ByteSink)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 File (com.hierynomus.smbj.share.File)3 DataOutputStream (java.io.DataOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3