Example usage for java.util.zip CRC32 getValue

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

Introduction

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

Prototype

@Override
public long getValue() 

Source Link

Document

Returns CRC-32 value.

Usage

From source file:odml.core.Value.java

/**
 * Base64 encodes the content if it represents either a File, URL, URI, or String that can be converted to a file.
 *
 * @param content - the content that should be encoded.
 * @return encoded content as {@link String}
 *//* w ww .  j  av  a 2 s .c  om*/
private String encodeContent(Object content) {
    if (content == null) {
        return null;
    }
    System.out.println("Encoding content: " + content.toString());
    String encoded = null;
    File file;
    if (content instanceof String) {
        try {
            URI uri = new URI((String) content);
            file = new File(uri);
        } catch (Exception e) {
            return (String) content;
        }
    } else if (content instanceof URL) {
        try {
            file = new File(((URL) content).toURI());
        } catch (Exception e) {
            System.out.println("Could not create a file from the specified URL: " + content.toString());
            file = null;
        }
    } else if (content instanceof URI) {
        try {
            file = new File((URI) content);
        } catch (Exception e) {
            System.out.println("Could not create a file from the specified URI: " + content.toString());
            file = null;
        }
    } else if (content instanceof File) {
        file = (File) content;
    } else {
        System.out.println("Could not create a File from input! Class: " + content.getClass().getSimpleName()
                + " Content: " + content.toString());
        file = null;
    }
    if (file == null) {
        return "";
    }
    Base64 enc = new Base64();
    //the value has to be converted to String; if it is already just take it, if it is not
    //try different things 
    try {
        byte[] bytes = enc.encode(getBytesFromFile(file));
        CRC32 crc = new CRC32();
        crc.update(bytes);
        this.setChecksum("CRC32$" + crc.getValue());
        this.setFilename(file.getName());
        this.setEncoder("Base64");
        encoded = new String(bytes, "UTF-8");
    } catch (Exception e) {
        System.out.println("An error occurred during encoding: " + e.getLocalizedMessage());
    }
    return encoded;
}

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

/**
 * Loads the optional pre-compiled graph entry from the given tar file.
 *
 * @return graph buffer, or {@code null} if one was not found
 * @throws IOException if the tar file could not be read
 *//*from  w  w w  .j av a  2 s  . c om*/
private ByteBuffer loadGraph() throws IOException {
    // read the graph metadata just before the tar index entry
    int pos = access.length() - 2 * BLOCK_SIZE - getEntrySize(index.remaining());
    ByteBuffer meta = access.read(pos - 16, 16);
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

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

    if (count < 0 || bytes < count * 16 + 16 || BLOCK_SIZE + bytes > pos) {
        log.warn("Invalid graph metadata in tar file {}", file);
        return null; // impossible uuid 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 graph = access.read(pos - bytes, bytes);

    byte[] b = new byte[bytes - 16];
    graph.mark();
    graph.get(b);
    graph.reset();

    CRC32 checksum = new CRC32();
    checksum.update(b);
    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid graph checksum in tar file {}", file);
        return null; // checksum mismatch
    }

    return graph;
}

From source file:org.tangram.components.CodeExporter.java

@LinkAction("/codes.zip")
public TargetDescriptor codes(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!request.getRequestURI().endsWith(".zip")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    } // if/*w  w w  . j  a va  2 s. co  m*/
    if (request.getAttribute(Constants.ATTRIBUTE_ADMIN_USER) == null) {
        throw new IOException("User may not execute action");
    } // if

    long now = System.currentTimeMillis();

    response.setContentType("application/x-zip-compressed");

    CRC32 crc = new CRC32();

    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
    zos.setComment("Tangram Repository Codes");
    zos.setLevel(9);
    Collection<CodeResource> codes = codeResourceCache.getCodes();
    for (CodeResource code : codes) {
        if (StringUtils.isNotBlank(code.getAnnotation())) {
            String mimeType = CodeHelper.getNormalizedMimeType(code.getMimeType());
            String folder = CodeHelper.getFolder(mimeType);
            String extension = CodeHelper.getExtension(mimeType);
            if (mimeType.startsWith("text/")) {
                byte[] bytes = code.getCodeText().getBytes("UTF-8");
                ZipEntry ze = new ZipEntry(folder + "/" + getFilename(code) + extension);
                ze.setTime(now);
                crc.reset();
                crc.update(bytes);
                ze.setCrc(crc.getValue());
                zos.putNextEntry(ze);
                zos.write(bytes);
                zos.closeEntry();
            } // if
        } // if
    } // for
    zos.finish();
    zos.close();

    return TargetDescriptor.DONE;
}

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

