Example usage for org.apache.commons.io FileUtils checksumCRC32

List of usage examples for org.apache.commons.io FileUtils checksumCRC32

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils checksumCRC32.

Prototype

public static long checksumCRC32(File file) throws IOException 

Source Link

Document

Computes the checksum of a file using the CRC32 checksum routine.

Usage

From source file:org.jumpmind.symmetric.model.FileSnapshot.java

public FileSnapshot(FileTriggerRouter fileTriggerRouter, File file, LastEventType lastEventType) {
    boolean isDelete = lastEventType == LastEventType.DELETE;
    this.triggerId = fileTriggerRouter.getFileTrigger().getTriggerId();
    this.channelId = fileTriggerRouter.getFileTrigger().getChannelId();
    this.reloadChannelId = fileTriggerRouter.getFileTrigger().getReloadChannelId();
    this.routerId = fileTriggerRouter.getRouter().getRouterId();
    this.lastEventType = lastEventType;
    this.lastUpdateTime = new Date();
    this.fileName = file.getName();
    this.relativeDir = file.getPath().replace('\\', '/');
    String baseDir = fileTriggerRouter.getFileTrigger().getBaseDir().replace('\\', '/');
    if (this.relativeDir.startsWith(baseDir)) {
        this.relativeDir = this.relativeDir.substring(baseDir.length());
    }/*from  w w  w  .  j  a va 2  s  .c  o  m*/

    if (this.relativeDir.endsWith(fileName)) {
        this.relativeDir = this.relativeDir.substring(0, this.relativeDir.lastIndexOf(fileName));
    }

    if (this.relativeDir.startsWith("/")) {
        this.relativeDir = this.relativeDir.substring(1);
    }

    if (this.relativeDir.endsWith("/")) {
        this.relativeDir = this.relativeDir.substring(0, this.relativeDir.length() - 1);
    }

    if (StringUtils.isBlank(relativeDir)) {
        this.relativeDir = ".";
    }

    this.fileSize = isDelete ? 0 : file.length();
    this.fileModifiedTime = isDelete ? 0 : file.lastModified();

    if (file.isFile() && !isDelete) {
        try {
            this.crc32Checksum = FileUtils.checksumCRC32(file);
        } catch (FileNotFoundException ex) {
            this.lastEventType = LastEventType.DELETE;
            this.fileSize = 0;
            this.fileModifiedTime = 0;
        } catch (IOException ex) {
            throw new IoException(ex);
        }
    } else {
        this.crc32Checksum = -1;
    }

}

From source file:org.lobid.lodmill.LobidOrganisationsUpdateTest.java

@Test
public void testPicaXmlSplits() throws URISyntaxException, IOException {

    final FileOpener opener = new FileOpener();
    final XmlDecoder xmlDecoder = new XmlDecoder();
    XmlTee tee = new XmlTee();
    xmlDecoder.setReceiver(tee);/*from  w w  w  .  ja  va  2 s.c om*/

    final XmlEntitySplitter xmlSplitter = new XmlEntitySplitter();
    xmlSplitter.setEntityName("metadata");
    XmlFilenameWriter xmlFilenameWriter = createXmlFilenameWriter(PATH);
    xmlSplitter.setReceiver(xmlFilenameWriter);
    tee.addReceiver(xmlSplitter);

    final PicaXmlHandler handler = new PicaXmlHandler();
    final Metamorph metamorph = new Metamorph("morph_zdb-isil-file-pica2ld.xml");
    final PipeLobidOrganisationEnrichment enrich = createEnricher();
    final Triples2RdfModel triple2model = new Triples2RdfModel();
    triple2model.setInput("TURTLE");
    final RdfModelFileWriter writer = createWriter(PATH);
    handler.setReceiver(metamorph).setReceiver(enrich).setReceiver(triple2model).setReceiver(writer);
    tee.addReceiver(handler);

    opener.setReceiver(xmlDecoder).setReceiver(tee);
    File infile = new File(
            Thread.currentThread().getContextClassLoader().getResource("Bibdat1303pp_sample1.xml").toURI());
    opener.process(infile.getAbsolutePath());
    opener.closeStream();
    assertEquals(Long.parseLong("2748079330"),
            FileUtils.checksumCRC32(new File(PATH + File.separator + "DE" + File.separator + "DE-Tir1.xml")));
    deleteTestFiles();
}

From source file:org.ngrinder.jnlp.impl.JNLPLoaderImpl.java

