/** * Copy chars from a <code>Reader</code> to a <code>Writer</code>. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedReader</code>. * <p> * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. * * @param input the <code>Reader</code> to read from * @param output the <code>Writer</code> to write to * @return the number of characters copied * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs */ public static long copy(Reader input, Writer output) throws NestedIOException { return copy(input, output, new char[DEFAULT_BUFFER_SIZE]); }
/** * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * <p> * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. * * @param input the <code>InputStream</code> to read from * @param output the <code>OutputStream</code> to write to * @return the number of bytes copied * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs */ public static long copy(InputStream input, OutputStream output) throws NestedIOException { return copy(input, output, new byte[DEFAULT_BUFFER_SIZE]); }
/** * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code> using the default character * encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * <p> * This method uses {@link InputStreamReader}. * * @param input the <code>InputStream</code> to read from * @param output the <code>Writer</code> to write to * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs * @since 1.1 */ public static void copy(InputStream input, Writer output) throws NestedIOException { copy(input, output, Charset.defaultCharset()); }
/** * Get the contents of a <code>Reader</code> as a String. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedReader</code>. * * @param input the <code>Reader</code> to read from * @return the requested String * @throws NullPointerException if the input is null * @throws NestedIOException if an I/O error occurs */ public static String readString(Reader input) throws NestedIOException { StringWriter sw = new StringWriter(); copy(input, sw); return sw.toString(); }
/** * Get the contents of an <code>InputStream</code> as a String using the specified character encoding. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * </p> * * @param input the <code>InputStream</code> to read from * @param encoding the encoding to use, null means platform default * @return the requested String * @throws NullPointerException if the input is null * @throws NestedIOException if an I/O error occurs */ public static String readString(InputStream input, Charset encoding) throws NestedIOException { StringWriter sw = new StringWriter(); copy(input, sw, encoding); return sw.toString(); }
/** * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from * @return the requested byte array * @throws NullPointerException if the input is null * @throws NestedIOException if an I/O error occurs */ public static byte[] readByteArray(InputStream input) throws NestedIOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
public static byte[] readByteArray(Reader reader) { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out); try{ copy(reader,writer); writer.flush(); return out.toByteArray(); }catch(IOException e){ throw Exceptions.wrap(e); }finally{ close(writer); } }
/** * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code> using the specified character * encoding. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * <p> * Character encoding names can be found at <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method uses {@link InputStreamReader}. * * @param input the <code>InputStream</code> to read from * @param output the <code>Writer</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs * @throws UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 1.1 */ public static void copy(InputStream input, Writer output, String encoding) throws NestedIOException { copy(input, output, Charsets.get(encoding)); }
/** * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code> using the specified character * encoding. * <p> * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. * <p> * This method uses {@link InputStreamReader}. * * @param input the <code>InputStream</code> to read from * @param output the <code>Writer</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if the input or output is null * @throws NestedIOException if an I/O error occurs * @since 2.3 */ public static void copy(InputStream input, Writer output,@Nullable Charset encoding) throws NestedIOException { InputStreamReader in = new InputStreamReader(input,Charsets.get(encoding)); copy(in, output); }
IO.copy(body, out);