public void testRead_putSingleByte() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); int b = in.read(); assertEquals('y', b); verify(hasher).putByte((byte) 'y'); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testRead_putByteArrayAtPos() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); byte[] buf = new byte[3]; int numOfByteRead = in.read(buf, 0, 3); assertEquals(3, numOfByteRead); for (int i = 0; i < numOfByteRead; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(Arrays.copyOf(testBytes, 3), 0, 3); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testRead_putByteArray() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); for (int i = 0; i < testBytes.length; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(testBytes, 0, testBytes.length); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testHash_hashesCorrectly() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testRead_putByteArrayOutOfBound() throws Exception { byte[] buf = new byte[100]; byte[] expectedBytes = buf.clone(); System.arraycopy(testBytes, 0, expectedBytes, 0, testBytes.length); HashingInputStream in = new HashingInputStream(hashFunction, buffer); int numOfByteRead = in.read(buf, 0, 100); assertEquals(4, numOfByteRead); for (int i = 0; i < numOfByteRead; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(expectedBytes, 0, 4); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testChecksForNull() throws Exception { NullPointerTester tester = new NullPointerTester(); tester.testAllPublicInstanceMethods(new HashingInputStream(Hashing.md5(), buffer)); tester.testAllPublicStaticMethods(HashingInputStream.class); tester.testAllPublicConstructors(HashingInputStream.class); } }
public void testHash_hashesCorrectlyForSkipping() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'}); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); long numOfByteSkipped = in.skip(2); assertEquals(2, numOfByteSkipped); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(2, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyReadOutOfBound() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[100]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(-1, in.read()); // additional read assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try { HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try { HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try { HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
public void testRead_putSingleByte() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); int b = in.read(); assertEquals('y', b); verify(hasher).putByte((byte) 'y'); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testRead_putByteArrayAtPos() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); byte[] buf = new byte[3]; int numOfByteRead = in.read(buf, 0, 3); assertEquals(3, numOfByteRead); for (int i = 0; i < numOfByteRead; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(Arrays.copyOf(testBytes, 3), 0, 3); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testRead_putByteArray() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); for (int i = 0; i < testBytes.length; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(testBytes, 0, testBytes.length); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testHash_hashesCorrectly() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testRead_putByteArrayOutOfBound() throws Exception { byte[] buf = new byte[100]; byte[] expectedBytes = buf.clone(); System.arraycopy(testBytes, 0, expectedBytes, 0, testBytes.length); HashingInputStream in = new HashingInputStream(hashFunction, buffer); int numOfByteRead = in.read(buf, 0, 100); assertEquals(4, numOfByteRead); for (int i = 0; i < numOfByteRead; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(expectedBytes, 0, 4); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
private BlobId save(InputStream data, BlobId id) { String containerName = this.containerName.value(); HashingInputStream hashingInputStream = new HashingInputStream(Hashing.sha256(), data); Payload payload = payloadCodec.write(hashingInputStream); Blob blob = blobStore.blobBuilder(id.asString()).payload(payload).build(); blobStore.putBlob(containerName, blob); return blobIdFactory.from(hashingInputStream.hash().toString()); }
public void testHash_hashesCorrectlyReadOutOfBound() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[100]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(-1, in.read()); // additional read assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyForSkipping() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'}); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); long numOfByteSkipped = in.skip(2); assertEquals(2, numOfByteSkipped); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(2, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testChecksForNull() throws Exception { NullPointerTester tester = new NullPointerTester(); tester.testAllPublicInstanceMethods(new HashingInputStream(Hashing.md5(), buffer)); tester.testAllPublicStaticMethods(HashingInputStream.class); tester.testAllPublicConstructors(HashingInputStream.class); } }