Example usage for java.util.zip Checksum getValue

List of usage examples for java.util.zip Checksum getValue

Introduction

In this page you can find the example usage for java.util.zip Checksum getValue.

Prototype

public long getValue();

Source Link

Document

Returns the current checksum value.

Usage

From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java

/**
 * Gets the checksum from the given byte array with an instance of.
 *
 * @param bytes// w  w  w. j  a  v a 2 s .c o m
 *            The byte array.
 * @return The checksum from the byte array as long. {@link java.util.zip.CRC32} object.
 */
public static long getCheckSumCRC32(final byte[] bytes) {
    final Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    final long cs = checksum.getValue();
    return cs;
}

From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java

/**
 * Gets the checksum from the given byte array with an instance of.
 *
 * @param bytes/*from   w w w  .j a  v  a 2s .c  o m*/
 *            The byte array.
 * @return The checksum from the byte array as long. {@link java.util.zip.Adler32} object.
 */
public static long getCheckSumAdler32(final byte[] bytes) {
    final Checksum checksum = new Adler32();
    checksum.update(bytes, 0, bytes.length);
    final long cs = checksum.getValue();
    return cs;
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Generates a unique path and file name combination based on the parameters
 * provided./*from  w  w  w  . j  a v  a2  s.c o m*/
 *
 * @param aPathName Path name.
 * @param aFilePrefix File name prefix (appended with random id)
 * @param aFileExtension File name extension.
 *
 * @return A unique path and file name combination.
 */
static public String generateUniquePathFileName(String aPathName, String aFilePrefix, String aFileExtension) {
    String pathFileName;

    if (StringUtils.isNotEmpty(aPathName))
        pathFileName = String.format("%s%c%s", aPathName, File.separatorChar, aFilePrefix);
    else
        pathFileName = aFilePrefix;

    // http://www.javapractices.com/topic/TopicAction.do?Id=56

    UUID uniqueId = UUID.randomUUID();
    byte idBytes[] = uniqueId.toString().getBytes();
    Checksum checksumValue = new CRC32();
    checksumValue.update(idBytes, 0, idBytes.length);
    long longValue = checksumValue.getValue();

    if (StringUtils.startsWith(aFileExtension, "."))
        return String.format("%s_%d%s", pathFileName, longValue, aFileExtension);
    else
        return String.format("%s_%d.%s", pathFileName, longValue, aFileExtension);
}

From source file:com.bigdata.dastor.db.commitlog.CommitLog.java

public static void recover(File[] clogs) throws IOException {
    Set<Table> tablesRecovered = new HashSet<Table>();
    List<Future<?>> futures = new ArrayList<Future<?>>();
    for (File file : clogs) {
        int bufferSize = (int) Math.min(file.length(), 32 * 1024 * 1024);
        BufferedRandomAccessFile reader = new BufferedRandomAccessFile(file.getAbsolutePath(), "r", bufferSize);

        try {//from  w ww  .  j  a va2 s  . c  om
            final CommitLogHeader clHeader;
            try {
                clHeader = CommitLogHeader.readCommitLogHeader(reader);
            } catch (EOFException eofe) {
                logger.info(
                        "Attempted to recover an incomplete CommitLogHeader.  Everything is ok, don't panic.");
                continue;
            }

            /* seek to the lowest position where any CF has non-flushed data */
            int lowPos = CommitLogHeader.getLowestPosition(clHeader);
            if (lowPos == 0)
                continue;

            reader.seek(lowPos);
            if (logger.isDebugEnabled())
                logger.debug("Replaying " + file + " starting at " + lowPos);

            /* read the logs populate RowMutation and apply */
            while (!reader.isEOF()) {
                if (logger.isDebugEnabled())
                    logger.debug("Reading mutation at " + reader.getFilePointer());

                long claimedCRC32;
                byte[] bytes;
                try {
                    bytes = new byte[(int) reader.readLong()]; // readlong can throw EOFException too
                    reader.readFully(bytes);
                    claimedCRC32 = reader.readLong();
                } catch (EOFException e) {
                    // last CL entry didn't get completely written.  that's ok.
                    break;
                }

                ByteArrayInputStream bufIn = new ByteArrayInputStream(bytes);
                Checksum checksum = new CRC32();
                checksum.update(bytes, 0, bytes.length);
                if (claimedCRC32 != checksum.getValue()) {
                    // this part of the log must not have been fsynced.  probably the rest is bad too,
                    // but just in case there is no harm in trying them.
                    continue;
                }

                /* deserialize the commit log entry */
                final RowMutation rm = RowMutation.serializer().deserialize(new DataInputStream(bufIn));
                if (logger.isDebugEnabled())
                    logger.debug(String.format("replaying mutation for %s.%s: %s", rm.getTable(), rm.key(),
                            "{" + StringUtils.join(rm.getColumnFamilies(), ", ") + "}"));
                final Table table = Table.open(rm.getTable());
                tablesRecovered.add(table);
                final Collection<ColumnFamily> columnFamilies = new ArrayList<ColumnFamily>(
                        rm.getColumnFamilies());
                final long entryLocation = reader.getFilePointer();
                Runnable runnable = new WrappedRunnable() {
                    public void runMayThrow() throws IOException {
                        /* remove column families that have already been flushed before applying the rest */
                        for (ColumnFamily columnFamily : columnFamilies) {
                            int id = table.getColumnFamilyId(columnFamily.name());
                            if (!clHeader.isDirty(id) || entryLocation <= clHeader.getPosition(id)) {
                                rm.removeColumnFamily(columnFamily);
                            }
                        }
                        if (!rm.isEmpty()) {
                            Table.open(rm.getTable()).apply(rm, null, false);
                        }
                    }
                };
                futures.add(StageManager.getStage(StageManager.MUTATION_STAGE).submit(runnable));
                if (futures.size() > MAX_OUTSTANDING_REPLAY_COUNT) {
                    FBUtilities.waitOnFutures(futures);
                    futures.clear();
                }
            }
        } finally {
            reader.close();
            logger.info("Finished reading " + file);
        }
    }

    // wait for all the writes to finish on the mutation stage
    FBUtilities.waitOnFutures(futures);
    logger.debug("Finished waiting on mutations from recovery");

    // flush replayed tables
    futures.clear();
    for (Table table : tablesRecovered)
        futures.addAll(table.flush());
    FBUtilities.waitOnFutures(futures);
}

From source file:com.jkoolcloud.tnt4j.streams.configure.state.AbstractFileStreamStateHandler.java

/**
 * Check the line CRC.//w w  w . j a  v  a 2  s  .  c o  m
 *
 * @param line
 *            line to check
 * @param crc
 *            CRC to match
 *
 * @return {@code true} if matched
 */
private static boolean checkCrc(String line, Long crc) {
    if (line == null || crc == null) {
        return false;
    }

    Checksum crcLine = new CRC32();
    final byte[] bytes4Line = line.getBytes();
    crcLine.update(bytes4Line, 0, bytes4Line.length);
    final long lineCRC = crcLine.getValue();
    return lineCRC == crc;
}

From source file:com.autoupdateapk.AutoUpdateApk.java

private static int crc32(String str) {
    byte bytes[] = str.getBytes();
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    return (int) checksum.getValue();
}

From source file:com.jkoolcloud.tnt4j.streams.configure.state.AbstractFileStreamStateHandler.java

/**
 * Calculates CRC value for bytes read from provided input stream.
 *
 * @param in//from   ww w . j  ava  2  s.co m
 *            reader to read bytes for CRC calculation
 *
 * @return calculated CRC value
 *
 * @throws IOException
 *             if input can't be read.
 */
protected static Long getInputCrc(Reader in) throws IOException {
    char[] buff = new char[BYTES_TO_COUNT_CRC];
    Checksum crc = new CRC32();
    int readLen = in.read(buff);

    if (readLen > 0) {
        String str = new String(buff, 0, readLen);
        final byte[] bytes = str.getBytes(Utils.UTF8);
        crc.update(bytes, 0, bytes.length);
    }

    return crc.getValue();
}

From source file:MultiFileChecksumHelper.java

public static long getChecksum(File[] files) {
    CheckedInputStream cis = null;
    FileInputStream is = null;//w  w w . j av a 2 s .  c om
    Checksum checksum = new Adler32();
    byte[] tempBuf = new byte[128];

    for (int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++) {
        try {
            is = new FileInputStream(files[i]);
            cis = new CheckedInputStream(is, checksum);
            while (cis.read(tempBuf) >= 0) {
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (cis != null) {
                try {
                    cis.close();
                } catch (IOException ioe) {
                }
                cis = null;
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) {
                }
                is = null;
            }
        }
    }
    return checksum.getValue();
}

From source file:de.berlin.magun.nfcmime.core.CrcGenerator.java

/**
 * Generates a CRC32 checksum for an array of NdefRecords.
 * @param records//  www. jav  a  2  s .co m
 * @return CRC32 checksum represented as long.
 */
public long getChecksum(NdefRecord[] records) {
    byte[] overallBytes = getByteArray(records);
    Checksum cs = new CRC32();
    cs.update(overallBytes, 0, overallBytes.length);
    return cs.getValue();
}

From source file:org.codice.ddf.checksum.impl.CRC32ChecksumProvider.java

@Override
public String calculateChecksum(InputStream inputStream) throws IOException {

    if (inputStream == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }//w w w.  j  av  a2  s. c  om

    byte[] bytes = IOUtils.toByteArray(inputStream);
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    long checkSumValue = checksum.getValue();

    return Long.toHexString(checkSumValue);
}