Java Examples for java.util.zip.CRC32
The following java examples will help you to understand the usage of java.util.zip.CRC32. These source code samples are taken from different open source projects.
Example 1
| Project: common-java-cookbook-master File: ChecksumStream.java View source code |
public static void main(String[] args) throws Exception {
InputStream test = ChecksumStream.class.getResourceAsStream("test.data");
byte[] byteArray = ByteStreams.toByteArray(test);
CRC32 crc32 = new CRC32();
long checksum = ByteStreams.getChecksum(ByteStreams.newInputStreamSupplier(byteArray), crc32);
System.out.printf("Checksum: %d", checksum);
}Example 2
| Project: mdrill-master File: RecordCount.java View source code |
public void setCrcRecord(String newparentGroup) {
int len = newparentGroup.length();
CRC32 crc32 = new CRC32();
crc32.update(new String(newparentGroup).getBytes());
int crcvalue = (int) crc32.getValue();
int hashCode = newparentGroup.hashCode();
ArrayList<Integer> key = new ArrayList<Integer>(3);
key.add(crcvalue);
key.add(len);
key.add(hashCode);
this.adduniq(key);
}Example 3
| Project: android-sdk-sources-for-api-level-23-master File: CRC32Test.java View source code |
/**
* java.util.zip.CRC32#getValue()
*/
public void test_getValue() {
// test methods of java.util.zip.crc32.getValue()
CRC32 crc = new CRC32();
assertEquals("getValue() should return a zero as a result of constructing a CRC32 instance", 0, crc.getValue());
crc.reset();
crc.update(Integer.MAX_VALUE);
// System.out.print("value of crc " + crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 4278190080
assertEquals("update(max) failed to update the checksum to the correct value ", 4278190080L, crc.getValue());
crc.reset();
byte byteEmpty[] = new byte[10000];
crc.update(byteEmpty);
// System.out.print("value of crc"+crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 1295764014
assertEquals("update(byte[]) failed to update the checksum to the correct value ", 1295764014L, crc.getValue());
crc.reset();
crc.update(1);
// System.out.print("value of crc"+crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 2768625435
// assertEquals("update(int) failed to update the checksum to the correct
// value ",2768625435L, crc.getValue());
crc.reset();
assertEquals("reset failed to reset the checksum value to zero", 0, crc.getValue());
}Example 4
| Project: android_libcore-master File: CRC32Test.java View source code |
/**
* @tests java.util.zip.CRC32#getValue()
*/
@TestTargetNew(level = TestLevel.COMPLETE, notes = "", method = "getValue", args = {})
public void test_getValue() {
// test methods of java.util.zip.crc32.getValue()
CRC32 crc = new CRC32();
assertEquals("getValue() should return a zero as a result of constructing a CRC32 instance", 0, crc.getValue());
crc.reset();
crc.update(Integer.MAX_VALUE);
// System.out.print("value of crc " + crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 4278190080
assertEquals("update(max) failed to update the checksum to the correct value ", 4278190080L, crc.getValue());
crc.reset();
byte byteEmpty[] = new byte[10000];
crc.update(byteEmpty);
// System.out.print("value of crc"+crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 1295764014
assertEquals("update(byte[]) failed to update the checksum to the correct value ", 1295764014L, crc.getValue());
crc.reset();
crc.update(1);
// System.out.print("value of crc"+crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 2768625435
// assertEquals("update(int) failed to update the checksum to the
// correct
// value ",2768625435L, crc.getValue());
crc.reset();
assertEquals("reset failed to reset the checksum value to zero", 0, crc.getValue());
}Example 5
| Project: ARTPart-master File: OldAndroidChecksumTest.java View source code |
private void cRC32Test(byte[] values, long expected) {
CRC32 crc = new CRC32();
// try it all at once
crc.update(values);
assertEquals(crc.getValue(), expected);
// try resetting and computing one byte at a time
crc.reset();
for (int i = 0; i < values.length; i++) {
crc.update(values[i]);
}
assertEquals(crc.getValue(), expected);
}Example 6
| Project: j2objc-master File: OldAndroidChecksumTest.java View source code |
private void cRC32Test(byte[] values, long expected) {
CRC32 crc = new CRC32();
// try it all at once
crc.update(values);
assertEquals(crc.getValue(), expected);
// try resetting and computing one byte at a time
crc.reset();
for (int i = 0; i < values.length; i++) {
crc.update(values[i]);
}
assertEquals(crc.getValue(), expected);
}Example 7
| Project: open-mika-master File: CRC32Test.java View source code |
/**
* @tests java.util.zip.CRC32#getValue()
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "getValue",
args = {}
)
*/
public void test_getValue() {
// test methods of java.util.zip.crc32.getValue()
CRC32 crc = new CRC32();
assertEquals("getValue() should return a zero as a result of constructing a CRC32 instance", 0, crc.getValue());
crc.reset();
crc.update(Integer.MAX_VALUE);
// System.out.print("value of crc " + crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 4278190080
assertEquals("update(max) failed to update the checksum to the correct value ", 4278190080L, crc.getValue());
crc.reset();
byte byteEmpty[] = new byte[10000];
crc.update(byteEmpty);
// System.out.print("value of crc"+crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 1295764014
assertEquals("update(byte[]) failed to update the checksum to the correct value ", 1295764014L, crc.getValue());
crc.reset();
crc.update(1);
// System.out.print("value of crc"+crc.getValue());
// Ran JDK and discovered that the value of the CRC should be
// 2768625435
// assertEquals("update(int) failed to update the checksum to the
// correct
// value ",2768625435L, crc.getValue());
crc.reset();
assertEquals("reset failed to reset the checksum value to zero", 0, crc.getValue());
}Example 8
| Project: openjdk-master File: LargeJarEntry.java View source code |
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i = 0; i < 8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i = 0; i < 8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb", jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}Example 9
| Project: elasticsearch-knapsack-master File: IndexHash.java View source code |
public void validate(InputStream in) throws IOException {
// Index Indicator (0x00) has already been read by BlockInputStream
// so add 0x00 to the CRC32 here.
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
crc32.update('\0');
CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
// Get and validate the Number of Records field.
long storedRecordCount = DecoderUtil.decodeVLI(inChecked);
if (storedRecordCount != recordCount) {
throw new CorruptedInputException("XZ Index is corrupt");
}
// Decode and hash the Index field and compare it to
// the hash value calculated from the decoded Blocks.
IndexHash stored = new IndexHash();
for (long i = 0; i < recordCount; ++i) {
long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
try {
stored.add(unpaddedSize, uncompressedSize);
} catch (XZIOException e) {
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum > blocksSum || stored.uncompressedSum > uncompressedSum || stored.indexListSize > indexListSize) {
throw new CorruptedInputException("XZ Index is corrupt");
}
}
if (stored.blocksSum != blocksSum || stored.uncompressedSum != uncompressedSum || stored.indexListSize != indexListSize || !Arrays.equals(stored.hash.finish(), hash.finish())) {
throw new CorruptedInputException("XZ Index is corrupt");
}
// Index Padding
DataInputStream inData = new DataInputStream(inChecked);
for (int i = getIndexPaddingSize(); i > 0; --i) {
if (inData.readUnsignedByte() != 0x00) {
throw new CorruptedInputException("XZ Index is corrupt");
}
}
// CRC32
long value = crc32.getValue();
for (int i = 0; i < 4; ++i) {
if (((value >>> (i * 8)) & 0xFF) != inData.readUnsignedByte()) {
throw new CorruptedInputException("XZ Index is corrupt");
}
}
}Example 10
| Project: Evening-IDE-master File: IndexHash.java View source code |
public void validate(InputStream in) throws IOException {
// Index Indicator (0x00) has already been read by BlockInputStream
// so add 0x00 to the CRC32 here.
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
crc32.update('\0');
CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
// Get and validate the Number of Records field.
long storedRecordCount = DecoderUtil.decodeVLI(inChecked);
if (storedRecordCount != recordCount)
throw new CorruptedInputException("XZ Index is corrupt");
// Decode and hash the Index field and compare it to
// the hash value calculated from the decoded Blocks.
IndexHash stored = new IndexHash();
for (long i = 0; i < recordCount; ++i) {
long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
try {
stored.add(unpaddedSize, uncompressedSize);
} catch (XZIOException e) {
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum > blocksSum || stored.uncompressedSum > uncompressedSum || stored.indexListSize > indexListSize)
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum != blocksSum || stored.uncompressedSum != uncompressedSum || stored.indexListSize != indexListSize || !Arrays.equals(stored.hash.finish(), hash.finish()))
throw new CorruptedInputException("XZ Index is corrupt");
// Index Padding
DataInputStream inData = new DataInputStream(inChecked);
for (int i = getIndexPaddingSize(); i > 0; --i) if (inData.readUnsignedByte() != 0x00)
throw new CorruptedInputException("XZ Index is corrupt");
// CRC32
long value = crc32.getValue();
for (int i = 0; i < 4; ++i) if (((value >>> (i * 8)) & 0xFF) != inData.readUnsignedByte())
throw new CorruptedInputException("XZ Index is corrupt");
}Example 11
| Project: LocalGSMLocationProvider-master File: IndexHash.java View source code |
public void validate(InputStream in) throws IOException {
// Index Indicator (0x00) has already been read by BlockInputStream
// so add 0x00 to the CRC32 here.
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
crc32.update('\0');
CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
// Get and validate the Number of Records field.
long storedRecordCount = DecoderUtil.decodeVLI(inChecked);
if (storedRecordCount != recordCount)
throw new CorruptedInputException("XZ Index is corrupt");
// Decode and hash the Index field and compare it to
// the hash value calculated from the decoded Blocks.
IndexHash stored = new IndexHash();
for (long i = 0; i < recordCount; ++i) {
long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
try {
stored.add(unpaddedSize, uncompressedSize);
} catch (XZIOException e) {
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum > blocksSum || stored.uncompressedSum > uncompressedSum || stored.indexListSize > indexListSize)
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum != blocksSum || stored.uncompressedSum != uncompressedSum || stored.indexListSize != indexListSize || !Arrays.equals(stored.hash.finish(), hash.finish()))
throw new CorruptedInputException("XZ Index is corrupt");
// Index Padding
DataInputStream inData = new DataInputStream(inChecked);
for (int i = getIndexPaddingSize(); i > 0; --i) if (inData.readUnsignedByte() != 0x00)
throw new CorruptedInputException("XZ Index is corrupt");
// CRC32
long value = crc32.getValue();
for (int i = 0; i < 4; ++i) if (((value >>> (i * 8)) & 0xFF) != inData.readUnsignedByte())
throw new CorruptedInputException("XZ Index is corrupt");
}Example 12
| Project: package-drone-master File: IndexHash.java View source code |
public void validate(InputStream in) throws IOException {
// Index Indicator (0x00) has already been read by BlockInputStream
// so add 0x00 to the CRC32 here.
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
crc32.update('\0');
CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
// Get and validate the Number of Records field.
long storedRecordCount = DecoderUtil.decodeVLI(inChecked);
if (storedRecordCount != recordCount)
throw new CorruptedInputException("XZ Index is corrupt");
// Decode and hash the Index field and compare it to
// the hash value calculated from the decoded Blocks.
IndexHash stored = new IndexHash();
for (long i = 0; i < recordCount; ++i) {
long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
try {
stored.add(unpaddedSize, uncompressedSize);
} catch (XZIOException e) {
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum > blocksSum || stored.uncompressedSum > uncompressedSum || stored.indexListSize > indexListSize)
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum != blocksSum || stored.uncompressedSum != uncompressedSum || stored.indexListSize != indexListSize || !Arrays.equals(stored.hash.finish(), hash.finish()))
throw new CorruptedInputException("XZ Index is corrupt");
// Index Padding
DataInputStream inData = new DataInputStream(inChecked);
for (int i = getIndexPaddingSize(); i > 0; --i) if (inData.readUnsignedByte() != 0x00)
throw new CorruptedInputException("XZ Index is corrupt");
// CRC32
long value = crc32.getValue();
for (int i = 0; i < 4; ++i) if (((value >>> (i * 8)) & 0xFF) != inData.readUnsignedByte())
throw new CorruptedInputException("XZ Index is corrupt");
}Example 13
| Project: BeeDeeDee-master File: IntegrityCheckUniqueTable.java View source code |
private int nodeChecksum(int id) {
ByteBuffer byteBuffer = ByteBuffer.allocate((getNodeSize() - 1) * 4);
byteBuffer.putInt(ut[id * getNodeSize() + VAR_OFFSET]);
byteBuffer.putInt(ut[id * getNodeSize() + LOW_OFFSET]);
byteBuffer.putInt(ut[id * getNodeSize() + HIGH_OFFSET]);
byteBuffer.putInt(ut[id * getNodeSize() + NEXT_OFFSET]);
byteBuffer.putInt(ut[id * getNodeSize() + HASHCODEAUX_OFFSET]);
byte[] byteArray = byteBuffer.array();
Checksum checksum = new CRC32();
checksum.update(byteArray, 0, byteArray.length);
return (int) checksum.getValue();
}Example 14
| Project: complexion-master File: Resource.java View source code |
/// Compute the CRC32 of the resource file specified by filename public static long getCRC32(String filename) { // Attempt to read the file from disk try { RandomAccessFile file = new RandomAccessFile(filename, "r"); // Convert the file to a buffer byte[] buffer = new byte[(int) file.length()]; file.read(buffer); // Generate the CRC from the buffer CRC32 crc = new CRC32(); crc.update(buffer); file.close(); // Return the generated CRC return crc.getValue(); } catch (IOException e) { return 0; } }
Example 15
| Project: OpenCDC-master File: CRC32Test.java View source code |
public static void main(String[] args) throws Exception {
// byte[] bs = new byte[]{(byte)0x8D,0x56,(byte)0xB8,0x53,(byte)0x1E,0x01,0x00,0x00,0x00,0x2D,0x00,0x00,0x00,0x19,0x01,0x00,0x00,0x00,0x00,0x46,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x02,(byte)0xFF,(byte)0xFC,0x20,0x00,0x00,0x00,0x04,0x74,0x65,0x73,0x74 };
//
// CRC32 crc = new CRC32();
// crc.update(bs);
// long crc32 = crc.getValue();
// System.out.println(Long.toHexString(crc32));
// 0x75,0x3F,(byte)0xF6,(byte)0xB1
// Socket socket = null;
// socket.setReceiveBufferSize(1000);
// socket.setSendBufferSize(1000);
int a = 63487, b = 32895;
System.out.println(a | b);
System.out.println(a & b);
System.out.println(Integer.toHexString(a));
System.out.println(Integer.toHexString(b));
System.out.println(Integer.toHexString(a - b));
}Example 16
| Project: vraptor4-master File: ZipDownload.java View source code |
@Override
public void write(HttpServletResponse response) throws IOException {
response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setHeader("Content-type", "application/zip");
CheckedOutputStream stream = new CheckedOutputStream(response.getOutputStream(), new CRC32());
try (ZipOutputStream zip = new ZipOutputStream(stream)) {
for (Path file : files) {
zip.putNextEntry(new ZipEntry(file.getFileName().toString()));
copy(file, zip);
zip.closeEntry();
}
}
}Example 17
| Project: wro4j-master File: CRC32HashStrategy.java View source code |
/**
* {@inheritDoc}
*/
public String getHash(final InputStream input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("Content cannot be null!");
}
try {
LOG.debug("creating hash using CRC32 algorithm");
final Checksum checksum = new CRC32();
final byte[] bytes = new byte[1024];
int len = 0;
while ((len = input.read(bytes)) >= 0) {
checksum.update(bytes, 0, len);
}
final String hash = new BigInteger(Long.toString(checksum.getValue())).toString(16);
LOG.debug("CRC32 hash: {}", hash);
return hash;
} finally {
IOUtils.closeQuietly(input);
}
}Example 18
| Project: albatross-master File: AlbatrossUtils.java View source code |
public static String crc32(byte[] bytes) {
java.util.zip.CRC32 x = new java.util.zip.CRC32();
x.update(bytes);
//java CRC is reversed (byte order) for what TS needs. SO....
String crcString = Long.toHexString(x.getValue());
//pad with leading 0s
if (crcString.length() % 2 != 0) {
crcString = "0" + crcString;
}
//reverse in pairs so bytes are in opposite order
String reverseCrcString = crcString.substring(6, 8) + crcString.substring(4, 6) + crcString.substring(2, 4) + crcString.substring(0, 2);
return reverseCrcString;
}Example 19
| Project: Amigo-master File: CrcUtils.java View source code |
/* Package visible for testing */
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();
}Example 20
| Project: android-sdk-master File: Crc32.java View source code |
/**
* 对文件内容计算crc32æ ¡éªŒç ?
*
* @param f 需è¦?计算crc32æ ¡éªŒç ?的文件
* @return crcæ ¡éªŒç ?
* @throws IOException 读�文件异常
*/
public static long file(File f) throws IOException {
FileInputStream fi = new FileInputStream(f);
byte[] buff = new byte[64 * 1024];
int len;
CRC32 crc32 = new CRC32();
try {
while ((len = fi.read(buff)) != -1) {
crc32.update(buff, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fi.close();
}
return crc32.getValue();
}Example 21
| Project: Asper-master File: ContextControllerHashedGetterCRC32Serialized.java View source code |
public Object get(EventBean eventBean) throws PropertyAccessException {
EventBean[] events = new EventBean[] { eventBean };
Object[] parameters = new Object[evaluators.length];
for (int i = 0; i < serializers.length; i++) {
parameters[i] = evaluators[i].evaluate(events, true, null);
}
byte[] bytes;
try {
bytes = SerializerFactory.serialize(serializers, parameters);
} catch (IOException e) {
log.error("Exception serializing parameters for computing consistent hash for statement '" + statementName + "': " + e.getMessage(), e);
bytes = new byte[0];
}
CRC32 crc = new CRC32();
crc.update(bytes);
long value = crc.getValue() % granularity;
int result = (int) value;
if (result >= 0) {
return result;
}
return -result;
}Example 22
| Project: aws-java-sdk-master File: CRC32ChecksumInputStreamTest.java View source code |
@Test
public void testCRC32Checksum() throws IOException {
CRC32 crc32 = new CRC32();
crc32.update(TEST_DARA.getBytes());
long expectedCRC32Checksum = crc32.getValue();
CRC32ChecksumCalculatingInputStream crc32InputStream = new CRC32ChecksumCalculatingInputStream(new ByteArrayInputStream(TEST_DARA.getBytes()));
while (crc32InputStream.read() != -1) ;
assertEquals(expectedCRC32Checksum, crc32InputStream.getCRC32Checksum());
}Example 23
| Project: aws-sdk-android-master File: CRC32ChecksumInputStreamTest.java View source code |
@Test
public void testCRC32Checksum() throws IOException {
CRC32 crc32 = new CRC32();
crc32.update(TEST_DARA.getBytes(StringUtils.UTF8));
long expectedCRC32Checksum = crc32.getValue();
CRC32ChecksumCalculatingInputStream crc32InputStream = new CRC32ChecksumCalculatingInputStream(new ByteArrayInputStream(TEST_DARA.getBytes(StringUtils.UTF8)));
while (crc32InputStream.read() != -1) ;
assertEquals(expectedCRC32Checksum, crc32InputStream.getCRC32Checksum());
}Example 24
| Project: aws-sdk-java-master File: CRC32ChecksumInputStreamTest.java View source code |
@Test
public void testCRC32Checksum() throws IOException {
CRC32 crc32 = new CRC32();
crc32.update(TEST_DARA.getBytes());
long expectedCRC32Checksum = crc32.getValue();
CRC32ChecksumCalculatingInputStream crc32InputStream = new CRC32ChecksumCalculatingInputStream(new ByteArrayInputStream(TEST_DARA.getBytes()));
while (crc32InputStream.read() != -1) ;
assertEquals(expectedCRC32Checksum, crc32InputStream.getCRC32Checksum());
}Example 25
| Project: bazel-master File: ZipOutputFileProvider.java View source code |
private static void writeStoredEntry(ZipOutputStream out, String filename, byte[] content) throws IOException {
// Need to pre-compute checksum for STORED (uncompressed) entries)
CRC32 checksum = new CRC32();
checksum.update(content);
ZipEntry result = new ZipEntry(filename);
// Use stable timestamp Jan 1 1980
result.setTime(0L);
result.setCrc(checksum.getValue());
result.setSize(content.length);
result.setCompressedSize(content.length);
// Write uncompressed, since this is just an intermediary artifact that
// we will convert to .dex
result.setMethod(ZipEntry.STORED);
out.putNextEntry(result);
out.write(content);
out.closeEntry();
}Example 26
| Project: bspsrc-master File: BspChecksum.java View source code |
public long getMapCRC() throws IOException {
CRC32 crc = new CRC32();
// CRC across all lumps except for the Entities lump
for (Lump lump : bspFile.getLumps()) {
if (lump.getType() == LumpType.LUMP_ENTITIES) {
continue;
}
try (InputStream in = new CheckedInputStream(lump.getInputStream(), crc)) {
// copy to /dev/null, we need the checksum only
IOUtils.copy(in, new NullOutputStream());
}
}
return crc.getValue();
}Example 27
| Project: btpka3.github.com-master File: Zip.java View source code |
public static void main(String[] args) throws IOException {
String zipFile = "/tmp/big.zip";
new File(zipFile).delete();
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
ZipEntry entry = new ZipEntry("test.txt");
zos.putNextEntry(entry);
WritableByteChannel channel = Channels.newChannel(zos);
ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
int COUNT = 12;
// COUNT = 300* 1024 * 1024;
int c = 0;
while (c < COUNT) {
int fillCount = COUNT - c >= buf.capacity() ? buf.capacity() : COUNT - c;
fillBuf(buf, fillCount);
buf.flip();
channel.write(buf);
buf.compact();
c += fillCount;
}
channel.close();
System.out.printf("Done. see : " + zipFile);
}Example 28
| Project: Correct-master File: ZipOutputFileProvider.java View source code |
private static void writeStoredEntry(ZipOutputStream out, String filename, byte[] content) throws IOException {
// Need to pre-compute checksum for STORED (uncompressed) entries)
CRC32 checksum = new CRC32();
checksum.update(content);
ZipEntry result = new ZipEntry(filename);
// Use stable timestamp Jan 1 1980
result.setTime(0L);
result.setCrc(checksum.getValue());
result.setSize(content.length);
result.setCompressedSize(content.length);
// Write uncompressed, since this is just an intermediary artifact that
// we will convert to .dex
result.setMethod(ZipEntry.STORED);
out.putNextEntry(result);
out.write(content);
out.closeEntry();
}Example 29
| Project: cxf-master File: BinaryDataService.java View source code |
/**
* @param data
* @param length
* @param crc32
* @return
*/
private String getStatusForData(byte[] data, int length, long crc32) {
String status;
if (data.length != length) {
status = "data.length == " + data.length + ", should be " + length;
} else {
CRC32 computedCrc32 = new CRC32();
computedCrc32.update(data);
if (computedCrc32.getValue() != crc32) {
status = "Computed crc32 == " + computedCrc32.getValue() + ", should be " + crc32;
} else {
status = "OK";
}
}
return status;
}Example 30
| Project: CyclesMod-master File: DefaultBikesTest.java View source code |
@Test
public void test() throws IOException {
final InputStream is = new DefaultBikes().getInputStream();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
IOUtils.copy(is, os, 128);
} finally {
IOUtils.closeQuietly(os, is);
}
Assert.assertEquals(BikesInf.FILE_SIZE, os.size());
final CRC32 crc = new CRC32();
crc.update(os.toByteArray());
Assert.assertEquals(DefaultBikes.CRC, crc.getValue());
crc.reset();
crc.update(new DefaultBikes().getByteArray());
Assert.assertEquals(DefaultBikes.CRC, crc.getValue());
}Example 31
| Project: dss-master File: AbstractGetDataToSignASiCS.java View source code |
/* In case of multi-files and ASiC-S, we need to create a zip with all files to be signed */
protected DSSDocument createPackageZip(List<DSSDocument> documents, Date signingDate) {
ByteArrayOutputStream baos = null;
ZipOutputStream zos = null;
try {
baos = new ByteArrayOutputStream();
zos = new ZipOutputStream(baos);
for (DSSDocument document : documents) {
final String documentName = document.getName();
final String name = documentName != null ? documentName : ZIP_ENTRY_DETACHED_FILE;
final ZipEntry entryDocument = new ZipEntry(name);
entryDocument.setTime(signingDate.getTime());
entryDocument.setMethod(ZipEntry.STORED);
byte[] byteArray = DSSUtils.toByteArray(document);
entryDocument.setSize(byteArray.length);
entryDocument.setCompressedSize(byteArray.length);
final CRC32 crc = new CRC32();
crc.update(byteArray);
entryDocument.setCrc(crc.getValue());
zos.putNextEntry(entryDocument);
Utils.write(byteArray, zos);
}
} catch (IOException e) {
throw new DSSException("Unable to create package.zip file", e);
} finally {
Utils.closeQuietly(zos);
Utils.closeQuietly(baos);
}
return new InMemoryDocument(baos.toByteArray(), "package.zip");
}Example 32
| Project: esper-master File: ContextControllerHashedGetterCRC32Serialized.java View source code |
public Object get(EventBean eventBean) throws PropertyAccessException {
EventBean[] events = new EventBean[] { eventBean };
Object[] parameters = new Object[evaluators.length];
for (int i = 0; i < serializers.length; i++) {
parameters[i] = evaluators[i].evaluate(events, true, null);
}
byte[] bytes;
try {
bytes = SerializerFactory.serialize(serializers, parameters);
} catch (IOException e) {
log.error("Exception serializing parameters for computing consistent hash for statement '" + statementName + "': " + e.getMessage(), e);
bytes = new byte[0];
}
CRC32 crc = new CRC32();
crc.update(bytes);
long value = crc.getValue() % granularity;
int result = (int) value;
if (result >= 0) {
return result;
}
return -result;
}Example 33
| Project: jfastnet-master File: ChecksumFeature.java View source code |
public boolean check(Message message) {
long crcValue = this.crcValue;
// Set CRC value to 0 so we can correctly compare both checksum'd messages
this.crcValue = 0L;
Config config = message.getConfig();
message.payload = config.serialiser.serialise(message);
CRC32 crc32 = config.serialiser.getChecksum(message, salt);
log.trace("Sent CRC: {}, Calculated CRC: {}", crcValue, crc32.getValue());
this.crcValue = crcValue;
return crcValue == crc32.getValue();
}Example 34
| Project: jsilver-master File: CrcFunction.java View source code |
/**
* @param args 1 string expression
* @return CRC-32 of string as number value
*/
public Value execute(Value... args) {
String string = args[0].asString();
// This function produces a 'standard' CRC-32 (IV -1, reflected polynomial,
// and final complement step). The CRC-32 of "123456789" is 0xCBF43926.
Checksum crc = new CRC32();
byte[] b;
try {
b = string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 must be supported");
}
crc.update(b, 0, b.length);
// CRC fits into 32 bits by definition.
return literalConstant((int) crc.getValue(), args[0]);
}Example 35
| Project: lucene-solr-master File: TestBufferedChecksum.java View source code |
public void testRandom() {
Checksum c1 = new CRC32();
Checksum c2 = new BufferedChecksum(new CRC32());
int iterations = atLeast(10000);
for (int i = 0; i < iterations; i++) {
switch(random().nextInt(4)) {
case 0:
// update(byte[], int, int)
int length = random().nextInt(1024);
byte bytes[] = new byte[length];
random().nextBytes(bytes);
c1.update(bytes, 0, bytes.length);
c2.update(bytes, 0, bytes.length);
break;
case 1:
// update(int)
int b = random().nextInt(256);
c1.update(b);
c2.update(b);
break;
case 2:
// reset()
c1.reset();
c2.reset();
break;
case 3:
// getValue()
assertEquals(c1.getValue(), c2.getValue());
break;
}
}
assertEquals(c1.getValue(), c2.getValue());
}Example 36
| Project: lzmajio-master File: LzmaCompatTest.java View source code |
@Test
public void decode() throws IOException {
System.out.printf("%s:", this);
FileInputStream fis = new FileInputStream(file);
LzmaInputStream lis = new LzmaInputStream(fis);
CRC32 sum = new CRC32();
CheckedInputStream cis = new CheckedInputStream(lis, sum);
byte[] buf = new byte[4096];
int k, n = 0;
while (-1 != (k = cis.read(buf))) {
n += k;
}
cis.close();
long val = sum.getValue();
System.out.printf("%d bytes, sum %x\n", n, val);
assertTrue(file.getName().startsWith(Long.toHexString(val)));
}Example 37
| Project: MoparScape-master File: CacheVerifier.java View source code |
public static void main(String[] args) throws IOException {
try (FileStore store = FileStore.open("../game/data/cache/")) {
for (int type = 0; type < store.getFileCount(255); type++) {
ReferenceTable table = ReferenceTable.decode(Container.decode(store.read(255, type)).getData());
for (int file = 0; file < table.capacity(); file++) {
Entry entry = table.getEntry(file);
if (entry == null)
continue;
ByteBuffer buffer;
try {
buffer = store.read(type, file);
} catch (IOException ex) {
System.out.println(type + ":" + file + " error");
continue;
}
if (buffer.capacity() <= 2) {
System.out.println(type + ":" + file + " missing");
continue;
}
// last two bytes are the version and shouldn't be included
byte[] bytes = new byte[buffer.limit() - 2];
buffer.position(0);
buffer.get(bytes, 0, bytes.length);
CRC32 crc = new CRC32();
crc.update(bytes, 0, bytes.length);
if ((int) crc.getValue() != entry.getCrc()) {
System.out.println(type + ":" + file + " corrupt");
}
buffer.position(buffer.limit() - 2);
if ((buffer.getShort() & 0xFFFF) != entry.getVersion()) {
System.out.println(type + ":" + file + " out of date");
}
}
}
}
}Example 38
| Project: oodt-master File: XMLTest.java View source code |
/** Test the {@link XML#createDOMParser} and {@link XML#serialize} methods.
*/
public void testDOM() throws Exception {
DOMParser p = XML.createDOMParser();
p.parse(inputSource);
Document doc = p.getDocument();
doc.normalize();
assertEquals("log", doc.getDocumentElement().getTagName());
java.util.zip.CRC32 crc = new java.util.zip.CRC32();
String result = XML.serialize(doc);
crc.update(result.getBytes());
long value = crc.getValue();
assertTrue("Stringified DOM document CRC mismatch, got value = " + value, 3880488030L == value || 2435419114L == value || /* added by Chris Mattmann: pretty print fix */
3688328384L == value || /* other newline treatment */
750262163L == value || 3738296466L == value || /* Apache incubator warmed up the file, so it suffered thermal expansion */
1102069581L == value);
}Example 39
| Project: test-master File: ZipOutputFileProvider.java View source code |
private static void writeStoredEntry(ZipOutputStream out, String filename, byte[] content) throws IOException {
// Need to pre-compute checksum for STORED (uncompressed) entries)
CRC32 checksum = new CRC32();
checksum.update(content);
ZipEntry result = new ZipEntry(filename);
// Use stable timestamp Jan 1 1980
result.setTime(0L);
result.setCrc(checksum.getValue());
result.setSize(content.length);
result.setCompressedSize(content.length);
// Write uncompressed, since this is just an intermediary artifact that
// we will convert to .dex
result.setMethod(ZipEntry.STORED);
out.putNextEntry(result);
out.write(content);
out.closeEntry();
}Example 40
| Project: WInS_Demos-master File: CRC32Util.java View source code |
public static String getCRC32Checksum(String pString) {
long decimalChecksum = 0;
String hexChecksum = null;
final String ZEROS = "00000000";
final int CRC32_CHECKSUM_LENGTH = 8;
CRC32 checksumEngine = new CRC32();
checksumEngine.update(pString.getBytes());
decimalChecksum = checksumEngine.getValue();
hexChecksum = Long.toHexString(decimalChecksum).toUpperCase();
hexChecksum = ZEROS.substring(0, CRC32_CHECKSUM_LENGTH - hexChecksum.length()) + hexChecksum;
return hexChecksum;
}Example 41
| Project: jaulp.core-master File: ChecksumUtils.java View source code |
/** * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance * of <code>java.util.zip.Adler32</code>. * * @param file * The file The file from what to get the checksum. * @param crc * The crc If the flag crc is true than the CheckedInputStream is constructed with an * instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an * instance of * @return The checksum from the given file as long. * @throws FileNotFoundException * Is thrown if the file is not found. * @throws IOException * Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object * otherwise it is constructed with an instance of {@link java.util.zip.Adler32} * object. {@link java.util.zip.Adler32} object. */ public static long getChecksum(final File file, final boolean crc) throws FileNotFoundException, IOException { CheckedInputStream cis = null; if (crc) { cis = new CheckedInputStream(new FileInputStream(file), new CRC32()); } else { cis = new CheckedInputStream(new FileInputStream(file), new Adler32()); } final int length = (int) file.length(); final byte[] buffer = new byte[length]; long checksum = 0; while (cis.read(buffer) >= 0) { checksum = cis.getChecksum().getValue(); } checksum = cis.getChecksum().getValue(); StreamUtils.closeInputStream(cis); return checksum; }
Example 42
| Project: hbase-cache-master File: ChecksumType.java View source code |
@Override
public void initialize() {
final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
final String JDKCRC = "java.util.zip.CRC32";
LOG = LogFactory.getLog(ChecksumType.class);
// check if hadoop library is available
try {
ctor = ChecksumFactory.newConstructor(PURECRC32);
LOG.info("Checksum using " + PURECRC32);
} catch (Exception e) {
LOG.trace(PURECRC32 + " not available.");
}
try {
// This is available on all JVMs.
if (ctor == null) {
ctor = ChecksumFactory.newConstructor(JDKCRC);
LOG.info("Checksum can use " + JDKCRC);
}
} catch (Exception e) {
LOG.trace(JDKCRC + " not available.");
}
}Example 43
| Project: HBase-Research-master File: ChecksumType.java View source code |
@Override
public void initialize() {
final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
final String JDKCRC = "java.util.zip.CRC32";
LOG = LogFactory.getLog(ChecksumType.class);
// check if hadoop library is available
try {
ctor = ChecksumFactory.newConstructor(PURECRC32);
LOG.info("Checksum using " + PURECRC32);
} catch (Exception e) {
LOG.trace(PURECRC32 + " not available.");
}
try {
// This is available on all JVMs.
if (ctor == null) {
ctor = ChecksumFactory.newConstructor(JDKCRC);
LOG.info("Checksum can use " + JDKCRC);
}
} catch (Exception e) {
LOG.trace(JDKCRC + " not available.");
}
}Example 44
| Project: hbase-trunk-mttr-master File: ChecksumType.java View source code |
@Override
public void initialize() {
final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
final String JDKCRC = "java.util.zip.CRC32";
LOG = LogFactory.getLog(ChecksumType.class);
// check if hadoop library is available
try {
ctor = ChecksumFactory.newConstructor(PURECRC32);
LOG.info("Checksum using " + PURECRC32);
} catch (Exception e) {
LOG.trace(PURECRC32 + " not available.");
}
try {
// This is available on all JVMs.
if (ctor == null) {
ctor = ChecksumFactory.newConstructor(JDKCRC);
LOG.info("Checksum can use " + JDKCRC);
}
} catch (Exception e) {
LOG.trace(JDKCRC + " not available.");
}
}Example 45
| Project: hindex-master File: ChecksumType.java View source code |
@Override
public void initialize() {
final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
final String JDKCRC = "java.util.zip.CRC32";
LOG = LogFactory.getLog(ChecksumType.class);
// check if hadoop library is available
try {
ctor = ChecksumFactory.newConstructor(PURECRC32);
LOG.info("Checksum using " + PURECRC32);
} catch (Exception e) {
LOG.trace(PURECRC32 + " not available.");
}
try {
// This is available on all JVMs.
if (ctor == null) {
ctor = ChecksumFactory.newConstructor(JDKCRC);
LOG.info("Checksum can use " + JDKCRC);
}
} catch (Exception e) {
LOG.trace(JDKCRC + " not available.");
}
}Example 46
| Project: pbase-master File: ChecksumType.java View source code |
@Override
public void initialize() {
final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
final String JDKCRC = "java.util.zip.CRC32";
LOG = LogFactory.getLog(ChecksumType.class);
// check if hadoop library is available
try {
ctor = ChecksumFactory.newConstructor(PURECRC32);
LOG.debug(PURECRC32 + " available");
} catch (Exception e) {
LOG.trace(PURECRC32 + " not available.");
}
try {
// This is available on all JVMs.
if (ctor == null) {
ctor = ChecksumFactory.newConstructor(JDKCRC);
LOG.debug(JDKCRC + " available");
}
} catch (Exception e) {
LOG.trace(JDKCRC + " not available.");
}
}Example 47
| Project: AllArkhamPlugins-master File: CRCUtil.java View source code |
/**
* Calculates CRC of a file
*
* @param inputFile - file for which crc has to be calculated
* @return crc of the file
* @throws ZipException
*/
public static long computeFileCRC(String inputFile, ProgressMonitor progressMonitor) throws ZipException {
if (!Zip4jUtil.isStringNotNullAndNotEmpty(inputFile)) {
throw new ZipException("input file is null or empty, cannot calculate CRC for the file");
}
InputStream inputStream = null;
try {
Zip4jUtil.checkFileReadAccess(inputFile);
inputStream = new FileInputStream(new File(inputFile));
byte[] buff = new byte[BUF_SIZE];
int readLen = -2;
CRC32 crc32 = new CRC32();
while ((readLen = inputStream.read(buff)) != -1) {
crc32.update(buff, 0, readLen);
if (progressMonitor != null) {
progressMonitor.updateWorkCompleted(readLen);
if (progressMonitor.isCancelAllTasks()) {
progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED);
progressMonitor.setState(ProgressMonitor.STATE_READY);
return 0;
}
}
}
return crc32.getValue();
} catch (IOException e) {
throw new ZipException(e);
} catch (Exception e) {
throw new ZipException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new ZipException("error while closing the file after calculating crc");
}
}
}
}Example 48
| Project: android-ePub-Library-master File: CRCUtil.java View source code |
/**
* Calculates CRC of a file
* @param inputFile - file for which crc has to be calculated
* @return crc of the file
* @throws ZipException
*/
public static long computeFileCRC(String inputFile, ProgressMonitor progressMonitor) throws ZipException {
if (!Zip4jUtil.isStringNotNullAndNotEmpty(inputFile)) {
throw new ZipException("input file is null or empty, cannot calculate CRC for the file");
}
InputStream inputStream = null;
try {
Zip4jUtil.checkFileReadAccess(inputFile);
inputStream = new FileInputStream(new File(inputFile));
byte[] buff = new byte[BUF_SIZE];
int readLen = -2;
CRC32 crc32 = new CRC32();
while ((readLen = inputStream.read(buff)) != -1) {
crc32.update(buff, 0, readLen);
if (progressMonitor != null) {
progressMonitor.updateWorkCompleted(readLen);
if (progressMonitor.isCancelAllTasks()) {
progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED);
progressMonitor.setState(ProgressMonitor.STATE_READY);
return 0;
}
}
}
return crc32.getValue();
} catch (IOException e) {
throw new ZipException(e);
} catch (Exception e) {
throw new ZipException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new ZipException("error while closing the file after calculating crc");
}
}
}
}Example 49
| Project: android-vts-master File: ZipBug9950697.java View source code |
public boolean isVulnerable(Context context) throws Exception {
ByteArrayOutputStream fileNameFunz = new ByteArrayOutputStream();
ModdedZipArchiveOutputStream zaos = new ModdedZipArchiveOutputStream(fileNameFunz);
ZipArchiveEntry zae = new ZipArchiveEntry("test_file");
byte[] originalData = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes();
byte[] modifiedData = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB".getBytes();
zae.setMethod(ZipEntry.STORED);
zae.setSize(originalData.length);
CRC32 checker = new CRC32();
checker.update(originalData);
zae.setCrc(checker.getValue());
zaos.putArchiveEntry(zae, originalData);
zaos.write(originalData);
zaos.closeArchiveEntry();
zaos.writeRaw(modifiedData, 0, modifiedData.length);
for (int i = 0; i < originalData.length - modifiedData.length; i++) {
zaos.writeRaw(new byte[] { 0 }, 0, 1);
}
zaos.flush();
List<ZipArchiveEntry> entries = new ArrayList<>();
entries.add(zae);
zaos.finish(entries, new ArrayList<ZipArchiveEntry>());
byte[] testZip = fileNameFunz.toByteArray();
// write the result to a file
File outputDir = context.getCacheDir();
File badZip = File.createTempFile("prefix", "extension", outputDir);
badZip.deleteOnExit();
FileOutputStream outstream = new FileOutputStream(badZip);
outstream.write(testZip);
outstream.close();
// see if we can still handle it
ZipFile bad = new ZipFile(badZip);
if (bad.size() != 1)
throw new ZipException("Unexpected number of entries");
ZipEntry ze = bad.entries().nextElement();
DataInputStream dis = new DataInputStream(bad.getInputStream(ze));
byte[] buf = new byte[(int) ze.getSize()];
dis.readFully(buf);
bad.close();
return new String(buf).startsWith("AAAAAAAAAAAAAAA");
}Example 50
| Project: Android-Zip4j-master File: CRCUtil.java View source code |
/**
* Calculates CRC of a file
* @param inputFile - file for which crc has to be calculated
* @return crc of the file
* @throws ZipException
*/
public static long computeFileCRC(String inputFile, ProgressMonitor progressMonitor) throws ZipException {
if (!Zip4jUtil.isStringNotNullAndNotEmpty(inputFile)) {
throw new ZipException("input file is null or empty, cannot calculate CRC for the file");
}
InputStream inputStream = null;
try {
Zip4jUtil.checkFileReadAccess(inputFile);
inputStream = new FileInputStream(new File(inputFile));
byte[] buff = new byte[BUF_SIZE];
int readLen = -2;
CRC32 crc32 = new CRC32();
while ((readLen = inputStream.read(buff)) != -1) {
crc32.update(buff, 0, readLen);
if (progressMonitor != null) {
progressMonitor.updateWorkCompleted(readLen);
if (progressMonitor.isCancelAllTasks()) {
progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED);
progressMonitor.setState(ProgressMonitor.STATE_READY);
return 0;
}
}
}
return crc32.getValue();
} catch (IOException e) {
throw new ZipException(e);
} catch (Exception e) {
throw new ZipException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new ZipException("error while closing the file after calculating crc");
}
}
}
}Example 51
| Project: Aspose_Email_Java-master File: PSTPasswordProtectionProperties.java View source code |
private static boolean isPasswordValid(String password, PersonalStorage pst) {
// If the property exists and is nonzero, then the PST file is password protected.
if (pst.getStore().getProperties().containsKey(MapiPropertyTag.PR_PST_PASSWORD)) {
// The property value contains the CRC-32 hash of the password string of PST.
long passwordHash = pst.getStore().getProperties().get_Item(MapiPropertyTag.PR_PST_PASSWORD).getLong();
CRC32 crc = new CRC32();
crc.update(password.getBytes());
return passwordHash != 0 && passwordHash == crc.getValue();
}
return false;
}Example 52
| Project: drftpd-master File: ZipscriptHandler.java View source code |
private SFVInfo getSFVFile(Slave slave, String path) throws IOException {
BufferedReader reader = null;
CRC32 checksum = null;
try {
File file = slave.getRoots().getFile(path);
checksum = new CRC32();
reader = new BufferedReader(new InputStreamReader(new CheckedInputStream(new FileInputStream(file), checksum)));
SFVInfo sfvInfo = SFVInfo.importSFVInfoFromFile(reader);
sfvInfo.setSFVFileName(file.getName());
sfvInfo.setChecksum(checksum.getValue());
return sfvInfo;
} finally {
if (reader != null) {
reader.close();
}
}
}Example 53
| Project: drftpd3-extended-master File: ZipscriptHandler.java View source code |
private SFVInfo getSFVFile(Slave slave, String path) throws IOException {
BufferedReader reader = null;
CRC32 checksum = null;
try {
File file = slave.getRoots().getFile(path);
checksum = new CRC32();
reader = new BufferedReader(new InputStreamReader(new CheckedInputStream(new FileInputStream(file), checksum)));
SFVInfo sfvInfo = SFVInfo.importSFVInfoFromFile(reader);
sfvInfo.setSFVFileName(file.getName());
sfvInfo.setChecksum(checksum.getValue());
return sfvInfo;
} finally {
if (reader != null) {
reader.close();
}
}
}Example 54
| Project: eclipse.platform.ui-master File: ZipFileExporter.java View source code |
/**
* Write the contents of the file to the tar archive.
*
* @param entry
* @param contents
* @exception java.io.IOException
* @exception org.eclipse.core.runtime.CoreException
*/
private void write(ZipEntry entry, IFile contents) throws IOException, CoreException {
byte[] readBuffer = new byte[4096];
// If the contents are being compressed then we get the below for free.
if (!useCompression) {
entry.setMethod(ZipEntry.STORED);
InputStream contentStream = contents.getContents(false);
int length = 0;
CRC32 checksumCalculator = new CRC32();
try {
int n;
while ((n = contentStream.read(readBuffer)) > 0) {
checksumCalculator.update(readBuffer, 0, n);
length += n;
}
} finally {
if (contentStream != null) {
contentStream.close();
}
}
entry.setSize(length);
entry.setCrc(checksumCalculator.getValue());
}
// set the timestamp
long localTimeStamp = contents.getLocalTimeStamp();
if (localTimeStamp != IResource.NULL_STAMP)
entry.setTime(localTimeStamp);
outputStream.putNextEntry(entry);
InputStream contentStream = contents.getContents(false);
try {
int n;
while ((n = contentStream.read(readBuffer)) > 0) {
outputStream.write(readBuffer, 0, n);
}
} finally {
if (contentStream != null) {
contentStream.close();
}
}
outputStream.closeEntry();
}Example 55
| Project: freehep-ncolor-pdf-master File: ThreadingTest.java View source code |
public void run() {
try {
URL url = new URL(null, file, new XrootdStreamHandler());
URLConnection conn = url.openConnection();
conn.setRequestProperty(XrootdURLConnection.XROOT_AUTHORIZATION_SCHEME, "anonymous");
DaemonInputStream in = (DaemonInputStream) conn.getInputStream();
try {
in.setPosition(start);
byte[] buffer = new byte[length];
int l = in.read(buffer);
CRC32 cs = new CRC32();
cs.update(buffer);
long cksum = cs.getValue();
//System.out.println(file+" cksum="+cksum);
if (result == 0)
result = cksum;
else
assertEquals(result, cksum);
} finally {
in.close();
}
} catch (Exception x) {
throw new RuntimeException("Exception reading data", x);
}
}Example 56
| Project: gboot-master File: DummyJarCreator.java View source code |
public static void createTestJar(File file, boolean unpackNested) throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream(file);
JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream);
try {
writeManifest(jarOutputStream, "j1");
writeEntry(jarOutputStream, "1.dat", 1);
writeEntry(jarOutputStream, "2.dat", 2);
writeDirEntry(jarOutputStream, "d/");
writeEntry(jarOutputStream, "d/9.dat", 9);
writeDirEntry(jarOutputStream, "special/");
writeEntry(jarOutputStream, "special/ë.dat", 'ë');
JarEntry nestedEntry = new JarEntry("nested.jar");
byte[] nestedJarData = getNestedJarData();
nestedEntry.setSize(nestedJarData.length);
nestedEntry.setCompressedSize(nestedJarData.length);
if (unpackNested) {
nestedEntry.setComment("UNPACK:0000000000000000000000000000000000000000");
}
CRC32 crc32 = new CRC32();
crc32.update(nestedJarData);
nestedEntry.setCrc(crc32.getValue());
nestedEntry.setMethod(ZipEntry.STORED);
jarOutputStream.putNextEntry(nestedEntry);
jarOutputStream.write(nestedJarData);
jarOutputStream.closeEntry();
} finally {
jarOutputStream.close();
}
}Example 57
| Project: karaf-master File: BundleUtils.java View source code |
public static File fixBundleWithUpdateLocation(InputStream is, String uri) throws IOException {
File file = File.createTempFile("update-", ".jar");
try (ZipInputStream zis = new ZipInputStream(is);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {
byte[] buf = new byte[8192];
zos.setLevel(0);
while (true) {
ZipEntry entry = zis.getNextEntry();
if (entry == null) {
break;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int n;
while (-1 != (n = zis.read(buf))) {
baos.write(buf, 0, n);
}
if (entry.getName().equals(JarFile.MANIFEST_NAME)) {
Manifest man = new Manifest(new ByteArrayInputStream(baos.toByteArray()));
if (man.getMainAttributes().getValue(Constants.BUNDLE_UPDATELOCATION) == null) {
man.getMainAttributes().putValue(Constants.BUNDLE_UPDATELOCATION, uri);
}
baos.reset();
man.write(baos);
}
byte[] data = baos.toByteArray();
CRC32 crc = new CRC32();
crc.update(data);
entry = new ZipEntry(entry.getName());
entry.setSize(data.length);
entry.setCrc(crc.getValue());
zos.putNextEntry(entry);
zos.write(data);
zis.closeEntry();
zos.closeEntry();
}
}
return file;
}Example 58
| Project: MinecraftAutoInstaller-master File: CRCUtil.java View source code |
/**
* Calculates CRC of a file
* @param inputFile - file for which crc has to be calculated
* @return crc of the file
* @throws ZipException
*/
public static long computeFileCRC(String inputFile, ProgressMonitor progressMonitor) throws ZipException {
if (!Zip4jUtil.isStringNotNullAndNotEmpty(inputFile)) {
throw new ZipException("input file is null or empty, cannot calculate CRC for the file");
}
InputStream inputStream = null;
try {
Zip4jUtil.checkFileReadAccess(inputFile);
inputStream = new FileInputStream(new File(inputFile));
byte[] buff = new byte[BUF_SIZE];
int readLen = -2;
CRC32 crc32 = new CRC32();
while ((readLen = inputStream.read(buff)) != -1) {
crc32.update(buff, 0, readLen);
if (progressMonitor != null) {
progressMonitor.updateWorkCompleted(readLen);
if (progressMonitor.isCancelAllTasks()) {
progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED);
progressMonitor.setState(ProgressMonitor.STATE_READY);
return 0;
}
}
}
return crc32.getValue();
} catch (IOException e) {
throw new ZipException(e);
} catch (Exception e) {
throw new ZipException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new ZipException("error while closing the file after calculating crc");
}
}
}
}Example 59
| Project: pentaho-aggdesigner-master File: MondrianFileSchemaModel.java View source code |
public long recalculateSchemaChecksum() {
if (getMondrianSchemaFilename() != null) {
try {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(getMondrianSchemaFilename()), new CRC32());
byte[] buf = new byte[1024];
while (cis.read(buf) >= 0) {
}
return cis.getChecksum().getValue();
} catch (IOException e) {
e.printStackTrace();
}
}
return -1;
}Example 60
| Project: Pulsar-master File: JavaCrc32.java View source code |
@Override
public StatelessIntHash asStateless() {
return new AbstractStatelessIntHash() {
@Override
public String algorithm() {
return ALGORITHM;
}
@Override
public int length() {
return LENGTH;
}
@Override
public StatefulIntHash createStateful() {
return new JavaCrc32();
}
@Override
protected int calculateUnchecked(byte[] input, int index, int length) {
final CRC32 crc32 = new CRC32();
crc32.update(input, index, length);
return (int) crc32.getValue();
}
};
}Example 61
| Project: revolance-ui-monitoring-master File: ArchiveHelper.java View source code |
public static File buildArchive(File archive, File... files) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream(archive);
ZipOutputStream zos = new ZipOutputStream(fos);
int bytesRead;
byte[] buffer = new byte[1024];
CRC32 crc = new CRC32();
for (File file : files) {
if (!file.exists()) {
System.err.println("Skipping: " + file);
continue;
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
crc.reset();
while ((bytesRead = bis.read(buffer)) != -1) {
crc.update(buffer, 0, bytesRead);
}
bis.close();
// Reset to beginning of input stream
bis = new BufferedInputStream(new FileInputStream(file));
String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file);
ZipEntry entry = new ZipEntry(entryPath);
entry.setMethod(ZipEntry.STORED);
entry.setCompressedSize(file.length());
entry.setSize(file.length());
entry.setCrc(crc.getValue());
zos.putNextEntry(entry);
while ((bytesRead = bis.read(buffer)) != -1) {
zos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(bis);
}
}
IOUtils.closeQuietly(zos);
return archive;
}Example 62
| Project: SurvivalGamesX-master File: CRCUtil.java View source code |
/**
* Calculates CRC of a file
* @param inputFile - file for which crc has to be calculated
* @return crc of the file
* @throws ZipException
*/
public static long computeFileCRC(String inputFile, ProgressMonitor progressMonitor) throws ZipException {
if (!Zip4jUtil.isStringNotNullAndNotEmpty(inputFile)) {
throw new ZipException("input file is null or empty, cannot calculate CRC for the file");
}
InputStream inputStream = null;
try {
Zip4jUtil.checkFileReadAccess(inputFile);
inputStream = new FileInputStream(new File(inputFile));
byte[] buff = new byte[BUF_SIZE];
int readLen = -2;
CRC32 crc32 = new CRC32();
while ((readLen = inputStream.read(buff)) != -1) {
crc32.update(buff, 0, readLen);
if (progressMonitor != null) {
progressMonitor.updateWorkCompleted(readLen);
if (progressMonitor.isCancelAllTasks()) {
progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED);
progressMonitor.setState(ProgressMonitor.STATE_READY);
return 0;
}
}
}
return crc32.getValue();
} catch (IOException e) {
throw new ZipException(e);
} catch (Exception e) {
throw new ZipException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new ZipException("error while closing the file after calculating crc");
}
}
}
}Example 63
| Project: voltdb-master File: getCRCFromRep.java View source code |
public long run(int id) {
CRC32 crc = new CRC32();
voltQueueSQL(Stmt, id);
VoltTable[] result = voltExecuteSQL(true);
while (result[0].advanceRow()) {
long counter = result[0].getLong("counter");
byte[] b = new byte[8];
for (int i = 0; i < 8; i++) {
b[7 - i] = (byte) (counter >>> (i * 8));
}
crc.update(b);
}
return crc.getValue();
}Example 64
| Project: Wire-Desktop-master File: TCPTransport.java View source code |
public synchronized void send(ByteBuffer packet) throws Exception {
ByteBuffer buffer = ByteBuffer.allocateDirect(packet.capacity() + 12);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(packet.capacity() + 12);
buffer.putInt(packetIndex);
packetIndex++;
packet.rewind();
buffer.put(packet);
CRC32 crc = new CRC32();
for (int i = 0; i < buffer.capacity() - 4; i++) {
crc.update(buffer.get(i));
}
buffer.putInt((int) crc.getValue());
buffer.rewind();
/*for (int i = 0; i < buffer.capacity(); i++) {
System.out.print(String.format("%02x ", buffer.get(i)));
if (i % 16 == 15) {
System.out.println();
}
}
System.out.println();*/
channel.write(buffer);
}Example 65
| Project: Work_book-master File: NIOTest.java View source code |
public static long checksumMappedFile(String filename) throws IOException {
FileInputStream in = new FileInputStream(filename);
FileChannel channel = in.getChannel();
CRC32 crc = new CRC32();
int lenght = (int) channel.size();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, lenght);
for (int p = 0; p < lenght; p++) {
int c = buffer.get(p);
crc.update(c);
}
return crc.getValue();
}Example 66
| Project: yoursway-sunrise-master File: AboutBundleGroupData.java View source code |
public Long getFeatureImageCrc() {
if (featureImageCrc != null) {
return featureImageCrc;
}
URL url = getFeatureImageUrl();
if (url == null) {
return null;
}
// Get the image bytes
InputStream in = null;
try {
CRC32 checksum = new CRC32();
in = new CheckedInputStream(url.openStream(), checksum);
// the contents don't matter, the read just needs a place to go
byte[] sink = new byte[1024];
while (true) {
if (in.read(sink) <= 0) {
break;
}
}
featureImageCrc = new Long(checksum.getValue());
return featureImageCrc;
} catch (IOException e) {
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}Example 67
| Project: damp.ekeko.snippets-master File: ElementImpl.java View source code |
/** Computes and returns CRC32 of element's body @return CRC32 object describing the body */ protected Object getBodyHash() { if (bodyBounds == null) { return null; } if (!bodyBounds.getBegin().getEditorSupport().isDocumentLoaded()) { // loaded. return null; } java.util.zip.CRC32 crc = null; try { java.util.zip.CRC32 crc2 = new java.util.zip.CRC32(); crc2.update(bodyBounds.getText().getBytes()); crc = crc2; } catch (BadLocationException e) { } catch (IOException e) { } return crc; }
Example 68
| Project: android-libcore64-master File: OldAndroidChecksumTest.java View source code |
private void cRC32Test(byte[] values, long expected) {
CRC32 crc = new CRC32();
// try it all at once
crc.update(values);
assertEquals(crc.getValue(), expected);
// try resetting and computing one byte at a time
crc.reset();
for (int i = 0; i < values.length; i++) {
crc.update(values[i]);
}
assertEquals(crc.getValue(), expected);
}Example 69
| Project: android_platform_libcore-master File: OldAndroidChecksumTest.java View source code |
private void cRC32Test(byte[] values, long expected) {
CRC32 crc = new CRC32();
// try it all at once
crc.update(values);
assertEquals(crc.getValue(), expected);
// try resetting and computing one byte at a time
crc.reset();
for (int i = 0; i < values.length; i++) {
crc.update(values[i]);
}
assertEquals(crc.getValue(), expected);
}Example 70
| Project: archive-patcher-master File: MinimalZipArchiveTest.java View source code |
@Test
public void testListEntries() throws IOException {
// Ensure all entries are found, and that they are in file order.
List<MinimalZipEntry> parsedEntries = MinimalZipArchive.listEntries(tempFile);
long lastSeenHeaderOffset = -1;
for (int x = 0; x < UnitTestZipArchive.allEntriesInFileOrder.size(); x++) {
UnitTestZipEntry expected = UnitTestZipArchive.allEntriesInFileOrder.get(x);
MinimalZipEntry actual = parsedEntries.get(x);
Assert.assertEquals(expected.path, actual.getFileName());
Assert.assertEquals(expected.level == 0 ? 0 : 8, actual.getCompressionMethod());
Assert.assertEquals(expected.getCompressedBinaryContent().length, actual.getCompressedSize());
Assert.assertEquals(expected.getUncompressedBinaryContent().length, actual.getUncompressedSize());
Assert.assertEquals(false, actual.getGeneralPurposeFlagBit11());
CRC32 crc32 = new CRC32();
crc32.update(expected.getUncompressedBinaryContent());
Assert.assertEquals(crc32.getValue(), actual.getCrc32OfUncompressedData());
// Offset verification is a little trickier
// 1. Verify that the offsets are in ascending order and increasing.
Assert.assertTrue(actual.getFileOffsetOfLocalEntry() > lastSeenHeaderOffset);
lastSeenHeaderOffset = actual.getFileOffsetOfLocalEntry();
// 2. Verify that the local signature header is at the calculated position
byte[] expectedSignatureBlock = new byte[] { 0x50, 0x4b, 0x03, 0x04 };
for (int index = 0; index < 4; index++) {
byte actualByte = unitTestZipArchive[((int) actual.getFileOffsetOfLocalEntry()) + index];
Assert.assertEquals(expectedSignatureBlock[index], actualByte);
}
// 3. Verify that the data is at the calculated position
byte[] expectedContent = expected.getCompressedBinaryContent();
int calculatedDataOffset = (int) actual.getFileOffsetOfCompressedData();
for (int index = 0; index < expectedContent.length; index++) {
Assert.assertEquals(expectedContent[index], unitTestZipArchive[calculatedDataOffset + index]);
}
}
}Example 71
| Project: atlas-master File: ZipUtil.java View source code |
/* Package visible for testing */
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();
}Example 72
| Project: com.revolsys.open-master File: FileResponseCache.java View source code |
private File toFile(final URI uri) {
final String scheme = uri.getScheme();
if (scheme.equals("http") || scheme.equals("https")) {
File file = new File(this.directory, scheme);
final String host = uri.getHost();
file = new File(file, host);
final int port = uri.getPort();
if (port != -1) {
file = new File(file, String.valueOf(port));
}
String extension = null;
String fileName = null;
final String path = uri.getPath();
if (path != null) {
file = new File(file, path);
if (!path.endsWith("/")) {
extension = FileUtil.getFileNameExtension(file);
if (extension.length() > 0) {
fileName = FileUtil.getFileNamePrefix(file);
} else {
fileName = FileUtil.getFileName(file);
}
file = file.getParentFile();
}
}
if (fileName == null) {
final CRC32 crc32 = new CRC32();
crc32.update(uri.toString().getBytes());
fileName = String.valueOf(crc32.getValue());
}
final String query = uri.getQuery();
if (query != null) {
final CRC32 crc32 = new CRC32();
crc32.update(query.getBytes());
fileName += "-q" + crc32.getValue();
}
if (extension.length() > 0) {
fileName = fileName + "." + extension;
}
return new File(file, fileName);
}
return null;
}Example 73
| Project: commons-compress-master File: ExplodeSupportTest.java View source code |
private void testArchiveWithImplodeCompression(final String filename, final String entryName) throws IOException {
final ZipFile zip = new ZipFile(new File(filename));
final ZipArchiveEntry entry = zip.getEntries().nextElement();
assertEquals("entry name", entryName, entry.getName());
assertTrue("entry can't be read", zip.canReadEntryData(entry));
assertEquals("method", ZipMethod.IMPLODING.getCode(), entry.getMethod());
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final CheckedOutputStream out = new CheckedOutputStream(bout, new CRC32());
IOUtils.copy(zip.getInputStream(entry), out);
out.flush();
assertEquals("CRC32", entry.getCrc(), out.getChecksum().getValue());
zip.close();
}Example 74
| Project: copper-engine-master File: FileUtil.java View source code |
private static void processChecksum(File dir, CRC32 crc32, String fileSuffix) {
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
processChecksum(f, crc32, fileSuffix);
} else {
if (fileSuffix == null || f.getName().endsWith(fileSuffix)) {
crc32.update(Long.toString(f.lastModified()).getBytes());
crc32.update(f.getAbsolutePath().getBytes());
}
}
}
}
}Example 75
| Project: coprhd-controller-master File: KerberosConfig.java View source code |
private long checksumCRC32(String string) throws IOException {
Checksum sum = new CRC32();
InputStream in = null;
try {
in = new CheckedInputStream(new ByteArrayInputStream(string.getBytes()), sum);
IOUtils.copy(in, new NullOutputStream());
} finally {
IOUtils.closeQuietly(in);
}
return sum.getValue();
}Example 76
| Project: fred-master File: CRCChecksumChecker.java View source code |
@Override
public byte[] appendChecksum(byte[] data) {
byte[] output = new byte[data.length + 4];
System.arraycopy(data, 0, output, 0, data.length);
Checksum crc = new CRC32();
crc.update(data, 0, data.length);
byte[] checksum = Fields.intToBytes((int) crc.getValue());
System.arraycopy(checksum, 0, output, data.length, 4);
return output;
}Example 77
| Project: gennai-master File: UploadProcessor.java View source code |
public static void finishUpload(StatementEntity statement, int fileSize, long checksum) throws UploadProcessorException {
try {
MetaStore metaStore = GungnirManager.getManager().getMetaStore();
FileStat fileStat = metaStore.findFile(statement.getUploadingFileName(), statement.getOwner());
int size = 0;
CRC32 crc32 = new CRC32();
ChunkIterator it = null;
try {
for (it = metaStore.findChunks(fileStat); it.hasNext(); ) {
byte[] bytes = it.next();
size += bytes.length;
crc32.update(bytes);
}
} finally {
it.close();
}
if (size != fileSize) {
metaStore.deleteChunks(fileStat);
metaStore.deleteFile(fileStat);
throw new UploadProcessorException(new TaskExecuteException("Incorrect size"));
}
if (crc32.getValue() != checksum) {
metaStore.deleteChunks(fileStat);
metaStore.deleteFile(fileStat);
throw new UploadProcessorException(new TaskExecuteException("Invalid checksum"));
}
fileStat.setSize(fileSize);
fileStat.setChecksum(checksum);
metaStore.changeFileStat(fileStat);
statement.setUploadingFileName(null);
} catch (MetaStoreException e) {
throw new UploadProcessorException(e);
} catch (NotStoredException e) {
throw new UploadProcessorException(e);
}
}Example 78
| Project: httpcomponents-client-master File: TestDecompressingEntity.java View source code |
@Test
public void testNonStreaming() throws Exception {
final CRC32 crc32 = new CRC32();
final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
Assert.assertFalse(entity.isStreaming());
final String s = EntityUtils.toString(entity);
Assert.assertEquals("1234567890", s);
Assert.assertEquals(639479525L, crc32.getValue());
final InputStream in1 = entity.getContent();
final InputStream in2 = entity.getContent();
Assert.assertTrue(in1 != in2);
}Example 79
| Project: intellij-community-master File: ZipOutputWrapper.java View source code |
private void zipBytes(String entryPath, OptByteArrayOutputStream byteOut) throws IOException {
addDirs(entryPath, false);
ZipEntry entry = new ZipEntry(entryPath);
if (!myCompressed) {
entry.setSize(byteOut.size());
CRC32 crc = new CRC32();
byteOut.updateChecksum(crc);
entry.setCrc(crc.getValue());
}
myOut.putNextEntry(entry);
byteOut.writeTo(myOut);
myOut.closeEntry();
}Example 80
| Project: jaamsim-master File: BlockWriter.java View source code |
/**
* Write the block and sub blocks to the output stream
* Can throw a DataBlock.Error
* @param out
* @param d
*/
public static void writeBlock(OutputStream out, DataBlock b) {
try {
// Write out the block header
byte[] nameBytes = b.getName().getBytes("UTF-8");
if (nameBytes.length > 128) {
throw new DataBlock.Error("Block name too large");
}
long payloadSize = getBlockDataSize(b);
byte[] sizeBytes = new byte[8];
BlockUtils.longToBytes(payloadSize, sizeBytes, 0);
byte[] numChildrenBytes = new byte[4];
BlockUtils.intToBytes(b.getChildren().size(), numChildrenBytes, 0);
CRC32 headerCRC = new CRC32();
headerCRC.update(nameBytes);
headerCRC.update(0);
headerCRC.update(numChildrenBytes);
headerCRC.update(sizeBytes);
byte[] crcBytes = new byte[4];
BlockUtils.intToBytes((int) headerCRC.getValue(), crcBytes, 0);
// header is ready, so start writing
out.write(BlockUtils.header);
out.write(crcBytes);
out.write(nameBytes);
out.write(0);
out.write(numChildrenBytes);
out.write(sizeBytes);
// Now wrap the output stream to get a total payload CRC, while still being recursive
BlockUtils.CRCOutputStream crcStream = new BlockUtils.CRCOutputStream(out);
for (DataBlock child : b.getChildren()) {
writeBlock(crcStream, child);
}
// Write the data section
crcStream.write(b.getData(), 0, b.getDataSize());
// re-use the CRC bytes array
BlockUtils.intToBytes((int) crcStream.getCRC(), crcBytes, 0);
out.write(crcBytes);
out.write(BlockUtils.footer);
} catch (IOException e) {
throw new DataBlock.Error(e.getMessage());
}
}Example 81
| Project: jcodec-master File: HLSFixPMT.java View source code |
public static void fixPAT(ByteBuffer data) {
ByteBuffer table = data.duplicate();
MTSUtils.parseSection(data);
ByteBuffer newPmt = data.duplicate();
while (data.remaining() > 4) {
short num = data.getShort();
short pid = data.getShort();
if (num != 0) {
newPmt.putShort(num);
newPmt.putShort(pid);
}
}
if (newPmt.position() != data.position()) {
// rewrite Section len
ByteBuffer section = table.duplicate();
section.get();
int sectionLen = newPmt.position() - table.position() + 1;
section.putShort((short) ((sectionLen & 0xfff) | 0xB000));
// Redo crc32
CRC32 crc32 = new CRC32();
table.limit(newPmt.position());
crc32.update(NIOUtils.toArray(table));
newPmt.putInt((int) crc32.getValue());
// fill with 0xff
while (newPmt.hasRemaining()) newPmt.put((byte) 0xff);
}
}Example 82
| Project: jdk7u-jdk-master File: LargeJarEntry.java View source code |
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i = 0; i < 8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i = 0; i < 8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb", jarName, "b" };
// now, try to sign it
try {
JarSigner.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}Example 83
| Project: ManagedRuntimeInitiative-master File: LargeJarEntry.java View source code |
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i = 0; i < 8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i = 0; i < 8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb", jarName, "b" };
// now, try to sign it
try {
JarSigner.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}Example 84
| Project: MPQTree-master File: MpqWriter.java View source code |
private void prepareWrite() {
if (isSingleUnit) {
sectorSize = (int) fileSize;
} else {
sectorSize = archive.getSectorSize();
}
if ((isCompressed || isImploded) && !isSingleUnit) {
sectorTable = new int[(int) ((fileSize + sectorSize - 1) / sectorSize + 1) + 1];
sectorTable[0] = sectorTable.length * 4;
}
crc32 = new CRC32();
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}Example 85
| Project: nagini-master File: NaginiZipUtils.java View source code |
public static void zip(String src, String dest, PrintStream stream) throws IOException {
src = src.replace("~", System.getProperty("user.home"));
dest = dest.replace("~", System.getProperty("user.home"));
File srcFile = new File(src);
if (!srcFile.exists()) {
throw new RuntimeException(src + " does not exist.");
}
ZipOutputStream zos = new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(dest), new CRC32()));
innerZip(zos, srcFile, null, stream);
zos.flush();
zos.close();
}Example 86
| Project: nifi-master File: FileHeaderTest.java View source code |
@Before
public void setup() throws IOException {
TestBinaryReaderBuilder testBinaryReaderBuilder = new TestBinaryReaderBuilder();
testBinaryReaderBuilder.putString(FileHeader.ELF_FILE);
testBinaryReaderBuilder.putQWord(oldestChunk);
testBinaryReaderBuilder.putQWord(currentChunkNumber);
testBinaryReaderBuilder.putQWord(nextRecordNumber);
testBinaryReaderBuilder.putDWord(headerSize);
testBinaryReaderBuilder.putWord(minorVersion);
testBinaryReaderBuilder.putWord(majorVersion);
testBinaryReaderBuilder.putWord(headerChunkSize);
testBinaryReaderBuilder.putWord(chunkCount);
byte[] unused = new byte[75];
random.nextBytes(unused);
testBinaryReaderBuilder.put(unused);
testBinaryReaderBuilder.put((byte) 0);
CRC32 crc32 = new CRC32();
crc32.update(testBinaryReaderBuilder.toByteArray());
testBinaryReaderBuilder.putDWord(flags);
testBinaryReaderBuilder.putDWord(UnsignedInteger.valueOf(crc32.getValue()));
fileHeader = new FileHeader(new ByteArrayInputStream(testBinaryReaderBuilder.toByteArray(4096)), mock(ComponentLog.class));
}Example 87
| Project: onedrive-java-client-master File: ROFileSystemProvider.java View source code |
public long getChecksum(File file) throws IOException {
// Compute CRC32 checksum
CheckedInputStream cis = null;
try {
cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
byte[] buf = new byte[1024];
//noinspection StatementWithEmptyBody
while (cis.read(buf) >= 0) {
}
return cis.getChecksum().getValue();
} finally {
if (cis != null) {
cis.close();
}
}
}Example 88
| Project: openjdk8-jdk-master File: LargeJarEntry.java View source code |
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i = 0; i < 8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i = 0; i < 8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb", jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}Example 89
| Project: OpenSpaceDVR-master File: HLSFixPMT.java View source code |
public static void fixPAT(ByteBuffer data) {
ByteBuffer table = data.duplicate();
MTSUtils.parseSection(data);
ByteBuffer newPmt = data.duplicate();
while (data.remaining() > 4) {
short num = data.getShort();
short pid = data.getShort();
if (num != 0) {
newPmt.putShort(num);
newPmt.putShort(pid);
}
}
if (newPmt.position() != data.position()) {
// rewrite Section len
ByteBuffer section = table.duplicate();
section.get();
int sectionLen = newPmt.position() - table.position() + 1;
section.putShort((short) ((sectionLen & 0xfff) | 0xB000));
// Redo crc32
CRC32 crc32 = new CRC32();
table.limit(newPmt.position());
crc32.update(NIOUtils.toArray(table));
newPmt.putInt((int) crc32.getValue());
// fill with 0xff
while (newPmt.hasRemaining()) newPmt.put((byte) 0xff);
}
}Example 90
| Project: org.eclipse.ui.ide.format.extension.patch-master File: ZipFileExporter.java View source code |
/**
* Write the contents of the file to the tar archive.
*
* @param entry
* @param contents
* @exception java.io.IOException
* @exception org.eclipse.core.runtime.CoreException
*/
private void write(ZipEntry entry, IFile contents) throws IOException, CoreException {
byte[] readBuffer = new byte[4096];
// If the contents are being compressed then we get the below for free.
if (!useCompression) {
entry.setMethod(ZipEntry.STORED);
InputStream contentStream = contents.getContents(false);
int length = 0;
CRC32 checksumCalculator = new CRC32();
try {
int n;
while ((n = contentStream.read(readBuffer)) > 0) {
checksumCalculator.update(readBuffer, 0, n);
length += n;
}
} finally {
if (contentStream != null) {
contentStream.close();
}
}
entry.setSize(length);
entry.setCrc(checksumCalculator.getValue());
}
// set the timestamp
long localTimeStamp = contents.getLocalTimeStamp();
if (localTimeStamp != IResource.NULL_STAMP)
entry.setTime(localTimeStamp);
outputStream.putNextEntry(entry);
InputStream contentStream = contents.getContents(false);
try {
int n;
while ((n = contentStream.read(readBuffer)) > 0) {
outputStream.write(readBuffer, 0, n);
}
} finally {
if (contentStream != null) {
contentStream.close();
}
}
outputStream.closeEntry();
}Example 91
| Project: pdt-master File: ZipFileExporter.java View source code |
private void completeEntry(ZipEntry entry, InputStream contentStream) throws IOException {
byte[] readBuffer = new byte[4096];
// If the contents are being compressed then we get the below for free.
if (pharPackage.getCompressType() == PharConstants.NONE_COMPRESSED) {
entry.setMethod(ZipEntry.STORED);
int length = 0;
CRC32 checksumCalculator = new CRC32();
try {
int n;
while ((n = contentStream.read(readBuffer)) > 0) {
checksumCalculator.update(readBuffer, 0, n);
length += n;
}
} finally {
if (contentStream != null) {
contentStream.close();
}
}
entry.setSize(length);
entry.setCrc(checksumCalculator.getValue());
}
}Example 92
| Project: PSXperia-master File: ZpakCreate.java View source code |
public static long getCRC32(File file) throws IOException {
CheckedInputStream cis = null;
long fileSize = 0;
// Computer CRC32 checksum
cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
fileSize = file.length();
byte[] buf = new byte[128];
while (cis.read(buf) != -1) ;
long checksum = cis.getChecksum().getValue();
cis.close();
Logger.verbose("CRC32 of %s is %d", file.getPath(), checksum);
return checksum;
}Example 93
| Project: robolectric-master File: CachedDependencyResolver.java View source code |
public String getName(String prefix, DependencyJar... dependencies) {
StringBuilder sb = new StringBuilder();
sb.append(prefix).append("#");
for (DependencyJar dependency : dependencies) {
sb.append(dependency.getGroupId()).append(":").append(dependency.getArtifactId()).append(":").append(dependency.getVersion()).append(",");
}
CRC32 crc = new CRC32();
crc.update(sb.toString().getBytes());
return crc.getValue() + "";
}Example 94
| Project: robovm-master File: OldAndroidChecksumTest.java View source code |
private void cRC32Test(byte[] values, long expected) {
CRC32 crc = new CRC32();
// try it all at once
crc.update(values);
assertEquals(crc.getValue(), expected);
// try resetting and computing one byte at a time
crc.reset();
for (int i = 0; i < values.length; i++) {
crc.update(values[i]);
}
assertEquals(crc.getValue(), expected);
}Example 95
| Project: RouteConverter-master File: KmzFormat.java View source code |
private void writeIntermediate(OutputStream target, byte[] bytes) throws IOException {
CRC32 crc = new CRC32();
crc.reset();
crc.update(bytes);
ZipOutputStream outputStream = new ZipOutputStream(target);
try {
ZipEntry entry = new ZipEntry("doc.kml");
entry.setSize(bytes.length);
entry.setCrc(crc.getValue());
outputStream.putNextEntry(entry);
outputStream.write(bytes, 0, bytes.length);
outputStream.finish();
} finally {
outputStream.flush();
outputStream.close();
}
}Example 96
| Project: shrinkwrap-master File: ZipOnDemandInputStream.java View source code |
@Override
protected void putNextEntry(final ZipOutputStream outputStream, final String context, final Asset asset) throws IOException {
ZipEntry zipEntry = new ZipEntry(context);
if (!compressed) {
zipEntry.setMethod(ZipEntry.STORED);
zipEntry.setTime(SYSTIME);
long contentSize = 0;
long crc = 0;
// If it is not a directory
if (asset != null) {
// Calculates the CRC
CRC32 crc32 = new CRC32();
byte[] buf = new byte[1024];
int len;
InputStream is = null;
try {
is = new BufferedInputStream(asset.openStream());
while ((len = is.read(buf, 0, buf.length)) != -1) {
crc32.update(buf, 0, len);
// Updates the size of the file
contentSize += len;
}
} finally {
if (is != null) {
is.close();
}
}
// Gets calculated value
crc = crc32.getValue();
}
zipEntry.setCrc(crc);
zipEntry.setSize(contentSize);
}
outputStream.putNextEntry(zipEntry);
}Example 97
| Project: Soen6471Frinika-master File: MidiPacket.java View source code |
public int checksum() {
if (checksumset)
return checksum_value;
CRC32 crc32 = new CRC32();
crc32.update((byte) channel);
crc32.update((byte) program);
crc32.update((byte) pitchbend_data1);
crc32.update((byte) pitchbend_data2);
for (int i = 0; i < events.length; i++) {
crc32.update(events[i].getMessage().getMessage());
}
if (controls != null) {
int[] sorted = new int[controls.length];
for (int i = 0; i < controls.length; i++) {
sorted[i] = (controls[i] << 2) + controls_values[i];
}
Arrays.sort(sorted);
for (int i = 0; i < sorted.length; i++) {
crc32.update((byte) (sorted[i] & 0xFF));
crc32.update((byte) ((sorted[i] & 0xFF00) >> 2));
}
}
if (activenotes != null) {
int[] sorted = new int[activenotes.length];
for (int i = 0; i < activenotes.length; i++) {
sorted[i] = (activenotes[i] << 2) + activenotes_velocity[i];
}
Arrays.sort(sorted);
for (int i = 0; i < sorted.length; i++) {
crc32.update((byte) (sorted[i] & 0xFF));
crc32.update((byte) ((sorted[i] & 0xFF00) >> 2));
}
}
checksumset = true;
checksum_value = (int) crc32.getValue();
return checksum_value;
}Example 98
| Project: spring-boot-master File: TestJarCreator.java View source code |
private static void writeNestedEntry(String name, boolean unpackNested, JarOutputStream jarOutputStream) throws Exception, IOException {
JarEntry nestedEntry = new JarEntry(name);
byte[] nestedJarData = getNestedJarData();
nestedEntry.setSize(nestedJarData.length);
nestedEntry.setCompressedSize(nestedJarData.length);
if (unpackNested) {
nestedEntry.setComment("UNPACK:0000000000000000000000000000000000000000");
}
CRC32 crc32 = new CRC32();
crc32.update(nestedJarData);
nestedEntry.setCrc(crc32.getValue());
nestedEntry.setMethod(ZipEntry.STORED);
jarOutputStream.putNextEntry(nestedEntry);
jarOutputStream.write(nestedJarData);
jarOutputStream.closeEntry();
}Example 99
| Project: spring-ide-master File: ZipDiff.java View source code |
public Map<String, Long> computeHashes(InputStream expectedZipData) throws IOException {
Map<String, Long> hashes = new HashMap<>();
ZipInputStream zip = new ZipInputStream(expectedZipData);
ZipEntry ze;
byte[] buffer = new byte[1024];
while (null != (ze = zip.getNextEntry())) {
String path = ze.getName();
if (ze.isDirectory()) {
hashes.put(path, -1L);
} else {
CRC32 hasher = new CRC32();
int len;
while ((len = zip.read(buffer)) > 0) {
hasher.update(buffer, 0, len);
}
hashes.put(path, hasher.getValue());
}
}
return hashes;
}Example 100
| Project: tika-master File: ZipWriter.java View source code |
private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer) throws IOException {
ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString());
zipEntry.setMethod(ZipOutputStream.STORED);
zipEntry.setSize(dataBuffer.length);
CRC32 crc32 = new CRC32();
crc32.update(dataBuffer);
zipEntry.setCrc(crc32.getValue());
try {
zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
} catch (ZipException ex) {
if (name != null) {
zipStoreBuffer(zip, "x-" + name, dataBuffer);
return;
}
}
zip.write(dataBuffer);
zip.closeArchiveEntry();
}Example 101
| Project: TodayNews-master File: VideoPathDecoder.java View source code |
@Override
public Observable<ResultResponse<VideoModel>> call(String response) {
Pattern pattern = Pattern.compile("videoid:\'(.+)\'");
Matcher matcher = pattern.matcher(response);
if (matcher.find()) {
String videoId = matcher.group(1);
Logger.i(videoId);
//å°†/video/urls/v/1/toutiao/mp4/{videoid}?r={Math.random()},进行crc32åŠ å¯†ã€‚
String r = getRandom();
CRC32 crc32 = new CRC32();
String s = String.format(ApiService.URL_VIDEO, videoId, r);
crc32.update(s.getBytes());
String crcString = crc32.getValue() + "";
String url = ApiService.HOST_VIDEO + s + "&s=" + crcString;
Logger.i(url);
return AppClient.getApiService().getVideoData(url);
}
return null;
}