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");
}
}
Aggregations