Search in sources :

Example 21 with FilterOutputStream

use of java.io.FilterOutputStream in project javaee7-samples by javaee-samples.

the class MyServerWriterInterceptor method aroundWriteTo.

@Override
public void aroundWriteTo(WriterInterceptorContext wic) throws IOException, WebApplicationException {
    System.out.println("MyServerWriterInterceptor");
    wic.setOutputStream(new FilterOutputStream(wic.getOutputStream()) {

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        @Override
        public void write(int b) throws IOException {
            baos.write(b);
            super.write(b);
        }

        @Override
        public void close() throws IOException {
            System.out.println("MyServerWriterInterceptor --> " + baos.toString());
            super.close();
        }
    });
    wic.proceed();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream)

Example 22 with FilterOutputStream

use of java.io.FilterOutputStream in project javaee7-samples by javaee-samples.

the class MyClientWriterInterceptor method aroundWriteTo.

@Override
public void aroundWriteTo(WriterInterceptorContext wic) throws IOException, WebApplicationException {
    System.out.println("MyClientWriterInterceptor");
    wic.setOutputStream(new FilterOutputStream(wic.getOutputStream()) {

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        @Override
        public void write(int b) throws IOException {
            baos.write(b);
            super.write(b);
        }

        @Override
        public void close() throws IOException {
            System.out.println("MyClientWriterInterceptor --> " + baos.toString());
            super.close();
        }
    });
    //        wic.setOutputStream(new FilterOutputStream(wic.getOutputStream()) {
    //            
    //            @Override
    //            public void write(int b) throws IOException {
    //                System.out.println("**** "  + (char)b);
    //                super.write(b);
    //            }
    //            
    //        });
    wic.proceed();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream)

Example 23 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.<Throwable>alwaysTrue());
    Assert.assertTrue(gzFile.exists());
    try (final InputStream inputStream = CompressionUtils.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(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) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) 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 24 with FilterOutputStream

use of java.io.FilterOutputStream in project j2objc by google.

the class OldFilterOutputStreamTest method test_writeI.

public void test_writeI() throws IOException {
    Support_OutputStream sos = new Support_OutputStream(1);
    os = new FilterOutputStream(sos);
    os.write(42);
    bis = new ByteArrayInputStream(sos.toByteArray());
    assertTrue("Test 1: Byte has not been written.", bis.available() == 1);
    assertEquals("Test 2: Incorrect byte written or read;", 42, bis.read());
    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)

Example 25 with FilterOutputStream

use of java.io.FilterOutputStream in project j2objc by google.

the class OldFilterOutputStreamTest method test_write$BII_Exception.

public void test_write$BII_Exception() throws IOException {
    Support_OutputStream sos = new Support_OutputStream(testLength);
    os = new FilterOutputStream(sos);
    byte[] buf = new byte[10];
    try {
        os.write(buf, -1, 1);
        fail("IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        os.write(buf, 0, -1);
        fail("IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        os.write(buf, 10, 1);
        fail("IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) FilterOutputStream(java.io.FilterOutputStream)

Aggregations

FilterOutputStream (java.io.FilterOutputStream)36 IOException (java.io.IOException)27 Support_OutputStream (tests.support.Support_OutputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 File (java.io.File)4 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 FileInputStream (java.io.FileInputStream)3 FilterInputStream (java.io.FilterInputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ByteSink (com.google.common.io.ByteSink)2 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 Writer (java.io.Writer)2