@Override public synchronized void write(byte[] b) throws IOException { write(b, 0, b.length); }
/** * 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(); } }
@Override public synchronized void write(int b) throws IOException { if(getRemainingBytes()==0) throw new StreamOutOfBoundException(allowedBytes); out.write(b); totalWritten++; }
BoundedOutputStream bout = new BoundedOutputStream(new SinkOutputStream(), 4); assert 0 == bout.getProcessedBytes(); assert 4 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); bout.write(0); assert 1 == bout.getProcessedBytes(); assert 3 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); bout.write(new byte[1]); assert 2 == bout.getProcessedBytes(); assert 2 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); bout.write(new byte[2], 0, 2); assert 4 == bout.getProcessedBytes(); assert 0 == bout.getRemainingBytes(); assert 4 == bout.getAllowedBytes(); try { bout.write(0); } catch(StreamOutOfBoundException e) { exceptionThrown = true; } try { bout.write(new byte[1]); } catch(StreamOutOfBoundException e) { exceptionThrown = true; } try { bout.write(new byte[1], 0, 1); } catch(StreamOutOfBoundException e) { exceptionThrown = true; }
@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; } }