List of usage examples for java.util.zip Adler32 getValue
@Override public long getValue()
From source file:edu.cornell.med.icb.util.SimpleChecksum.java
/** * Takes a string and returns a that same string * with two additional characters which are a simple * checksum, each char will be "A".."Z". An empty * (no characters) or null string will just be * returned with no change.// w ww.j a va 2 s .c o m * @param valToChecksum the string to add the checksum to * @return the string with the checksum */ public static String simpleChecksum(final String valToChecksum) { if (StringUtils.isEmpty(valToChecksum)) { return valToChecksum; } final Adler32 adler = new Adler32(); adler.update(valToChecksum.getBytes()); final long[] result = splitLong(adler.getValue()); return String.format("%s%c%c", valToChecksum, CHECKSUM_CHARS[(int) result[0]], CHECKSUM_CHARS[(int) result[1]]); }
From source file:com.petercho.Encoder.java
public static long checksumAdler32(String text) { Adler32 crc = new Adler32(); try (InputStream is = new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING))) { checksum(is, crc);/*from ww w . j a v a 2 s . c o m*/ return crc.getValue(); } catch (IOException e) { throw new IllegalStateException("Error getting checksum, e:" + e, e); } }
From source file:com.hadoop.compression.lzo.LzopOutputStream.java
/** * Write an lzop-compatible header to the OutputStream provided. *///w w w . jav a2 s .c om protected static void writeLzopHeader(OutputStream out, LzoCompressor.CompressionStrategy strategy) throws IOException { DataOutputBuffer dob = new DataOutputBuffer(); try { dob.writeShort(LzopCodec.LZOP_VERSION); dob.writeShort(LzoCompressor.LZO_LIBRARY_VERSION); dob.writeShort(LzopCodec.LZOP_COMPAT_VERSION); switch (strategy) { case LZO1X_1: dob.writeByte(1); dob.writeByte(5); break; case LZO1X_15: dob.writeByte(2); dob.writeByte(1); break; case LZO1X_999: dob.writeByte(3); dob.writeByte(9); break; default: throw new IOException("Incompatible lzop strategy: " + strategy); } dob.writeInt(0); // all flags 0 dob.writeInt(0x81A4); // mode dob.writeInt((int) (System.currentTimeMillis() / 1000)); // mtime dob.writeInt(0); // gmtdiff ignored dob.writeByte(0); // no filename Adler32 headerChecksum = new Adler32(); headerChecksum.update(dob.getData(), 0, dob.getLength()); int hc = (int) headerChecksum.getValue(); dob.writeInt(hc); out.write(LzopCodec.LZO_MAGIC); out.write(dob.getData(), 0, dob.getLength()); } finally { dob.close(); } }
From source file:de.fu_berlin.inf.dpp.core.util.FileUtils.java
/** * Calculate Adler32 checksum for given file. * * @return checksum of file//from w w w . j a v a 2 s . c o m * @throws IOException if checksum calculation has been failed. */ public static long checksum(IFile file) throws IOException { InputStream in; try { in = file.getContents(); } catch (IOException e) { throw new IOException("failed to calculate checksum.", e); } byte[] buffer = new byte[BUFFER_SIZE]; Adler32 adler = new Adler32(); int read; try { while ((read = in.read(buffer)) != -1) { adler.update(buffer, 0, read); } } finally { IOUtils.closeQuietly(in); } return adler.getValue(); }
From source file:es.mityc.firmaJava.libreria.utilidades.CopiaFicherosManager.java
/** * Comprueba si el fichero indicado es ntegro. * /*from ww w . j a v a 2 s .c o m*/ * @param file * @param crcValue * @param size * @return */ private boolean checkIntegrityFile(File file, long crcValue, long size) throws FileNotFoundException, IOException { if (log.isTraceEnabled()) log.trace(I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_6) + ConstantesXADES.ESPACIO + file.getAbsolutePath()); if (!file.exists()) return false; // Primero comprueba el tamao if (file.length() != size) return false; // despus comprueba el crc Adler32 crc = new Adler32(); InputStream entrada = new BufferedInputStream(new FileInputStream(file), BUFFER_IN_SIZE); byte[] buffer = new byte[BUFFER_OUT_SIZE]; int readed = entrada.read(buffer); while (readed > 0) { crc.update(buffer, 0, readed); readed = entrada.read(buffer); } if (log.isTraceEnabled()) log.trace(I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_7) + ConstantesXADES.ESPACIO + crc.getValue() + ConstantesXADES.ESPACIO + I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_8) + ConstantesXADES.ESPACIO + crcValue); if (crc.getValue() != crcValue) return false; return true; }
From source file:com.adito.core.CoreUtil.java
/** * @param f/*from www. j a v a 2s. com*/ * @return long * @throws IOException */ public static long generateChecksum(File f) throws IOException { Adler32 alder = new Adler32(); FileInputStream fin = new FileInputStream(f); CheckedInputStream in = new CheckedInputStream(fin, alder); byte[] buf = new byte[32768]; Util.readFullyIntoBuffer(in, buf); alder = (Adler32) in.getChecksum(); try { in.close(); } catch (IOException ex) { } try { fin.close(); } catch (IOException ex1) { } return alder.getValue(); }
From source file:com.google.gwt.resources.rg.GssResourceGenerator.java
private String computeDefaultPrefix(ResourceContext context) { SortedSet<JClassType> gssResources = computeOperableTypes(context); Adler32 checksum = new Adler32(); for (JClassType type : gssResources) { checksum.update(Util.getBytes(type.getQualifiedSourceName())); }//from w w w. j a v a 2 s.c o m int seed = Math.abs((int) checksum.getValue()); return encode(seed) + "-"; }
From source file:com.hadoop.compression.lzo.LzopInputStream.java
/** * Read and verify an lzo header, setting relevant block checksum options * and ignoring most everything else./*from w w w .ja va2 s .co m*/ * @param in InputStream * @throws IOException if there is a error in lzo header */ protected void readHeader(InputStream in) throws IOException { readFully(in, buf, 0, 9); if (!Arrays.equals(buf, LzopCodec.LZO_MAGIC)) { throw new IOException("Invalid LZO header"); } Arrays.fill(buf, (byte) 0); Adler32 adler = new Adler32(); CRC32 crc32 = new CRC32(); int hitem = readHeaderItem(in, buf, 2, adler, crc32); // lzop version if (hitem > LzopCodec.LZOP_VERSION) { LOG.debug("Compressed with later version of lzop: " + Integer.toHexString(hitem) + " (expected 0x" + Integer.toHexString(LzopCodec.LZOP_VERSION) + ")"); } hitem = readHeaderItem(in, buf, 2, adler, crc32); // lzo library version if (hitem < LzoDecompressor.MINIMUM_LZO_VERSION) { throw new IOException("Compressed with incompatible lzo version: 0x" + Integer.toHexString(hitem) + " (expected at least 0x" + Integer.toHexString(LzoDecompressor.MINIMUM_LZO_VERSION) + ")"); } hitem = readHeaderItem(in, buf, 2, adler, crc32); // lzop extract version if (hitem > LzopCodec.LZOP_VERSION) { throw new IOException("Compressed with incompatible lzop version: 0x" + Integer.toHexString(hitem) + " (expected 0x" + Integer.toHexString(LzopCodec.LZOP_VERSION) + ")"); } hitem = readHeaderItem(in, buf, 1, adler, crc32); // method if (hitem < 1 || hitem > 3) { throw new IOException("Invalid strategy: " + Integer.toHexString(hitem)); } readHeaderItem(in, buf, 1, adler, crc32); // ignore level // flags hitem = readHeaderItem(in, buf, 4, adler, crc32); try { for (DChecksum f : dflags) { if (0 == (f.getHeaderMask() & hitem)) { dflags.remove(f); } else { dcheck.put(f, (int) f.getChecksumClass().newInstance().getValue()); } } for (CChecksum f : cflags) { if (0 == (f.getHeaderMask() & hitem)) { cflags.remove(f); } else { ccheck.put(f, (int) f.getChecksumClass().newInstance().getValue()); } } } catch (InstantiationException e) { throw new RuntimeException("Internal error", e); } catch (IllegalAccessException e) { throw new RuntimeException("Internal error", e); } ((LzopDecompressor) decompressor).initHeaderFlags(dflags, cflags); boolean useCRC32 = 0 != (hitem & 0x00001000); // F_H_CRC32 boolean extraField = 0 != (hitem & 0x00000040); // F_H_EXTRA_FIELD if (0 != (hitem & 0x400)) { // F_MULTIPART throw new IOException("Multipart lzop not supported"); } if (0 != (hitem & 0x800)) { // F_H_FILTER throw new IOException("lzop filter not supported"); } if (0 != (hitem & 0x000FC000)) { // F_RESERVED throw new IOException("Unknown flags in header"); } // known !F_H_FILTER, so no optional block readHeaderItem(in, buf, 4, adler, crc32); // ignore mode readHeaderItem(in, buf, 4, adler, crc32); // ignore mtime readHeaderItem(in, buf, 4, adler, crc32); // ignore gmtdiff hitem = readHeaderItem(in, buf, 1, adler, crc32); // fn len if (hitem > 0) { // skip filename int filenameLen = Math.max(4, hitem); // buffer must be at least 4 bytes for readHeaderItem to work. readHeaderItem(in, new byte[filenameLen], hitem, adler, crc32); } int checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue()); hitem = readHeaderItem(in, buf, 4, adler, crc32); // read checksum if (hitem != checksum) { throw new IOException("Invalid header checksum: " + Long.toHexString(checksum) + " (expected 0x" + Integer.toHexString(hitem) + ")"); } if (extraField) { // lzop 1.08 ultimately ignores this LOG.debug("Extra header field not processed"); adler.reset(); crc32.reset(); hitem = readHeaderItem(in, buf, 4, adler, crc32); readHeaderItem(in, new byte[hitem], hitem, adler, crc32); checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue()); if (checksum != readHeaderItem(in, buf, 4, adler, crc32)) { throw new IOException("Invalid checksum for extra header field"); } } }
From source file:net.sourceforge.pmd.cache.AbstractAnalysisCache.java
private long computeClassPathHash(final URL... classpathEntry) { final Adler32 adler32 = new Adler32(); for (final URL url : classpathEntry) { try (CheckedInputStream inputStream = new CheckedInputStream(url.openStream(), adler32)) { // Just read it, the CheckedInputStream will update the checksum on it's own while (IOUtils.skip(inputStream, Long.MAX_VALUE) == Long.MAX_VALUE) { // just loop }// w ww . j a va 2 s . c o m } catch (final FileNotFoundException ignored) { LOG.warning("Auxclasspath entry " + url.toString() + " doesn't exist, ignoring it"); } catch (final IOException e) { // Can this even happen? LOG.log(Level.SEVERE, "Incremental analysis can't check auxclasspath contents", e); throw new RuntimeException(e); } } return adler32.getValue(); }
From source file:org.anarres.lzo.LzopInputStream.java
/** * Read and verify an lzo header, setting relevant block checksum options * and ignoring most everything else./*from w ww .j a v a 2 s . com*/ */ protected int readHeader() throws IOException { byte[] buf = new byte[9]; readBytes(buf, 0, 9); if (!Arrays.equals(buf, LzopConstants.LZOP_MAGIC)) throw new IOException("Invalid LZO header"); Arrays.fill(buf, (byte) 0); Adler32 adler = new Adler32(); CRC32 crc32 = new CRC32(); int hitem = readHeaderItem(buf, 2, adler, crc32); // lzop version if (hitem > LzopConstants.LZOP_VERSION) { LOG.debug("Compressed with later version of lzop: " + Integer.toHexString(hitem) + " (expected 0x" + Integer.toHexString(LzopConstants.LZOP_VERSION) + ")"); } hitem = readHeaderItem(buf, 2, adler, crc32); // lzo library version if (hitem > LzoVersion.LZO_LIBRARY_VERSION) { throw new IOException("Compressed with incompatible lzo version: 0x" + Integer.toHexString(hitem) + " (expected 0x" + Integer.toHexString(LzoVersion.LZO_LIBRARY_VERSION) + ")"); } hitem = readHeaderItem(buf, 2, adler, crc32); // lzop extract version if (hitem > LzopConstants.LZOP_VERSION) { throw new IOException("Compressed with incompatible lzop version: 0x" + Integer.toHexString(hitem) + " (expected 0x" + Integer.toHexString(LzopConstants.LZOP_VERSION) + ")"); } hitem = readHeaderItem(buf, 1, adler, crc32); // method switch (hitem) { case LzopConstants.M_LZO1X_1: case LzopConstants.M_LZO1X_1_15: case LzopConstants.M_LZO1X_999: break; default: throw new IOException("Invalid strategy " + Integer.toHexString(hitem)); } readHeaderItem(buf, 1, adler, crc32); // ignore level // flags int flags = readHeaderItem(buf, 4, adler, crc32); boolean useCRC32 = (flags & LzopConstants.F_H_CRC32) != 0; boolean extraField = (flags & LzopConstants.F_H_EXTRA_FIELD) != 0; if ((flags & LzopConstants.F_MULTIPART) != 0) throw new IOException("Multipart lzop not supported"); if ((flags & LzopConstants.F_H_FILTER) != 0) throw new IOException("lzop filter not supported"); if ((flags & LzopConstants.F_RESERVED) != 0) throw new IOException("Unknown flags in header"); // known !F_H_FILTER, so no optional block readHeaderItem(buf, 4, adler, crc32); // ignore mode readHeaderItem(buf, 4, adler, crc32); // ignore mtime readHeaderItem(buf, 4, adler, crc32); // ignore gmtdiff hitem = readHeaderItem(buf, 1, adler, crc32); // fn len if (hitem > 0) { byte[] tmp = (hitem > buf.length) ? new byte[hitem] : buf; readHeaderItem(tmp, hitem, adler, crc32); // skip filename } int checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue()); hitem = readHeaderItem(buf, 4, adler, crc32); // read checksum if (hitem != checksum) { throw new IOException("Invalid header checksum: " + Long.toHexString(checksum) + " (expected 0x" + Integer.toHexString(hitem) + ")"); } if (extraField) { // lzop 1.08 ultimately ignores this LOG.debug("Extra header field not processed"); adler.reset(); crc32.reset(); hitem = readHeaderItem(buf, 4, adler, crc32); readHeaderItem(new byte[hitem], hitem, adler, crc32); checksum = (int) (useCRC32 ? crc32.getValue() : adler.getValue()); if (checksum != readHeaderItem(buf, 4, adler, crc32)) { throw new IOException("Invalid checksum for extra header field"); } } return flags; }