@Override
public List<File> resolveRemoteJars(File jnlpLibPath) {

    List<File> fileString = new ArrayList<File>();

    JNLPClassLoader jnlpClassLoader = (JNLPClassLoader) localClassLoader;
    try {//  w ww  . j av  a2s.  c  o m
        URL[] urls = jnlpClassLoader.getURLs();

        for (URL each : urls) {
            String jarName = FilenameUtils.getName(each.toString());
            JarFile jar = jnlpClassLoader.getJarFile(each);

            String jarLocalPath = jar.getName();
            File srcFile = new File(jarLocalPath);
            long srcFIleStamp = FileUtils.checksumCRC32(srcFile);
            File desFile = new File(jnlpLibPath, jarName);
            if (!desFile.exists() || (FileUtils.checksumCRC32(desFile) != srcFIleStamp)) {
                FileUtils.copyFile(srcFile, desFile);
            }
            if (jarName.equals("native.jar")) {
                CompressionUtil.unjar(desFile, jnlpLibPath.getAbsolutePath());
            }
            fileString.add(desFile);
        }
    } catch (IOException e) {
        LOG.error("Resolving remote jars error: {}", e.getMessage());
    }
    return fileString;
}

From source file:org.opendatakit.briefcase.reused.UncheckedFiles.java

