/** 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(); }
ClientCHK readKey(int blockNumber) throws IOException, MissingKeyException { byte[] buf = parent.innerReadSegmentKey(segNo, blockNumber); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(segNo); dos.writeInt(blockNumber); dos.close(); byte[] prefix = baos.toByteArray(); byte[] checkBuf = new byte[prefix.length + buf.length]; System.arraycopy(prefix, 0, checkBuf, 0, prefix.length); int checksumLength = parent.checker.checksumLength(); System.arraycopy(buf, 0, checkBuf, prefix.length, buf.length - checksumLength); byte[] checksum = Arrays.copyOfRange(buf, buf.length - checksumLength, buf.length); if(parent.checker.checkChecksum(checkBuf, 0, checkBuf.length, checksum)) throw new MissingKeyException(); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(buf)); byte b = dis.readByte(); if(b != 1) throw new MissingKeyException(); ClientCHK key = innerReadKey(dis); setHasKey(blockNumber); if(logDEBUG) Logger.debug(this, "Returning "+key); return key; }
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(); } }
System.arraycopy(checksumTypeBuf, 0, buf, 8, 2); System.arraycopy(versionBuf, 0, buf, 10, 4); if(!checksumChecker.checkChecksum(buf, 0, 14, checksum)) throw new StorageFormatException("Checksum failed on basic settings length and version"); dis = new DataInputStream(new ByteArrayInputStream(buf));