public void truncateBlock(long newBlockLen)
throws IOException {
if (newBlockLen == 0) {
// Special case for truncating to 0 length, since there's no previous
// chunk.
RandomAccessor ra = blockDataFile.getRandomAccessor();
try {
ra.setLength(BlockInlineChecksumReader.getHeaderSize());
} finally {
ra.close();
}
return;
}
DataChecksum dcs = DataChecksum.newDataChecksum(this.checksumType, this.bytesPerChecksum);
this.checksumSize = dcs.getChecksumSize();
long newBlockFileSize = BlockInlineChecksumReader
.getFileLengthFromBlockSize(newBlockLen, bytesPerChecksum, checksumSize);
int lastchunksize = (int) (newBlockLen % bytesPerChecksum);
RandomAccessor ra = blockDataFile.getRandomAccessor();
try {
// truncate blockFile
ra.setLength(newBlockFileSize);
if (lastchunksize != 0) {
// Calculate last partial checksum.
long lastchunkoffset = BlockInlineChecksumReader.getPosFromBlockOffset(
newBlockLen - lastchunksize, bytesPerChecksum, checksumSize);
byte[] b = new byte[Math.max(lastchunksize, checksumSize)];
// read last chunk
ra.seek(lastchunkoffset);
ra.readFully(b, 0, lastchunksize);
// compute checksum
dcs.update(b, 0, lastchunksize);
dcs.writeValue(b, 0, false);
ra.seek(newBlockFileSize - checksumSize);
ra.write(b, 0, checksumSize);
}
} finally {
ra.close();
}
}