Search in sources :

Example 1 with CountingOutputStream

use of org.apache.commons.io.output.CountingOutputStream in project hudson-2.x by hudson.

the class LargeText method writeLogTo.

/**
     * Writes the tail portion of the file to the {@link Writer}.
     *
     * <p>
     * The text file is assumed to be in the system default encoding.
     *
     * @param start
     *      The byte offset in the input file where the write operation starts.
     *
     * @return
     *      if the file is still being written, this method writes the file
     *      until the last newline character and returns the offset to start
     *      the next write operation.
     */
public long writeLogTo(long start, Writer w) throws IOException {
    CountingOutputStream os = new CountingOutputStream(new WriterOutputStream(w));
    Session f = source.open();
    f.skip(start);
    if (completed) {
        // write everything till EOF
        byte[] buf = new byte[1024];
        int sz;
        while ((sz = f.read(buf)) >= 0) os.write(buf, 0, sz);
    } else {
        ByteBuf buf = new ByteBuf(null, f);
        HeadMark head = new HeadMark(buf);
        TailMark tail = new TailMark(buf);
        while (tail.moveToNextLine(f)) {
            head.moveTo(tail, os);
        }
        head.finish(os);
    }
    f.close();
    os.flush();
    return os.getCount() + start;
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) WriterOutputStream(org.kohsuke.stapler.framework.io.WriterOutputStream)

Example 2 with CountingOutputStream

use of org.apache.commons.io.output.CountingOutputStream in project jackrabbit-oak by apache.

the class StatsCollectingStreamsTest method downloadCallback.

@Test
public void downloadCallback() throws Exception {
    NullInputStream in = new NullInputStream(1042);
    TestCollector stats = new TestCollector();
    InputStream wrappedStream = StatsCollectingStreams.wrap(stats, "foo", in);
    assertEquals(0, stats.callbackCount);
    //Copy the content to get size
    CountingOutputStream cos = new CountingOutputStream(new NullOutputStream());
    IOUtils.copy(wrappedStream, cos);
    assertEquals(1042, cos.getCount());
    //Stream not closed so no callback
    assertEquals(0, stats.callbackCount);
    wrappedStream.close();
    assertEquals(1042, stats.size);
    assertEquals(1, stats.downloadCompletedCount);
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) NullInputStream(org.apache.commons.io.input.NullInputStream) InputStream(java.io.InputStream) NullInputStream(org.apache.commons.io.input.NullInputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Test(org.junit.Test)

Example 3 with CountingOutputStream

use of org.apache.commons.io.output.CountingOutputStream in project jackrabbit-oak by apache.

the class AbstractBlobStoreTest method downloadCallback.

@Test
public void downloadCallback() throws Exception {
    assumeTrue(supportsStatsCollection());
    TestCollector collector = new TestCollector();
    setupCollector(collector);
    int size = 10 * 1024;
    String id = store.writeBlob(randomStream(42, size));
    store.clearCache();
    collector.reset();
    InputStream is = store.getInputStream(id);
    CountingOutputStream cos = new CountingOutputStream(new NullOutputStream());
    IOUtils.copy(is, cos);
    is.close();
    assertEquals(size, cos.getCount());
    //For chunked storage the actual stored size is greater than the file size
    assertCollectedSize(collector.size, size);
    assertEquals(1, collector.downloadCount);
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Test(org.junit.Test)

Example 4 with CountingOutputStream

use of org.apache.commons.io.output.CountingOutputStream in project uPortal by Jasig.

the class PortletHttpServletResponseWrapper method getOutputStream.

@Override
public ServletOutputStream getOutputStream() throws IOException {
    if (this.servletOutputStream == null) {
        final OutputStream out;
        if (logger.isDebugEnabled()) {
            out = new ByteArrayOutputStream() {

                @Override
                public void close() throws IOException {
                    super.close();
                    final byte[] data = this.toByteArray();
                    if (data.length > 0) {
                        logger.warn("Ignored {} bytes written to ServletOutputStream by {}\n\n{}", new Object[] { data.length, portletWindow, new String(data) });
                    }
                }
            };
        } else {
            out = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM) {

                @Override
                public void close() throws IOException {
                    super.close();
                    final long byteCount = this.getByteCount();
                    if (byteCount > 0) {
                        logger.warn("Ignored {} bytes written to ServletOutputStream by {}, turn on DEBUG logging to see the output", byteCount, portletWindow);
                    }
                }
            };
        }
        this.servletOutputStream = new DelegatingServletOutputStream(out);
    }
    return this.servletOutputStream;
}
Also used : DelegatingServletOutputStream(org.apereo.portal.utils.DelegatingServletOutputStream) CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) OutputStream(java.io.OutputStream) CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) DelegatingServletOutputStream(org.apereo.portal.utils.DelegatingServletOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 5 with CountingOutputStream

use of org.apache.commons.io.output.CountingOutputStream in project indy by Commonjava.

the class TransferStreamingOutput method write.

@Override
public void write(final OutputStream out) throws IOException, WebApplicationException {
    try {
        CountingOutputStream cout = new CountingOutputStream(out);
        IOUtils.copy(stream, cout);
        Logger logger = LoggerFactory.getLogger(getClass());
        logger.debug("Wrote: {} bytes", cout.getByteCount());
    } finally {
        IOUtils.closeQuietly(stream);
    }
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) Logger(org.slf4j.Logger)

Aggregations

CountingOutputStream (org.apache.commons.io.output.CountingOutputStream)6 NullOutputStream (org.apache.commons.io.output.NullOutputStream)4 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 NullInputStream (org.apache.commons.io.input.NullInputStream)1 DelegatingServletOutputStream (org.apereo.portal.utils.DelegatingServletOutputStream)1 WriterOutputStream (org.kohsuke.stapler.framework.io.WriterOutputStream)1 Logger (org.slf4j.Logger)1