@Test public void readShouldReadInputStreamCorrectlyAndShouldCloseStream() throws Exception { // Read content shorter than buffer size ... 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)); // Read content longer than buffer size ... for (int i = 0; i != 10; ++i) { content += content; // note this doubles each time! } 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 { // Read content shorter than buffer size ... 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)); // Read content longer than buffer size ... for (int i = 0; i != 10; ++i) { content += content; // note this doubles each time! } reader = new StringReader(content); wrapper = new ReaderWrapper(reader); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); }
/** * Read and return the entire contents of the supplied {@link InputStream}. This method always * closes the stream when finished reading. * * @param stream the streamed contents; may be null * @return the contents, or an empty string if the supplied stream is null * @throws IOException if there is an error reading the content */ public static String read(InputStream stream) throws IOException { return IoUtil.read(stream); }
/** * Read and return the entire contents of the supplied {@link Reader}. This method always closes * the reader when finished reading. * * @param reader the reader of the contents; may be null * @return the contents, or an empty string if the supplied reader is null * @throws IOException if there is an error reading the content */ public static String read(Reader reader) throws IOException { return IoUtil.read(reader); }
@Test public void readShouldReturnEmptyStringForNullReader() throws Exception { assertThat(IoUtil.read((Reader) null), is("")); }
@Test public void readShouldReturnEmptyStringForNullInputStream() throws Exception { assertThat(IoUtil.read((InputStream) null), is("")); }