/**
 * Loads the optional pre-compiled graph entry from the given tar file.
 *
 * @return graph buffer, or {@code null} if one was not found
 * @throws IOException if the tar file could not be read
 *//*from  www.ja v  a 2  s.co m*/
private ByteBuffer loadGraph() throws IOException {
    // read the graph metadata just before the tar index entry
    int pos = access.length() - 2 * BLOCK_SIZE - getEntrySize(index.remaining() + 16);
    ByteBuffer meta = access.read(pos - 16, 16);
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

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

    if (count < 0 || bytes < count * 16 + 16 || BLOCK_SIZE + bytes > pos) {
        log.warn("Invalid graph metadata in tar file {}", file);
        return null; // impossible uuid 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 graph = access.read(pos - bytes, bytes);

    byte[] b = new byte[bytes - 16];
    graph.mark();
    graph.get(b);
    graph.reset();

    CRC32 checksum = new CRC32();
    checksum.update(b);
    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid graph checksum in tar file {}", file);
        return null; // checksum mismatch
    }

    hasGraph = true;
    return graph;
}

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

private long[] createRandomFile(Path file, int repl, int numBlocks) throws IOException {

    long[] crcs = new long[numBlocks];
    CRC32 crc = new CRC32();
    Random rand = new Random();
    FSDataOutputStream stm = fileSys.create(file, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, BLOCK_SIZE);
    // Write whole blocks.
    byte[] b = new byte[(int) BLOCK_SIZE];
    for (int i = 1; i < numBlocks; i++) {
        rand.nextBytes(b);/* ww w . j  a  v a 2s .c om*/
        stm.write(b);

        crc.update(b);
        crcs[i - 1] = crc.getValue();
        crc.reset();
    }
    // Write partial block.
    b = new byte[(int) BLOCK_SIZE / 2 - 1];
    rand.nextBytes(b);
    stm.write(b);
    crc.update(b);
    crcs[crcs.length - 1] = crc.getValue();

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

From source file:com.oneops.sensor.Sensor.java

/**
 * Adds the ci thresholds./*www  .  ja v  a  2 s.co m*/
 *
 * @param ciId       the ci id
 * @param manifestId the manifest id
 * @param monitor    the monitor
 * @throws SensorException
 */
public void addCiThresholdsList(long ciId, long manifestId, List<CmsRfcCISimple> monitors)
        throws SensorException {

    if (!isInited || (manifestId % this.poolSize) != this.instanceId) {
        // this is not my manifestId will post it on mgmt queue for other guy to pick up
        throw new SensorException("Got Monitor request for the wrong instance - manifestId:" + manifestId
                + "; pool size:" + this.poolSize + "; my insatnceId:" + this.instanceId);
    }

    Set<String> processedMonitors = new HashSet<>();

    for (CmsRfcCISimple monitor : monitors) {

        if (monitor.getCiAttributes().containsKey("enable")
                && monitor.getCiAttributes().get("enable").equals("false")) {
            continue;
        }

        long checksum = 0;

        String thresholdsJson = monitor.getCiAttributes().get("thresholds");
        String source = monitor.getCiName();

        if (thresholdsJson != null) {
            CRC32 crc = new CRC32();
            String crcStr = thresholdsJson + monitor.getCiAttributes().get(HEARTBEAT)
                    + monitor.getCiAttributes().get(DURATION);
            crc.update(crcStr.getBytes());
            checksum = crc.getValue();
        } else {
            // need to clean up thresholds
            continue;
        }

        processedMonitors.add(source);

        //String key = manifestId + source;
        ThresholdStatements trStmt = loadedThresholds.containsKey(manifestId)
                ? loadedThresholds.get(manifestId).get(source)
                : null;
        if (trStmt == null) {
            //load stmts
            persistAndaddToEngine(ciId, manifestId, source, checksum, thresholdsJson,
                    monitor.getCiAttributes().get(HEARTBEAT).equals("true"),
                    monitor.getCiAttributes().get(DURATION));
        } else if (trStmt.getChecksum() != checksum
                || monitor.getCiAttributes().get(HEARTBEAT).equals("true") != trStmt.isHeartbeat()) {
            // if checksum is different we assume there was an monitor update
            // we need to remove old stmts and insert new ones
            // but before that lets insert fake event to clear out heart beats
            // if this new mon is not a heartbeat one
            if (!monitor.getCiAttributes().get(HEARTBEAT).equals("true")) {
                insertFakeEvent(ciId, manifestId, source);
            }
            for (String eplName : trStmt.getStmtNames()) {
                removeStmtFromEngine(manifestId, source, eplName);
            }
            loadedThresholds.get(manifestId).remove(source);

            persistAndaddToEngine(ciId, manifestId, source, checksum, thresholdsJson,
                    monitor.getCiAttributes().get(HEARTBEAT).equals("true"),
                    monitor.getCiAttributes().get(DURATION));
        }
    }
    // now we need to clean up the deleted monitors
    if (loadedThresholds.containsKey(manifestId)) {
        Set<String> monsToRemove = new HashSet<>();
        for (String loadedMon : loadedThresholds.get(manifestId).keySet()) {
            if (!processedMonitors.contains(loadedMon)) {
                //this is old monitor that need to be removed
                //insert fake event to shut down Heartbeat retrigger
                insertFakeEvent(ciId, manifestId, loadedMon);
                //and do it for the rest bom guys
                for (long ciMapedBomId : tsDao.getManifestCiIds(manifestId)) {
                    insertFakeEvent(ciMapedBomId, manifestId, loadedMon);
                }

                ThresholdStatements trStmt = loadedThresholds.get(manifestId).get(loadedMon);
                for (String eplName : trStmt.getStmtNames()) {
                    removeStmtFromEngine(manifestId, loadedMon, eplName);
                }
                monsToRemove.add(loadedMon);
                tsDao.removeManifestThreshold(manifestId, loadedMon);
            }
        }
        for (String monToRemove : monsToRemove) {
            loadedThresholds.get(manifestId).remove(monToRemove);
        }
    }
}

From source file:com.webpagebytes.cms.local.WPBLocalFileStorage.java

public void storeFile(InputStream is, WPBFilePath file) throws IOException {
    String fullFilePath = getLocalFullDataPath(file);
    OutputStream fos = createStorageOutputStream(fullFilePath);
    byte[] buffer = new byte[4096];
    CRC32 crc = new CRC32();
    MessageDigest md = null;/*ww  w.java2  s . co m*/
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        IOUtils.closeQuietly(fos);
        throw new IOException("Cannot calculate md5 to store the file", e);
    }

    int count = 0;
    int size = 0;
    while ((count = is.read(buffer)) != -1) {
        size += count;
        fos.write(buffer, 0, count);
        crc.update(buffer, 0, count);
        md.update(buffer, 0, count);
    }

    IOUtils.closeQuietly(fos);

    Properties props = new Properties();
    props.put("path", file.getPath());
    props.put("contentType", "application/octet-stream");
    props.put("crc32", String.valueOf(crc.getValue()));
    props.put("md5", DatatypeConverter.printBase64Binary(md.digest()));
    props.put("creationTime",
            String.valueOf(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime().getTime()));
    props.put("size", String.valueOf(size));

    String metaPath = getLocalFullMetaPath(file);
    storeFileProperties(props, metaPath);

}

