### Question:
IoUtil { public static String read( Reader reader ) throws IOException { if (reader == null) return ""; StringBuilder sb = new StringBuilder(); boolean error = false; try { int numRead = 0; char[] buffer = new char[1024]; while ((numRead = reader.read(buffer)) > -1) { sb.append(buffer, 0, numRead); } } catch (IOException e) { error = true; throw e; } catch (RuntimeException e) { error = true; throw e; } finally { try { reader.close(); } catch (IOException e) { if (!error) throw e; } } return sb.toString(); } private  IoUtil(); static byte[] readBytes( InputStream stream ); static byte[] readBytes( File file ); static String read( Reader reader ); static String read( InputStream stream ); static String read( File file ); static void write( String content,
                              OutputStream stream ); static void write( String content,
                              Writer writer ); static void write( InputStream input,
                              OutputStream stream ); static void write( Reader input,
                              Writer writer ); static boolean isSame( InputStream input1,
                                  InputStream input2 ); static InputStream getResourceAsStream( String resourcePath,
                                                   ClassLoader classLoader,
                                                   Class<?> clazz );  }### Answer:
@Test public void readShouldReturnEmptyStringForNullInputStream() throws Exception { assertThat(IoUtil.read((InputStream)null), is("")); }
@Test public void readShouldReturnEmptyStringForNullReader() throws Exception { assertThat(IoUtil.read((Reader)null), is("")); }
@Test public void readShouldReadInputStreamCorrectlyAndShouldCloseStream() throws Exception { String content = "This is the way to grandma's house."; InputStream stream = new ByteArrayInputStream(content.getBytes()); InputStreamWrapper wrapper = new InputStreamWrapper(stream); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); for (int i = 0; i != 10; ++i) { content += content; } stream = new ByteArrayInputStream(content.getBytes()); wrapper = new InputStreamWrapper(stream); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); }
@Test public void readShouldReadReaderCorrectlyAndShouldCloseStream() throws Exception { String content = "This is the way to grandma's house."; Reader reader = new StringReader(content); ReaderWrapper wrapper = new ReaderWrapper(reader); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); for (int i = 0; i != 10; ++i) { content += content; } reader = new StringReader(content); wrapper = new ReaderWrapper(reader); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); }