本文整理汇总了Java中java.util.zip.CRC32.update方法的典型用法代码示例。如果您正苦于以下问题:Java CRC32.update方法的具体用法?Java CRC32.update怎么用?Java CRC32.update使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.CRC32
的用法示例。
在下文中一共展示了CRC32.update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeCrcOfCentralDir
import java.util.zip.CRC32; //导入方法依赖的package包/类
static long computeCrcOfCentralDir(RandomAccessFile raf, CentralDirectory dir)
throws IOException {
CRC32 crc = new CRC32();
long stillToRead = dir.size;
raf.seek(dir.offset);
int length = (int) Math.min(BUFFER_SIZE, stillToRead);
byte[] buffer = new byte[BUFFER_SIZE];
length = raf.read(buffer, 0, length);
while (length != -1) {
crc.update(buffer, 0, length);
stillToRead -= length;
if (stillToRead == 0) {
break;
}
length = (int) Math.min(BUFFER_SIZE, stillToRead);
length = raf.read(buffer, 0, length);
}
return crc.getValue();
}
示例2: 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();
}
示例3: CRC32
import java.util.zip.CRC32; //导入方法依赖的package包/类
public static String CRC32(byte[] bArr) throws Throwable {
CRC32 crc32 = new CRC32();
crc32.update(bArr);
long value = crc32.getValue();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 56))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 48))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 40))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 32))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 24))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 16))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
(value >>> 8))) & 255)}));
stringBuilder.append(String.format("%02x", new Object[]{Integer.valueOf(((byte) ((int)
value)) & 255)}));
while (stringBuilder.charAt(0) == '0') {
stringBuilder = stringBuilder.deleteCharAt(0);
}
return stringBuilder.toString().toLowerCase();
}
示例4: writeString
import java.util.zip.CRC32; //导入方法依赖的package包/类
private void writeString(ByteBuffer buffer, CRC32 crc32, String string) throws UnsupportedEncodingException
{
char[] chars = string.toCharArray();
byte[] bytes = new byte[chars.length];
for (int i = 0; i < chars.length; i++)
{
if (chars[i] > 0xFF)
{
throw new UnsupportedEncodingException();
}
bytes[i] = (byte) chars[i];
}
buffer.putInt(bytes.length);
buffer.put(bytes);
crc32.update(bytes);
}
示例5: main
import java.util.zip.CRC32; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (args.length == 0) {
// Dump and use shared archive with different flag combinations
dumpAndUseSharedArchive("+", "-");
dumpAndUseSharedArchive("-", "+");
} else {
// Call intrinsified java.lang.Math::fma()
Math.fma(1.0, 2.0, 3.0);
byte[] buffer = new byte[256];
// Call intrinsified java.util.zip.CRC32::update()
CRC32 crc32 = new CRC32();
crc32.update(buffer, 0, 256);
// Call intrinsified java.util.zip.CRC32C::updateBytes(..)
CRC32C crc32c = new CRC32C();
crc32c.update(buffer, 0, 256);
}
}
示例6: CRCCheck
import java.util.zip.CRC32; //导入方法依赖的package包/类
/**
* Check if the CRC of the snapshot file matches the digest.
* @param f The snapshot file object
* @return The table list as a string
* @throws IOException If CRC does not match
*/
public static String CRCCheck(File f) throws IOException {
final FileInputStream fis = new FileInputStream(f);
try {
final BufferedInputStream bis = new BufferedInputStream(fis);
ByteBuffer crcBuffer = ByteBuffer.allocate(4);
if (4 != bis.read(crcBuffer.array())) {
throw new EOFException("EOF while attempting to read CRC from snapshot digest");
}
final int crc = crcBuffer.getInt();
final InputStreamReader isr = new InputStreamReader(bis, "UTF-8");
CharArrayWriter caw = new CharArrayWriter();
while (true) {
int nextChar = isr.read();
if (nextChar == -1) {
throw new EOFException("EOF while reading snapshot digest");
}
if (nextChar == '\n') {
break;
}
caw.write(nextChar);
}
String tableList = caw.toString();
byte tableListBytes[] = tableList.getBytes("UTF-8");
CRC32 tableListCRC = new CRC32();
tableListCRC.update(tableListBytes);
tableListCRC.update("\n".getBytes("UTF-8"));
final int calculatedValue = (int)tableListCRC.getValue();
if (crc != calculatedValue) {
throw new IOException("CRC of snapshot digest did not match digest contents");
}
return tableList;
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {}
}
}
示例7: ZioEntry
import java.util.zip.CRC32; //导入方法依赖的package包/类
public ZioEntry( String name, String sourceDataFile)
throws IOException
{
zipInput = new ZipInput( sourceDataFile);
filename = name;
fileComment = "";
this.compression = 0;
this.size = (int)zipInput.getFileLength();
this.compressedSize = this.size;
if (getLogger().isDebugEnabled())
getLogger().debug(String.format("Computing CRC for %s, size=%d",sourceDataFile,size));
// compute CRC
CRC32 crc = new CRC32();
byte[] buffer = new byte[8096];
int numRead = 0;
while (numRead != size) {
int count = zipInput.read( buffer, 0, Math.min( buffer.length, (this.size - numRead)));
if (count > 0) {
crc.update( buffer, 0, count);
numRead += count;
}
}
this.crc32 = (int)crc.getValue();
zipInput.seek(0);
this.dataPosition = 0;
extraData = new byte[0];
setTime( new File(sourceDataFile).lastModified());
}
示例8: computeCRC
import java.util.zip.CRC32; //导入方法依赖的package包/类
long computeCRC(long minlength) {
CRC32 crc = new CRC32();
byte[] buffer = new byte[BUFFER_LEN];
long count = getCount(minlength);
for (long i = 0; i < count; i++) {
crc.update(buffer);
}
return crc.getValue();
}
示例9: crc32AsInt
import java.util.zip.CRC32; //导入方法依赖的package包/类
/**
* 对输入字符串进行crc32散列返回int, 返回值有可能是负数.
*
* Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
*/
public static int crc32AsInt(@NotNull byte[] input) {
CRC32 crc32 = new CRC32();
crc32.update(input);
// CRC32 只是 32bit int,为了CheckSum接口强转成long,此处再次转回来
return (int) crc32.getValue();
}
示例10: deflate
import java.util.zip.CRC32; //导入方法依赖的package包/类
public static DeflateResult deflate(ByteBuffer input) {
byte[] inputBuf;
int inputOffset;
int inputLength = input.remaining();
if (input.hasArray()) {
inputBuf = input.array();
inputOffset = input.arrayOffset() + input.position();
input.position(input.limit());
} else {
inputBuf = new byte[inputLength];
inputOffset = 0;
input.get(inputBuf);
}
CRC32 crc32 = new CRC32();
crc32.update(inputBuf, inputOffset, inputLength);
long crc32Value = crc32.getValue();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Deflater deflater = new Deflater(9, true);
deflater.setInput(inputBuf, inputOffset, inputLength);
deflater.finish();
byte[] buf = new byte[65536];
while (!deflater.finished()) {
int chunkSize = deflater.deflate(buf);
out.write(buf, 0, chunkSize);
}
return new DeflateResult(inputLength, crc32Value, out.toByteArray());
}
示例11: calc
import java.util.zip.CRC32; //导入方法依赖的package包/类
public static long calc(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
int blockSize = 64 * 1024;
byte[] buffer = new byte[blockSize];
int read = 0;
CRC32 crc32 = new CRC32();
while ((read = fis.read(buffer, 0, buffer.length)) > 0) {
crc32.update(buffer, 0, read);
}
return crc32.getValue();
}
示例12: testCrc32Checksum
import java.util.zip.CRC32; //导入方法依赖的package包/类
@Test
public void testCrc32Checksum() throws IOException {
CRC32 crc32 = new CRC32();
crc32.update(TEST_DATA.getBytes());
long expectedCRC32Checksum = crc32.getValue();
Crc32ChecksumCalculatingInputStream crc32InputStream =
new Crc32ChecksumCalculatingInputStream(new ByteArrayInputStream(TEST_DATA.getBytes()));
while (crc32InputStream.read() != -1) {
;
}
assertEquals(expectedCRC32Checksum, crc32InputStream.getCrc32Checksum());
}
示例13: crcForJar
import java.util.zip.CRC32; //导入方法依赖的package包/类
static public long crcForJar(String jarpath) throws IOException {
InputStream fin = openJarFile(jarpath);
if (fin == null)
throw new FileNotFoundException();
CRC32 crc = new CRC32();
int b = -1;
while ((b = fin.read()) != -1)
crc.update(b);
return crc.getValue();
}
示例14: readString
import java.util.zip.CRC32; //导入方法依赖的package包/类
private String readString(ByteBuffer buffer, CRC32 crc32) throws UnsupportedEncodingException
{
int size = buffer.getInt();
byte[] bytes = new byte[size];
buffer.get(bytes);
char[] chars = new char[size];
for (int i = 0; i < size; i++)
{
chars[i] = (char) bytes[i];
}
crc32.update(bytes);
return new String(chars);
}
示例15: crc32
import java.util.zip.CRC32; //导入方法依赖的package包/类
public static int crc32(ByteBuffer buf) {
CRC32 checksum = new CRC32();
while (buf.hasRemaining()) {
checksum.update(buf.get());
}
return (int) checksum.getValue();
}