From source file:org.kuali.kfs.module.ar.report.service.impl.TransmitContractsAndGrantsInvoicesServiceImpl.java

/**
 *
 * @param report/*www .ja va  2 s  . c  o  m*/
 * @param invoiceFileWritten
 * @param zos
 * @param buffer
 * @param crc
 * @return
 * @throws IOException
 */
private boolean writeFile(byte[] arrayToWrite, ZipOutputStream zos, String fileName) throws IOException {
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    if (ObjectUtils.isNotNull(arrayToWrite) && arrayToWrite.length > 0) {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
        try {
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }
            bis.close();
            // Reset to beginning of input stream
            bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
            ZipEntry entry = new ZipEntry(fileName);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(arrayToWrite.length);
            entry.setSize(arrayToWrite.length);
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } finally {
            bis.close();
        }
        return true;
    }
    return false;
}

From source file:srebrinb.compress.sevenzip.SevenZFile.java

private Archive readHeaders(final byte[] password) throws IOException {
    ByteBuffer buf = ByteBuffer.allocate(12 /* signature + 2 bytes version + 4 bytes CRC */)
            .order(ByteOrder.LITTLE_ENDIAN);
    readFully(buf);//from  w  w w.j  a v a 2 s . co  m
    final byte[] signature = new byte[6];
    buf.get(signature);
    if (!Arrays.equals(signature, sevenZSignature)) {
        throw new IOException("Bad 7z signature");
    }
    // 7zFormat.txt has it wrong - it's first major then minor
    final byte archiveVersionMajor = buf.get();
    final byte archiveVersionMinor = buf.get();
    if (archiveVersionMajor != 0) {
        throw new IOException(
                String.format("Unsupported 7z version (%d,%d)", archiveVersionMajor, archiveVersionMinor));
    }

    final long startHeaderCrc = 0xffffFFFFL & buf.getInt();
    final StartHeader startHeader = readStartHeader(startHeaderCrc);

    final int nextHeaderSizeInt = (int) startHeader.nextHeaderSize;
    if (nextHeaderSizeInt != startHeader.nextHeaderSize) {
        throw new IOException("cannot handle nextHeaderSize " + startHeader.nextHeaderSize);
    }
    channel.position(SIGNATURE_HEADER_SIZE + startHeader.nextHeaderOffset);
    buf = ByteBuffer.allocate(nextHeaderSizeInt).order(ByteOrder.LITTLE_ENDIAN);
    readFully(buf);
    final CRC32 crc = new CRC32();
    crc.update(buf.array());
    if (startHeader.nextHeaderCrc != crc.getValue()) {
        throw new IOException("NextHeader CRC mismatch");
    }

    Archive archive = new Archive();
    int nid = getUnsignedByte(buf);
    if (nid == NID.kEncodedHeader) {
        buf = readEncodedHeader(buf, archive, password);
        // Archive gets rebuilt with the new header
        archive = new Archive();
        nid = getUnsignedByte(buf);
    }
    if (nid == NID.kHeader) {
        readHeader(buf, archive);
    } else {
        throw new IOException("Broken or unsupported archive: no Header");
    }
    return archive;
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private Archive readHeaders(final byte[] password) throws IOException {
    final byte[] signature = new byte[6];
    file.readFully(signature);/*from   w  ww.jav a 2  s  .  c  o  m*/
    if (!Arrays.equals(signature, sevenZSignature)) {
        throw new IOException("Bad 7z signature");
    }
    // 7zFormat.txt has it wrong - it's first major then minor
    final byte archiveVersionMajor = file.readByte();
    final byte archiveVersionMinor = file.readByte();
    if (archiveVersionMajor != 0) {
        throw new IOException(
                String.format("Unsupported 7z version (%d,%d)", archiveVersionMajor, archiveVersionMinor));
    }

    final long startHeaderCrc = 0xffffFFFFL & Integer.reverseBytes(file.readInt());
    final StartHeader startHeader = readStartHeader(startHeaderCrc);

    final int nextHeaderSizeInt = (int) startHeader.nextHeaderSize;
    if (nextHeaderSizeInt != startHeader.nextHeaderSize) {
        throw new IOException("cannot handle nextHeaderSize " + startHeader.nextHeaderSize);
    }
    file.seek(SIGNATURE_HEADER_SIZE + startHeader.nextHeaderOffset);
    final byte[] nextHeader = new byte[nextHeaderSizeInt];
    file.readFully(nextHeader);
    final CRC32 crc = new CRC32();
    crc.update(nextHeader);
    if (startHeader.nextHeaderCrc != crc.getValue()) {
        throw new IOException("NextHeader CRC mismatch");
    }

    final ByteArrayInputStream byteStream = new ByteArrayInputStream(nextHeader);
    DataInputStream nextHeaderInputStream = new DataInputStream(byteStream);
    Archive archive = new Archive();
    int nid = nextHeaderInputStream.readUnsignedByte();
    if (nid == NID.kEncodedHeader) {
        nextHeaderInputStream = readEncodedHeader(nextHeaderInputStream, archive, password);
        // Archive gets rebuilt with the new header
        archive = new Archive();
        nid = nextHeaderInputStream.readUnsignedByte();
    }
    if (nid == NID.kHeader) {
        readHeader(nextHeaderInputStream, archive);
        nextHeaderInputStream.close();
    } else {
        throw new IOException("Broken or unsupported archive: no Header");
    }
    return archive;
}