@Test public void write() throws Exception { byte output[] = new byte[data.length]; int crc; try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) { try (FileWriter writer = new Crc32AppendingFileWriter(new FileChannelWriter(fd, 0), 4)) { ByteBuffer buf = ByteBuffer.wrap(data); while (buf.hasRemaining()) writer.write(buf); } MappedByteBuffer mapped = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size()); mapped.order(ByteOrder.BIG_ENDIAN); mapped.get(output); for (int i = 0; i < padLen; ++i) assertEquals((byte)0, mapped.get()); crc = mapped.getInt(); assertFalse(mapped.hasRemaining()); } assertArrayEquals(data, output); assertEquals(expectedCrc, crc); }
@Override public int write(ByteBuffer buf) throws IOException { final int wlen = out.write(buf); written += wlen; return wlen; }
Crc32AppendingFileWriter implements FileWriter { @Override public int write(ByteBuffer buf) throws IOException { final int wlen = out.write(buf); written += wlen; return wlen; } }
Crc32AppendingFileWriter implements FileWriter { @Override public int write(ByteBuffer buf) throws IOException { final int wlen = out.write(buf); written += wlen; return wlen; } Crc32AppendingFileWriter(@NonNull FileWriter out, int roundUp); }
Crc32AppendingFileWriter implements FileWriter { @Override public int write(ByteBuffer buf) throws IOException { final int wlen = out.write(buf); written += wlen; return wlen; } Crc32AppendingFileWriter(@NonNull FileWriter out, int roundUp); @Override int write(ByteBuffer buf); @Override void close(); @Override ByteBuffer allocateByteBuffer(int size); }
Crc32AppendingFileWriter implements FileWriter { @Override public int write(ByteBuffer buf) throws IOException { final int wlen = out.write(buf); written += wlen; return wlen; } Crc32AppendingFileWriter(@NonNull FileWriter out, int roundUp); @Override int write(ByteBuffer buf); @Override void close(); @Override ByteBuffer allocateByteBuffer(int size); static final int CRC_LEN; }