Package org.apache.hadoop.util
Class DataChecksum
java.lang.Object
org.apache.hadoop.util.DataChecksum
- All Implemented Interfaces:
Checksum
@LimitedPrivate({"HDFS","MapReduce"})
@Evolving
public class DataChecksum
extends Object
implements Checksum
This class provides interface and utilities for processing checksums for
DFS data transfers.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Method Summary
Modifier and TypeMethodDescriptionvoidcalculateChunkedSums(byte[] data, int dataOffset, int dataLength, byte[] sums, int sumsOffset) Implementation of chunked calculation specifically on byte arrays.voidcalculateChunkedSums(ByteBuffer data, ByteBuffer checksums) Calculate checksums for the given data.booleancompare(byte[] buf, int offset) Compares the checksum located at buf[offset] with the current checksum.booleanintstatic intintthe size for a checksum.intgetChecksumSize(int dataSize) the required checksum size given the data length.byte[]intlonggetValue()inthashCode()static ChecksumnewCrc32()Create a Crc32 Checksum object.static DataChecksumnewDataChecksum(byte[] bytes, int offset) Creates a DataChecksum from HEADER_LEN bytes from arr[offset].static DataChecksumThis constructs a DataChecksum by reading HEADER_LEN bytes from input stream in.static DataChecksumnewDataChecksum(DataChecksum.Type type, int bytesPerChecksum) voidreset()toString()voidupdate(byte[] b, int off, int len) voidupdate(int b) voidverifyChunkedSums(ByteBuffer data, ByteBuffer checksums, String fileName, long basePos) Verify that the given checksums match the given data.voidWrites the checksum header to the output stream out.intwriteValue(byte[] buf, int offset, boolean reset) Writes the current checksum to a buffer.intwriteValue(DataOutputStream out, boolean reset) Writes the current checksum to the stream.
-
Field Details
-
CHECKSUM_NULL
public static final int CHECKSUM_NULL- See Also:
-
CHECKSUM_CRC32
public static final int CHECKSUM_CRC32- See Also:
-
CHECKSUM_CRC32C
public static final int CHECKSUM_CRC32C- See Also:
-
CHECKSUM_DEFAULT
public static final int CHECKSUM_DEFAULT- See Also:
-
CHECKSUM_MIXED
public static final int CHECKSUM_MIXED- See Also:
-
SIZE_OF_INTEGER
public static final int SIZE_OF_INTEGER- See Also:
-
-
Method Details
-
newCrc32
Create a Crc32 Checksum object. The implementation of the Crc32 algorithm is chosen depending on the platform.- Returns:
- Checksum.
-
newDataChecksum
-
newDataChecksum
public static DataChecksum newDataChecksum(byte[] bytes, int offset) throws InvalidChecksumSizeException Creates a DataChecksum from HEADER_LEN bytes from arr[offset].- Parameters:
bytes- bytes.offset- offset.- Returns:
- DataChecksum of the type in the array or null in case of an error.
- Throws:
InvalidChecksumSizeException- when the stored checksum is invalid.
-
newDataChecksum
This constructs a DataChecksum by reading HEADER_LEN bytes from input stream in.- Parameters:
in- data input stream.- Returns:
- DataChecksum by reading HEADER_LEN bytes from input stream.
- Throws:
IOException- raised on errors performing I/O.
-
writeHeader
Writes the checksum header to the output stream out.- Parameters:
out- output stream.- Throws:
IOException- raised on errors performing I/O.
-
getHeader
public byte[] getHeader() -
writeValue
Writes the current checksum to the stream. If reset is true, then resets the checksum.- Parameters:
out- out.reset- reset.- Returns:
- number of bytes written. Will be equal to getChecksumSize();
- Throws:
IOException- raised on errors performing I/O.
-
writeValue
Writes the current checksum to a buffer. If reset is true, then resets the checksum.- Parameters:
buf- buf.offset- offset.reset- reset.- Returns:
- number of bytes written. Will be equal to getChecksumSize();
- Throws:
IOException- raised on errors performing I/O.
-
compare
public boolean compare(byte[] buf, int offset) Compares the checksum located at buf[offset] with the current checksum.- Parameters:
buf- buf.offset- offset.- Returns:
- true if the checksum matches and false otherwise.
-
getChecksumType
- Returns:
- the checksum algorithm type.
-
getChecksumSize
public int getChecksumSize()the size for a checksum.- Returns:
- the size for a checksum.
-
getChecksumSize
public int getChecksumSize(int dataSize) the required checksum size given the data length.- Parameters:
dataSize- data size.- Returns:
- the required checksum size given the data length.
-
getBytesPerChecksum
public int getBytesPerChecksum() -
getNumBytesInSum
public int getNumBytesInSum() -
getChecksumHeaderSize
public static int getChecksumHeaderSize() -
getValue
public long getValue() -
reset
public void reset() -
update
public void update(byte[] b, int off, int len) -
update
public void update(int b) -
verifyChunkedSums
public void verifyChunkedSums(ByteBuffer data, ByteBuffer checksums, String fileName, long basePos) throws ChecksumException Verify that the given checksums match the given data. The 'mark' of the ByteBuffer parameters may be modified by this function,. but the position is maintained.- Parameters:
data- the DirectByteBuffer pointing to the data to verify.checksums- the DirectByteBuffer pointing to a series of stored checksumsfileName- the name of the file being read, for error-reportingbasePos- the file position to which the start of 'data' corresponds- Throws:
ChecksumException- if the checksums do not match
-
calculateChunkedSums
Calculate checksums for the given data. The 'mark' of the ByteBuffer parameters may be modified by this function, but the position is maintained.- Parameters:
data- the DirectByteBuffer pointing to the data to checksum.checksums- the DirectByteBuffer into which checksums will be stored. Enough space must be available in this buffer to put the checksums.
-
calculateChunkedSums
public void calculateChunkedSums(byte[] data, int dataOffset, int dataLength, byte[] sums, int sumsOffset) Implementation of chunked calculation specifically on byte arrays. This is to avoid the copy when dealing with ByteBuffers that have array backing.- Parameters:
data- data.dataOffset- dataOffset.dataLength- dataLength.sums- sums.sumsOffset- sumsOffset.
-
equals
-
hashCode
public int hashCode() -
toString
-