Search in sources :

Example 51 with FilterOutputStream

use of java.io.FilterOutputStream in project nutz by nutzam.

the class Sender method getOutputStream.

protected OutputStream getOutputStream() throws IOException {
    OutputStream out = conn.getOutputStream();
    if (progressListener == null) {
        return out;
    }
    return new FilterOutputStream(out) {

        int count;

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            super.write(b, off, len);
            count += len;
            progressListener.invoke(count);
        }
    };
}
Also used : OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 52 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 53 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 54 with FilterOutputStream

use of java.io.FilterOutputStream in project ceylon by eclipse.

the class JarEntryManifestFileObject method openOutputStream.

@Override
public OutputStream openOutputStream() throws IOException {
    this.baos = new ByteArrayOutputStream();
    return new FilterOutputStream(this.baos) {

        @Override
        public void close() throws IOException {
            // If the last line of a MANIFEST.MF lacks a newline
            // then it gets ignored, so let's add one here
            // to avoid that trap
            super.write('\n');
            super.close();
        }
    };
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 55 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() {
            return new ByteArrayInputStream(bytes);
        }
    }, new ByteSink() {

        @Override
        public OutputStream openStream() {
            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)

Aggregations

FilterOutputStream (java.io.FilterOutputStream)59 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 OutputStream (java.io.OutputStream)19 FileOutputStream (java.io.FileOutputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 File (java.io.File)12 Support_OutputStream (tests.support.Support_OutputStream)12 Test (org.junit.Test)9 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)7 FilterInputStream (java.io.FilterInputStream)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ByteSink (com.google.common.io.ByteSink)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 File (com.hierynomus.smbj.share.File)3 DataOutputStream (java.io.DataOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3