use of java.io.FileInputStream in project druid by druid-io.
the class CompressionUtilsTest method testStreamErrorGunzip.
@Test(expected = IOException.class)
public void testStreamErrorGunzip() throws Exception {
final File tmpDir = temporaryFolder.newFolder("testGoodGzipByteSource");
final File gzFile = new File(tmpDir, testFile.getName() + ".gz");
Assert.assertFalse(gzFile.exists());
CompressionUtils.gzip(Files.asByteSource(testFile), Files.asByteSink(gzFile), Predicates.<Throwable>alwaysTrue());
Assert.assertTrue(gzFile.exists());
try (final InputStream inputStream = CompressionUtils.gzipInputStream(new FileInputStream(gzFile))) {
assertGoodDataStream(inputStream);
}
if (testFile.exists() && !testFile.delete()) {
throw new RuntimeException(String.format("Unable to delete file [%s]", testFile.getAbsolutePath()));
}
Assert.assertFalse(testFile.exists());
final AtomicLong flushes = new AtomicLong(0L);
CompressionUtils.gunzip(new FileInputStream(gzFile), new FilterOutputStream(new FileOutputStream(testFile) {
@Override
public void flush() throws IOException {
if (flushes.getAndIncrement() > 0) {
super.flush();
} else {
throw new IOException("Test exception");
}
}
}));
}
use of java.io.FileInputStream in project druid by druid-io.
the class CompressionUtilsTest method testGoodZipStream.
@Test
public void testGoodZipStream() throws IOException {
final File tmpDir = temporaryFolder.newFolder("testGoodZipStream");
final File zipFile = new File(tmpDir, "compressionUtilTest.zip");
CompressionUtils.zip(testDir, new FileOutputStream(zipFile));
final File newDir = new File(tmpDir, "newDir");
newDir.mkdir();
CompressionUtils.unzip(new FileInputStream(zipFile), newDir);
final Path newPath = Paths.get(newDir.getAbsolutePath(), testFile.getName());
Assert.assertTrue(newPath.toFile().exists());
try (final FileInputStream inputStream = new FileInputStream(newPath.toFile())) {
assertGoodDataStream(inputStream);
}
}
use of java.io.FileInputStream in project druid by druid-io.
the class CompressionUtilsTest method testGoodGZStream.
@Test
public void testGoodGZStream() throws IOException {
final File tmpDir = temporaryFolder.newFolder("testGoodGZStream");
final File gzFile = new File(tmpDir, testFile.getName() + ".gz");
Assert.assertFalse(gzFile.exists());
CompressionUtils.gzip(new FileInputStream(testFile), new FileOutputStream(gzFile));
Assert.assertTrue(gzFile.exists());
try (final InputStream inputStream = new GZIPInputStream(new FileInputStream(gzFile))) {
assertGoodDataStream(inputStream);
}
if (!testFile.delete()) {
throw new IOException(String.format("Unable to delete file [%s]", testFile.getAbsolutePath()));
}
Assert.assertFalse(testFile.exists());
CompressionUtils.gunzip(new FileInputStream(gzFile), testFile);
Assert.assertTrue(testFile.exists());
try (final InputStream inputStream = new FileInputStream(testFile)) {
assertGoodDataStream(inputStream);
}
}
use of java.io.FileInputStream in project druid by druid-io.
the class CompressionUtilsTest method testGoodZipCompressUncompress.
@Test
public void testGoodZipCompressUncompress() throws IOException {
final File tmpDir = temporaryFolder.newFolder("testGoodZipCompressUncompress");
final File zipFile = new File(tmpDir, "compressionUtilTest.zip");
try {
CompressionUtils.zip(testDir, zipFile);
final File newDir = new File(tmpDir, "newDir");
newDir.mkdir();
CompressionUtils.unzip(zipFile, newDir);
final Path newPath = Paths.get(newDir.getAbsolutePath(), testFile.getName());
Assert.assertTrue(newPath.toFile().exists());
try (final FileInputStream inputStream = new FileInputStream(newPath.toFile())) {
assertGoodDataStream(inputStream);
}
} finally {
if (zipFile.exists()) {
zipFile.delete();
}
if (tmpDir.exists()) {
tmpDir.delete();
}
}
}
use of java.io.FileInputStream in project druid by druid-io.
the class CompressionUtilsTest method testGunzipBugworkarround.
@Test
public // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7036144
void testGunzipBugworkarround() throws IOException {
testFile.delete();
Assert.assertFalse(testFile.exists());
final ByteArrayOutputStream tripleGzByteStream = new ByteArrayOutputStream(gzBytes.length * 3);
tripleGzByteStream.write(gzBytes);
tripleGzByteStream.write(gzBytes);
tripleGzByteStream.write(gzBytes);
final ByteSource inputStreamFactory = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ZeroRemainingInputStream(new ByteArrayInputStream(tripleGzByteStream.toByteArray()));
}
};
Assert.assertEquals((long) (expected.length * 3), CompressionUtils.gunzip(inputStreamFactory, testFile).size());
try (final InputStream inputStream = new FileInputStream(testFile)) {
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(expected.length * 3)) {
Assert.assertEquals("Read terminated too soon (7036144)", expected.length * 3, ByteStreams.copy(inputStream, outputStream));
final byte[] found = outputStream.toByteArray();
Assert.assertEquals(expected.length * 3, found.length);
Assert.assertArrayEquals(expected, Arrays.copyOfRange(found, expected.length * 0, expected.length * 1));
Assert.assertArrayEquals(expected, Arrays.copyOfRange(found, expected.length * 1, expected.length * 2));
Assert.assertArrayEquals(expected, Arrays.copyOfRange(found, expected.length * 2, expected.length * 3));
}
}
}
Aggregations