InterceptingInputStream extends InputStream { public String getContents() { return intercepted; } InterceptingInputStream(final InputStream source); String getContents(); @Override int read();  }
@Test void standard() throws IOException { final String contents = UUID.randomUUID() .toString(); final InputStream s = new ByteArrayInputStream(contents.getBytes()); try (final InterceptingInputStream s2 = new InterceptingInputStream(s)) { assertThat(s2.getContents()).isEqualTo(contents); assertThat(StringUtil.toString(s2)).isEqualTo(contents); } }
@Test void tooLong() throws IOException { final int maxLength = 1024; final int uuidLength = UUID.randomUUID() .toString() .length(); final String contents = IntStream.range(0, (maxLength / uuidLength) + 2) .mapToObj(i -> UUID.randomUUID() .toString()) .collect(Collectors.joining()); final InputStream s = new ByteArrayInputStream(contents.getBytes()); try (final InterceptingInputStream s2 = new InterceptingInputStream(s)) { assertThat(s2.getContents()).endsWith("...more..."); } }