KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > ChecksumValidator


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ChecksumValidator.java,v 1.31 2006/10/30 21:14:20 bostic Exp $
7  */

8
9 package com.sleepycat.je.log;
10
11 import java.nio.ByteBuffer JavaDoc;
12 import java.util.zip.Checksum JavaDoc;
13
14 import com.sleepycat.je.dbi.EnvironmentImpl;
15 import com.sleepycat.je.utilint.Adler32;
16 import com.sleepycat.je.utilint.DbLsn;
17
18 /**
19  * Checksum validator is used to check checksums on log entries.
20  */

21 class ChecksumValidator {
22     private static final boolean DEBUG = false;
23
24     private Checksum JavaDoc cksum;
25
26     ChecksumValidator() {
27         cksum = Adler32.makeChecksum();
28     }
29
30     void reset() {
31         cksum.reset();
32     }
33
34     /**
35      * Add this byte buffer to the checksum. Assume the byte buffer is already
36      * positioned at the data.
37      * @param buf target buffer
38      * @param length of data
39      */

40     void update(EnvironmentImpl env,
41         ByteBuffer JavaDoc 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         /*
93          * Pass null for env so that RunRecoveryException() does not
94          * invalidate the environment.
95          */

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