IOUtils { public static String toString(final InputStream inputStream, final String encoding) throws IOException { return (null == encoding) ? toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)) : toString(new InputStreamReader(inputStream, encoding)); }  static String toString(final InputStream inputStream, final String encoding); static String toString(final Reader reader);  }
@Test public void assertToStringFromInputStream() throws Exception { byte[] b = "toStringFromInputStream".getBytes(StandardCharsets.UTF_8); InputStream in = new ByteArrayInputStream(b); assertThat(IOUtils.toString(in, null), is("toStringFromInputStream")); }
@Test public void assertToStringFromInputStreamWithEncoding() throws Exception { byte[] b = "toStringFromInputStream".getBytes(StandardCharsets.UTF_8); InputStream in = new ByteArrayInputStream(b); assertThat(IOUtils.toString(in, "UTF-8"), is("toStringFromInputStream")); }
@Test public void assertToStringFromReader() throws Exception { byte[] b = "toStringFromReader".getBytes(StandardCharsets.UTF_8); InputStream is = new ByteArrayInputStream(b); Reader inr = new InputStreamReader(is, StandardCharsets.UTF_8); assertThat(IOUtils.toString(inr), is("toStringFromReader")); }