Search in sources :

Example 1 with SeekInputStream

use of com.axway.ats.core.io.SeekInputStream 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)

Aggregations

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 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CRC32 (java.util.zip.CRC32)1 CheckedInputStream (java.util.zip.CheckedInputStream)1