@Test public void write() throws Exception { byte output[] = new byte[data.length]; try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.WRITE)) { try (FileWriter writer = new SizeVerifyingWriter(new FileChannelWriter(fd, 0), data.length)) { ByteBuffer buf = ByteBuffer.wrap(data); while (buf.hasRemaining()) writer.write(buf); } } try (InputStream input = new FileInputStream(file)) { int off = 0; while (off < output.length) { int rlen = input.read(output, off, output.length - off); assertNotEquals("insufficient bytes were written", -1, rlen); off += rlen; } } assertArrayEquals(data, output); }
@Override public int write(ByteBuffer data) throws IOException { if (data.remaining() > expected - written) throw new IOLengthVerificationFailed(expected, written + data.remaining()); final int wlen = out.write(data); written += wlen; return wlen; }
SizeVerifyingWriter implements FileWriter { @Override public int write(ByteBuffer data) throws IOException { if (data.remaining() > expected - written) throw new IOLengthVerificationFailed(expected, written + data.remaining()); final int wlen = out.write(data); written += wlen; return wlen; } }
SizeVerifyingWriter implements FileWriter { @Override public int write(ByteBuffer data) throws IOException { if (data.remaining() > expected - written) throw new IOLengthVerificationFailed(expected, written + data.remaining()); final int wlen = out.write(data); written += wlen; return wlen; }  }
SizeVerifyingWriter implements FileWriter { @Override public int write(ByteBuffer data) throws IOException { if (data.remaining() > expected - written) throw new IOLengthVerificationFailed(expected, written + data.remaining()); final int wlen = out.write(data); written += wlen; return wlen; }  @Override int write(ByteBuffer data); @Override void close(); @Override ByteBuffer allocateByteBuffer(int size); }
SizeVerifyingWriter implements FileWriter { @Override public int write(ByteBuffer data) throws IOException { if (data.remaining() > expected - written) throw new IOLengthVerificationFailed(expected, written + data.remaining()); final int wlen = out.write(data); written += wlen; return wlen; }  @Override int write(ByteBuffer data); @Override void close(); @Override ByteBuffer allocateByteBuffer(int size);  }