use of java.util.zip.Checksum in project JGroups by belaban.
the class Encrypt method computeChecksum.
protected long computeChecksum(byte[] input, int offset, int length) {
Checksum checksummer = createChecksummer();
checksummer.update(input, offset, length);
return checksummer.getValue();
}
use of java.util.zip.Checksum in project rt.equinox.framework by eclipse.
the class ReliableFile method getInputStream.
/**
* Returns an InputStream object for reading the target file.
*
* @param generation the maximum generation to evaluate
* @param openMask mask used to open data.
* are invalid (corrupt, missing, etc).
* @return An InputStream object which can be used to read the target file.
* @throws IOException If an error occurs preparing the file.
*/
InputStream getInputStream(int generation, int openMask) throws IOException {
if (inputFile != null) {
// $NON-NLS-1$
throw new IOException("Input stream already open");
}
int[] generations = getFileGenerations(referenceFile);
if (generations == null) {
// $NON-NLS-1$
throw new FileNotFoundException("File not found");
}
String name = referenceFile.getName();
File parent = new File(referenceFile.getParent());
boolean failOnPrimary = (openMask & OPEN_FAIL_ON_PRIMARY) != 0;
if (failOnPrimary && generation == GENERATIONS_INFINITE)
generation = generations[0];
File textFile = null;
InputStream textIS = null;
for (int idx = 0; idx < generations.length; idx++) {
if (generation != 0) {
if (generations[idx] > generation || (failOnPrimary && generations[idx] != generation))
continue;
}
File file;
if (generations[idx] != 0)
file = new File(parent, name + '.' + generations[idx]);
else
file = referenceFile;
InputStream is = null;
CacheInfo info;
synchronized (cacheFiles) {
info = cacheFiles.get(file);
long timeStamp = file.lastModified();
if (info == null || timeStamp != info.timeStamp) {
InputStream tempIS = new FileInputStream(file);
try {
long fileSize = file.length();
if (fileSize < maxInputStreamBuffer) {
tempIS = new BufferedInputStream(tempIS, (int) fileSize);
// reuse the tempIS since it supports mark/reset
is = tempIS;
}
Checksum cksum = getChecksumCalculator();
int filetype = getStreamType(tempIS, cksum, fileSize);
info = new CacheInfo(filetype, cksum, timeStamp, fileSize);
cacheFiles.put(file, info);
} catch (IOException e) {
/*ignore*/
} finally {
if (is == null) {
// close the tempIS since it was simply used to get the check sum
try {
tempIS.close();
} catch (IOException e) {
/*ignore*/
}
}
}
}
}
// and return the result.
if (failOnPrimary) {
if (info != null && info.filetype == FILETYPE_VALID) {
inputFile = file;
if (is != null)
return is;
return new FileInputStream(file);
}
// $NON-NLS-1$
throw new IOException("ReliableFile is corrupt");
}
// if error, ignore this file & try next
if (info == null)
continue;
// we're not looking for a specific version, so let's pick the best case
switch(info.filetype) {
case FILETYPE_VALID:
inputFile = file;
if (is != null)
return is;
return new FileInputStream(file);
case FILETYPE_NOSIGNATURE:
if (textFile == null) {
textFile = file;
textIS = is;
}
break;
}
}
// use it instead
if (textFile != null) {
inputFile = textFile;
if (textIS != null)
return textIS;
return new FileInputStream(textFile);
}
// $NON-NLS-1$
throw new IOException("ReliableFile is corrupt");
}
use of java.util.zip.Checksum in project graal by oracle.
the class CRC32CSubstitutionsTest method updateBytes.
public static long updateBytes(byte[] input, int offset, int end) throws Throwable {
Class<?> crcClass = Class.forName("java.util.zip.CRC32C");
MethodHandle newMH = MethodHandles.publicLookup().findConstructor(crcClass, MethodType.methodType(void.class));
Checksum crc = (Checksum) newMH.invoke();
crc.update(input, offset, end);
return crc.getValue();
}
use of java.util.zip.Checksum in project graal by oracle.
the class CRC32CSubstitutionsTest method updateByteBuffer.
public static long updateByteBuffer(ByteBuffer buffer) throws Throwable {
Class<?> crcClass = Class.forName("java.util.zip.CRC32C");
MethodHandle newMH = MethodHandles.publicLookup().findConstructor(crcClass, MethodType.methodType(void.class));
MethodHandle updateMH = MethodHandles.publicLookup().findVirtual(crcClass, "update", MethodType.methodType(void.class, ByteBuffer.class));
Checksum crc = (Checksum) newMH.invoke();
buffer.rewind();
// Checksum.update(ByteBuffer) is also available since 9
updateMH.invokeExact(crc, buffer);
return crc.getValue();
}
use of java.util.zip.Checksum in project stdlib by petergeneric.
the class CRC32Digester method digest.
@Override
public String digest(InputStream is) throws IOException {
Checksum crc = newChecksum();
byte[] buffer = new byte[4096];
int readSize = 0;
while (readSize >= 0) {
readSize = is.read(buffer);
if (readSize >= 0) {
crc.update(buffer, 0, readSize);
}
}
return Long.toHexString(crc.getValue());
}
Aggregations