/** * Closes underlying output stream, and mark this as closed * * @throws IOException if an error occurs. */ @Override public void close() throws IOException { super.close(); closed = true; }
/** * Writes the specified byte to this output stream. * * @param b The byte to be written. * * @throws IOException if an error occurs. */ @Override public void write(final int b) throws IOException { checkThreshold(1); getStream().write(b); written++; }
@Test public void testSetByteCount() throws Exception { final AtomicBoolean reached = new AtomicBoolean(false); final ThresholdingOutputStream tos = new ThresholdingOutputStream(3) { { setByteCount(2); } @Override protected OutputStream getStream() throws IOException { return new ByteArrayOutputStream(4); } @Override protected void thresholdReached() throws IOException { reached.set( true); } }; tos.write(12); assertFalse( reached.get()); tos.write(12); assertTrue(reached.get()); tos.close(); } }
/** * Flushes this output stream and forces any buffered output bytes to be * written out. * * @throws IOException if an error occurs. */ @Override public void flush() throws IOException { getStream().flush(); }
/** * Checks to see if writing the specified number of bytes would cause the * configured threshold to be exceeded. If so, triggers an event to allow * a concrete implementation to take action on this. * * @param count The number of bytes about to be written to the underlying * output stream. * * @throws IOException if an error occurs. */ protected void checkThreshold(final int count) throws IOException { if (!thresholdExceeded && written + count > threshold) { thresholdExceeded = true; thresholdReached(); } }
@Test public void testTresholdCanBeSpecifiedForTimeConsumingResources() throws Exception { // GIVEN when(request.getAttribute(CacheResponseWrapper.ATTRIBUTE_IN_MEMORY_THRESHOLD)).thenReturn(20); CacheResponseWrapper responseWrapper = new CacheResponseWrapper(response, 10, false); ThresholdingOutputStream out = responseWrapper.getThresholdingOutputStream(); // WHEN out.write("1234567890".getBytes()); //10 bytes to the default threshold // THEN assertThat(out.isThresholdExceeded(), equalTo(false)); // WHEN out.write('c'); //one byte beyond the default threshold // THEN assertThat(out.isThresholdExceeded(), equalTo(false)); // WHEN out.write("123456789".getBytes()); //another 9 bytes to the threshold * 2 // THEN assertThat(out.isThresholdExceeded(), equalTo(false)); // WHEN out.write('c'); //one byte beyond the threshold * 2 // THEN assertThat(out.isThresholdExceeded(), equalTo(true)); }
@Override public void write(byte[] b, int off, int len) throws IOException { int remaining = this.getThreshold() - (int)this.getByteCount(); if (!isThresholdExceeded() && len > remaining) { super.write(b, off, remaining); super.write(b, off + remaining, len - remaining); } else { super.write(b, off, len); } } }
/** * Flushes this output stream and forces any buffered output bytes to be * written out. * * @exception IOException if an error occurs. */ public void flush() throws IOException { getStream().flush(); }
/** * Checks to see if writing the specified number of bytes would cause the * configured threshold to be exceeded. If so, triggers an event to allow * a concrete implementation to take action on this. * * @param count The number of bytes about to be written to the underlying * output stream. * * @exception IOException if an error occurs. */ protected void checkThreshold(int count) throws IOException { if (!thresholdExceeded && (written + count > threshold)) { thresholdReached(); thresholdExceeded = true; } }
/** * Writes <code>b.length</code> bytes from the specified byte array to this * output stream. * * @param b The array of bytes to be written. * * @throws IOException if an error occurs. */ @Override public void write(final byte b[]) throws IOException { checkThreshold(b.length); getStream().write(b); written += b.length; }
/** * Closes underlying output stream, and mark this as closed * * @exception IOException if an error occurs. */ public void close() throws IOException { super.close(); closed = true; }
/** * Flushes this output stream and forces any buffered output bytes to be * written out. * * @exception IOException if an error occurs. */ public void flush() throws IOException { getStream().flush(); }
/** * Checks to see if writing the specified number of bytes would cause the * configured threshold to be exceeded. If so, triggers an event to allow * a concrete implementation to take action on this. * * @param count The number of bytes about to be written to the underlying * output stream. * * @exception IOException if an error occurs. */ protected void checkThreshold(int count) throws IOException { if (!thresholdExceeded && written + count > threshold) { thresholdExceeded = true; thresholdReached(); } }
/** * Writes <code>len</code> bytes from the specified byte array starting at * offset <code>off</code> to this output stream. * * @param b The byte array from which the data will be written. * @param off The start offset in the byte array. * @param len The number of bytes to write. * * @throws IOException if an error occurs. */ @Override public void write(final byte b[], final int off, final int len) throws IOException { checkThreshold(len); getStream().write(b, off, len); written += len; }
/** * Closes underlying output stream, and mark this as closed * * @exception java.io.IOException if an error occurs. */ @Override public void close() throws IOException { super.close(); closed = true; currentOutputStream.close(); }
/** * Flushes this output stream and forces any buffered output bytes to be * written out. * * @throws IOException if an error occurs. */ @Override public void flush() throws IOException { getStream().flush(); }
/** * Checks to see if writing the specified number of bytes would cause the * configured threshold to be exceeded. If so, triggers an event to allow * a concrete implementation to take action on this. * * @param count The number of bytes about to be written to the underlying * output stream. * * @throws IOException if an error occurs. */ protected void checkThreshold(final int count) throws IOException { if (!thresholdExceeded && written + count > threshold) { thresholdExceeded = true; thresholdReached(); } }