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;
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations