Search in sources :

Example 1 with FilterOutputStream

use of java.io.FilterOutputStream 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 2 with FilterOutputStream

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

the class StreamUtilsTest method testRetryExceptionOnFlush.

@Test
public void testRetryExceptionOnFlush() {
    final byte[] bytes = new byte[1 << 10];
    Random random = new Random(47831947819L);
    random.nextBytes(bytes);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final AtomicLong outputFlushes = new AtomicLong(0);
    Assert.assertEquals(bytes.length, StreamUtils.retryCopy(new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }
    }, new ByteSink() {

        @Override
        public OutputStream openStream() throws IOException {
            byteArrayOutputStream.reset();
            return new FilterOutputStream(byteArrayOutputStream) {

                @Override
                public void flush() throws IOException {
                    if (outputFlushes.getAndIncrement() > 0) {
                        out.flush();
                    } else {
                        throw new IOException("Test exception");
                    }
                }
            };
        }
    }, FileUtils.IS_EXCEPTION, 10));
    // 2 closes and 2 manual flushes
    Assert.assertEquals(4, outputFlushes.get());
    Assert.assertArrayEquals(bytes, byteArrayOutputStream.toByteArray());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteSink(com.google.common.io.ByteSink) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) Test(org.junit.Test)

Example 3 with FilterOutputStream

use of java.io.FilterOutputStream in project poi by apache.

the class AesZipFileZipEntrySource method copyToFile.

private static void copyToFile(InputStream is, File tmpFile, CipherAlgorithm cipherAlgorithm, byte[] keyBytes, byte[] ivBytes) throws IOException, GeneralSecurityException {
    SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
    Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
    ZipInputStream zis = new ZipInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipOutputStream zos = new ZipOutputStream(fos);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        // the cipher output stream pads the data, therefore we can't reuse the ZipEntry with set sizes
        // as those will be validated upon close()
        ZipEntry zeNew = new ZipEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());
        zeNew.setTime(ze.getTime());
        // zeNew.setMethod(ze.getMethod());
        zos.putNextEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {

            // don't close underlying ZipOutputStream
            @Override
            public void close() {
            }
        };
        CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
        IOUtils.copy(zis, cos);
        cos.close();
        fos2.close();
        zos.closeEntry();
        zis.closeEntry();
    }
    zos.close();
    fos.close();
    zis.close();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) CipherOutputStream(javax.crypto.CipherOutputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Cipher(javax.crypto.Cipher) FilterOutputStream(java.io.FilterOutputStream)

Example 4 with FilterOutputStream

use of java.io.FilterOutputStream in project poi by apache.

the class StreamHelper method saveXmlInStream.

/**
	 * Save the document object in the specified output stream.
	 *
	 * @param xmlContent
	 *            The XML document.
	 * @param outStream
	 *            The OutputStream in which the XML document will be written.
	 * @return <b>true</b> if the xml is successfully written in the stream,
	 *         else <b>false</b>.
	 */
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
    try {
        Transformer trans = getIdentityTransformer();
        Source xmlSource = new DOMSource(xmlContent);
        // prevent close of stream by transformer:
        Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                out.write(b, off, len);
            }

            @Override
            public void close() throws IOException {
                // only flush, don't close!
                out.flush();
            }
        });
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
        trans.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        return false;
    }
    return true;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 5 with FilterOutputStream

use of java.io.FilterOutputStream in project robovm by robovm.

the class OldFilterOutputStreamTest method test_write$B.

public void test_write$B() throws IOException {
    Support_OutputStream sos = new Support_OutputStream(testLength);
    os = new FilterOutputStream(sos);
    os.write(fileString.getBytes());
    bis = new ByteArrayInputStream(sos.toByteArray());
    assertTrue("Test 1: Bytes have not been written.", bis.available() == testLength);
    byte[] wbytes = new byte[testLength];
    bis.read(wbytes, 0, testLength);
    assertTrue("Test 2: Incorrect bytes written or read.", fileString.equals(new String(wbytes)));
    try {
        // Support_OutputStream throws an IOException if the internal
        // buffer is full, which it should be now.
        os.write(42);
        fail("Test 2: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream)

Aggregations

FilterOutputStream (java.io.FilterOutputStream)54 IOException (java.io.IOException)36 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 OutputStream (java.io.OutputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)13 FileOutputStream (java.io.FileOutputStream)13 Support_OutputStream (tests.support.Support_OutputStream)12 File (java.io.File)11 Test (org.junit.Test)9 InputStream (java.io.InputStream)8 FileInputStream (java.io.FileInputStream)6 FilterInputStream (java.io.FilterInputStream)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 BufferedOutputStream (java.io.BufferedOutputStream)5 ByteSink (com.google.common.io.ByteSink)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 DataOutputStream (java.io.DataOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 ByteSource (com.google.common.io.ByteSource)2