DigestExtractor { @Nullable public static String extract(final InputStream stream) throws IOException { checkNotNull(stream); try (InputStreamReader isr = new InputStreamReader(ByteStreams.limit(stream, MAX_CHARS_NEEDED), StandardCharsets.UTF_8)) { return extract(CharStreams.toString(isr)); } finally { stream.close(); } } private  DigestExtractor(); @Nullable static String extract(final InputStream stream); @Nullable static String extract(final String input); static boolean isDigest(@Nullable final String digest);  }
@Test public void acceptedDigests() throws Exception { for (int i = 0; i < validDigests.length; i++) { String test = validDigests[i][0]; String expected = validDigests[i][1]; String digest = DigestExtractor.extract(stream(test)); assertThat("DigestExtractor did not accept " + test, digest, notNullValue()); assertThat("DigestExtractor did not accept " + test, digest, equalTo(expected)); } }
@Test public void rejectedDigests() { assertThat(DigestExtractor.extract("123456"), is(nullValue())); assertThat(DigestExtractor.extract(""), is(nullValue())); assertThat(DigestExtractor.extract(" "), is(nullValue())); assertThat(DigestExtractor.extract("902a360Xcad98a34b59863c1e65bcf71"), is(nullValue())); }