1 8 9 package com.sleepycat.je.utilint; 10 11 import java.util.zip.Checksum ; 12 13 import com.sleepycat.je.dbi.EnvironmentImpl; 14 15 54 public class Adler32 implements Checksum { 55 56 57 public static class ChunkingAdler32 extends java.util.zip.Adler32 { 58 int adler32ChunkSize = 0; 59 60 public ChunkingAdler32(int adler32ChunkSize) { 61 this.adler32ChunkSize = adler32ChunkSize; 62 } 63 64 public void update(byte[] b, int off, int len) { 65 if (len < adler32ChunkSize) { 66 super.update(b, off, len); 67 return; 68 } 69 70 int i = 0; 71 while (i < len) { 72 int bytesRemaining = len - i; 73 int nBytesThisChunk = 74 Math.min(bytesRemaining, adler32ChunkSize); 75 super.update(b, off + i, nBytesThisChunk); 76 i += nBytesThisChunk; 77 } 78 } 79 } 80 81 public static Checksum makeChecksum() { 82 if (EnvironmentImpl.JAVA5_AVAILABLE) { 83 int adler32ChunkSize = EnvironmentImpl.getAdler32ChunkSize(); 84 if (adler32ChunkSize > 0) { 85 return new ChunkingAdler32(adler32ChunkSize); 86 } else { 87 return new java.util.zip.Adler32 (); 88 } 89 } else { 90 return new Adler32(); 91 } 92 } 93 94 private long adler = 1; 95 96 100 private static final int BASE = 65521; 101 private static final int NMAX = 5552; 102 103 106 public void update(int b) { 107 long s1 = adler & 0xffff; 108 long s2 = (adler >> 16) & 0xffff; 109 s1 = (s1 + (b & 0xff)) % BASE; 110 s2 = (s1 + s2) % BASE; 111 adler = (s2 << 16) | s1; 112 } 113 114 117 public void update(byte[] b, int off, int len) { 118 long s1 = adler & 0xffff; 119 long s2 = (adler >> 16) & 0xffff; 120 121 while (len > 0) { 122 int k = len < NMAX ? len : NMAX; 123 len -= k; 124 125 126 while (k-- > 0) { 127 s1 += (b[off++] & 0xff); 128 s2 += s1; 129 } 130 131 s1 %= BASE; 132 s2 %= BASE; 133 } 134 adler = (s2 << 16) | s1; 135 } 136 137 140 public void reset() { 141 adler = 1; 142 } 143 144 147 public long getValue() { 148 return adler; 149 } 150 } 151 | Popular Tags |