Search in sources :

Example 1 with ChecksumInputStream

use of com.mucommander.commons.io.ChecksumInputStream in project mucommander by mucommander.

the class SplitFileJob method jobCompleted.

@Override
protected void jobCompleted() {
    // create checksum file
    if (isIntegrityCheckEnabled()) {
        if (origFileStream != null && (origFileStream instanceof ChecksumInputStream)) {
            String crcFileName = sourceFile.getName() + ".sfv";
            try {
                String sourceChecksum;
                if (recalculateCRC) {
                    origFileStream = sourceFile.getInputStream();
                    sourceChecksum = AbstractFile.calculateChecksum(origFileStream, MessageDigest.getInstance("CRC32"));
                    origFileStream.close();
                } else {
                    sourceChecksum = ((ChecksumInputStream) origFileStream).getChecksumString();
                }
                AbstractFile crcFile = baseDestFolder.getDirectChild(crcFileName);
                OutputStream crcStream = crcFile.getOutputStream();
                String line = sourceFile.getName() + " " + sourceChecksum;
                crcStream.write(line.getBytes("utf-8"));
                crcStream.close();
            } catch (Exception e) {
                LOGGER.debug("Caught exception", e);
                showErrorDialog(errorDialogTitle, Translator.get("error_while_transferring", crcFileName), Arrays.asList(FileJobAction.CANCEL));
            }
        }
    }
    super.jobCompleted();
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) ChecksumInputStream(com.mucommander.commons.io.ChecksumInputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) FileTransferException(com.mucommander.commons.io.FileTransferException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with ChecksumInputStream

use of com.mucommander.commons.io.ChecksumInputStream in project mucommander by mucommander.

the class TransferFileJob method copyFile.

/**
 * Copies the given source file to the specified destination file, optionally resuming the operation.
 * As much as the source and destination protocols allow, the source file's date and permissions will be preserved.
 */
protected void copyFile(AbstractFile sourceFile, AbstractFile destFile, boolean append) throws FileTransferException {
    // Reset this field in case it was set to true for the previous file
    isCheckingIntegrity = false;
    // Throw a specific FileTransferException if source and destination files are identical
    if (sourceFile.equalsCanonical(destFile))
        throw new FileTransferException(FileTransferError.SOURCE_AND_DESTINATION_IDENTICAL);
    // Determine whether or not AbstractFile.copyRemotelyTo() should be used to copy the file.
    // Some file protocols do not provide a getOutputStream() method and require the use of copyRemotelyTo(). Some other
    // may also offer server to server copy which is more efficient than stream copy.
    boolean copied = false;
    if (sourceFile.isFileOperationSupported(FileOperation.COPY_REMOTELY)) {
        try {
            sourceFile.copyRemotelyTo(destFile);
            copied = true;
        } catch (IOException e) {
        // The file will be copied manually
        }
    }
    // If the file wasn't copied using copyRemotelyTo(), or if copyRemotelyTo() failed
    InputStream in = null;
    if (!copied) {
        // Copy source file stream to destination file
        try {
            long inLength = sourceFile.getSize();
            // Try to open InputStream
            try {
                long destFileSize = destFile.getSize();
                if (append && destFileSize != -1) {
                    in = sourceFile.getInputStream(destFileSize);
                    // Do not calculate checksum, as it needs to be calculated on the whole file
                    inLength -= destFileSize;
                    // Increase current file ByteCounter by the number of bytes skipped
                    currentFileByteCounter.add(destFileSize);
                    // Increase skipped ByteCounter by the number of bytes skipped
                    currentFileSkippedByteCounter.add(destFileSize);
                } else {
                    in = sourceFile.getInputStream();
                    if (integrityCheckEnabled)
                        in = new ChecksumInputStream(in, MessageDigest.getInstance(CHECKSUM_VERIFICATION_ALGORITHM));
                }
                setCurrentInputStream(in);
            } catch (Exception e) {
                LOGGER.debug("IOException caught, throwing FileTransferException", e);
                throw new FileTransferException(FileTransferError.OPENING_SOURCE);
            }
            // Copy source stream to destination file
            destFile.copyStream(tlin, append, inLength);
        } finally {
            // This block will always be executed, even if an exception
            // was thrown in the catch block
            // Tries to close the streams no matter what happened before
            closeCurrentInputStream();
        }
    }
    // Preserve source file's date
    tryCopyFileDate(sourceFile, destFile);
    // Preserve source file's permissions: preserve only the permissions bits that are supported by the source file
    // and use default permissions for the rest of them.
    tryCopyFilePermissions(sourceFile, destFile);
    // Under Mac OS X only, preserving the file type and creator
    DesktopManager.postCopy(sourceFile, destFile);
    // This block is executed only if integrity check has been enabled (disabled by default)
    if (integrityCheckEnabled) {
        String sourceChecksum;
        String destinationChecksum;
        // Indicate that integrity is being checked, the value is reset when the next file starts
        isCheckingIntegrity = true;
        if (in != null && (in instanceof ChecksumInputStream)) {
            // The file was copied with a ChecksumInputStream, the checksum is already calculated, simply
            // retrieve it
            sourceChecksum = ((ChecksumInputStream) in).getChecksumString();
        } else {
            // we have to calculate the source file's checksum from scratch.
            try {
                sourceChecksum = calculateChecksum(sourceFile);
            } catch (Exception e) {
                throw new FileTransferException(FileTransferError.READING_SOURCE);
            }
        }
        LOGGER.debug("Source checksum= " + sourceChecksum);
        // Calculate the destination file's checksum
        try {
            destinationChecksum = calculateChecksum(destFile);
        } catch (Exception e) {
            throw new FileTransferException(FileTransferError.READING_DESTINATION);
        }
        LOGGER.debug("Destination checksum= " + destinationChecksum);
        // Compare both checksums and throw an exception if they don't match
        if (!sourceChecksum.equals(destinationChecksum)) {
            throw new FileTransferException(FileTransferError.CHECKSUM_MISMATCH);
        }
    }
}
Also used : FileTransferException(com.mucommander.commons.io.FileTransferException) CounterInputStream(com.mucommander.commons.io.CounterInputStream) ThroughputLimitInputStream(com.mucommander.commons.io.ThroughputLimitInputStream) ChecksumInputStream(com.mucommander.commons.io.ChecksumInputStream) InputStream(java.io.InputStream) ChecksumInputStream(com.mucommander.commons.io.ChecksumInputStream) IOException(java.io.IOException) IOException(java.io.IOException) FileTransferException(com.mucommander.commons.io.FileTransferException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ChecksumInputStream (com.mucommander.commons.io.ChecksumInputStream)2 FileTransferException (com.mucommander.commons.io.FileTransferException)2 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 AbstractFile (com.mucommander.commons.file.AbstractFile)1 CounterInputStream (com.mucommander.commons.io.CounterInputStream)1 ThroughputLimitInputStream (com.mucommander.commons.io.ThroughputLimitInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1