Search in sources :

Example 21 with FileInputStream

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");
            }
        }
    }));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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 22 with FileInputStream

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);
    }
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 23 with FileInputStream

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 24 with FileInputStream

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();
        }
    }
}
Also used : Path(java.nio.file.Path) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 25 with FileInputStream

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));
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

FileInputStream (java.io.FileInputStream)14180 File (java.io.File)6801 IOException (java.io.IOException)6283 InputStream (java.io.InputStream)4121 FileOutputStream (java.io.FileOutputStream)2210 FileNotFoundException (java.io.FileNotFoundException)2002 InputStreamReader (java.io.InputStreamReader)1733 BufferedInputStream (java.io.BufferedInputStream)1593 Test (org.junit.Test)1548 BufferedReader (java.io.BufferedReader)1414 Properties (java.util.Properties)1410 ArrayList (java.util.ArrayList)960 OutputStream (java.io.OutputStream)753 ByteArrayInputStream (java.io.ByteArrayInputStream)599 HashMap (java.util.HashMap)597 ZipEntry (java.util.zip.ZipEntry)575 DataInputStream (java.io.DataInputStream)549 GZIPInputStream (java.util.zip.GZIPInputStream)435 KeyStore (java.security.KeyStore)430 ByteArrayOutputStream (java.io.ByteArrayOutputStream)407