@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)); }
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(); }
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(); } }
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(); }
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( InputStream stream,
                               String charset ); static String read( File file ); static void write( String content,
                              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( InputStream input,
                              OutputStream stream,
                              int bufferSize ); static void write( Reader input,
                              Writer writer ); static boolean isSame( InputStream input1,
                                  InputStream input2 ); static InputStream getResourceAsStream( String resourcePath,
                                                   ClassLoader classLoader,
                                                   Class<?> clazz ); static void closeQuietly( Closeable closeable ); }
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( InputStream stream,
                               String charset ); static String read( File file ); static void write( String content,
                              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( InputStream input,
                              OutputStream stream,
                              int bufferSize ); static void write( Reader input,
                              Writer writer ); static boolean isSame( InputStream input1,
                                  InputStream input2 ); static InputStream getResourceAsStream( String resourcePath,
                                                   ClassLoader classLoader,
                                                   Class<?> clazz ); static void closeQuietly( Closeable closeable );  }