-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from vintmd/crc32c
feature: 支持 crc32c 校验
- Loading branch information
Showing
8 changed files
with
199 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.apache.hadoop.fs; | ||
|
||
import org.apache.hadoop.io.WritableUtils; | ||
import org.apache.hadoop.util.StringUtils; | ||
|
||
import java.io.DataInput; | ||
import java.io.DataOutput; | ||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
|
||
/** | ||
* An etag as a checksum. | ||
* Consider these suitable for checking if an object has changed, but | ||
* not suitable for comparing two different objects for equivalence, | ||
* especially between hadoop compatible filesystem. | ||
*/ | ||
public class CRC32CCheckSum extends FileChecksum { | ||
private static final String ALGORITHM_NAME = "COMPOSITE-CRC32C"; | ||
|
||
private int crc32c = 0; | ||
|
||
public CRC32CCheckSum() { | ||
} | ||
|
||
|
||
public CRC32CCheckSum(String crc32cecma) { | ||
try { | ||
BigInteger bigInteger = new BigInteger(crc32cecma); | ||
this.crc32c = bigInteger.intValue(); | ||
} catch (NumberFormatException e) { | ||
this.crc32c = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public String getAlgorithmName() { | ||
return CRC32CCheckSum.ALGORITHM_NAME; | ||
} | ||
|
||
@Override | ||
public int getLength() { | ||
return Integer.SIZE / Byte.SIZE; | ||
} | ||
|
||
@Override | ||
public byte[] getBytes() { | ||
return CrcUtil.intToBytes(crc32c); | ||
} | ||
|
||
@Override | ||
public void write(DataOutput dataOutput) throws IOException { | ||
dataOutput.writeInt(this.crc32c); | ||
} | ||
|
||
@Override | ||
public void readFields(DataInput dataInput) throws IOException { | ||
this.crc32c = dataInput.readInt(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getAlgorithmName() + ":" + String.format("0x%08x", crc32c); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.apache.hadoop.fs; | ||
|
||
import java.io.IOException; | ||
|
||
public class CrcUtil { | ||
private CrcUtil() { | ||
} | ||
|
||
/** | ||
* @return 4-byte array holding the big-endian representation of | ||
* {@code value}. | ||
*/ | ||
public static byte[] intToBytes(int value) { | ||
byte[] buf = new byte[4]; | ||
try { | ||
writeInt(buf, 0, value); | ||
} catch (IOException ioe) { | ||
// Since this should only be able to occur from code bugs within this | ||
// class rather than user input, we throw as a RuntimeException | ||
// rather than requiring this method to declare throwing IOException | ||
// for something the caller can't control. | ||
throw new RuntimeException(ioe); | ||
} | ||
return buf; | ||
} | ||
|
||
/** | ||
* Writes big-endian representation of {@code value} into {@code buf} | ||
* starting at {@code offset}. buf.length must be greater than or | ||
* equal to offset + 4. | ||
*/ | ||
public static void writeInt(byte[] buf, int offset, int value) | ||
throws IOException { | ||
if (offset + 4 > buf.length) { | ||
throw new IOException(String.format( | ||
"writeInt out of bounds: buf.length=%d, offset=%d", | ||
buf.length, offset)); | ||
} | ||
buf[offset + 0] = (byte) ((value >>> 24) & 0xff); | ||
buf[offset + 1] = (byte) ((value >>> 16) & 0xff); | ||
buf[offset + 2] = (byte) ((value >>> 8) & 0xff); | ||
buf[offset + 3] = (byte) (value & 0xff); | ||
} | ||
|
||
} |
Oops, something went wrong.