1616package org .apache .commons .io ;
1717
1818import java .io .ByteArrayInputStream ;
19+ import java .io .CharArrayReader ;
1920import java .io .IOException ;
2021import java .io .InputStream ;
2122import java .io .InputStreamReader ;
8687 *
8788 * 7 copy byte[] Writer 3
8889 * 8 copy byte[] OutputStream (trivial)
90+ *
91+ * 9 copy char[] OutputStream 4
92+ * 10 copy char[] Writer (trivial)
8993 * </pre>
9094 *
9195 * <p>Note that only the first two methods shuffle bytes; the rest use these
99103 * @author Jeff Turner
100104 * @author Matthew Hawthorne
101105 * @author Stephen Colebourne
102- * @version $Id: CopyUtils.java,v 1.7 2004/07/31 09:52:09 scolebourne Exp $
106+ * @version $Id: CopyUtils.java,v 1.8 2004/07/31 10:40:47 scolebourne Exp $
103107 */
104108public class CopyUtils {
105109
@@ -118,7 +122,7 @@ public CopyUtils() {}
118122 /**
119123 * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
120124 *
121- * @param input the byte array to read from
125+ * @param input the byte array to read from, do not modify during output
122126 * @param output the <code>OutputStream</code> to write to
123127 * @throws NullPointerException if the input or output is null
124128 * @throws IOException if an I/O error occurs
@@ -130,8 +134,10 @@ public static void copy(byte[] input, OutputStream output) throws IOException {
130134 /**
131135 * Copy bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
132136 * using the default character encoding of the platform.
137+ * <p>
138+ * This method uses {@link ByteArrayInputStream} and {@link InputStreamReader}.
133139 *
134- * @param input the byte array to read from
140+ * @param input the byte array to read from, do not modify during output
135141 * @param output the <code>Writer</code> to write to
136142 * @throws NullPointerException if the input or output is null
137143 * @throws IOException if an I/O error occurs
@@ -147,8 +153,10 @@ public static void copy(byte[] input, Writer output) throws IOException {
147153 * <p>
148154 * Character encoding names can be found at
149155 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
156+ * <p>
157+ * This method uses {@link ByteArrayInputStream} and {@link InputStreamReader}.
150158 *
151- * @param input the byte array to read from
159+ * @param input the byte array to read from, do not modify during output
152160 * @param output the <code>Writer</code> to write to
153161 * @param encoding the encoding to use, null means platform default
154162 * @throws NullPointerException if the input or output is null
@@ -184,6 +192,8 @@ public static int copy(InputStream input, OutputStream output) throws IOExceptio
184192 /**
185193 * Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
186194 * using the default character encoding of the platform.
195+ * <p>
196+ * This method uses {@link InputStreamReader}.
187197 *
188198 * @param input the <code>InputStream</code> to read from
189199 * @param output the <code>Writer</code> to write to
@@ -201,6 +211,8 @@ public static void copy(InputStream input, Writer output) throws IOException {
201211 * <p>
202212 * Character encoding names can be found at
203213 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
214+ * <p>
215+ * This method uses {@link InputStreamReader}.
204216 *
205217 * @param input the <code>InputStream</code> to read from
206218 * @param output the <code>Writer</code> to write to
@@ -217,6 +229,58 @@ public static void copy(InputStream input, Writer output, String encoding) throw
217229 }
218230 }
219231
232+ // from char[]
233+ //-----------------------------------------------------------------------
234+ /**
235+ * Copy chars from a <code>char[]</code> to a <code>Writer</code>
236+ * using the default character encoding of the platform.
237+ * <p>
238+ * This method uses {@link CharArrayReader}.
239+ *
240+ * @param input the char array to read from, do not modify during output
241+ * @param output the <code>Writer</code> to write to
242+ * @throws NullPointerException if the input or output is null
243+ * @throws IOException if an I/O error occurs
244+ */
245+ public static void copy (char [] input , Writer output ) throws IOException {
246+ output .write (input );
247+ }
248+
249+ /**
250+ * Copy chars from a <code>char[]</code> to bytes on an <code>OutputStream</code>.
251+ * <p>
252+ * This method uses {@link CharArrayReader} and {@link OutputStreamWriter}.
253+ *
254+ * @param input the char array to read from, do not modify during output
255+ * @param output the <code>OutputStream</code> to write to
256+ * @throws NullPointerException if the input or output is null
257+ * @throws IOException if an I/O error occurs
258+ */
259+ public static void copy (char [] input , OutputStream output ) throws IOException {
260+ CharArrayReader in = new CharArrayReader (input );
261+ copy (in , output );
262+ }
263+
264+ /**
265+ * Copy chars from a <code>char[]</code> to bytes on an <code>OutputStream</code>
266+ * using the specified character encoding.
267+ * <p>
268+ * Character encoding names can be found at
269+ * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
270+ * <p>
271+ * This method uses {@link CharArrayReader} and {@link OutputStreamWriter}.
272+ *
273+ * @param input the char array to read from, do not modify during output
274+ * @param output the <code>OutputStream</code> to write to
275+ * @param encoding the encoding to use, null means platform default
276+ * @throws NullPointerException if the input or output is null
277+ * @throws IOException if an I/O error occurs
278+ */
279+ public static void copy (char [] input , OutputStream output , String encoding ) throws IOException {
280+ CharArrayReader in = new CharArrayReader (input );
281+ copy (in , output , encoding );
282+ }
283+
220284 // from Reader
221285 //-----------------------------------------------------------------------
222286 /**
@@ -244,6 +308,8 @@ public static int copy(Reader input, Writer output) throws IOException {
244308 * using the default character encoding of the platform, and calling flush.
245309 * <p>
246310 * Due to the implementation of OutputStreamWriter, this method performs a flush.
311+ * <p>
312+ * This method uses {@link OutputStreamWriter}.
247313 *
248314 * @param input the <code>Reader</code> to read from
249315 * @param output the <code>OutputStream</code> to write to
@@ -265,6 +331,8 @@ public static void copy(Reader input, OutputStream output) throws IOException {
265331 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
266332 * <p>
267333 * Due to the implementation of OutputStreamWriter, this method performs a flush.
334+ * <p>
335+ * This method uses {@link OutputStreamWriter}.
268336 *
269337 * @param input the <code>Reader</code> to read from
270338 * @param output the <code>OutputStream</code> to write to
@@ -302,6 +370,8 @@ public static void copy(String input, Writer output) throws IOException {
302370 * using the default character encoding of the platform, and calling flush.
303371 * <p>
304372 * Due to the implementation of OutputStreamWriter, this method performs a flush.
373+ * <p>
374+ * This method uses {@link StringReader} and {@link OutputStreamWriter}.
305375 *
306376 * @param input the <code>String</code> to read from
307377 * @param output the <code>OutputStream</code> to write to
@@ -324,6 +394,8 @@ public static void copy(String input, OutputStream output) throws IOException {
324394 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
325395 * <p>
326396 * Due to the implementation of OutputStreamWriter, this method performs a flush.
397+ * <p>
398+ * This method uses {@link StringReader} and {@link OutputStreamWriter}.
327399 *
328400 * @param input the <code>String</code> to read from
329401 * @param output the <code>OutputStream</code> to write to
0 commit comments