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());
}
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);
}
}
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();
}
}
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();
}
}
Aggregations