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:uk.ac.cam.cl.dtg.isaac.dos.eventbookings.PgEventBookings.java

/**
 * Release a globally unique database lock.
 * This method will release a previously acquired lock.
 *
 * @param resourceId - the unique id for the object to be locked.
 *///from  w w w. j av  a  2  s.c  o m
@Override
public void releaseDistributedLock(final String resourceId) throws SegueDatabaseException {

    // generate 32 bit CRC based on table id and resource id so that is is more likely to be unique globally.
    CRC32 crc = new CRC32();
    crc.update((TABLE_NAME + resourceId).getBytes());

    // acquire lock
    try (Connection conn = ds.getDatabaseConnection()) {
        PreparedStatement pst;
        pst = conn.prepareStatement("SELECT pg_advisory_unlock(?)");
        pst.setLong(1, crc.getValue());
        log.debug(String.format("Releasing advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue()));
        pst.executeQuery();
    } catch (SQLException e) {
        String msg = String.format("Unable to release lock for event (%s).", resourceId);
        log.error(msg);
        throw new SegueDatabaseException(msg);
    }
    log.debug(String.format("Released advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue()));
}

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

private 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
    final byte[] b = new byte[(int) blocksize];
    for (int i = 0; i < numBlocks; i++) {
        rand.nextBytes(b);/*from  www .  j  a  v  a 2s  .com*/
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:com.sastix.cms.server.services.cache.hazelcast.HazelcastCacheService.java

@Override
public String getUID(String region) {
    CRC32 CRC_32 = new CRC32();
    LOG.info("HazelcastCacheService->GET_UID");
    IdGenerator idGenerator = cm.getIdGenerator(region);
    final String uid = String.valueOf(idGenerator.newId()); //assures uniqueness during the life cycle of the cluster
    final String uuid = UUID.randomUUID().toString();
    String ret = new StringBuilder(uuid).append(region).append(uid).toString();
    CRC_32.reset();//  w  ww . j a va  2  s.  co m
    CRC_32.update(ret.getBytes());
    return Long.toHexString(CRC_32.getValue());
}

From source file:org.klco.email2html.OutputWriter.java

/**
 * Adds the attachment to the EmailMessage. Call this method when the email
 * content has most likely already been loaded.
 * /*from  ww  w  . j a  v a  2  s  .  c o  m*/
 * @param containingMessage
 *            the Email Message to add the attachment to
 * @param part
 *            the content of the attachment
 * @throws IOException
 * @throws MessagingException
 */
public void addAttachment(EmailMessage containingMessage, Part part) throws IOException, MessagingException {
    log.trace("addAttachment");

    File attachmentFolder = new File(outputDir.getAbsolutePath() + File.separator + config.getImagesSubDir()
            + File.separator + FILE_DATE_FORMAT.format(containingMessage.getSentDate()));
    File attachmentFile = new File(attachmentFolder, part.getFileName());

    boolean addAttachment = true;
    boolean writeAttachment = false;
    if (!attachmentFolder.exists() || !attachmentFile.exists()) {
        log.warn("Attachment or folder missing, writing attachment {}", attachmentFile.getName());
        writeAttachment = true;
    }

    if (!writeAttachment && part.getContentType().toLowerCase().startsWith("image")) {
        for (Rendition rendition : renditions) {
            File renditionFile = new File(attachmentFolder, rendition.getName() + "-" + part.getFileName());
            if (!renditionFile.exists()) {
                log.warn("Rendition {} missing, writing attachment {}", renditionFile.getName(),
                        attachmentFile.getName());
                writeAttachment = true;
                break;
            }
        }
    }
    if (writeAttachment) {
        addAttachment = writeAttachment(containingMessage, part);
    } else {
        if (this.excludeDuplicates) {
            log.debug("Computing checksum");
            InputStream is = null;
            try {
                CRC32 checksum = new CRC32();
                is = new BufferedInputStream(new FileInputStream(attachmentFile));
                for (int read = is.read(); read != -1; read = is.read()) {
                    checksum.update(read);
                }
                long value = checksum.getValue();
                if (attachmentChecksums.contains(value)) {
                    addAttachment = false;
                } else {
                    attachmentChecksums.add(checksum.getValue());
                }
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
    if (addAttachment) {
        containingMessage.getAttachments().add(attachmentFile);
    } else {
        log.debug("Attachment is a duplicate, not adding as message attachment");
    }
}

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

/**
 * Save current file access state. Actually takes the current streamed file line, and calculates CRC of that line.
 *
 * @param line/*w  w w.  j  a va 2s  . c o  m*/
 *            line currently streamed
 * @param streamName
 *            stream name
 */
public void saveState(AbstractFileLineStream.Line line, String streamName) {
    AbstractFileLineStream.Line procLine = prevLine;
    prevLine = line;
    if (procLine == null) {
        return;
    }

    String lineStr = procLine.getText();
    int lineNr = procLine.getLineNumber();

    try {
        fileAccessState.currentLineNumber = lineNr;
        fileAccessState.lastReadTime = System.currentTimeMillis();

        CRC32 crc = new CRC32();
        final byte[] bytes4Line = lineStr.getBytes(Utils.UTF8);
        crc.update(bytes4Line, 0, bytes4Line.length);
        fileAccessState.currentLineCrc = crc.getValue();
    } catch (IOException exc) {
        logger().log(OpLevel.ERROR, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                "FileStreamStateHandler.file.error"), exc);
    }
}

From source file:nl.nn.adapterframework.compression.ZipWriter.java

public void writeEntryWithCompletedHeader(String filename, Object contents, boolean close, String charset)
        throws CompressionException, IOException {
    if (StringUtils.isEmpty(filename)) {
        throw new CompressionException("filename cannot be empty");
    }//from   w w w. j a v a2 s.c om

    byte[] contentBytes = null;
    BufferedInputStream bis = null;
    long size = 0;
    if (contents != null) {
        if (contents instanceof byte[]) {
            contentBytes = (byte[]) contents;
        } else if (contents instanceof InputStream) {
            contentBytes = Misc.streamToBytes((InputStream) contents);
        } else {
            contentBytes = contents.toString().getBytes(charset);
        }
        bis = new BufferedInputStream(new ByteArrayInputStream(contentBytes));
        size = bis.available();
    } else {
        log.warn("contents of zip entry [" + filename + "] is null");
    }

    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();
    crc.reset();
    if (bis != null) {
        while ((bytesRead = bis.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
        }
        bis.close();
    }
    if (contents != null) {
        bis = new BufferedInputStream(new ByteArrayInputStream(contentBytes));
    }
    ZipEntry entry = new ZipEntry(filename);
    entry.setMethod(ZipEntry.STORED);
    entry.setCompressedSize(size);
    entry.setSize(size);
    entry.setCrc(crc.getValue());
    getZipoutput().putNextEntry(entry);
    if (bis != null) {
        while ((bytesRead = bis.read(buffer)) != -1) {
            getZipoutput().write(buffer, 0, bytesRead);
        }
        bis.close();
    }
    getZipoutput().closeEntry();
}

From source file:org.alfresco.repo.domain.audit.AbstractAuditDAOImpl.java

/**
 * {@inheritDoc}/*  w ww .  jav a2s.c om*/
 */
public Pair<Long, ContentData> getOrCreateAuditModel(URL url) {
    InputStream is = null;
    try {
        is = url.openStream();
        // Calculate the CRC and find an entry that matches
        CRC32 crcCalc = new CRC32();
        byte[] buffer = new byte[1024];
        int read = -1;
        do {
            read = is.read(buffer);
            if (read < 0) {
                break;
            }
            crcCalc.update(buffer, 0, read);
        } while (true);
        long crc = crcCalc.getValue();
        // Find an existing entry
        AuditModelEntity existingEntity = getAuditModelByCrc(crc);
        if (existingEntity != null) {
            Long existingEntityId = existingEntity.getId();
            // Locate the content
            ContentData existingContentData = contentDataDAO.getContentData(existingEntity.getContentDataId())
                    .getSecond();
            Pair<Long, ContentData> result = new Pair<Long, ContentData>(existingEntityId, existingContentData);
            // Done
            if (logger.isDebugEnabled()) {
                logger.debug("Found existing model with same CRC: \n" + "   URL:    " + url + "\n"
                        + "   CRC:    " + crc + "\n" + "   Result: " + result);
            }
            return result;
        } else {
            // Upload the content afresh
            is.close();
            is = url.openStream();
            ContentWriter writer = contentService.getWriter(null, null, false);
            writer.setEncoding("UTF-8");
            writer.setMimetype(MimetypeMap.MIMETYPE_XML);
            writer.putContent(is);
            ContentData newContentData = writer.getContentData();
            Long newContentDataId = contentDataDAO.createContentData(newContentData).getFirst();
            AuditModelEntity newEntity = createAuditModel(newContentDataId, crc);
            Pair<Long, ContentData> result = new Pair<Long, ContentData>(newEntity.getId(), newContentData);
            // Done
            if (logger.isDebugEnabled()) {
                logger.debug("Created new audit model: \n" + "   URL:    " + url + "\n" + "   CRC:    " + crc
                        + "\n" + "   Result: " + result);
            }
            return result;
        }
    } catch (IOException e) {
        throw new AlfrescoRuntimeException("Failed to read Audit model: " + url);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Throwable e) {
            }
        }
    }
}

From source file:org.klco.email2html.OutputWriter.java

/**
 * Writes the attachment contained in the body part to a file.
 * /*from w w w  .  j a v  a2 s .c om*/
 * @param containingMessage
 *            the message this body part is contained within
 * @param part
 *            the part containing the attachment
 * @return the file that was created/written to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws MessagingException
 *             the messaging exception
 */
public boolean writeAttachment(EmailMessage containingMessage, Part part)
        throws IOException, MessagingException {
    log.trace("writeAttachment");

    File attachmentFolder;
    File attachmentFile;
    InputStream in = null;
    OutputStream out = null;
    try {

        attachmentFolder = new File(outputDir.getAbsolutePath() + File.separator + config.getImagesSubDir()
                + File.separator + FILE_DATE_FORMAT.format(containingMessage.getSentDate()));
        if (!attachmentFolder.exists()) {
            log.debug("Creating attachment folder");
            attachmentFolder.mkdirs();
        }

        attachmentFile = new File(attachmentFolder, part.getFileName());
        log.debug("Writing attachment file: {}", attachmentFile.getAbsolutePath());
        if (!attachmentFile.exists()) {
            attachmentFile.createNewFile();
        }

        in = new BufferedInputStream(part.getInputStream());
        out = new BufferedOutputStream(new FileOutputStream(attachmentFile));

        log.debug("Downloading attachment");
        CRC32 checksum = new CRC32();
        for (int b = in.read(); b != -1; b = in.read()) {
            checksum.update(b);
            out.write(b);
        }

        if (this.excludeDuplicates) {
            log.debug("Computing checksum");
            long value = checksum.getValue();
            if (this.attachmentChecksums.contains(value)) {
                log.info("Skipping duplicate attachment: {}", part.getFileName());
                attachmentFile.delete();
                return false;
            } else {
                attachmentChecksums.add(value);
            }
        }

        log.debug("Attachement saved");
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }

    if (part.getContentType().toLowerCase().startsWith("image")) {
        log.debug("Creating renditions");
        String contentType = part.getContentType().substring(0, part.getContentType().indexOf(";"));
        log.debug("Creating renditions of type: " + contentType);

        for (Rendition rendition : renditions) {
            File renditionFile = new File(attachmentFolder, rendition.getName() + "-" + part.getFileName());
            try {
                if (!renditionFile.exists()) {
                    renditionFile.createNewFile();
                }
                log.debug("Creating rendition file: {}", renditionFile.getAbsolutePath());
                createRendition(attachmentFile, renditionFile, rendition);
                log.debug("Rendition created");
            } catch (OutOfMemoryError oome) {
                Runtime rt = Runtime.getRuntime();
                rt.gc();
                log.warn("Ran out of memory creating rendition: " + rendition, oome);

                log.warn("Free Memory: {}", rt.freeMemory());
                log.warn("Max Memory: {}", rt.maxMemory());
                log.warn("Total Memory: {}", rt.totalMemory());

                String[] command = null;
                if (rendition.getFill()) {
                    command = new String[] { "convert", attachmentFile.getAbsolutePath(), "-resize",
                            (rendition.getHeight() * 2) + "x", "-resize",
                            "'x" + (rendition.getHeight() * 2) + "<'", "-resize", "50%", "-gravity", "center",
                            "-crop", rendition.getHeight() + "x" + rendition.getWidth() + "+0+0", "+repage",
                            renditionFile.getAbsolutePath() };
                } else {
                    command = new String[] { "convert", attachmentFile.getAbsolutePath(), "-resize",
                            rendition.getHeight() + "x" + rendition.getWidth(),
                            renditionFile.getAbsolutePath() };

                }
                log.debug("Trying to resize with ImageMagick: " + StringUtils.join(command, " "));

                rt.exec(command);
            } catch (Exception t) {
                log.warn("Exception creating rendition: " + rendition, t);
            }
        }
    }
    return true;
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.DownloadItem.java

public void writeToZip(ZipArchiveOutputStream zipOutput) throws IOException {
    if (contentInputStream == null)
        return;/*from ww w. j a v a  2s  .co m*/
    String[] domainParts = StringUtils.split(uri.getHost(), '.');
    StringBuilder path = new StringBuilder();
    for (int i = domainParts.length - 1; i >= 0; i--) {
        path.append(domainParts[i]);
        path.append('/');
    }
    String[] pathParts = StringUtils.split(uri.getPath(), '/');
    for (int i = 0; i < pathParts.length - 1; i++) {
        if (StringUtils.isEmpty(pathParts[i]))
            continue;
        path.append(pathParts[i]);
        path.append('/');
    }
    if (contentDispositionFilename != null)
        path.append(contentDispositionFilename);
    else {
        String lastPart = pathParts == null || pathParts.length == 0 ? null : pathParts[pathParts.length - 1];
        if (StringUtils.isEmpty(lastPart))
            path.append("index");
        else
            path.append(lastPart);
    }
    if (uri.getPath().endsWith("/"))
        path.append("/_index");
    String query = uri.getQuery();
    String fragment = uri.getFragment();
    if (!StringUtils.isEmpty(query) || !StringUtils.isEmpty(fragment)) {
        CRC32 crc32 = new CRC32();
        if (!StringUtils.isEmpty(query))
            crc32.update(query.getBytes());
        if (!StringUtils.isEmpty(fragment))
            crc32.update(fragment.getBytes());
        path.append('.');
        path.append(crc32.getValue());
    }
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(path.toString());
    zipOutput.putArchiveEntry(zipEntry);
    BufferedInputStream bis = null;
    byte[] buffer = new byte[65536];
    try {
        bis = new BufferedInputStream(contentInputStream);
        int l;
        while ((l = bis.read(buffer)) != -1)
            zipOutput.write(buffer, 0, l);
        zipOutput.closeArchiveEntry();
    } finally {
        IOUtils.close(bis);
    }
}

From source file:net.sourceforge.squirrel_sql.fw.util.IOUtilitiesImpl.java

/**
 * @see net.sourceforge.squirrel_sql.fw.util.IOUtilities#getCheckSum(java.io.File)
 *///from w w  w  . ja  va  2  s .co m
public long getCheckSum(File f) throws IOException {
    CRC32 result = new CRC32();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
        int len = 0;
        byte[] buffer = new byte[DISK_DATA_BUFFER_SIZE];
        while ((len = fis.read(buffer)) != -1) {
            result.update(buffer, 0, len);
        }
    } finally {
        closeInputStream(fis);
    }
    return result.getValue();
}