PreviewInputStream extends InputStream { public String previewAsString(final int limit, final String encoding) throws IOException { fill(limit); return new String(buffer, encoding); } PreviewInputStream(final InputStream inputStream); int read(); int read(final byte[] b, final int off, final int len); long skip(long len); int available(); void mark(int howMuch); boolean markSupported(); void close(); String previewAsString(final int limit, final String encoding);  }
@Test void test() throws IOException { final StringBuilder sb = new StringBuilder(); for (int line = 1; line <= 100; line++) { sb.append("line"); sb.append(line); for (int col = 1; col <= 100; col++) { sb.append(",col"); sb.append(col); } sb.append("\n"); } final String input = sb.toString(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes(StreamUtil.DEFAULT_CHARSET)); final PreviewInputStream previewInputStream = new PreviewInputStream(inputStream); String data = previewInputStream.previewAsString(200, StreamUtil.DEFAULT_CHARSET_NAME); System.out.println(data); assertThat(input.startsWith(data)).isTrue(); data = previewInputStream.previewAsString(200, StreamUtil.DEFAULT_CHARSET_NAME); System.out.println(data); assertThat(input.startsWith(data)).isTrue(); data = previewInputStream.previewAsString(200, StreamUtil.DEFAULT_CHARSET_NAME); System.out.println(data); assertThat(input.startsWith(data)).isTrue(); final String output = StreamUtil.streamToString(previewInputStream, true); assertThat(output).isEqualTo(input); }