/** * Creates and returns a <code>ChecksumOutputStream</code> that generates an <code>md5</code> checksum as data * is written to it. * * @param out the underlying OutputStream used by the DigestOutputStream * @return a ChecksumOutputStream that generates an md5 checksum as data is written to it * @throws NoSuchAlgorithmException should not happen */ public ChecksumOutputStream getMd5OutputStream(OutputStream out) throws NoSuchAlgorithmException { return new ChecksumOutputStream(out, MessageDigest.getInstance("md5")); }
/** * Returns this stream's digest, expressed as an hexadecimal string. * * @return this stream's digest, expressed as an hexadecimal string */ public String getChecksumString() { return ByteUtils.toHexString(getChecksumBytes()); } }
/** * Returns this stream's digest, expressed as a byte array. * * @return this stream's digest, expressed as a byte array */ public byte[] getChecksumBytes() { return getMessageDigest().digest(); }
/** * Tests {@link AbstractFile#getAppendOutputStream()} when the operation is supported. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected void testGetAppendOutputStreamSupported() throws IOException, NoSuchAlgorithmException { // Assert that: // - getAppendOutputStream does not throw an IOException // - returns a non-null value // - the file exists after OutputStream out = tempFile.getAppendOutputStream(); assert out != null; assert tempFile.exists(); assert 0 == tempFile.getSize(); out.close(); // Assert that getAppendOutputStream() does not overwrite the existing file contents. // Appending to the file may not be supported, catch IOException thrown by getAppendOutputStream() and only those createFile(tempFile, 1); out = tempFile.getAppendOutputStream(); out.write('a'); out.close(); assert 2 == tempFile.getSize(); // Test the integrity of the OutputStream after writing a somewhat large amount of random data ChecksumOutputStream md5Out = getMd5OutputStream(tempFile.getOutputStream()); writeRandomData(md5Out, 100000, 1000); md5Out.close(); assert md5Out.getChecksumString().equals(calculateMd5(tempFile)); }
/** * Tests {@link AbstractFile#getOutputStream()} when the operation is supported. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected void testGetOutputStreamSupported() throws IOException, NoSuchAlgorithmException { // Assert that: // - getOutputStream does not throw an IOException // - returns a non-null value // - the file exists after OutputStream out = tempFile.getOutputStream(); assert out != null; assert tempFile.exists(); assert 0 == tempFile.getSize(); out.close(); // Assert that getOutputStream() overwrites the existing file contents (resets the file size to 0) createFile(tempFile, 1); out = tempFile.getOutputStream(); out.close(); assert 0 == tempFile.getSize(); // Test the integrity of the OutputStream after writing a somewhat large amount of random data ChecksumOutputStream md5Out = getMd5OutputStream(tempFile.getOutputStream()); writeRandomData(md5Out, 100000, 1000); md5Out.close(); assert md5Out.getChecksumString().equals(calculateMd5(tempFile)); }
md5Out.close(); assert md5Out.getChecksumString().equals(calculateMd5(tempFile));
md5Out.close(); assert md5Out.getChecksumString().equals(calculateMd5(tempFile)); tempFile.delete();