/** Verify a checksum or throw */ public void verifyChecksum(byte[] data, int offset, int length, byte[] checksum) throws ChecksumFailedException { if(!checkChecksum(data, offset, length, checksum)) throw new ChecksumFailedException(); }
@Override public void readAndChecksum(DataInput is, byte[] buf, int offset, int length) throws IOException, ChecksumFailedException { is.readFully(buf, offset, length); byte[] checksum = new byte[checksumLength()]; is.readFully(checksum); if(!checkChecksum(buf, offset, length, checksum)) { Arrays.fill(buf, offset, offset+length, (byte)0); throw new ChecksumFailedException(); } }
@Override public void copyAndStripChecksum(InputStream is, OutputStream destination, long length) throws IOException, ChecksumFailedException { // FIXME refactor via a base class for ChecksumOutputStream. CRC32 crc = new CRC32(); long remaining = length; byte[] buffer = new byte[FileUtil.BUFFER_SIZE]; int read = 0; DataInputStream source = new DataInputStream(is); while ((remaining == -1) || (remaining > 0)) { read = source.read(buffer, 0, ((remaining > FileUtil.BUFFER_SIZE) || (remaining == -1)) ? FileUtil.BUFFER_SIZE : (int) remaining); if (read == -1) { if (length == -1) { return; } throw new EOFException("stream reached eof"); } if(read == 0) throw new IOException("stream returning 0 bytes"); if(read != 0) crc.update(buffer, 0, read); destination.write(buffer, 0, read); if (remaining > 0) remaining -= read; } byte[] checksum = new byte[checksumLength()]; source.readFully(checksum); byte[] myChecksum = Fields.intToBytes((int)crc.getValue()); if(!Arrays.equals(checksum, myChecksum)) throw new ChecksumFailedException(); }
byte[] preadChecksummedWithLength(long fileOffset) throws IOException, ChecksumFailedException, StorageFormatException { byte[] checksumBuf = new byte[checksumLength]; RAFLock lock = raf.lockOpen(); byte[] lengthBuf = new byte[8]; byte[] buf; int length; try { raf.pread(fileOffset, lengthBuf, 0, lengthBuf.length); long len = new DataInputStream(new ByteArrayInputStream(lengthBuf)).readLong(); if(len + fileOffset > rafLength || len > Integer.MAX_VALUE || len < 0) throw new StorageFormatException("Bogus length "+len); length = (int)len; buf = new byte[length]; raf.pread(fileOffset+lengthBuf.length, buf, 0, length); raf.pread(fileOffset+length+lengthBuf.length, checksumBuf, 0, checksumLength); } finally { lock.unlock(); } if(!checksumChecker.checkChecksum(buf, 0, length, checksumBuf)) { Arrays.fill(buf, 0, length, (byte)0); throw new ChecksumFailedException(); } return buf; }
void preadChecksummed(long fileOffset, byte[] buf, int offset, int length) throws IOException, ChecksumFailedException { byte[] checksumBuf = new byte[checksumLength]; RAFLock lock = raf.lockOpen(); try { raf.pread(fileOffset, buf, offset, length); raf.pread(fileOffset+length, checksumBuf, 0, checksumLength); } finally { lock.unlock(); } if(!checksumChecker.checkChecksum(buf, offset, length, checksumBuf)) { Arrays.fill(buf, offset, offset+length, (byte)0); throw new ChecksumFailedException(); } }
byte[] preadChecksummedWithLength(long fileOffset) throws IOException, ChecksumFailedException, StorageFormatException { byte[] checksumBuf = new byte[checker.checksumLength()]; RAFLock lock = raf.lockOpen(); byte[] lengthBuf = new byte[8]; byte[] buf; int length; try { raf.pread(fileOffset, lengthBuf, 0, lengthBuf.length); long len = new DataInputStream(new ByteArrayInputStream(lengthBuf)).readLong(); if(len + fileOffset > rafLength || len > Integer.MAX_VALUE || len < 0) throw new StorageFormatException("Bogus length "+len); length = (int)len; buf = new byte[length]; raf.pread(fileOffset+lengthBuf.length, buf, 0, length); raf.pread(fileOffset+length+lengthBuf.length, checksumBuf, 0, checker.checksumLength()); } finally { lock.unlock(); } if(!checker.checkChecksum(buf, 0, length, checksumBuf)) { Arrays.fill(buf, 0, length, (byte)0); throw new ChecksumFailedException(); } return buf; }
void preadChecksummed(long fileOffset, byte[] buf, int offset, int length) throws IOException, ChecksumFailedException { byte[] checksumBuf = new byte[checker.checksumLength()]; RAFLock lock = raf.lockOpen(); try { raf.pread(fileOffset, buf, offset, length); raf.pread(fileOffset+length, checksumBuf, 0, checker.checksumLength()); } finally { lock.unlock(); } if(!checker.checkChecksum(buf, offset, length, checksumBuf)) { Arrays.fill(buf, offset, offset+length, (byte)0); throw new ChecksumFailedException(); } }