1 21 22 package org.apache.derby.impl.store.raw.log; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import org.apache.derby.iapi.services.io.Formatable; 26 import org.apache.derby.iapi.services.io.FormatIdUtil; 27 import org.apache.derby.iapi.services.io.StoredFormatIds; 28 import org.apache.derby.catalog.UUID; 29 30 import org.apache.derby.iapi.store.raw.Transaction; 31 import org.apache.derby.iapi.store.raw.Loggable; 32 import org.apache.derby.iapi.store.raw.log.LogInstant; 33 import org.apache.derby.iapi.store.raw.log.LogFactory; 34 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 35 36 import org.apache.derby.iapi.error.StandardException; 37 38 import org.apache.derby.iapi.services.io.CompressedNumber; 39 import org.apache.derby.iapi.util.ByteArray; 40 41 import java.io.Externalizable ; 42 import java.io.OutputStream ; 43 import java.io.InputStream ; 44 import java.io.ObjectInput ; 45 import java.io.ObjectOutput ; 46 import java.io.IOException ; 47 import org.apache.derby.iapi.services.io.LimitObjectInput; 48 49 import java.util.zip.Checksum ; 50 import java.util.zip.CRC32 ; 51 52 53 73 74 public class ChecksumOperation implements Loggable 75 { 76 77 private byte checksumAlgo; 78 private long checksumValue; 79 private int dataLength; 80 private Checksum checksum; 81 82 85 public static final byte CRC32_ALGORITHM = (byte) 0x1; 87 private static final int formatLength = FormatIdUtil.getFormatIdByteLength(StoredFormatIds.LOGOP_CHECKSUM); 88 89 public void init() 90 { 91 this.checksumAlgo = CRC32_ALGORITHM; 92 initializeChecksumAlgo(); 93 dataLength = 0; 94 } 95 96 97 protected void update(byte[] buf, int off, int len) 99 { 100 checksum.update(buf, off , len); 101 dataLength += len; 102 } 103 104 105 protected void reset() 107 { 108 checksum.reset(); 109 dataLength = 0; 110 } 111 112 113 private void initializeChecksumAlgo() 114 { 115 if(checksumAlgo == CRC32_ALGORITHM) 116 this.checksum = new CRC32 (); 117 } 118 119 120 123 124 public ChecksumOperation() { super();} 126 127 public void writeExternal(ObjectOutput out) throws IOException 128 { 129 checksumValue = checksum.getValue(); 130 out.writeByte(checksumAlgo); 131 out.writeInt(dataLength); 132 out.writeLong(checksumValue); 133 } 134 135 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 136 { 137 checksumAlgo = (byte) in.readUnsignedByte(); 138 dataLength = in.readInt(); 139 checksumValue = in.readLong(); 140 initializeChecksumAlgo(); 141 } 142 143 144 public int getStoredSize() 145 { 146 return formatLength + 1 + 4 + 8; 147 } 148 149 150 151 154 public int getTypeFormatId() { 155 return StoredFormatIds.LOGOP_CHECKSUM; 156 } 157 158 159 160 161 162 165 166 170 public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) throws StandardException 171 { 172 } 173 174 183 public ByteArray getPreparedLog() 184 { 185 return (ByteArray) null; 186 } 187 188 192 public boolean needsRedo(Transaction xact) 193 { 194 return false; 195 } 196 197 198 201 public void releaseResource(Transaction xact) 202 {} 203 204 207 public int group() 208 { 209 return Loggable.RAWSTORE | Loggable.CHECKSUM; 210 } 211 212 213 214 215 218 219 protected int getDataLength() 220 { 221 return dataLength; 222 } 223 224 225 protected boolean isChecksumValid(byte[] data, int off , int length) 226 { 227 checksum.reset(); 228 checksum.update(data , off , length); 229 return checksum.getValue()== checksumValue; 230 231 } 232 233 234 237 public String toString() 238 { 239 if (SanityManager.DEBUG) 240 { 241 StringBuffer str = new StringBuffer (200) 242 .append("Checksum Operation ") 243 .append(" algorithm = ") 244 .append(checksumAlgo) 245 .append(" value = ") 246 .append(checksumValue) 247 .append(" data length= ").append(dataLength); 248 249 return str.toString(); 250 } 251 else 252 return null; 253 } 254 } 255 256 257 258 259 260 261 262 263 264 265 266 267 | Popular Tags |