本文整理汇总了Java中java.util.zip.Checksum类的典型用法代码示例。如果您正苦于以下问题:Java Checksum类的具体用法?Java Checksum怎么用?Java Checksum使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Checksum类属于java.util.zip包,在下文中一共展示了Checksum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calCrc32
import java.util.zip.Checksum; //导入依赖的package包/类
public static byte[] calCrc32(byte[] data) {
Checksum checksum = new CRC32();
// update the current checksum with the specified array of bytes
checksum.update(data, 0, data.length);
// get the current checksum value
long checksumValue = checksum.getValue();
String hex = Long.toHexString(checksumValue).toUpperCase();
while (hex.length() < 8) {
hex = "0" + hex;
}
byte[] crc32 = Hex.decode(hex);
inverseBytes(crc32);
reverseArray(crc32);
return crc32;
}
示例2: computeCrc32
import java.util.zip.Checksum; //导入依赖的package包/类
/**
* Compute the CRC-32 of the contents of a stream.
* \r\n and \r are both normalized to \n for purposes of the calculation.
*/
static String computeCrc32(InputStream is) throws IOException {
Checksum crc = new CRC32();
int last = -1;
int curr;
while ((curr = is.read()) != -1) {
if (curr != '\n' && last == '\r') {
crc.update('\n');
}
if (curr != '\r') {
crc.update(curr);
}
last = curr;
}
if (last == '\r') {
crc.update('\n');
}
int val = (int)crc.getValue();
String hex = Integer.toHexString(val);
while (hex.length() < 8) {
hex = "0" + hex; // NOI18N
}
return hex;
}
示例3: computeCrc32
import java.util.zip.Checksum; //导入依赖的package包/类
private static String computeCrc32(final FileObject fo) throws IOException {
final Checksum crc = new CRC32();
try (final InputStream in = new BufferedInputStream(fo.getInputStream())) {
int last = -1;
int curr;
while ((curr = in.read()) != -1) {
if (curr != '\n' && last == '\r') { //NOI18N
crc.update('\n'); //NOI18N
}
if (curr != '\r') { //NOI18N
crc.update(curr);
}
last = curr;
}
if (last == '\r') { //NOI18N
crc.update('\n'); //NOI18N
}
}
int val = (int)crc.getValue();
String hex = Integer.toHexString(val);
while (hex.length() < 8) {
hex = "0" + hex; // NOI18N
}
return hex;
}
示例4: crc32
import java.util.zip.Checksum; //导入依赖的package包/类
/**
* Generates a CRC 32 Value of String passed
*
* @param data
* @return long crc32 value of input. -1 if string is null
* @throws RuntimeException if UTF-8 is not a supported character set
*/
public static long crc32(String data) {
if (data == null) {
return -1;
}
try {
// get bytes from string
byte bytes[] = data.getBytes("UTF-8");
Checksum checksum = new CRC32();
// update the current checksum with the specified array of bytes
checksum.update(bytes, 0, bytes.length);
// get the current checksum value
return checksum.getValue();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
示例5: doBench
import java.util.zip.Checksum; //导入依赖的package包/类
private static void doBench(final List<Class<? extends Checksum>> crcs,
final PrintStream out) throws Exception {
final byte[] bytes = new byte[MAX_LEN];
new Random().nextBytes(bytes);
// Print header
out.printf("\nPerformance Table (The unit is MB/sec; #T = #Theads)\n");
// Warm up implementations to get jit going.
for (Class<? extends Checksum> c : crcs) {
doBench(c, 1, bytes, 2);
doBench(c, 1, bytes, 2101);
}
// Test on a variety of sizes with different number of threads
for (int size = 32; size <= MAX_LEN; size <<= 1) {
doBench(crcs, bytes, size, out);
}
}
示例6: ZipArchiveOutputStream
import java.util.zip.Checksum; //导入依赖的package包/类
public ZipArchiveOutputStream(OutputStream out,
Checksum cksum, ZipEntry e) {
super(out, cksum);
if (out == null) {
throw new NullPointerException("out == null");
}
if (cksum == null) {
throw new NullPointerException("cksum == null");
}
if (e == null) {
throw new NullPointerException("e == null");
}
entry = e;
}
示例7: createIndexFile
import java.util.zip.Checksum; //导入依赖的package包/类
private static void createIndexFile(File indexFile, Configuration conf)
throws IOException {
if (indexFile.exists()) {
System.out.println("Deleting existing file");
indexFile.delete();
}
indexFile.createNewFile();
FSDataOutputStream output = FileSystem.getLocal(conf).getRaw().append(
new Path(indexFile.getAbsolutePath()));
Checksum crc = new PureJavaCrc32();
crc.reset();
CheckedOutputStream chk = new CheckedOutputStream(output, crc);
String msg = "Writing new index file. This file will be used only " +
"for the testing.";
chk.write(Arrays.copyOf(msg.getBytes(),
MapTask.MAP_OUTPUT_INDEX_RECORD_LENGTH));
output.writeLong(chk.getChecksum().getValue());
output.close();
}
示例8: testUpdate
import java.util.zip.Checksum; //导入依赖的package包/类
@Test
public void testUpdate() {
final byte[] bytes = "Any String you want".getBytes();
final int len = bytes.length;
Checksum crc1 = Crc32C.create();
Checksum crc2 = Crc32C.create();
Checksum crc3 = Crc32C.create();
crc1.update(bytes, 0, len);
for (int i = 0; i < len; i++)
crc2.update(bytes[i]);
crc3.update(bytes, 0, len / 2);
crc3.update(bytes, len / 2, len - len / 2);
assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
assertEquals("Crc values should be the same", crc1.getValue(), crc3.getValue());
}
示例9: updateChecksumInt
import java.util.zip.Checksum; //导入依赖的package包/类
public static void updateChecksumInt(Checksum checksum, int v)
{
checksum.update((v >>> 24) & 0xFF);
checksum.update((v >>> 16) & 0xFF);
checksum.update((v >>> 8) & 0xFF);
checksum.update((v >>> 0) & 0xFF);
}
示例10: newInstance
import java.util.zip.Checksum; //导入依赖的package包/类
/**
* Create a new instance of a Checksum object.
* @return The newly created Checksum object
*/
static public Checksum newInstance(String className) throws IOException {
try {
Class<?> clazz = getClassByName(className);
return (Checksum)newInstance(clazz);
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
示例11: testLittleEndianReadonlyByteBufferOffset
import java.util.zip.Checksum; //导入依赖的package包/类
private static void testLittleEndianReadonlyByteBufferOffset(Checksum checksum, long expected) {
byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
checksum.reset();
System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789).asReadOnlyBuffer();
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.position(i);
bb.limit(i + BYTES_123456789.length);
checksum.update(bb);
checkChecksumOffset(checksum, expected, i);
}
}
示例12: throwChecksumException
import java.util.zip.Checksum; //导入依赖的package包/类
private static void throwChecksumException(Type type, Checksum algorithm,
String filename, long errPos, int expected, int computed)
throws ChecksumException {
throw new ChecksumException("Checksum " + type
+ " not matched for file " + filename + " at position "+ errPos
+ String.format(": expected=%X but computed=%X", expected, computed)
+ ", algorithm=" + algorithm.getClass().getSimpleName(), errPos);
}
示例13: verifyChunked
import java.util.zip.Checksum; //导入依赖的package包/类
@Override
public void verifyChunked(ByteBuffer data, int bytesPerCrc,
ByteBuffer crcs, String filename, long basePos)
throws ChecksumException {
final Checksum algorithm = newAlgorithm();
if (data.hasArray() && crcs.hasArray()) {
DataChecksum.verifyChunked(DataChecksum.Type.CRC32, algorithm,
data.array(), data.position(), data.remaining(), bytesPerCrc,
crcs.array(), crcs.position(), filename, basePos);
} else {
DataChecksum.verifyChunked(DataChecksum.Type.CRC32, algorithm,
data, bytesPerCrc, crcs, filename, basePos);
}
}
示例14: crc32
import java.util.zip.Checksum; //导入依赖的package包/类
public Encoder crc32() {
Checksum checksum = new CRC32();
checksum.update(data, 0, data.length);
long checksumValue = checksum.getValue();
data = Long.toHexString(checksumValue).getBytes();
return unhex();
}
示例15: SpillRecord
import java.util.zip.Checksum; //导入依赖的package包/类
public SpillRecord(Path indexFileName, JobConf job, Checksum crc,
String expectedIndexOwner)
throws IOException {
final FileSystem rfs = FileSystem.getLocal(job).getRaw();
final FSDataInputStream in =
SecureIOUtils.openFSDataInputStream(new File(indexFileName.toUri()
.getRawPath()), expectedIndexOwner, null);
try {
final long length = rfs.getFileStatus(indexFileName).getLen();
final int partitions = (int) length / MAP_OUTPUT_INDEX_RECORD_LENGTH;
final int size = partitions * MAP_OUTPUT_INDEX_RECORD_LENGTH;
buf = ByteBuffer.allocate(size);
if (crc != null) {
crc.reset();
CheckedInputStream chk = new CheckedInputStream(in, crc);
IOUtils.readFully(chk, buf.array(), 0, size);
if (chk.getChecksum().getValue() != in.readLong()) {
throw new ChecksumException("Checksum error reading spill index: " +
indexFileName, -1);
}
} else {
IOUtils.readFully(in, buf.array(), 0, size);
}
entries = buf.asLongBuffer();
} finally {
in.close();
}
}