Search in sources :

Example 6 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project ats-framework by Axway.

the class MimePackage method getPartChecksum.

/**
     * Return the CRC checksum of a given part
     *
     * @param partIndex
     *            the index of the part
     * @param isAttachment
     *            true if the part is an attachment
     * @return the part checksum
     * @throws PackageException
     */
@PublicAtsApi
public long getPartChecksum(int partIndex, boolean isAttachment) throws PackageException {
    InputStream partDataStream = getPartData(partIndex, isAttachment);
    if (partDataStream != null) {
        try {
            SeekInputStream seekDataStream = new SeekInputStream(partDataStream);
            seekDataStream.seek(0);
            // create a new crc and reset it
            CRC32 crc = new CRC32();
            // use checked stream to get the checksum
            CheckedInputStream stream = new CheckedInputStream(seekDataStream, crc);
            int bufLen = 4096;
            byte[] buffer = new byte[bufLen];
            int numBytesRead = bufLen;
            while (numBytesRead == bufLen) {
                numBytesRead = stream.read(buffer, 0, bufLen);
            }
            long checksum = stream.getChecksum().getValue();
            stream.close();
            return checksum;
        } catch (IOException ioe) {
            throw new PackageException(ioe);
        }
    } else {
        throw new MimePartWithoutContentException("MIME part does not have any content");
    }
}
Also used : SeekInputStream(com.axway.ats.core.io.SeekInputStream) CRC32(java.util.zip.CRC32) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SeekInputStream(com.axway.ats.core.io.SeekInputStream) InputStream(java.io.InputStream) MimePartWithoutContentException(com.axway.ats.action.objects.model.MimePartWithoutContentException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) IOException(java.io.IOException) CheckedInputStream(java.util.zip.CheckedInputStream) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 7 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project ddf by codice.

the class Adler32ChecksumProvider method calculateChecksum.

@Override
public String calculateChecksum(InputStream inputStream) throws IOException, NoSuchAlgorithmException {
    if (inputStream == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }
    long checksumValue = 0L;
    try (CheckedInputStream cis = new CheckedInputStream(inputStream, new Adler32())) {
        byte[] buf = new byte[4096];
        while (cis.read(buf, 0, buf.length - 1) != -1) {
        }
        checksumValue = cis.getChecksum().getValue();
    }
    return Long.toHexString(checksumValue);
}
Also used : CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32)

Example 8 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project processdash by dtuma.

the class PackageLaunchProfile method calcChecksum.

private long calcChecksum(File f, Checksum cksum) throws IOException {
    CheckedInputStream in = new CheckedInputStream(new BufferedInputStream(new FileInputStream(f)), cksum);
    while (in.read() != -1) ;
    in.close();
    return cksum.getValue();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream)

Example 9 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project cassandra by apache.

the class VerifyTest method simpleFullChecksum.

protected long simpleFullChecksum(String filename) throws IOException {
    try (FileInputStream inputStream = new FileInputStream(filename)) {
        CRC32 checksum = new CRC32();
        CheckedInputStream cinStream = new CheckedInputStream(inputStream, checksum);
        byte[] b = new byte[128];
        while (cinStream.read(b) >= 0) {
        }
        return cinStream.getChecksum().getValue();
    }
}
Also used : CRC32(java.util.zip.CRC32) CheckedInputStream(java.util.zip.CheckedInputStream)

Example 10 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project zookeeper by apache.

the class CRCTest method getCheckSum.

/** return if checksum matches for a snapshot **/
private boolean getCheckSum(FileSnap snap, File snapFile) throws IOException {
    DataTree dt = new DataTree();
    Map<Long, Integer> sessions = new ConcurrentHashMap<Long, Integer>();
    InputStream snapIS = new BufferedInputStream(new FileInputStream(snapFile));
    CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32());
    InputArchive ia = BinaryInputArchive.getArchive(crcIn);
    try {
        snap.deserialize(dt, sessions, ia);
    } catch (IOException ie) {
        // we failed on the most recent snapshot
        // must be incomplete
        // try reading the next one
        // after corrupting
        snapIS.close();
        crcIn.close();
        throw ie;
    }
    long checksum = crcIn.getChecksum().getValue();
    long val = ia.readLong("val");
    snapIS.close();
    crcIn.close();
    return (val != checksum);
}
Also used : CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) BufferedInputStream(java.io.BufferedInputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

CheckedInputStream (java.util.zip.CheckedInputStream)18 FileInputStream (java.io.FileInputStream)14 IOException (java.io.IOException)13 CRC32 (java.util.zip.CRC32)11 BufferedInputStream (java.io.BufferedInputStream)8 InputStream (java.io.InputStream)6 Adler32 (java.util.zip.Adler32)6 File (java.io.File)3 BinaryInputArchive (org.apache.jute.BinaryInputArchive)3 InputArchive (org.apache.jute.InputArchive)3 MimePartWithoutContentException (com.axway.ats.action.objects.model.MimePartWithoutContentException)1 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)1 PackageException (com.axway.ats.action.objects.model.PackageException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 SeekInputStream (com.axway.ats.core.io.SeekInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1