本文整理汇总了Java中java.util.zip.CRC32类的典型用法代码示例。如果您正苦于以下问题:Java CRC32类的具体用法?Java CRC32怎么用?Java CRC32使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CRC32类属于java.util.zip包,在下文中一共展示了CRC32类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calCrc32
import java.util.zip.CRC32; //导入依赖的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: validateFileWithChecksum
import java.util.zip.CRC32; //导入依赖的package包/类
private static void validateFileWithChecksum(FileSystem fs, Path filePath, BackupFileInfo backupFileInfo) throws IOException {
final CheckedInputStream cin = new CheckedInputStream(fs.open(filePath), new CRC32());
final BufferedReader reader = new BufferedReader(new InputStreamReader(cin));
final ObjectMapper objectMapper = new ObjectMapper();
String line;
long records = 0;
// parse records just to make sure formatting is correct
while ((line = reader.readLine()) != null) {
objectMapper.readValue(line, BackupRecord.class);
++records;
}
cin.close();
long found = cin.getChecksum().getValue();
if (backupFileInfo.getChecksum() != found) {
throw new IOException(format("Corrupt backup data file %s. Expected checksum %x, found %x", filePath, backupFileInfo.getChecksum(), found));
}
if (backupFileInfo.getRecords() != records) {
throw new IOException(format("Corrupt backup data file %s. Expected records %x, found %x", filePath, backupFileInfo.getRecords(), records));
}
}
示例3: writeJarEntry
import java.util.zip.CRC32; //导入依赖的package包/类
private static void writeJarEntry(JarOutputStream jos, String path, File f) throws IOException, FileNotFoundException {
JarEntry je = new JarEntry(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(f);
try {
copyStreams(is, baos);
} finally {
is.close();
}
byte[] data = baos.toByteArray();
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
示例4: newDataChecksum
import java.util.zip.CRC32; //导入依赖的package包/类
public static DataChecksum newDataChecksum(Type type, int bytesPerChecksum ) {
if ( bytesPerChecksum <= 0 ) {
return null;
}
switch ( type ) {
case NULL :
return new DataChecksum(type, new ChecksumNull(), bytesPerChecksum );
case CRC32 :
return new DataChecksum(type, newCrc32(), bytesPerChecksum );
case CRC32C:
return new DataChecksum(type, new PureJavaCrc32C(), bytesPerChecksum);
default:
return null;
}
}
示例5: getFileCRC
import java.util.zip.CRC32; //导入依赖的package包/类
public static long getFileCRC(File file) throws IOException {
BufferedInputStream bsrc = null;
CRC32 crc = new CRC32();
try {
bsrc = new BufferedInputStream( new FileInputStream( file ) );
byte[] bytes = new byte[1024];
int i;
while( (i = bsrc.read(bytes)) != -1 ) {
crc.update(bytes, 0, i );
}
}
finally {
if ( bsrc != null ) {
bsrc.close();
}
}
return crc.getValue();
}
示例6: crc32
import java.util.zip.CRC32; //导入依赖的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);
}
}
示例7: getApkFileChecksum
import java.util.zip.CRC32; //导入依赖的package包/类
private static long getApkFileChecksum(Context context) {
String apkPath = context.getPackageCodePath();
Long chksum = null;
try {
// Open the file and build a CRC32 checksum.
FileInputStream fis = new FileInputStream(new File(apkPath));
CRC32 chk = new CRC32();
CheckedInputStream cis = new CheckedInputStream(fis, chk);
byte[] buff = new byte[80];
while (cis.read(buff) >= 0) ;
chksum = chk.getValue();
} catch (Exception e) {
e.printStackTrace();
}
return chksum;
}
示例8: getCrc
import java.util.zip.CRC32; //导入依赖的package包/类
private long getCrc(ZipFile file, ZipEntry entry) throws IOException {
long crc = -1;
if (entry != null) {
crc = entry.getCrc();
if (crc < 0) {
CRC32 checksum = new CRC32();
final InputStream in = file.getInputStream(entry);
try {
final byte[] buffer = new byte[1024];
int count;
while ((count = in.read(buffer)) >= 0) {
checksum.update(buffer, 0, count);
}
in.close();
crc = checksum.getValue();
}
finally {
IOUtils.closeQuietly(in);
}
}
}
return crc;
}
示例9: validateAndStripChecksum
import java.util.zip.CRC32; //导入依赖的package包/类
/**
* extracts the checksum of the source, calculates a new one on the cut off source and compares them.
* returns the cut off data only if the checksums matched
*
* @param source
* @return
*/
protected byte[] validateAndStripChecksum(byte[] source) {
// retrieve checksum
int lastIndexOf = Bytes.lastIndexOf(source, (byte) SEPARATOR);
if (lastIndexOf < 0) {
throw new RuntimeException("no checksum was found on source: " + new String(source));
}
long checkSum = Long.parseLong(new String(Arrays.copyOfRange(source, lastIndexOf + 1, source.length)));
byte[] data = Arrays.copyOfRange(source, 0, lastIndexOf);
CRC32 crc32 = new CRC32();
crc32.update(data);
long calculatedCheckSum = crc32.getValue();
// validate checksum
if (Long.compare(checkSum, calculatedCheckSum) != 0) {
throw new RuntimeException("checksums do not match! calculated: " + calculatedCheckSum + ", provided: " + checkSum);
}
return data;
}
示例10: getQNameCrc
import java.util.zip.CRC32; //导入依赖的package包/类
/**
* Find a CRC value for the full QName using UTF-8 conversion.
*
* @param qname the association qname
* @return Returns the CRC value (UTF-8 compatible)
*/
public static Long getQNameCrc(QName qname)
{
CRC32 crc = new CRC32();
try
{
crc.update(qname.getNamespaceURI().getBytes("UTF-8"));
crc.update(qname.getLocalName().getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException("UTF-8 encoding is not supported");
}
return crc.getValue();
}
示例11: file
import java.util.zip.CRC32; //导入依赖的package包/类
public static long file(File f) throws IOException {
FileInputStream fi = new FileInputStream(f);
byte[] buff = new byte[65536];
CRC32 crc32 = new CRC32();
while (true) {
try {
int len = fi.read(buff);
if (len == -1) {
break;
}
crc32.update(buff, 0, len);
} catch (Exception e) {
e.printStackTrace();
} finally {
fi.close();
}
}
return crc32.getValue();
}
示例12: transformerInPlaceTest
import java.util.zip.CRC32; //导入依赖的package包/类
@Test
public void transformerInPlaceTest() throws Exception {
assertTrue(new BytesTransformer.BitSwitchTransformer(0, true).supportInPlaceTransformation());
assertTrue(new BytesTransformer.BitWiseOperatorTransformer(new byte[]{}, BytesTransformer.BitWiseOperatorTransformer.Mode.XOR).supportInPlaceTransformation());
assertTrue(new BytesTransformer.NegateTransformer().supportInPlaceTransformation());
assertTrue(new BytesTransformer.ShiftTransformer(0, BytesTransformer.ShiftTransformer.Type.LEFT_SHIFT).supportInPlaceTransformation());
assertTrue(new BytesTransformer.ReverseTransformer().supportInPlaceTransformation());
assertFalse(new BytesTransformer.MessageDigestTransformer("SHA1").supportInPlaceTransformation());
assertFalse(new BytesTransformer.CopyTransformer(0, 0).supportInPlaceTransformation());
assertFalse(new BytesTransformer.ResizeTransformer(0, BytesTransformer.ResizeTransformer.Mode.RESIZE_KEEP_FROM_MAX_LENGTH).supportInPlaceTransformation());
assertFalse(new BytesTransformer.ConcatTransformer(new byte[]{}).supportInPlaceTransformation());
assertFalse(new BytesTransformers.GzipCompressor(false).supportInPlaceTransformation());
assertFalse(new BytesTransformers.ChecksumTransformer(new CRC32(), ChecksumTransformer.Mode.TRANSFORM, 4).supportInPlaceTransformation());
assertTrue(new BytesTransformers.SortTransformer().supportInPlaceTransformation());
assertFalse(new BytesTransformers.SortTransformer(new Comparator<Byte>() {
@Override
public int compare(Byte o1, Byte o2) {
return 0;
}
}).supportInPlaceTransformation());
assertTrue(new BytesTransformers.ShuffleTransformer(new SecureRandom()).supportInPlaceTransformation());
}
示例13: computeCrc
import java.util.zip.CRC32; //导入依赖的package包/类
/**
* Computes the CRC checksum for the given file.
*
* @param file The file to compute checksum for.
* @return A CRC32 checksum.
* @throws IOException If an I/O error occurs.
*/
private long computeCrc(File file) throws IOException {
CRC32 crc = new CRC32();
InputStream in = new FileInputStream(file);
try {
byte[] buf = new byte[8192];
int n = in.read(buf);
while (n != -1) {
crc.update(buf, 0, n);
n = in.read(buf);
}
} finally {
in.close();
}
return crc.getValue();
}
示例14: readBytesByBufferedCheckedInputStream
import java.util.zip.CRC32; //导入依赖的package包/类
private static void readBytesByBufferedCheckedInputStream(byte[] bytes){
InputStream is = null;
int data = -1;
try {
is = new ByteArrayInputStream(bytes);
// 先用BufferedInputStream这个装饰器对ByteArrayInputStream进行包装
is = new BufferedInputStream(is);
// 再用CheckedInputStream这个装饰器对已经缓冲的流进行再次包装
CheckedInputStream cis = new CheckedInputStream(is, new CRC32());
while (-1 != (data = cis.read())){
System.out.print((char) data);
}
System.out.println(" crc32:" + cis.getChecksum().getValue());
System.out.println("-------------");
} catch (IOException e) {
e.printStackTrace();
}
}
示例15: zipFiles
import java.util.zip.CRC32; //导入依赖的package包/类
/**
* Compressed file or directory
*
* @param srcPath The address of the file or folder
* @param zipFilePath The address of the compressed package
*/
public static void zipFiles(String srcPath,String zipFilePath) {
File file = new File(srcPath);
if (!file.exists())
throw new RuntimeException(srcPath + "not exist!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFilePath);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String baseDir="";
zip(file,out,baseDir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}