Skip to content

Commit 6afa3db

Browse files
author
Stephen Colebourne
committed
Add encoding specific methods, Javadoc, Reformat to standards
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140593 13f79535-47bb-0310-9956-ffa450edef68
1 parent 891b02a commit 6afa3db

2 files changed

Lines changed: 363 additions & 336 deletions

File tree

src/java/org/apache/commons/io/CopyUtils.java

Lines changed: 162 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@
9898
* @author Peter Donald
9999
* @author Jeff Turner
100100
* @author Matthew Hawthorne
101-
* @version $Id: CopyUtils.java,v 1.6 2004/04/24 23:49:25 bayard Exp $
101+
* @author Stephen Colebourne
102+
* @version $Id: CopyUtils.java,v 1.7 2004/07/31 09:52:09 scolebourne Exp $
102103
*/
103104
public class CopyUtils {
104105

105106
/**
106-
* The name says it all.
107+
* The default buffer size to use.
107108
*/
108109
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
109110

@@ -112,75 +113,64 @@ public class CopyUtils {
112113
*/
113114
public CopyUtils() {}
114115

115-
// ----------------------------------------------------------------
116-
// byte[] -> OutputStream
117-
// ----------------------------------------------------------------
118-
116+
// from byte[]
117+
//-----------------------------------------------------------------------
119118
/**
120119
* Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
121-
* @param input the byte array to read from
122-
* @param output the <code>OutputStream</code> to write to
123-
* @throws IOException In case of an I/O problem
120+
*
121+
* @param input the byte array to read from
122+
* @param output the <code>OutputStream</code> to write to
123+
* @throws NullPointerException if the input or output is null
124+
* @throws IOException if an I/O error occurs
124125
*/
125-
public static void copy(byte[] input, OutputStream output)
126-
throws IOException {
126+
public static void copy(byte[] input, OutputStream output) throws IOException {
127127
output.write(input);
128128
}
129129

130-
// ----------------------------------------------------------------
131-
// byte[] -> Writer
132-
// ----------------------------------------------------------------
133-
134130
/**
135-
* Copy and convert bytes from a <code>byte[]</code> to chars on a
136-
* <code>Writer</code>.
137-
* The platform's default encoding is used for the byte-to-char conversion.
138-
* @param input the byte array to read from
139-
* @param output the <code>Writer</code> to write to
140-
* @throws IOException In case of an I/O problem
131+
* Copy bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
132+
* using the default character encoding of the platform.
133+
*
134+
* @param input the byte array to read from
135+
* @param output the <code>Writer</code> to write to
136+
* @throws NullPointerException if the input or output is null
137+
* @throws IOException if an I/O error occurs
141138
*/
142-
public static void copy(byte[] input, Writer output)
143-
throws IOException {
139+
public static void copy(byte[] input, Writer output) throws IOException {
144140
ByteArrayInputStream in = new ByteArrayInputStream(input);
145141
copy(in, output);
146142
}
147143

148-
149144
/**
150-
* Copy and convert bytes from a <code>byte[]</code> to chars on a
151-
* <code>Writer</code>, using the specified encoding.
152-
* @param input the byte array to read from
153-
* @param output the <code>Writer</code> to write to
154-
* @param encoding The name of a supported character encoding. See the
155-
* <a href="http://www.iana.org/assignments/character-sets">IANA
156-
* Charset Registry</a> for a list of valid encoding types.
157-
* @throws IOException In case of an I/O problem
145+
* Copy bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
146+
* using the specified character encoding.
147+
* <p>
148+
* Character encoding names can be found at
149+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
150+
*
151+
* @param input the byte array to read from
152+
* @param output the <code>Writer</code> to write to
153+
* @param encoding the encoding to use, null means platform default
154+
* @throws NullPointerException if the input or output is null
155+
* @throws IOException if an I/O error occurs
158156
*/
159-
public static void copy(
160-
byte[] input,
161-
Writer output,
162-
String encoding)
163-
throws IOException {
157+
public static void copy(byte[] input, Writer output, String encoding) throws IOException {
164158
ByteArrayInputStream in = new ByteArrayInputStream(input);
165159
copy(in, output, encoding);
166160
}
167161

168-
169-
// ----------------------------------------------------------------
170-
// Core copy methods
171-
// ----------------------------------------------------------------
172-
162+
// from InputStream
163+
//-----------------------------------------------------------------------
173164
/**
174165
* Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
175-
* @param input the <code>InputStream</code> to read from
176-
* @param output the <code>OutputStream</code> to write to
166+
*
167+
* @param input the <code>InputStream</code> to read from
168+
* @param output the <code>OutputStream</code> to write to
177169
* @return the number of bytes copied
178-
* @throws IOException In case of an I/O problem
170+
* @throws NullPointerException if the input or output is null
171+
* @throws IOException if an I/O error occurs
179172
*/
180-
public static int copy(
181-
InputStream input,
182-
OutputStream output)
183-
throws IOException {
173+
public static int copy(InputStream input, OutputStream output) throws IOException {
184174
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
185175
int count = 0;
186176
int n = 0;
@@ -191,21 +181,54 @@ public static int copy(
191181
return count;
192182
}
193183

194-
// ----------------------------------------------------------------
195-
// Reader -> Writer
196-
// ----------------------------------------------------------------
184+
/**
185+
* Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
186+
* using the default character encoding of the platform.
187+
*
188+
* @param input the <code>InputStream</code> to read from
189+
* @param output the <code>Writer</code> to write to
190+
* @throws NullPointerException if the input or output is null
191+
* @throws IOException if an I/O error occurs
192+
*/
193+
public static void copy(InputStream input, Writer output) throws IOException {
194+
InputStreamReader in = new InputStreamReader(input);
195+
copy(in, output);
196+
}
197197

198+
/**
199+
* Copy bytes from an <code>InputStream</code> to chars on a <code>Writer</code>
200+
* using the specified character encoding.
201+
* <p>
202+
* Character encoding names can be found at
203+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
204+
*
205+
* @param input the <code>InputStream</code> to read from
206+
* @param output the <code>Writer</code> to write to
207+
* @param encoding the encoding to use, null means platform default
208+
* @throws NullPointerException if the input or output is null
209+
* @throws IOException if an I/O error occurs
210+
*/
211+
public static void copy(InputStream input, Writer output, String encoding) throws IOException {
212+
if (encoding == null) {
213+
copy(input, output);
214+
} else {
215+
InputStreamReader in = new InputStreamReader(input, encoding);
216+
copy(in, output);
217+
}
218+
}
219+
220+
// from Reader
221+
//-----------------------------------------------------------------------
198222
/**
199223
* Copy chars from a <code>Reader</code> to a <code>Writer</code>.
200-
* @param input the <code>Reader</code> to read from
201-
* @param output the <code>Writer</code> to write to
224+
*
225+
* @param input the <code>Reader</code> to read from
226+
* @param output the <code>Writer</code> to write to
202227
* @return the number of characters copied
203-
* @throws IOException In case of an I/O problem
228+
* @throws NullPointerException if the input or output is null
229+
* @throws IOException if an I/O error occurs
204230
*/
205-
public static int copy(
206-
Reader input,
207-
Writer output)
208-
throws IOException {
231+
public static int copy(Reader input, Writer output) throws IOException {
209232
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
210233
int count = 0;
211234
int n = 0;
@@ -216,102 +239,108 @@ public static int copy(
216239
return count;
217240
}
218241

219-
// ----------------------------------------------------------------
220-
// InputStream -> Writer
221-
// ----------------------------------------------------------------
222-
223242
/**
224-
* Copy and convert bytes from an <code>InputStream</code> to chars on a
225-
* <code>Writer</code>.
226-
* The platform's default encoding is used for the byte-to-char conversion.
227-
* @param input the <code>InputStream</code> to read from
228-
* @param output the <code>Writer</code> to write to
229-
* @throws IOException In case of an I/O problem
243+
* Copy chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>
244+
* using the default character encoding of the platform, and calling flush.
245+
* <p>
246+
* Due to the implementation of OutputStreamWriter, this method performs a flush.
247+
*
248+
* @param input the <code>Reader</code> to read from
249+
* @param output the <code>OutputStream</code> to write to
250+
* @throws NullPointerException if the input or output is null
251+
* @throws IOException if an I/O error occurs
230252
*/
231-
public static void copy(
232-
InputStream input,
233-
Writer output)
234-
throws IOException {
235-
InputStreamReader in = new InputStreamReader(input);
236-
copy(in, output);
253+
public static void copy(Reader input, OutputStream output) throws IOException {
254+
OutputStreamWriter out = new OutputStreamWriter(output);
255+
copy(input, out);
256+
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
257+
out.flush();
237258
}
238259

239260
/**
240-
* Copy and convert bytes from an <code>InputStream</code> to chars on a
241-
* <code>Writer</code>, using the specified encoding.
242-
* @param input the <code>InputStream</code> to read from
243-
* @param output the <code>Writer</code> to write to
244-
* @param encoding The name of a supported character encoding. See the
245-
* <a href="http://www.iana.org/assignments/character-sets">IANA
246-
* Charset Registry</a> for a list of valid encoding types.
247-
* @throws IOException In case of an I/O problem
261+
* Copy chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>
262+
* using the specified character encoding, and calling flush.
263+
* <p>
264+
* Character encoding names can be found at
265+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
266+
* <p>
267+
* Due to the implementation of OutputStreamWriter, this method performs a flush.
268+
*
269+
* @param input the <code>Reader</code> to read from
270+
* @param output the <code>OutputStream</code> to write to
271+
* @param encoding the encoding to use, null means platform default
272+
* @throws NullPointerException if the input or output is null
273+
* @throws IOException if an I/O error occurs
248274
*/
249-
public static void copy(
250-
InputStream input,
251-
Writer output,
252-
String encoding)
253-
throws IOException {
254-
InputStreamReader in = new InputStreamReader(input, encoding);
255-
copy(in, output);
275+
public static void copy(Reader input, OutputStream output, String encoding) throws IOException {
276+
if (encoding == null) {
277+
copy(input, output);
278+
} else {
279+
OutputStreamWriter out = new OutputStreamWriter(output, encoding);
280+
copy(input, out);
281+
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
282+
out.flush();
283+
}
256284
}
257285

258-
259-
// ----------------------------------------------------------------
260-
// Reader -> OutputStream
261-
// ----------------------------------------------------------------
262-
286+
// from String
287+
//-----------------------------------------------------------------------
263288
/**
264-
* Serialize chars from a <code>Reader</code> to bytes on an
265-
* <code>OutputStream</code>, and flush the <code>OutputStream</code>.
266-
* @param input the <code>Reader</code> to read from
267-
* @param output the <code>OutputStream</code> to write to
268-
* @throws IOException In case of an I/O problem
289+
* Copy chars from a <code>String</code> to a <code>Writer</code>.
290+
*
291+
* @param input the <code>String</code> to read from
292+
* @param output the <code>Writer</code> to write to
293+
* @throws NullPointerException if the input or output is null
294+
* @throws IOException if an I/O error occurs
269295
*/
270-
public static void copy(
271-
Reader input,
272-
OutputStream output)
273-
throws IOException {
274-
OutputStreamWriter out = new OutputStreamWriter(output);
275-
copy(input, out);
276-
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
277-
out.flush();
296+
public static void copy(String input, Writer output) throws IOException {
297+
output.write(input);
278298
}
279299

280-
// ----------------------------------------------------------------
281-
// String -> OutputStream
282-
// ----------------------------------------------------------------
283-
284300
/**
285-
* Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
286-
* flush the <code>OutputStream</code>.
287-
* @param input the <code>String</code> to read from
288-
* @param output the <code>OutputStream</code> to write to
289-
* @throws IOException In case of an I/O problem
301+
* Copy chars from a <code>String</code> to bytes on an <code>OutputStream</code>
302+
* using the default character encoding of the platform, and calling flush.
303+
* <p>
304+
* Due to the implementation of OutputStreamWriter, this method performs a flush.
305+
*
306+
* @param input the <code>String</code> to read from
307+
* @param output the <code>OutputStream</code> to write to
308+
* @throws NullPointerException if the input or output is null
309+
* @throws IOException if an I/O error occurs
290310
*/
291-
public static void copy(
292-
String input,
293-
OutputStream output)
294-
throws IOException {
311+
public static void copy(String input, OutputStream output) throws IOException {
295312
StringReader in = new StringReader(input);
296313
OutputStreamWriter out = new OutputStreamWriter(output);
297314
copy(in, out);
298315
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
299316
out.flush();
300317
}
301318

302-
// ----------------------------------------------------------------
303-
// String -> Writer
304-
// ----------------------------------------------------------------
305-
306319
/**
307-
* Copy chars from a <code>String</code> to a <code>Writer</code>.
308-
* @param input the <code>String</code> to read from
309-
* @param output the <code>Writer</code> to write to
310-
* @throws IOException In case of an I/O problem
320+
* Copy chars from a <code>String</code> to bytes on an <code>OutputStream</code>
321+
* using the specified character encoding, and calling flush.
322+
* <p>
323+
* Character encoding names can be found at
324+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
325+
* <p>
326+
* Due to the implementation of OutputStreamWriter, this method performs a flush.
327+
*
328+
* @param input the <code>String</code> to read from
329+
* @param output the <code>OutputStream</code> to write to
330+
* @param encoding the encoding to use, null means platform default
331+
* @throws NullPointerException if the input or output is null
332+
* @throws IOException if an I/O error occurs
311333
*/
312-
public static void copy(String input, Writer output)
313-
throws IOException {
314-
output.write(input);
334+
public static void copy(String input, OutputStream output, String encoding) throws IOException {
335+
if (encoding == null) {
336+
copy(input, output);
337+
} else {
338+
StringReader in = new StringReader(input);
339+
OutputStreamWriter out = new OutputStreamWriter(output, encoding);
340+
copy(in, out);
341+
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
342+
out.flush();
343+
}
315344
}
316345

317-
} // CopyUtils
346+
}

0 commit comments

Comments
 (0)