2121import java .io .OutputStream ;
2222import java .io .Reader ;
2323import java .io .StringWriter ;
24- import java .io .UnsupportedEncodingException ;
2524import java .io .Writer ;
2625
2726import org .apache .commons .io .output .ByteArrayOutputStream ;
4342 * @author Peter Donald
4443 * @author Jeff Turner
4544 * @author Stephen Colebourne
46- * @version CVS $Revision: 1.17 $ $Date: 2004/07/31 09:52:09 $
45+ * @version CVS $Revision: 1.18 $ $Date: 2004/07/31 11:26:39 $
4746 */
4847public class IOUtils {
4948
@@ -130,6 +129,9 @@ public static void closeQuietly( OutputStream output ) {
130129 //-----------------------------------------------------------------------
131130 /**
132131 * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
132+ * <p>
133+ * This method buffers the input internally, so there is no need to use a
134+ * <code>BufferedInputStream</code>.
133135 *
134136 * @param input the <code>InputStream</code> to read from
135137 * @return the requested byte array
@@ -145,6 +147,9 @@ public static byte[] toByteArray(InputStream input) throws IOException {
145147 /**
146148 * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
147149 * using the default character encoding of the platform.
150+ * <p>
151+ * This method buffers the input internally, so there is no need to use a
152+ * <code>BufferedReader</code>.
148153 *
149154 * @param input the <code>Reader</code> to read from
150155 * @return the requested byte array
@@ -163,12 +168,16 @@ public static byte[] toByteArray(Reader input) throws IOException {
163168 * <p>
164169 * Character encoding names can be found at
165170 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
171+ * <p>
172+ * This method buffers the input internally, so there is no need to use a
173+ * <code>BufferedReader</code>.
166174 *
167175 * @param input the <code>Reader</code> to read from
168176 * @param encoding the encoding to use, null means platform default
169177 * @return the requested byte array
170178 * @throws NullPointerException if the input is null
171179 * @throws IOException if an I/O error occurs
180+ * @since 1.1
172181 */
173182 public static byte [] toByteArray (Reader input , String encoding ) throws IOException {
174183 ByteArrayOutputStream output = new ByteArrayOutputStream ();
@@ -185,39 +194,21 @@ public static byte[] toByteArray(Reader input, String encoding) throws IOExcepti
185194 * @param input the <code>String</code> to convert
186195 * @return the requested byte array
187196 * @throws NullPointerException if the input is null
188- * @throws IOException if an I/O error occurs
189- * (never happens, but can't remove due to backwards compatibility)
197+ * @throws IOException if an I/O error occurs (never occurs)
198+ * @deprecated Use {@link String#getBytes()}
190199 */
191200 public static byte [] toByteArray (String input ) throws IOException {
192201 return input .getBytes ();
193202 }
194203
195- /**
196- * Get the contents of a <code>String</code> as a <code>byte[]</code>
197- * using the specified character encoding.
198- * <p>
199- * This is based on {@link String#getBytes(String)}.
200- * Character encoding names can be found at
201- * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
202- *
203- * @param input the <code>String</code> to convert
204- * @param encoding the encoding to use, null means platform default
205- * @return the requested byte array
206- * @throws NullPointerException if the input is null
207- * @throws UnsupportedEncodingException if the named charset is not supported
208- */
209- public static byte [] toByteArray (String input , String encoding ) throws UnsupportedEncodingException {
210- if (encoding == null ) {
211- return input .getBytes ();
212- }
213- return input .getBytes (encoding );
214- }
215-
216204 // toString
217205 //-----------------------------------------------------------------------
218206 /**
219207 * Get the contents of an <code>InputStream</code> as a String
220208 * using the default character encoding of the platform.
209+ * <p>
210+ * This method buffers the input internally, so there is no need to use a
211+ * <code>BufferedInputStream</code>.
221212 *
222213 * @param input the <code>InputStream</code> to read from
223214 * @return the requested String
@@ -236,6 +227,9 @@ public static String toString(InputStream input) throws IOException {
236227 * <p>
237228 * Character encoding names can be found at
238229 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
230+ * <p>
231+ * This method buffers the input internally, so there is no need to use a
232+ * <code>BufferedInputStream</code>.
239233 *
240234 * @param input the <code>InputStream</code> to read from
241235 * @param encoding the encoding to use, null means platform default
@@ -251,6 +245,9 @@ public static String toString(InputStream input, String encoding) throws IOExcep
251245
252246 /**
253247 * Get the contents of a <code>Reader</code> as a String.
248+ * <p>
249+ * This method buffers the input internally, so there is no need to use a
250+ * <code>BufferedReader</code>.
254251 *
255252 * @param input the <code>Reader</code> to read from
256253 * @return the requested String
@@ -270,12 +267,11 @@ public static String toString(Reader input) throws IOException {
270267 * @param input the byte array to read from
271268 * @return the requested String
272269 * @throws NullPointerException if the input is null
273- * @throws IOException if an I/O error occurs
270+ * @throws IOException if an I/O error occurs (never occurs)
271+ * @deprecated Use {@link String#String(byte[])}
274272 */
275273 public static String toString (byte [] input ) throws IOException {
276- StringWriter sw = new StringWriter ();
277- CopyUtils .copy (input , sw );
278- return sw .toString ();
274+ return new String (input );
279275 }
280276
281277 /**
@@ -289,17 +285,22 @@ public static String toString(byte[] input) throws IOException {
289285 * @param encoding the encoding to use, null means platform default
290286 * @return the requested String
291287 * @throws NullPointerException if the input is null
292- * @throws IOException if an I/O error occurs
288+ * @throws IOException if an I/O error occurs (never occurs)
289+ * @deprecated Use {@link String#String(byte[],String)}
293290 */
294291 public static String toString (byte [] input , String encoding ) throws IOException {
295- StringWriter sw = new StringWriter ();
296- CopyUtils .copy (input , sw , encoding );
297- return sw .toString ();
292+ if (encoding == null ) {
293+ return new String (input );
294+ } else {
295+ return new String (input , encoding );
296+ }
298297 }
299298
300299 //-----------------------------------------------------------------------
301300 /**
302301 * Compare the contents of two Streams to determine if they are equal or not.
302+ * <p>
303+ * This method buffers the input internally using <code>BufferedInputStream</code>.
303304 *
304305 * @param input1 the first stream
305306 * @param input2 the second stream
0 commit comments