private Option<E> readAndCheck(long address, MutableLong nextElementStart) throws IOException { if (address+4 > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } final int length = memory.getInt(address); if (length < 0) { return Option.none(); } if (address+8 > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } if (address+8+length > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } final int checksum = memory.getInt(address+4); MemoryDataInput in = new MemoryDataInput(memory); in.seek(address+8); CRC32 crc32 = new CRC32(); crc32.update(CRC_SEED); byte[] bytes = new byte[length]; in.readFully(bytes); crc32.update(bytes); if ((int)crc32.getValue() != checksum) { throw new ConsistencyException("checksum for record does not match: expected "+checksum+" actual "+(int)crc32.getValue()); } E ret = serializer.read(ByteStreams.newDataInput(bytes)); if (nextElementStart != null) nextElementStart.setValue(address+8+length); return Option.some(ret); }
private Option<E> readAndCheck(long address, MutableLong nextElementStart) throws IOException { if (address+4 > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } final int length = memory.getInt(address); if (length < 0) { return Option.none(); } if (address+8 > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } if (address+8+length > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } final int checksum = memory.getInt(address+4); MemoryDataInput in = new MemoryDataInput(memory); in.seek(address+8); CRC32 crc32 = new CRC32(); crc32.update(CRC_SEED); byte[] bytes = new byte[length]; in.readFully(bytes); crc32.update(bytes); if ((int)crc32.getValue() != checksum) { throw new ConsistencyException("checksum for record does not match: expected "+checksum+" actual "+(int)crc32.getValue()); } E ret = serializer.read(ByteStreams.newDataInput(bytes)); if (nextElementStart != null) nextElementStart.setValue(address+8+length); return Option.some(ret); }