@Override public synchronized void write(int b) throws IOException { if(getRemainingBytes()==0) throw new StreamOutOfBoundException(allowedBytes); out.write(b); totalWritten++; }
@Override public synchronized void write(byte[] b, int off, int len) throws IOException { int canWrite = (int)Math.min(getRemainingBytes(), len); if(canWrite==0) throw new StreamOutOfBoundException(allowedBytes); out.write(b, off, canWrite); totalWritten += canWrite; } }
/** * Tests a <code>BoundedOutputStream</code> operating in unbounded mode. * * @throws IOException should not happen */ @Test public void testUnboundedStream() throws IOException { BoundedOutputStream bout = new BoundedOutputStream(new SinkOutputStream(), -1); assert 0 == bout.getProcessedBytes(); assert Long.MAX_VALUE == bout.getRemainingBytes(); assert -1 == bout.getAllowedBytes(); bout.write(0); assert 1 == bout.getProcessedBytes(); assert Long.MAX_VALUE == bout.getRemainingBytes(); assert -1 == bout.getAllowedBytes(); bout.write(new byte[1]); assert 2 == bout.getProcessedBytes(); assert Long.MAX_VALUE == bout.getRemainingBytes(); assert -1 == bout.getAllowedBytes(); bout.write(new byte[2], 0, 2); assert 4 == bout.getProcessedBytes(); assert Long.MAX_VALUE == bout.getRemainingBytes(); assert -1 == bout.getAllowedBytes(); } }
assert 4 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); assert 3 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); assert 2 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); assert 0 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); assert 0 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); assert 0 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes();