1 8 9 package com.sleepycat.je.log; 10 11 import java.nio.ByteBuffer ; 12 import java.util.zip.Checksum ; 13 14 import com.sleepycat.je.dbi.EnvironmentImpl; 15 import com.sleepycat.je.utilint.Adler32; 16 import com.sleepycat.je.utilint.DbLsn; 17 18 21 class ChecksumValidator { 22 private static final boolean DEBUG = false; 23 24 private Checksum cksum; 25 26 ChecksumValidator() { 27 cksum = Adler32.makeChecksum(); 28 } 29 30 void reset() { 31 cksum.reset(); 32 } 33 34 40 void update(EnvironmentImpl env, 41 ByteBuffer buf, 42 int length, 43 boolean anticipateChecksumErrors) 44 throws DbChecksumException { 45 46 if (buf == null) { 47 throw new DbChecksumException 48 ((anticipateChecksumErrors ? null : env), 49 "null buffer given to checksum validation, probably " + 50 " result of 0's in log file. " + anticipateChecksumErrors); 51 } 52 53 int bufStart = buf.position(); 54 55 if (DEBUG) { 56 System.out.println("bufStart = " + bufStart + 57 " length = " + length); 58 } 59 60 if (buf.hasArray()) { 61 cksum.update(buf.array(), bufStart, length); 62 } else { 63 for (int i = bufStart; i < (length + bufStart); i++) { 64 cksum.update(buf.get(i)); 65 } 66 } 67 } 68 69 void validate(EnvironmentImpl env, 70 long expectedChecksum, 71 long lsn) 72 throws DbChecksumException { 73 74 if (expectedChecksum != cksum.getValue()) { 75 throw new DbChecksumException 76 (env, 77 "Location " + DbLsn.getNoFormatString(lsn) + 78 " expected " + expectedChecksum + " got " + cksum.getValue()); 79 } 80 } 81 82 void validate(EnvironmentImpl env, 83 long expectedChecksum, 84 long fileNum, 85 long fileOffset, 86 boolean anticipateChecksumErrors) 87 throws DbChecksumException { 88 89 if (expectedChecksum != cksum.getValue()) { 90 long problemLsn = DbLsn.makeLsn(fileNum, fileOffset); 91 92 96 throw new DbChecksumException 97 ((anticipateChecksumErrors ? null : env), 98 "Location " + DbLsn.getNoFormatString(problemLsn) + 99 " expected " + expectedChecksum + " got " + 100 cksum.getValue()); 101 } 102 } 103 } 104 | Popular Tags |