下面列出了java.util.zip.CRC32#reset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Writes the PNG file header and IHDR meta-information block
*
* @param width The with of the image
* @param height The height of the image
* @param alpha whether to support alpha
* @param outputByteBuffer the buffer to write into
* @param crc the checksum calculator
*/
private static void writeImageHeader(final int width, final int height, final boolean alpha, final ByteBuffer outputByteBuffer, final CRC32 crc) {
// File Signature - "\211PNG\r\n\032\n" - 8950 4e47 0d0a 1a0a
outputByteBuffer.put(new byte[] { (byte) 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a });
// IHDR
outputByteBuffer.putInt(13); // length of IHDR chunk
crc.reset(); // checksum starts after length field
write("IHDR".getBytes(), outputByteBuffer, crc);
write(width, outputByteBuffer, crc); // with 4 bytes
write(height, outputByteBuffer, crc); // height 4 bytes
// Bit depth: 1 byte 1*,2*,4*,8,16° (*:indexed only, °: not indexed)
// Color type: 1 byte 0 (grayscale), 2 (RGB), 3 (Indexed), 4 (grayscale+alpha),
// and 6 (RGBA).
// Compression method: 1 byte 0 (deflate/inflate compression with a 32K sliding
// window)
// Filter method: 1 byte 0 (adaptive filtering with five basic filter types)
// Interlace method: 1 byte 0 (no interlace) or 1 (Adam7 interlace)
write(alpha ? new byte[] { 8, 6, 0, 0, 0 } : new byte[] { 8, 2, 0, 0, 0 }, outputByteBuffer, crc); // RGB(A) Mode
outputByteBuffer.putInt((int) crc.getValue());
}
private long calculateCrc(Path path)
throws IOException
{
TempBuffer tBuf = TempBuffer.create();
byte []buffer = tBuf.buffer();
long result = 0;
try (InputStream is = Files.newInputStream(path)) {
CRC32 crc = new CRC32();
crc.reset();
int sublen = 0;
while ((sublen = is.read(buffer, 0, buffer.length)) > 0) {
crc.update(buffer, 0, sublen);
}
result = crc.getValue();
}
tBuf.free();
return result;
}
private ByteArrayOutputStream zipEntries(List<Pair<String, byte[]>> entryList) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(8192);
try (ZipOutputStream jar = new ZipOutputStream(buffer)) {
jar.setMethod(ZipOutputStream.STORED);
final CRC32 crc = new CRC32();
for (Pair<String, byte[]> entry : entryList) {
byte[] bytes = entry.second;
final ZipEntry newEntry = new ZipEntry(entry.first);
newEntry.setMethod(ZipEntry.STORED); // chose STORED method
crc.reset();
crc.update(entry.second);
newEntry.setCrc(crc.getValue());
newEntry.setSize(bytes.length);
writeEntryToJar(newEntry, bytes, jar);
}
jar.flush();
}
return buffer;
}
/**
* Generate crc32 for any op which only passes a stream name.
*/
public static Long streamOpCRC32(String stream) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
return crc.getValue();
} finally {
crc.reset();
}
}
/**
* Generate crc32 for WriteOp.
*/
public static Long writeOpCRC32(String stream, ByteBuf data) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
crc.update(data.nioBuffer());
return crc.getValue();
} finally {
crc.reset();
}
}
protected byte[] encodeHeader() throws EncryptionEngineException
{
int encPartOffset = getEncryptedHeaderPartOffset();
ByteBuffer bb = ByteBuffer.allocate(getEffectiveHeaderSize() + encPartOffset);
bb.order(ByteOrder.BIG_ENDIAN);
byte[] salt = new byte[SALT_SIZE];
getRandom().nextBytes(salt);
bb.put(salt);
bb.put(getHeaderSignature());
bb.putShort(CURRENT_HEADER_VERSION);
bb.putShort(getMinCompatibleProgramVersion());
byte[] mk = new byte[DATA_KEY_AREA_MAX_SIZE];
System.arraycopy(_masterKey, 0, mk, 0, _masterKey.length);
CRC32 crc = new CRC32();
crc.update(mk);
bb.putInt((int)crc.getValue());
bb.position(bb.position() + 16);
bb.putLong(calcHiddenVolumeSize(_volumeSize));
bb.putLong(_volumeSize);
bb.putLong(_encryptedAreaStart);
bb.putLong(_volumeSize);
//write flags
bb.putInt(0);
bb.putInt(SECTOR_SIZE);
crc.reset();
crc.update(bb.array(),encPartOffset,HEADER_CRC_OFFSET - encPartOffset);
bb.position(HEADER_CRC_OFFSET);
bb.putInt((int)crc.getValue());
bb.position(DATA_AREA_KEY_OFFSET);
bb.put(mk);
Arrays.fill(mk, (byte)0);
return bb.array();
}
void verifyData(Connection conn, int expectedRowCount) throws SQLException {
Statement s = conn.createStatement();
CRC32 checksum = new CRC32(); // holder for the checksum
ResultSet rs = s.executeQuery("SELECT DATA , DATACHECKSUM, ID FROM "
+ "T1" );
int count = 0;
while(rs.next())
{
byte[] dataBytes = rs.getBytes(1);
long ckmRead = rs.getLong(2);
int id = rs.getInt(3);
checksum.reset();
checksum.update(dataBytes, 0, dataBytes.length);
if(checksum.getValue() != ckmRead )
{
logMessage("CHECKSUMs ARE NOT MATCHING");
logMessage("ID=" + id + " Checksum From DB:" + ckmRead);
logMessage("Recalcaulted sum :" + checksum.getValue());
logMessage("Length of Data:" + dataBytes.length);
}
count++;
}
conn.commit();
if(count != expectedRowCount)
{
logMessage("Expected Number Of Rows (" +
expectedRowCount + ")" + "!=" +
"No Of rows in the Table(" +
count + ")");
}
}
public static void main(String[] args) throws Exception {
List<String> files = new ArchiveReader(new File(IMAGE_FILE)).getFiles();
CRC32 crc = new CRC32();
for (String file : files) {
if (file.endsWith(".dff") || file.endsWith(".txd")) {
crc.reset();
crc.update(file.substring(0, file.length() - 4).toUpperCase().getBytes());
System.out.println(file + ": " + (int) ~crc.getValue());
}
}
}
/**
* Generate crc32 for WriteOp.
*/
public static Long writeOpCRC32(String stream, byte[] payload) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
crc.update(payload);
return crc.getValue();
} finally {
crc.reset();
}
}
protected void walPhysArray(DataOutput2 out, long[] physPos, long[] logPos) {
//write byte[] data
int outPos = 0;
int logC = 0;
CRC32 crc32 = new CRC32();
for(int i=0;i<logPos.length;i++){
int c = i==logPos.length-1 ? 0: 8;
final long pos = logPos[i]&LOG_MASK_OFFSET;
int size = (int) (logPos[i]>>>48);
byte header = c==0 ? WAL_PHYS_ARRAY : WAL_PHYS_ARRAY_ONE_LONG;
log.putByte(pos - 8 - 1, header);
log.putLong(pos - 8, physPos[i]);
if(c>0){
log.putLong(pos, physPos[i + 1]);
}
log.putData(pos+c, out.buf, outPos, size - c);
crc32.reset();
crc32.update(out.buf,outPos, size-c);
logC |= LongHashMap.longHash( pos | header | physPos[i] | (c>0?physPos[i+1]:0) | crc32.getValue());
outPos +=size-c;
assert(logSize>=outPos);
}
logChecksumAdd(logC);
assert(outPos==out.pos);
}
@Activate
public void init(Configuration config) throws GeneralSecurityException, IOException {
this.config = config;
// init keystore
keystore = KeyStore.getInstance("JCEKS");
InputStream readStream = new FileInputStream(config.path());
keystore.load(readStream, config.password().toCharArray());
readStream.close();
aliasIds = new HashMap<>();
CRC32 crc = new CRC32();
for (String alias : config.secondaryAliases()) {
crc.update(alias.getBytes(StandardCharsets.UTF_8));
Object prior = aliasIds.put(crc.getValue(), alias);
if (prior != null) {
throw new GeneralSecurityException(
"Two aliases are being used that generate the same CRC-32 hash, please correct");
}
crc.reset();
}
crc.update(config.primaryAlias().getBytes(StandardCharsets.UTF_8));
primaryId = crc.getValue();
if (aliasIds.containsKey(primaryId)) {
throw new GeneralSecurityException(String.format(
"The primary alias %s is either the same as or has the same CRC-32 hash as %s in the secondary aliases, please correct",
config.primaryAlias(), aliasIds.get(primaryId)));
}
aliasIds.put(primaryId, config.primaryAlias());
crc.reset();
}
@Activate
public void init(Configuration config) throws GeneralSecurityException {
this.config = config;
// init keystore
aliasIds = new HashMap<>();
CRC32 crc = new CRC32();
String[] secondaryAlias = config.secondaryAliases();
if (secondaryAlias == null) {
secondaryAlias = new String[] {};
}
for (String alias : secondaryAlias) {
crc.update(alias.getBytes(StandardCharsets.UTF_8));
Object prior = aliasIds.put(crc.getValue() & 0xffff, alias);
if (prior != null) {
throw new GeneralSecurityException(
"Two keys are being used that generate the same CRC-32 hash, please correct");
}
crc.reset();
}
crc.update(config.primaryAlias().getBytes(StandardCharsets.UTF_8));
primaryId = crc.getValue() & 0xffff;
if (aliasIds.containsKey(primaryId)) {
throw new GeneralSecurityException(String.format(
"The primary key %s is either the same as or has the same CRC-32 hash as %s in the secondary keys, please correct",
config.primaryAlias(), aliasIds.get(primaryId)));
}
aliasIds.put(primaryId, config.primaryAlias());
crc.reset();
}
/**
* Generate crc32 for WriteOp.
*/
public static Long writeOpCRC32(String stream, ByteBuffer data) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
crc.update(data);
return crc.getValue();
} finally {
crc.reset();
}
}
/**
* Generate crc32 for TruncateOp.
*/
public static Long truncateOpCRC32(String stream, DLSN dlsn) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
crc.update(dlsn.serializeBytes());
return crc.getValue();
} finally {
crc.reset();
}
}
private void writeEntry(JarOutputStream j, String name,
long mtime, long lsize, boolean deflateHint,
ByteBuffer data0, ByteBuffer data1) throws IOException {
int size = (int)lsize;
if (size != lsize)
throw new IOException("file too large: "+lsize);
CRC32 crc32 = _crc32;
if (_verbose > 1)
Utils.log.fine("Writing entry: "+name+" size="+size
+(deflateHint?" deflated":""));
if (_buf.length < size) {
int newSize = size;
while (newSize < _buf.length) {
newSize <<= 1;
if (newSize <= 0) {
newSize = size;
break;
}
}
_buf = new byte[newSize];
}
assert(_buf.length >= size);
int fillp = 0;
if (data0 != null) {
int size0 = data0.capacity();
data0.get(_buf, fillp, size0);
fillp += size0;
}
if (data1 != null) {
int size1 = data1.capacity();
data1.get(_buf, fillp, size1);
fillp += size1;
}
while (fillp < size) {
// Fill in rest of data from the stream itself.
int nr = in.read(_buf, fillp, size - fillp);
if (nr <= 0) throw new IOException("EOF at end of archive");
fillp += nr;
}
ZipEntry z = new ZipEntry(name);
z.setTime(mtime * 1000);
if (size == 0) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(0);
z.setCrc(0);
z.setCompressedSize(0);
} else if (!deflateHint) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(size);
z.setCompressedSize(size);
crc32.reset();
crc32.update(_buf, 0, size);
z.setCrc(crc32.getValue());
} else {
z.setMethod(Deflater.DEFLATED);
z.setSize(size);
}
j.putNextEntry(z);
if (size > 0)
j.write(_buf, 0, size);
j.closeEntry();
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
private void writeEntry(JarOutputStream j, String name,
long mtime, long lsize, boolean deflateHint,
ByteBuffer data0, ByteBuffer data1) throws IOException {
int size = (int)lsize;
if (size != lsize)
throw new IOException("file too large: "+lsize);
CRC32 crc32 = _crc32;
if (_verbose > 1)
Utils.log.fine("Writing entry: "+name+" size="+size
+(deflateHint?" deflated":""));
if (_buf.length < size) {
int newSize = size;
while (newSize < _buf.length) {
newSize <<= 1;
if (newSize <= 0) {
newSize = size;
break;
}
}
_buf = new byte[newSize];
}
assert(_buf.length >= size);
int fillp = 0;
if (data0 != null) {
int size0 = data0.capacity();
data0.get(_buf, fillp, size0);
fillp += size0;
}
if (data1 != null) {
int size1 = data1.capacity();
data1.get(_buf, fillp, size1);
fillp += size1;
}
while (fillp < size) {
// Fill in rest of data from the stream itself.
int nr = in.read(_buf, fillp, size - fillp);
if (nr <= 0) throw new IOException("EOF at end of archive");
fillp += nr;
}
ZipEntry z = new ZipEntry(name);
z.setTime(mtime * 1000);
if (size == 0) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(0);
z.setCrc(0);
z.setCompressedSize(0);
} else if (!deflateHint) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(size);
z.setCompressedSize(size);
crc32.reset();
crc32.update(_buf, 0, size);
z.setCrc(crc32.getValue());
} else {
z.setMethod(Deflater.DEFLATED);
z.setSize(size);
}
j.putNextEntry(z);
if (size > 0)
j.write(_buf, 0, size);
j.closeEntry();
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
private void writeEntry(JarOutputStream j, String name,
long mtime, long lsize, boolean deflateHint,
ByteBuffer data0, ByteBuffer data1) throws IOException {
int size = (int)lsize;
if (size != lsize)
throw new IOException("file too large: "+lsize);
CRC32 crc32 = _crc32;
if (_verbose > 1)
Utils.log.fine("Writing entry: "+name+" size="+size
+(deflateHint?" deflated":""));
if (_buf.length < size) {
int newSize = size;
while (newSize < _buf.length) {
newSize <<= 1;
if (newSize <= 0) {
newSize = size;
break;
}
}
_buf = new byte[newSize];
}
assert(_buf.length >= size);
int fillp = 0;
if (data0 != null) {
int size0 = data0.capacity();
data0.get(_buf, fillp, size0);
fillp += size0;
}
if (data1 != null) {
int size1 = data1.capacity();
data1.get(_buf, fillp, size1);
fillp += size1;
}
while (fillp < size) {
// Fill in rest of data from the stream itself.
int nr = in.read(_buf, fillp, size - fillp);
if (nr <= 0) throw new IOException("EOF at end of archive");
fillp += nr;
}
ZipEntry z = new ZipEntry(name);
z.setTime(mtime * 1000);
if (size == 0) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(0);
z.setCrc(0);
z.setCompressedSize(0);
} else if (!deflateHint) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(size);
z.setCompressedSize(size);
crc32.reset();
crc32.update(_buf, 0, size);
z.setCrc(crc32.getValue());
} else {
z.setMethod(Deflater.DEFLATED);
z.setSize(size);
}
j.putNextEntry(z);
if (size > 0)
j.write(_buf, 0, size);
j.closeEntry();
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
private void writeEntry(JarOutputStream j, String name,
long mtime, long lsize, boolean deflateHint,
ByteBuffer data0, ByteBuffer data1) throws IOException {
int size = (int)lsize;
if (size != lsize)
throw new IOException("file too large: "+lsize);
CRC32 crc32 = _crc32;
if (_verbose > 1)
Utils.log.fine("Writing entry: "+name+" size="+size
+(deflateHint?" deflated":""));
if (_buf.length < size) {
int newSize = size;
while (newSize < _buf.length) {
newSize <<= 1;
if (newSize <= 0) {
newSize = size;
break;
}
}
_buf = new byte[newSize];
}
assert(_buf.length >= size);
int fillp = 0;
if (data0 != null) {
int size0 = data0.capacity();
data0.get(_buf, fillp, size0);
fillp += size0;
}
if (data1 != null) {
int size1 = data1.capacity();
data1.get(_buf, fillp, size1);
fillp += size1;
}
while (fillp < size) {
// Fill in rest of data from the stream itself.
int nr = in.read(_buf, fillp, size - fillp);
if (nr <= 0) throw new IOException("EOF at end of archive");
fillp += nr;
}
ZipEntry z = new ZipEntry(name);
z.setTime(mtime * 1000);
if (size == 0) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(0);
z.setCrc(0);
z.setCompressedSize(0);
} else if (!deflateHint) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(size);
z.setCompressedSize(size);
crc32.reset();
crc32.update(_buf, 0, size);
z.setCrc(crc32.getValue());
} else {
z.setMethod(Deflater.DEFLATED);
z.setSize(size);
}
j.putNextEntry(z);
if (size > 0)
j.write(_buf, 0, size);
j.closeEntry();
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
private void writeEntry(JarOutputStream j, String name,
long mtime, long lsize, boolean deflateHint,
ByteBuffer data0, ByteBuffer data1) throws IOException {
int size = (int)lsize;
if (size != lsize)
throw new IOException("file too large: "+lsize);
CRC32 crc32 = _crc32;
if (_verbose > 1)
Utils.log.fine("Writing entry: "+name+" size="+size
+(deflateHint?" deflated":""));
if (_buf.length < size) {
int newSize = size;
while (newSize < _buf.length) {
newSize <<= 1;
if (newSize <= 0) {
newSize = size;
break;
}
}
_buf = new byte[newSize];
}
assert(_buf.length >= size);
int fillp = 0;
if (data0 != null) {
int size0 = data0.capacity();
data0.get(_buf, fillp, size0);
fillp += size0;
}
if (data1 != null) {
int size1 = data1.capacity();
data1.get(_buf, fillp, size1);
fillp += size1;
}
while (fillp < size) {
// Fill in rest of data from the stream itself.
int nr = in.read(_buf, fillp, size - fillp);
if (nr <= 0) throw new IOException("EOF at end of archive");
fillp += nr;
}
ZipEntry z = new ZipEntry(name);
z.setTime(mtime * 1000);
if (size == 0) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(0);
z.setCrc(0);
z.setCompressedSize(0);
} else if (!deflateHint) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(size);
z.setCompressedSize(size);
crc32.reset();
crc32.update(_buf, 0, size);
z.setCrc(crc32.getValue());
} else {
z.setMethod(Deflater.DEFLATED);
z.setSize(size);
}
j.putNextEntry(z);
if (size > 0)
j.write(_buf, 0, size);
j.closeEntry();
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
private void writeEntry(JarOutputStream j, String name,
long mtime, long lsize, boolean deflateHint,
ByteBuffer data0, ByteBuffer data1) throws IOException {
int size = (int)lsize;
if (size != lsize)
throw new IOException("file too large: "+lsize);
CRC32 crc32 = _crc32;
if (_verbose > 1)
Utils.log.fine("Writing entry: "+name+" size="+size
+(deflateHint?" deflated":""));
if (_buf.length < size) {
int newSize = size;
while (newSize < _buf.length) {
newSize <<= 1;
if (newSize <= 0) {
newSize = size;
break;
}
}
_buf = new byte[newSize];
}
assert(_buf.length >= size);
int fillp = 0;
if (data0 != null) {
int size0 = data0.capacity();
data0.get(_buf, fillp, size0);
fillp += size0;
}
if (data1 != null) {
int size1 = data1.capacity();
data1.get(_buf, fillp, size1);
fillp += size1;
}
while (fillp < size) {
// Fill in rest of data from the stream itself.
int nr = in.read(_buf, fillp, size - fillp);
if (nr <= 0) throw new IOException("EOF at end of archive");
fillp += nr;
}
ZipEntry z = new ZipEntry(name);
z.setTime(mtime * 1000);
if (size == 0) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(0);
z.setCrc(0);
z.setCompressedSize(0);
} else if (!deflateHint) {
z.setMethod(ZipOutputStream.STORED);
z.setSize(size);
z.setCompressedSize(size);
crc32.reset();
crc32.update(_buf, 0, size);
z.setCrc(crc32.getValue());
} else {
z.setMethod(Deflater.DEFLATED);
z.setSize(size);
}
j.putNextEntry(z);
if (size > 0)
j.write(_buf, 0, size);
j.closeEntry();
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}