public static long checksumOf(Path file) {
    try {//ww w .java2  s.c  o  m
        return FileUtils.checksumCRC32(file.toFile());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.opendatakit.briefcase.util.ExportToCsv.java

private boolean processInstance(File instanceDir) {
    File submission = new File(instanceDir, "submission.xml");
    if (!submission.exists() || !submission.isFile()) {
        EventBus.publish(new ExportProgressEvent(
                "Submission not found for instance directory: " + instanceDir.getPath()));
        return false;
    }/*  w  w  w  .  ja  v  a2 s. co m*/
    EventBus.publish(new ExportProgressEvent("Processing instance: " + instanceDir.getName()));

    // If we are encrypted, be sure the temporary directory
    // that will hold the unencrypted files is created and empty.
    // If we aren't encrypted, the temporary directory
    // is the same as the instance directory.

    File unEncryptedDir;
    if (briefcaseLfd.isFileEncryptedForm()) {
        // create or clean-up the temp directory that will hold the unencrypted
        // files. Do this in the outputDir so that the briefcase storage location
        // can be a read-only network mount. issue 676.
        unEncryptedDir = new File(outputDir, ".temp");

        if (unEncryptedDir.exists()) {
            // silently delete it...
            try {
                FileUtils.deleteDirectory(unEncryptedDir);
            } catch (IOException e) {
                e.printStackTrace();
                EventBus.publish(new ExportProgressEvent(
                        "Unable to delete stale temp directory: " + unEncryptedDir.getAbsolutePath()));
                return false;
            }
        }

        if (!unEncryptedDir.mkdirs()) {
            EventBus.publish(new ExportProgressEvent(
                    "Unable to create temp directory: " + unEncryptedDir.getAbsolutePath()));
            return false;
        }
    } else {
        unEncryptedDir = instanceDir;
    }

    // parse the xml document (this is the manifest if encrypted)...
    Document doc;
    boolean isValidated = false;

    try {
        doc = XmlManipulationUtils.parseXml(submission);
    } catch (ParsingException e) {
        e.printStackTrace();
        EventBus.publish(new ExportProgressEvent(
                "Error parsing submission " + instanceDir.getName() + " Cause: " + e.toString()));
        return false;
    } catch (FileSystemException e) {
        e.printStackTrace();
        EventBus.publish(new ExportProgressEvent(
                "Error parsing submission " + instanceDir.getName() + " Cause: " + e.toString()));
        return false;
    }

    String submissionDate = null;
    // extract the submissionDate, if present, from the attributes
    // of the root element of the submission or submission manifest (if encrypted).
    submissionDate = doc.getRootElement().getAttributeValue(null, "submissionDate");
    if (submissionDate == null || submissionDate.length() == 0) {
        submissionDate = null;
    } else {
        Date theDate = WebUtils.parseDate(submissionDate);
        DateFormat formatter = DateFormat.getDateTimeInstance();
        submissionDate = formatter.format(theDate);

        // just return true to skip records out of range
        if (startDate != null && theDate.before(startDate)) {
            log.info("Submission date is before specified, skipping: " + instanceDir.getName());
            return true;
        }
        if (endDate != null && theDate.after(endDate)) {
            log.info("Submission date is after specified, skipping: " + instanceDir.getName());
            return true;
        }
        // don't export records without dates if either date is set
        if ((startDate != null || endDate != null) && submissionDate == null) {
            log.info("No submission date found, skipping: " + instanceDir.getName());
            return true;
        }
    }

    // Beyond this point, we need to have a finally block that
    // will clean up any decrypted files whenever there is any
    // failure.
    try {

        if (briefcaseLfd.isFileEncryptedForm()) {
            // Decrypt the form and all its media files into the
            // unEncryptedDir and validate the contents of all
            // those files.
            // NOTE: this changes the value of 'doc'
            try {
                FileSystemUtils.DecryptOutcome outcome = FileSystemUtils.decryptAndValidateSubmission(doc,
                        briefcaseLfd.getPrivateKey(), instanceDir, unEncryptedDir);
                doc = outcome.submission;
                isValidated = outcome.isValidated;
            } catch (ParsingException e) {
                e.printStackTrace();
                EventBus.publish(new ExportProgressEvent(
                        "Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString()));
                return false;
            } catch (FileSystemException e) {
                e.printStackTrace();
                EventBus.publish(new ExportProgressEvent(
                        "Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString()));
                return false;
            } catch (CryptoException e) {
                e.printStackTrace();
                EventBus.publish(new ExportProgressEvent(
                        "Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString()));
                return false;
            }
        }

        String instanceId = null;
        String base64EncryptedFieldKey = null;
        // find an instanceId to use...
        try {
            FormInstanceMetadata sim = XmlManipulationUtils.getFormInstanceMetadata(doc.getRootElement());
            instanceId = sim.instanceId;
            base64EncryptedFieldKey = sim.base64EncryptedFieldKey;
        } catch (ParsingException e) {
            e.printStackTrace();
            EventBus.publish(new ExportProgressEvent("Could not extract metadata from submission: "
                    + submission.getAbsolutePath() + " Cause: " + e.toString()));
            return false;
        }

        if (instanceId == null || instanceId.length() == 0) {
            // if we have no instanceID, and there isn't any in the file,
            // use the checksum as the id.
            // NOTE: encrypted submissions always have instanceIDs.
            // This is for legacy non-OpenRosa forms.
            long checksum;
            try {
                checksum = FileUtils.checksumCRC32(submission);
            } catch (IOException e1) {
                e1.printStackTrace();
                EventBus.publish(new ExportProgressEvent("Failed during computing of crc: " + e1.getMessage()));
                return false;
            }
            instanceId = "crc32:" + Long.toString(checksum);
        }

        if (terminationFuture.isCancelled()) {
            EventBus.publish(new ExportProgressEvent("ABORTED"));
            return false;
        }

        EncryptionInformation ei = null;
        if (base64EncryptedFieldKey != null) {
            try {
                ei = new EncryptionInformation(base64EncryptedFieldKey, instanceId,
                        briefcaseLfd.getPrivateKey());
            } catch (CryptoException e) {
                e.printStackTrace();
                EventBus.publish(new ExportProgressEvent("Error establishing field decryption for submission "
                        + instanceDir.getName() + " Cause: " + e.toString()));
                return false;
            }
        }

        // emit the csv record...
        try {
            OutputStreamWriter osw = fileMap.get(briefcaseLfd.getSubmissionElement());

            emitString(osw, true, submissionDate);
            emitSubmissionCsv(osw, ei, doc.getRootElement(), briefcaseLfd.getSubmissionElement(),
                    briefcaseLfd.getSubmissionElement(), false, instanceId, unEncryptedDir);
            emitString(osw, false, instanceId);
            if (briefcaseLfd.isFileEncryptedForm()) {
                emitString(osw, false, Boolean.toString(isValidated));
                if (!isValidated) {
                    EventBus.publish(new ExportProgressEvent("Decrypted submission " + instanceDir.getName()
                            + " may be missing attachments and could not be validated."));
                }
            }
            osw.append("\n");
            return true;

        } catch (IOException e) {
            e.printStackTrace();
            EventBus.publish(new ExportProgressEvent("Failed writing csv: " + e.getMessage()));
            return false;
        }
    } finally {
        if (briefcaseLfd.isFileEncryptedForm()) {
            // destroy the temp directory and its contents...
            try {
                FileUtils.deleteDirectory(unEncryptedDir);
            } catch (IOException e) {
                e.printStackTrace();
                EventBus.publish(
                        new ExportProgressEvent("Unable to remove decrypted files: " + e.getMessage()));
                return false;
            }
        }
    }
}

From source file:org.opendatakit.briefcase.util.XmlManipulationUtils.java

public static Document parseXml(File submission) throws ParsingException, FileSystemException {

    // parse the xml document...
    Document doc = null;/*  w w w.  j av a  2s.  c  om*/
    try {
        InputStream is = null;
        InputStreamReader isr = null;
        try {
            is = new FileInputStream(submission);
            isr = new InputStreamReader(is, UTF_8);
            Document tempDoc = new Document();
            KXmlParser parser = new KXmlParser();
            parser.setInput(isr);
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
            tempDoc.parse(parser);
            isr.close();
            doc = tempDoc;
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (Exception e) {
                    // no-op
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // no-op
                }
            }
        }
    } catch (XmlPullParserException e) {
        try {
            return BadXMLFixer.fixBadXML(submission);
        } catch (CannotFixXMLException e1) {
            File debugFileLocation = new File(FileSystemUtils.getBriefcaseFolder(), "debug");
            try {
                if (!debugFileLocation.exists()) {
                    FileUtils.forceMkdir(debugFileLocation);
                }
                long checksum = FileUtils.checksumCRC32(submission);
                File debugFile = new File(debugFileLocation, "submission-" + checksum + ".xml");
                FileUtils.copyFile(submission, debugFile);
            } catch (IOException e2) {
                throw new RuntimeException(e2);
            }
            throw new ParsingException("Failed during parsing of submission Xml: " + e.toString());
        }
    } catch (IOException e) {
        throw new FileSystemException("Failed while reading submission xml: " + e.toString());
    }
    return doc;
}

From source file:org.silverpeas.core.util.PdfUtilTest.java

@Test
public void testStamp() throws Exception {
    File pdfSource = getTestFile(PDF_FILE_NAME);
    long checksumSource = FileUtils.checksumCRC32(pdfSource);
    File stamp = getTestFile(STAMP_FILE_NAME);
    File pdfDestination = FileUtils.getFile(rootTempPath, RESULT_PDF_FILE_NAME);
    assertThat(pdfDestination.exists(), is(false));
    PdfUtil.stamp(pdfSource, stamp, pdfDestination);
    assertThat(FileUtils.checksumCRC32(pdfSource), is(checksumSource));
    assertThat(pdfDestination.exists(), is(true));
    assertThat(pdfDestination.length(), greaterThan(pdfSource.length()));
}

From source file:org.silverpeas.core.util.PdfUtilTest.java

@Test
public void testWatermark() throws Exception {
    File pdfSource = getTestFile(PDF_FILE_NAME);
    long checksumSource = FileUtils.checksumCRC32(pdfSource);
    File watermark = getTestFile(WATERMARK_FILE_NAME);
    File pdfDestination = FileUtils.getFile(rootTempPath, RESULT_PDF_FILE_NAME);
    assertThat(pdfDestination.exists(), is(false));
    PdfUtil.watermark(pdfSource, watermark, pdfDestination);
    assertThat(FileUtils.checksumCRC32(pdfSource), is(checksumSource));
    assertThat(pdfDestination.exists(), is(true));
    assertThat(pdfDestination.length(), greaterThan(pdfSource.length()));
}

From source file:org.silverpeas.core.web.http.FileResponse.java

/**
 * Gets the file identifier//from w ww.jav a  2s .  c  om
 * @param path the path to the physical content.
 * @return the file identifier.
 * @throws IOException if the file is not readable.
 */
String getFileIdentifier(final Path path) throws IOException {
    if (isDefined(forcedFileId)) {
        return forcedFileId;
    }
    final File file = path.toFile();
    final String sb = String.valueOf(FileUtils.checksumCRC32(file)) + "|" + file.getName() + "|" + file.length()
            + "|" + file.lastModified();
    return Base64.getEncoder().encodeToString(sb.getBytes());
}

From source file:org.sipfoundry.sipxconfig.cert.JavaKeyStoreTest.java

@Test
public void storeIfDifferent() throws IOException {
    JavaKeyStore a = new JavaKeyStore();
    String cert = IOUtils.toString(getClass().getResourceAsStream("test.crt"));
    String key = IOUtils.toString(getClass().getResourceAsStream("test.key"));
    a.addKey("test", cert, key);
    File f = new File(TestHelper.getTestDirectory(), "store-if-different-test.keystore");
    if (f.exists()) {
        f.delete();//w ww. j av  a  2s .  c  o m
    }

    a.storeIfDifferent(f);
    long checksumBefore = FileUtils.checksumCRC32(f);
    a.storeIfDifferent(f);
    long checksumAfter = FileUtils.checksumCRC32(f);
    assertEquals(checksumBefore, checksumAfter);
    a.addKey("test2", cert, key);
    a.storeIfDifferent(f);
    long checksumAfterChange = FileUtils.checksumCRC32(f);
    assertNotSame(checksumBefore, checksumAfterChange);
}