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:com.l2jserver.service.core.logging.TruncateToZipFileAppender.java

/**
 * This method creates archive with file instead of deleting it.
 * /*  w  ww .  j av a2s .c  o  m*/
 * @param file
 *            file to truncate
 */
protected void truncate(File file) {
    LogLog.debug("Compression of file: " + file.getAbsolutePath() + " started.");

    // Linux systems doesn't provide file creation time, so we have to hope
    // that log files
    // were not modified manually after server starup
    // We can use here Windowns-only solution but that suck :(
    if (FileUtils.isFileOlder(file, ManagementFactory.getRuntimeMXBean().getStartTime())) {
        File backupRoot = new File(getBackupDir());
        if (!backupRoot.exists() && !backupRoot.mkdirs()) {
            throw new Error("Can't create backup dir for backup storage");
        }

        SimpleDateFormat df;
        try {
            df = new SimpleDateFormat(getBackupDateFormat());
        } catch (Exception e) {
            throw new Error("Invalid date formate for backup files: " + getBackupDateFormat(), e);
        }
        String date = df.format(new Date(file.lastModified()));

        File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip");

        ZipOutputStream zos = null;
        FileInputStream fis = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            ZipEntry entry = new ZipEntry(file.getName());
            entry.setMethod(ZipEntry.DEFLATED);
            entry.setCrc(FileUtils.checksumCRC32(file));
            zos.putNextEntry(entry);
            fis = FileUtils.openInputStream(file);

            byte[] buffer = new byte[1024];
            int readed;
            while ((readed = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, readed);
            }

        } catch (Exception e) {
            throw new Error("Can't create zip file", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    // not critical error
                    LogLog.warn("Can't close zip file", e);
                }
            }

            if (fis != null) {
                try {
                    // not critical error
                    fis.close();
                } catch (IOException e) {
                    LogLog.warn("Can't close zipped file", e);
                }
            }
        }

        if (!file.delete()) {
            throw new Error("Can't delete old log file " + file.getAbsolutePath());
        }
    }
}

From source file:com.bbytes.jfilesync.sync.FileMonitor.java

private synchronized void fileModified(File file, FileMessageType fileMessageType, boolean isDirectory) {
    try {//from   w  w w. ja  v a2  s.  c om

        File srcFolder = new File(sourceFolderToMonitor);

        String filePath = file.getPath();
        filePath = filePath.replace(srcFolder.getPath().toString(), "");

        int index = filePath.lastIndexOf(File.separator);
        String filename = null;
        String relativePath = null;
        if (!isDirectory) {
            filename = filePath.substring(index + 1);
            relativePath = filePath.replace(filename, "");
        } else {
            relativePath = filePath;
        }

        FileSyncMessage fileSyncMessage = new FileSyncMessage(fileMessageType, file.getName());
        fileSyncMessage.setBaseFolderRelativePath(relativePath);
        fileSyncMessage.setDirectory(isDirectory);
        fileSyncMessage.setOriginalFilePath(file.getPath());

        if (!file.isDirectory() && !fileMessageType.equals(FileMessageType.FILE_DELETED) && file.exists()
                && file.canRead())
            fileSyncMessage.setChecksum(FileUtils.checksumCRC32(file));

        fileSyncChannel.send(new Message(null, null, fileSyncMessage));
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}

From source file:com.aionemu.commons.log4j.appenders.TruncateToZipFileAppender.java

/**
 * This method creates archive with file instead of deleting it.
 *
 * @param file file to truncate/*w  w w. j a v a 2  s  .  c o m*/
 */
protected void truncate(File file) {
    LogLog.debug("Compression of file: " + file.getAbsolutePath() + " started.");

    // Linux systems doesn't provide file creation time, so we have to hope
    // that log files
    // were not modified manually after server starup
    // We can use here Windowns-only solution but that suck :(
    if (FileUtils.isFileOlder(file, ManagementFactory.getRuntimeMXBean().getStartTime())) {
        File backupRoot = new File(getBackupDir());
        if (!backupRoot.exists() && !backupRoot.mkdirs()) {
            throw new AppenderInitializationError("Can't create backup dir for backup storage");
        }

        SimpleDateFormat df;
        try {
            df = new SimpleDateFormat(getBackupDateFormat());
        } catch (Exception e) {
            throw new AppenderInitializationError(
                    "Invalid date formate for backup files: " + getBackupDateFormat(), e);
        }
        String date = df.format(new Date(file.lastModified()));

        File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip");

        ZipOutputStream zos = null;
        FileInputStream fis = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            ZipEntry entry = new ZipEntry(file.getName());
            entry.setMethod(ZipEntry.DEFLATED);
            entry.setCrc(FileUtils.checksumCRC32(file));
            zos.putNextEntry(entry);
            fis = FileUtils.openInputStream(file);

            byte[] buffer = new byte[1024];
            int readed;
            while ((readed = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, readed);
            }

        } catch (Exception e) {
            throw new AppenderInitializationError("Can't create zip file", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    // not critical error
                    LogLog.warn("Can't close zip file", e);
                }
            }

            if (fis != null) {
                try {
                    // not critical error
                    fis.close();
                } catch (IOException e) {
                    LogLog.warn("Can't close zipped file", e);
                }
            }
        }

        if (!file.delete()) {
            throw new AppenderInitializationError("Can't delete old log file " + file.getAbsolutePath());
        }
    }
}

