/** * 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); }
/** * Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when * finished. * * @param content the content to write to the stream; may be null * @param stream the stream to which the content is to be written * @throws IOException * @throws IllegalArgumentException if the stream is null */ public static void write( String content, OutputStream stream ) throws IOException { IoUtil.write(content, stream); }
/** * Read and return the entire contents of the supplied {@link File file}. * * @param file the file containing the contents; may be null * @return the contents, or an empty byte array if the supplied file is null * @throws IOException if there is an error reading the content */ public static byte[] readBytes( File file ) throws IOException { if (file == null) return new byte[] {}; InputStream stream = new BufferedInputStream(new FileInputStream(file)); boolean error = false; try { return readBytes(stream); } catch (IOException e) { error = true; // this error should be thrown, even if there is an error closing stream throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an error closing stream throw e; } finally { try { stream.close(); } catch (IOException e) { if (!error) throw e; } } }
/** * 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); }
/** * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when * finished. * * @param content the content to write to the writer; may be null * @param writer the writer to which the content is to be written * @throws IOException * @throws IllegalArgumentException if the writer is null */ public static void write( String content, Writer writer ) throws IOException { IoUtil.write(content, writer); }
/** * 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 stream == null ? "" : read(new InputStreamReader(stream)); }
List<String> lines = null; try { String content = IoUtil.read(stream); lines = StringUtil.splitLines(content); } catch (IOException e) {