sclass LimitedOutputStream extends CountingOutputStream { long limit; *(OutputStream out) { super(out); } *(OutputStream out, long *limit) { super(out); } @Override public void write(int b) throws IOException { checkLimit(); super.write(b); } @Override public void write(byte[] b) throws IOException { checkLimit(); super.write(b); } @Override public void write(byte[] b, int off, int len) throws IOException { checkLimit(); super.write(b, off, len); } void checkLimit { if (counter >= limit) fail("Write limit reached (" + n2(limit) + " bytes)"); } }