public OutputStream checksumWriter(OutputStream os) { return checksumWriter(os, 0); }
OutputStream checksumOutputStream(OutputStream os) { return checksumChecker.checksumWriter(os); }
/** * Create an OutputStream that we can write formatted data to of a specific * length. On close(), it checks that the length is as expected, computes * the checksum, and writes the data to the specified position in the file. * * @param fileOffset * The position in the file (raf) of the first byte. * @param length * The length, including checksum, of the data to be written. * @return */ OutputStream writeChecksummedTo(final long fileOffset, final int length) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(length); OutputStream cos = checker.checksumWriter(baos); return new FilterOutputStream(cos) { public void close() throws IOException { out.close(); byte[] buf = baos.toByteArray(); if (buf.length != length) throw new IllegalStateException("Wrote wrong number of bytes: " + buf.length + " should be " + length); raf.pwrite(fileOffset, buf, 0, length); } }; }
/** Get an OutputStream that will write to a temporary Bucket, append a checksum and prepend a * length. * @param os The underlying stream, which will not be closed. * @param bf Used to allocate temporary storage. * @throws IOException */ public PrependLengthOutputStream checksumWriterWithLength(final OutputStream dos, BucketFactory bf) throws IOException { return PrependLengthOutputStream.create(checksumWriter(dos, 8), bf, 0, true); }
private void writeRequestIdentifier(DataOutput os, RequestIdentifier req) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream oos = checker.checksumWriter(baos); DataOutputStream dos = new DataOutputStream(oos); req.writeTo(dos); dos.close(); byte[] buf = baos.toByteArray(); os.writeShort(buf.length - checker.checksumLength()); os.write(buf); }