Example usage for java.util.zip CRC32 update

List of usage examples for java.util.zip CRC32 update

Introduction

In this page you can find the example usage for java.util.zip CRC32 update.

Prototype

@Override
public void update(ByteBuffer buffer) 

Source Link

Document

Updates the CRC-32 checksum with the bytes from the specified buffer.

Usage

From source file:edu.hm.cs.fs.scriptinat0r7.model.ScriptDocument.java

/**
 * Computes the hash value of a script document.
 * @return the 32 bit hash value as a long.
 *//*from  ww w .  java2s.c om*/
public long computeHashvalue() {
    final CRC32 crc = new CRC32();
    crc.update(getFile());
    return crc.getValue();
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks)./*from   w  w w .  j a va 2 s .  c o m*/
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * 24 + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * 24);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * 24);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[24];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = ByteBuffer.wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}

From source file:org.pentaho.di.trans.steps.checksum.CheckSum.java

private Long calculCheckSum(Object[] r) throws Exception {
    Long retval;// w ww .  j  av a2s  .com
    StringBuffer Buff = new StringBuffer();

    // Loop through fields
    for (int i = 0; i < data.fieldnr; i++) {
        String fieldvalue = getInputRowMeta().getString(r, data.fieldnrs[i]);
        Buff.append(fieldvalue);
    }

    if (meta.getCheckSumType().equals(CheckSumMeta.TYPE_CRC32)) {
        CRC32 crc32 = new CRC32();
        crc32.update(Buff.toString().getBytes());
        retval = new Long(crc32.getValue());
    } else {
        Adler32 adler32 = new Adler32();
        adler32.update(Buff.toString().getBytes());
        retval = new Long(adler32.getValue());
    }

    return retval;
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);//from w  w  w .  ja v  a 2s. co  m

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setMethod(ZipEntry.DEFLATED);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*ww w . ja  v a  2  s  .co  m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setTime(new Date().getTime());
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:org.apache.isis.objectstore.nosql.db.file.FileServerDb.java

private String checkData(final String data) {
    final String objectData = data.substring(8);

    final CRC32 inputChecksum = new CRC32();
    inputChecksum.update(objectData.getBytes());
    final long actualChecksum = inputChecksum.getValue();

    final String encodedChecksum = data.substring(0, 8);
    final long expectedChecksum = Long.valueOf(encodedChecksum, 16);

    if (actualChecksum != expectedChecksum) {
        throw new NoSqlStoreException("Data integrity error; checksums different");
    }/*  ww w  . j  a  v a  2  s .c o  m*/

    return objectData;
}

From source file:com.webpagebytes.cms.controllers.PageController.java

public void update(HttpServletRequest request, HttpServletResponse response, String requestUri)
        throws WPBException {
    try {/*from  w w  w.  j  a va 2  s . c om*/
        Long key = Long.valueOf((String) request.getAttribute("key"));
        String jsonRequest = httpServletToolbox.getBodyText(request);
        WPBPage webPage = (WPBPage) jsonObjectConverter.objectFromJSONString(jsonRequest, WPBPage.class);
        webPage.setPrivkey(key);
        Map<String, String> errors = pageValidator.validateUpdate(webPage);

        if (errors.size() > 0) {
            httpServletToolbox.writeBodyResponseAsJson(response, "{}", errors);
            return;
        }
        CRC32 crc = new CRC32();
        crc.update(webPage.getHtmlSource().getBytes());
        webPage.setHash(crc.getValue());

        webPage.setLastModified(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime());
        WPBPage newWebPage = adminStorage.update(webPage);

        WPBResource resource = new WPBResource(newWebPage.getExternalKey(), newWebPage.getName(),
                WPBResource.PAGE_TYPE);
        try {
            adminStorage.update(resource);
        } catch (Exception e) {
            // do not propate further
        }
        org.json.JSONObject returnJson = new org.json.JSONObject();
        returnJson.put(DATA, jsonObjectConverter.JSONFromObject(newWebPage));
        httpServletToolbox.writeBodyResponseAsJson(response, returnJson, null);

    } catch (Exception e) {
        Map<String, String> errors = new HashMap<String, String>();
        errors.put("", WPBErrors.WB_CANT_UPDATE_RECORD);
        httpServletToolbox.writeBodyResponseAsJson(response, jsonObjectConverter.JSONObjectFromMap(null),
                errors);
    }
}

From source file:it.jnrpe.net.JNRPEProtocolPacket.java

/**
 * Updates the CRC value.//  ww w.j a v  a 2 s  . com
 */
void updateCRC() {
    setCRC(0);
    CRC32 crcAlg = new CRC32();
    crcAlg.update(toByteArray());

    setCRC((int) crcAlg.getValue());
}

From source file:org.apache.jackrabbit.oak.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks)./*w  w w.  ja  va 2  s  . c  o  m*/
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * TarEntry.SIZE + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * TarEntry.SIZE);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * TarEntry.SIZE);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[TarEntry.SIZE];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}

From source file:org.apache.hadoop.raid.TestRaidNode.java

static long createOldFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize)
        throws IOException {
    CRC32 crc = new CRC32();
    FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, blocksize);
    // fill random data into file
    byte[] b = new byte[(int) blocksize];
    for (int i = 0; i < numBlocks; i++) {
        if (i == (numBlocks - 1)) {
            b = new byte[(int) blocksize / 2];
        }//from   www .j  a v a 2 s.c  o m
        rand.nextBytes(b);
        stm.write(b);
        crc.update(b);
    }

    stm.close();
    return crc.getValue();
}