From source file:at.tfr.securefs.client.SecurefsClient.java

public void run() {
    DateTime start = new DateTime();
    try (FileSystem fs = FileSystems.newFileSystem(new URI(baseDir), null)) {

        for (Path path : files) {

            Path sec = fs.getPath(path.toString() + (asyncTest ? "." + Thread.currentThread().getId() : ""));

            if (write) {
                if (!path.toFile().exists()) {
                    System.err.println(Thread.currentThread() + ": NoSuchFile: " + path + " currentWorkdir="
                            + Paths.get("./").toAbsolutePath());
                    continue;
                }//from  w w  w  . j a  va 2 s .  c  o  m

                if (path.getParent() != null) {
                    fs.provider().createDirectory(fs.getPath(path.getParent().toString()));
                }
                final OutputStream secOs = Files.newOutputStream(sec);

                System.out.println(Thread.currentThread() + ": Sending file: " + start + " : " + sec);

                IOUtils.copyLarge(Files.newInputStream(path), secOs, new byte[128 * 1024]);
                secOs.close();
            }

            Path out = path.resolveSibling(
                    path.getFileName() + (asyncTest ? "." + Thread.currentThread().getId() : "") + ".out");

            if (read) {
                System.out.println(Thread.currentThread() + ": Reading file: " + new DateTime() + " : " + out);
                if (out.getParent() != null) {
                    Files.createDirectories(out.getParent());
                }

                final InputStream secIs = Files.newInputStream(sec);
                IOUtils.copyLarge(secIs, Files.newOutputStream(out), new byte[128 * 1024]);
                secIs.close();
            }

            if (write && read) {
                long inputChk = FileUtils.checksumCRC32(path.toFile());
                long outputChk = FileUtils.checksumCRC32(out.toFile());

                if (inputChk != outputChk) {
                    throw new IOException(Thread.currentThread()
                            + ": Checksum Failed: failure to write/read: in=" + path + ", out=" + out);
                }
                System.out.println(Thread.currentThread() + ": Checked Checksums: " + new DateTime() + " : "
                        + inputChk + " / " + outputChk);
            }

            if (delete) {
                boolean deleted = fs.provider().deleteIfExists(sec);
                if (!deleted) {
                    throw new IOException(
                            Thread.currentThread() + ": Delete Failed: failure to delete: in=" + path);
                } else {
                    System.out.println(Thread.currentThread() + ": Deleted File: in=" + path);
                }
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    }
}

From source file:at.tfr.securefs.client.SecurefsFileServiceClient.java

public void run() {
    DateTime start = new DateTime();
    try {/*from www. j  av a  2  s. co  m*/
        FileService svc = new FileService(new URL(fileServiceUrl));
        BindingProvider binding = (BindingProvider) svc.getFileServicePort();
        binding.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
        binding.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);

        for (Path path : files) {

            if (write) {
                if (!path.toFile().exists()) {
                    System.err.println(Thread.currentThread() + ": NoSuchFile: " + path + " currentWorkdir="
                            + Paths.get("./").toAbsolutePath());
                    continue;
                }

                System.out.println(Thread.currentThread() + ": Sending file: " + start + " : " + path);
                svc.getFileServicePort().write(path.toString(),
                        IOUtils.toByteArray(Files.newInputStream(path)));
            }

            Path out = path.resolveSibling(
                    path.getFileName() + (asyncTest ? "." + Thread.currentThread().getId() : "") + ".out");

            if (read) {
                System.out.println(Thread.currentThread() + ": Reading file: " + new DateTime() + " : " + out);
                if (out.getParent() != null) {
                    Files.createDirectories(out.getParent());
                }

                byte[] arr = svc.getFileServicePort().read(path.toString());
                IOUtils.write(arr, Files.newOutputStream(out));
            }

            if (write && read) {
                long inputChk = FileUtils.checksumCRC32(path.toFile());
                long outputChk = FileUtils.checksumCRC32(out.toFile());
                if (inputChk != outputChk) {
                    throw new IOException(Thread.currentThread()
                            + ": Checksum Failed: failure to write/read: in=" + path + ", out=" + out);
                }
                System.out.println(Thread.currentThread() + ": Checked Checksums: " + new DateTime() + " : "
                        + inputChk + " / " + outputChk);
            }

            if (delete) {
                boolean deleted = svc.getFileServicePort().delete(path.toString());
                if (!deleted) {
                    throw new IOException(
                            Thread.currentThread() + ": Delete Failed: failure to delete: in=" + path);
                } else {
                    System.out.println(Thread.currentThread() + ": Deleted File: in=" + path);
                }
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    }
}

From source file:com.pieframework.runtime.utils.ArtifactManager.java

private void copy(String download, String filter, String localPath, String virtualPath) {

    /*System.out.println("download:"+download+
             "\nfilter:"+filter+/*from   ww  w.j  av a2  s  .c  o  m*/
             "\nlocalPath:"+localPath+
             "\nvirtualPath:"+virtualPath);
    */
    boolean match = true;
    if (!StringUtils.empty(filter)) {
        match = FilenameUtils.wildcardMatch(FilenameUtils.separatorsToSystem(download), filter);
    }

    if (match) {

        File destinationVirtualFile = new File(FilenameUtils.separatorsToSystem(virtualPath));
        File destination = new File(localPath + destinationVirtualFile.getParent());
        destination.mkdirs();

        if (destination.exists()) {
            File downloadFile = new File(download);
            try {
                FileUtils.copyFileToDirectory(downloadFile, destination);
                File copiedFile = new File(
                        destination.getPath() + File.separatorChar + destinationVirtualFile.getName());
                if (copiedFile.exists()) {
                    System.out.println(
                            "file copied:" + copiedFile + " crc:" + FileUtils.checksumCRC32(copiedFile));
                } else {
                    //TODO error
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            //TODO error
        }

    }
}

From source file:mase.evorbc.KdTreeRepertoire.java

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();//from   w  ww  .  ja  v  a 2s .  c o m
    long cr = FileUtils.checksumCRC32(repFile);
    if (repFileHash != cr) {
        throw new IOException("Repertoire file does not match the expected file");
    }
    if (coordsFile != null) {
        long cc = FileUtils.checksumCRC32(coordsFile);
        if (coordsFileHash != cc) {
            throw new IOException("Coordinates file does not match the expected file");
        }
    }
    load(repFile, coordsFile);
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditingTestsUtils.java

static long checksum(File file) {
    try {/*w w w.ja  v  a 2s.c om*/
        return FileUtils.checksumCRC32(file);
    } catch (Exception exception) {
        throw new AuditingException(exception, "Error computing checksum of file '%s'.",
                file.getAbsolutePath());
    }
}

From source file:de.extra.client.core.model.inputdata.impl.SingleFileInputData.java

@Override
public String getHashCode() {
    String checkSumCRC32String = null;
    try {//www  .j  av a 2  s. com
        final long checkSumCRC32 = FileUtils.checksumCRC32(inputDataFile);
        checkSumCRC32String = String.valueOf(checkSumCRC32);
    } catch (final IOException ioException) {
        throw new ExtraDataPluginRuntimeException(ioException);
    }

    return checkSumCRC32String;
}

From source file:algorithm.PDFFileAttacherTest.java

@Test
public void pdfMultipleFileAttacherTest() {
    try {/*from  w ww  .ja v a  2  s .c o m*/
        File carrier = TestDataProvider.PDF_FILE;
        File payload1 = TestDataProvider.TXT_FILE;
        File payload2 = TestDataProvider.TXT_FILE_2;
        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload1);
        payloadList.add(payload2);

        PDFFileAttacher algorithm = new PDFFileAttacher();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);
        assertTrue(outputFile.length() > carrier.length());
        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(3, outputHash.size());
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload1 = outputHash.get(payload1.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload1);
        assertNotNull(restoredPayload2);
        // assertEquals(FileUtils.checksumCRC32(carrier),
        // FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload1), FileUtils.checksumCRC32(restoredPayload1));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        assertEquals(algorithm, restoredCarrier.algorithm);
        // assertTrue(restoredCarrier.checksumValid);
        // assertTrue(restoredPayload1.checksumValid);
        // assertTrue(restoredPayload2.checksumValid);
        assertTrue(restoredCarrier.wasCarrier);
        assertFalse(restoredCarrier.wasPayload);
        assertTrue(restoredPayload1.wasPayload);
        assertFalse(restoredPayload1.wasCarrier);
        assertTrue(restoredPayload2.wasPayload);
        assertFalse(restoredPayload2.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload1));
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredPayload1.relatedFiles.contains(restoredPayload1));
    } catch (IOException e) {
        e.printStackTrace();
    }
}