1 package org.apache.maven.shared.utils.io;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24
25 import java.io.BufferedInputStream;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.io.Reader;
34 import java.io.StringReader;
35 import java.io.StringWriter;
36 import java.io.Writer;
37 import java.nio.channels.Channel;
38
39 /**
40 * <p>General IO Stream manipulation.</p>
41 * <p>
42 * This class provides static utility methods for input/output operations, particularly buffered
43 * copying between sources (<code>InputStream</code>, <code>Reader</code>, <code>String</code> and
44 * <code>byte[]</code>) and destinations (<code>OutputStream</code>, <code>Writer</code>,
45 * <code>String</code> and <code>byte[]</code>).</p>
46 *
47 * <p>Unless otherwise noted, these <code>copy</code> methods do <em>not</em> flush or close the
48 * streams. Often, doing so would require making non-portable assumptions about the streams' origin
49 * and further use. This means that both streams' <code>close()</code> methods must be called after
50 * copying. if one omits this step, then the stream resources (sockets, file descriptors) are
51 * released when the associated Stream is garbage-collected. It is not a good idea to rely on this
52 * mechanism.</p>
53 *
54 * <p>For each <code>copy</code> method, a variant is provided that allows the caller to specify the
55 * buffer size (the default is 4k). As the buffer size can have a fairly large impact on speed, this
56 * may be worth tweaking. Often "large buffer -> faster" does not hold, even for large data
57 * transfers.</p>
58 * <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding to be selected
59 * (otherwise the platform default is used).</p>
60 * <p>The <code>copy</code> methods use an internal buffer when copying. It is therefore advisable
61 * <em>not</em> to deliberately wrap the stream arguments to the <code>copy</code> methods in
62 * <code>Buffered*</code> streams. For example, don't do the
63 * following:</p>
64 * <p>
65 * <code>copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );</code>
66 * </p>
67 * <p>The rationale is as follows:</p>
68 *
69 * <p>Imagine that an InputStream's read() is a very expensive operation, which would usually suggest
70 * wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent
71 * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the underlying InputStream, to
72 * fill an internal buffer, from which further <code>read</code> requests can inexpensively get
73 * their data (until the buffer runs out).</p>
74 * <p>However, the <code>copy</code> methods do the same thing, keeping an internal buffer,
75 * populated by {@link InputStream#read(byte[] b, int off, int len)} requests. Having two buffers
76 * (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer
77 * management hurts performance slightly (about 3%, according to some simple experiments).</p>
78 *
79 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
80 * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
81 * @version CVS $Revision$ $Date$
82 *
83 */
84 public final class IOUtil
85 /*
86 * Behold, intrepid explorers; a map of this class:
87 *
88 * Method Input Output Dependency
89 * ------ ----- ------ -------
90 * 1 copy InputStream OutputStream (primitive)
91 * 2 copy Reader Writer (primitive)
92 *
93 * 3 copy InputStream Writer 2
94 * 4 toString InputStream String 3
95 * 5 toByteArray InputStream byte[] 1
96 *
97 * 6 copy Reader OutputStream 2
98 * 7 toString Reader String 2
99 * 8 toByteArray Reader byte[] 6
100 *
101 * 9 copy String OutputStream 2
102 * 10 copy String Writer (trivial)
103 * 11 toByteArray String byte[] 9
104 *
105 * 12 copy byte[] Writer 3
106 * 13 toString byte[] String 12
107 * 14 copy byte[] OutputStream (trivial)
108 *
109 *
110 * Note that only the first two methods shuffle bytes; the rest use these two, or (if possible) copy
111 * using native Java copy methods. As there are method variants to specify buffer size and encoding,
112 * each row may correspond to up to 4 methods.
113 *
114 */
115 {
116 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
117
118 /**
119 * Private constructor to prevent instantiation.
120 */
121 private IOUtil()
122 {
123 }
124
125 ///////////////////////////////////////////////////////////////
126 // Core copy methods
127 ///////////////////////////////////////////////////////////////
128
129 /**
130 * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
131 *
132 * @param input the stream to read from
133 * @param output the stream to write to
134 * @throws IOException in case of an error
135 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()} or in
136 * Java 9 and later {@code InputStream.transferTo()}.
137 */
138 @Deprecated
139 public static void copy( @Nonnull final InputStream input, @Nonnull final OutputStream output )
140 throws IOException
141 {
142 copy( input, output, DEFAULT_BUFFER_SIZE );
143 }
144
145 /**
146 * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
147 *
148 * In Java 9 and later this is replaced by {@code InputStream.transferTo()}.
149 *
150 * @param input the stream to read from
151 * @param output the stream to write to
152 * @param bufferSize size of internal buffer
153 * @throws IOException in case of an error
154 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()} or in
155 * Java 9 and later {@code InputStream.transferTo()}.
156 */
157 @Deprecated
158 public static void copy( @Nonnull final InputStream input, @Nonnull final OutputStream output,
159 final int bufferSize )
160 throws IOException
161 {
162 final byte[] buffer = new byte[bufferSize];
163 int n;
164 while ( -1 != ( n = input.read( buffer ) ) )
165 {
166 output.write( buffer, 0, n );
167 }
168 }
169
170 /**
171 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
172 *
173 * @param input the reader to read from
174 * @param output the writer to write to
175 * @throws IOException in case of failure * @deprecated use {@code org.apache.commons.io.IOUtils.copy()}.
176 */
177 @Deprecated
178 public static void copy( @Nonnull final Reader input, @Nonnull final Writer output )
179 throws IOException
180 {
181 copy( input, output, DEFAULT_BUFFER_SIZE );
182 }
183
184 /**
185 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
186 *
187 * @param input the reader to read from
188 * @param output the writer to write to
189 * @param bufferSize size of internal buffer
190 * @throws IOException in case of failure
191 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()}.
192 */
193 @Deprecated
194 public static void copy( @Nonnull final Reader input, @Nonnull final Writer output, final int bufferSize )
195 throws IOException
196 {
197 final char[] buffer = new char[bufferSize];
198 int n;
199 while ( -1 != ( n = input.read( buffer ) ) )
200 {
201 output.write( buffer, 0, n );
202 }
203 output.flush();
204 }
205
206 ///////////////////////////////////////////////////////////////
207 // Derived copy methods
208 // InputStream -> *
209 ///////////////////////////////////////////////////////////////
210
211 ///////////////////////////////////////////////////////////////
212 // InputStream -> Writer
213
214 /**
215 * Copy and convert bytes from an <code>InputStream</code> to chars on a
216 * <code>Writer</code>.
217 *
218 * The platform's default encoding is used for the byte-to-char conversion.
219 *
220 * @param input the reader to read from
221 * @param output the writer to write to
222 * @throws IOException in case of failure
223 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()}.
224 */
225 @Deprecated
226 public static void copy( @Nonnull final InputStream input, @Nonnull final Writer output )
227 throws IOException
228 {
229 copy( input, output, DEFAULT_BUFFER_SIZE );
230 }
231
232 /**
233 * Copy and convert bytes from an <code>InputStream</code> to chars on a
234 * <code>Writer</code>.
235 * The platform's default encoding is used for the byte-to-char conversion.
236 *
237 * @param input the input stream to read from
238 * @param output the writer to write to
239 * @param bufferSize size of internal buffer
240 * @throws IOException in case of failure
241 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()}.
242 */
243 @Deprecated
244 public static void copy( @Nonnull final InputStream input, @Nonnull final Writer output, final int bufferSize )
245 throws IOException
246 {
247 final InputStreamReader in = new InputStreamReader( input );
248 copy( in, output, bufferSize );
249 }
250
251 /**
252 * Copy and convert bytes from an <code>InputStream</code> to chars on a
253 * <code>Writer</code>, using the specified encoding.
254 *
255 * @param input the input stream to read from
256 * @param output the writer to write to
257 * @param encoding the name of a supported character encoding. See the
258 * <a href="https://www.iana.org/assignments/character-sets">IANA
259 * Charset Registry</a> for a list of valid encoding types.
260 * @throws IOException in case of failure
261 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()}.
262 */
263 @Deprecated
264 public static void copy( @Nonnull final InputStream input, @Nonnull final Writer output,
265 @Nonnull final String encoding )
266 throws IOException
267 {
268 final InputStreamReader in = new InputStreamReader( input, encoding );
269 copy( in, output );
270 }
271
272 /**
273 * Copy and convert bytes from an <code>InputStream</code> to chars on a
274 * <code>Writer</code>, using the specified encoding.
275 *
276 * @param encoding the name of a supported character encoding. See the
277 * <a href="https://www.iana.org/assignments/character-sets">IANA
278 * Charset Registry</a> for a list of valid encoding types.
279 * @param input the input stream to read from
280 * @param output the writer to write to
281 * @param bufferSize size of internal buffer
282 * @throws IOException in case of failure
283 * @deprecated use {@code org.apache.commons.io.IOUtils.copy()}.
284 */
285 @Deprecated
286 public static void copy( @Nonnull final InputStream input, @Nonnull final Writer output,
287 @Nonnull final String encoding, final int bufferSize )
288 throws IOException
289 {
290 final InputStreamReader in = new InputStreamReader( input, encoding );
291 copy( in, output, bufferSize );
292 }
293
294 ///////////////////////////////////////////////////////////////
295 // InputStream -> String
296
297 /**
298 * Get the contents of an <code>InputStream</code> as a String.
299 * The platform's default encoding is used for the byte-to-char conversion.
300 *
301 * @param input the InputStream to read from
302 * @return the resulting string
303 * @throws IOException in case of failure
304 * @deprecated always specify a character encoding
305 */
306 @Deprecated
307 @Nonnull public static String toString( @Nonnull final InputStream input )
308 throws IOException
309 {
310 return toString( input, DEFAULT_BUFFER_SIZE );
311 }
312
313 /**
314 * Get the contents of an <code>InputStream</code> as a String.
315 * The platform's default encoding is used for the byte-to-char conversion.
316 *
317 * @param input the InputStream to read from
318 * @param bufferSize size of internal buffer
319 * @return the resulting string
320 * @throws IOException in case of failure
321 * @deprecated always specify a character encoding
322 */
323 @Deprecated
324 @Nonnull public static String toString( @Nonnull final InputStream input, final int bufferSize )
325 throws IOException
326 {
327 final StringWriter sw = new StringWriter();
328 copy( input, sw, bufferSize );
329 return sw.toString();
330 }
331
332 /**
333 * Get the contents of an <code>InputStream</code> as a String.
334 *
335 * @param input the InputStream to read from
336 * @param encoding the name of a supported character encoding. See the
337 * <a href="https://www.iana.org/assignments/character-sets">IANA
338 * Charset Registry</a> for a list of valid encoding types.
339 * @return the converted string
340 * @throws IOException in case of failure
341 * @deprecated use {@code org.apache.commons.io.IOUtils.toString()}.
342 */
343 @Deprecated
344 @Nonnull public static String toString( @Nonnull final InputStream input, @Nonnull final String encoding )
345 throws IOException
346 {
347 return toString( input, encoding, DEFAULT_BUFFER_SIZE );
348 }
349
350 /**
351 * Get the contents of an <code>InputStream</code> as a String.
352 *
353 * @param input the InputStream to read from
354 * @param encoding the name of a supported character encoding. See the
355 * <a href="https://www.iana.org/assignments/character-sets">IANA
356 * Charset Registry</a> for a list of valid encoding types.
357 * @param bufferSize size of internal buffer
358 * @return The converted string.
359 * @throws IOException in case of failure
360 * @deprecated use {@code org.apache.commons.io.IOUtils.toString()}.
361 */
362 @Deprecated
363 @Nonnull public static String toString( @Nonnull final InputStream input, @Nonnull final String encoding,
364 final int bufferSize )
365 throws IOException
366 {
367 final StringWriter sw = new StringWriter();
368 copy( input, sw, encoding, bufferSize );
369 return sw.toString();
370 }
371
372 ///////////////////////////////////////////////////////////////
373 // InputStream -> byte[]
374
375 /**
376 * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
377 *
378 * @param input the InputStream to read from
379 * @return the resulting byte array.
380 * @throws IOException in case of failure
381 * @deprecated use {@code org.apache.commons.io.IOUtils.readFully()}.
382 */
383 @Deprecated
384 @Nonnull public static byte[] toByteArray( @Nonnull final InputStream input )
385 throws IOException
386 {
387 return toByteArray( input, DEFAULT_BUFFER_SIZE );
388 }
389
390 /**
391 * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
392 *
393 * @param input the InputStream to read from
394 * @param bufferSize size of internal buffer
395 * @return the resulting byte array.
396 * @throws IOException in case of failure
397 * @deprecated use {@code org.apache.commons.io.IOUtils.readFully()}.
398 */
399 @Deprecated
400 @Nonnull public static byte[] toByteArray( @Nonnull final InputStream input, final int bufferSize )
401 throws IOException
402 {
403 final ByteArrayOutputStream output = new ByteArrayOutputStream();
404 copy( input, output, bufferSize );
405 return output.toByteArray();
406 }
407
408 ///////////////////////////////////////////////////////////////
409 // Derived copy methods
410 // Reader -> *
411 ///////////////////////////////////////////////////////////////
412
413 ///////////////////////////////////////////////////////////////
414 // Reader -> OutputStream
415
416 /**
417 * Serialize chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>, and
418 * flush the <code>OutputStream</code>.
419 *
420 * @param input the InputStream to read from
421 * @param output the output stream to write to
422 * @throws IOException in case of failure
423 * @deprecated always specify a character encoding
424 */
425 @Deprecated
426 public static void copy( @Nonnull final Reader input, @Nonnull final OutputStream output )
427 throws IOException
428 {
429 copy( input, output, DEFAULT_BUFFER_SIZE );
430 }
431
432 /**
433 * Serialize chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>, and
434 * flush the <code>OutputStream</code>.
435 *
436 * @param input the InputStream to read from
437 * @param output the output to write to
438 * @param bufferSize size of internal buffer
439 * @throws IOException in case of failure
440 * @deprecated always specify a character encoding
441 */
442 @Deprecated
443 public static void copy( @Nonnull final Reader input, @Nonnull final OutputStream output, final int bufferSize )
444 throws IOException
445 {
446 final OutputStreamWriter out = new OutputStreamWriter( output );
447 copy( input, out, bufferSize );
448 // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
449 // here.
450 out.flush();
451 }
452
453 ///////////////////////////////////////////////////////////////
454 // Reader -> String
455
456 /**
457 * Get the contents of a <code>Reader</code> as a String.
458 * @param input the InputStream to read from
459 * @return The converted string.
460 * @throws IOException in case of failure
461 * @deprecated use {@code org.apache.commons.io.IOUtils.toString()}.
462 */
463 @Deprecated
464 @Nonnull public static String toString( @Nonnull final Reader input )
465 throws IOException
466 {
467 return toString( input, DEFAULT_BUFFER_SIZE );
468 }
469
470 /**
471 * Get the contents of a <code>Reader</code> as a String.
472 *
473 * @param input the reader to read from
474 * @param bufferSize size of internal buffer
475 * @return the resulting byte array.
476 * @throws IOException in case of failure
477 * @deprecated use {@code org.apache.commons.io.IOUtils.toString()}.
478 */
479 @Deprecated
480 @Nonnull public static String toString( @Nonnull final Reader input, final int bufferSize )
481 throws IOException
482 {
483 final StringWriter sw = new StringWriter();
484 copy( input, sw, bufferSize );
485 return sw.toString();
486 }
487
488 ///////////////////////////////////////////////////////////////
489 // Reader -> byte[]
490
491 /**
492 * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
493 *
494 * @param input the InputStream to read from
495 * @return the resulting byte array.
496 * @throws IOException in case of failure
497 * @deprecated always specify a character encoding
498 */
499 @Deprecated
500 @Nonnull public static byte[] toByteArray( @Nonnull final Reader input )
501 throws IOException
502 {
503 return toByteArray( input, DEFAULT_BUFFER_SIZE );
504 }
505
506 /**
507 * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
508 *
509 * @param input the InputStream to read from
510 * @param bufferSize size of internal buffer
511 * @return the resulting byte array.
512 * @throws IOException in case of failure
513 * @deprecated always specify a character encoding
514 */
515 @Deprecated
516 @Nonnull public static byte[] toByteArray( @Nonnull final Reader input, final int bufferSize )
517 throws IOException
518 {
519 ByteArrayOutputStream output = new ByteArrayOutputStream();
520 copy( input, output, bufferSize );
521 return output.toByteArray();
522 }
523
524 ///////////////////////////////////////////////////////////////
525 // Derived copy methods
526 // String -> *
527 ///////////////////////////////////////////////////////////////
528
529 ///////////////////////////////////////////////////////////////
530 // String -> OutputStream
531
532 /**
533 * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
534 * flush the <code>OutputStream</code>.
535 * @param input the InputStream to read from
536 * @param output the output to write to
537 * @throws IOException in case of failure
538 * @deprecated always specify a character encoding
539 */
540 @Deprecated
541 public static void copy( @Nonnull final String input, @Nonnull final OutputStream output )
542 throws IOException
543 {
544 copy( input, output, DEFAULT_BUFFER_SIZE );
545 }
546
547 /**
548 * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
549 * flush the <code>OutputStream</code>.
550 *
551 * @param input the InputStream to read from
552 * @param output the output to write to
553 * @param bufferSize size of internal buffer
554 * @throws IOException in case of failure
555 * @deprecated always specify a character encoding
556 */
557 @Deprecated
558 public static void copy( @Nonnull final String input, @Nonnull final OutputStream output, final int bufferSize )
559 throws IOException
560 {
561 final StringReader in = new StringReader( input );
562 final OutputStreamWriter out = new OutputStreamWriter( output );
563 copy( in, out, bufferSize );
564 // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
565 // here.
566 out.flush();
567 }
568
569 ///////////////////////////////////////////////////////////////
570 // String -> Writer
571
572 /**
573 * Copy chars from a <code>String</code> to a <code>Writer</code>.
574 *
575 * @param input the string to write
576 * @param output resulting output {@link Writer}
577 * @throws IOException in case of failure
578 * @deprecated use {@code org.apache.commons.io.IOUtils.write()}.
579 */
580 @Deprecated
581 public static void copy( @Nonnull final String input, @Nonnull final Writer output )
582 throws IOException
583 {
584 output.write( input );
585 }
586
587 ///////////////////////////////////////////////////////////////
588 // String -> byte[]
589
590 /**
591 * Get the contents of a <code>String</code> as a <code>byte[]</code>.
592 *
593 * @param input the String to read from
594 * @return the resulting byte array
595 * @throws IOException in case of failure
596 * @deprecated always specify a character encoding
597 */
598 @Deprecated
599 @Nonnull public static byte[] toByteArray( @Nonnull final String input )
600 throws IOException
601 {
602 return toByteArray( input, DEFAULT_BUFFER_SIZE );
603 }
604
605 /**
606 * Get the contents of a <code>String</code> as a <code>byte[]</code>.
607 *
608 * @param input the InputStream to read from
609 * @param bufferSize size of internal buffer
610 * @return the resulting byte array
611 * @throws IOException in case of failure
612 * @deprecated always specify a character encoding
613 */
614 @Deprecated
615 @Nonnull public static byte[] toByteArray( @Nonnull final String input, final int bufferSize )
616 throws IOException
617 {
618 ByteArrayOutputStream output = new ByteArrayOutputStream();
619 copy( input, output, bufferSize );
620 return output.toByteArray();
621 }
622
623 ///////////////////////////////////////////////////////////////
624 // Derived copy methods
625 // byte[] -> *
626 ///////////////////////////////////////////////////////////////
627
628 ///////////////////////////////////////////////////////////////
629 // byte[] -> Writer
630
631 /**
632 * Copy and convert bytes from a <code>byte[]</code> to chars on a
633 * <code>Writer</code>.
634 * The platform's default encoding is used for the byte-to-char conversion.
635 *
636 * @param input the InputStream to read from
637 * @param output the output to write to
638 * @throws IOException in case of failure
639 * @deprecated always specify a character encoding
640 */
641 @Deprecated
642 public static void copy( @Nonnull final byte[] input, @Nonnull final Writer output )
643 throws IOException
644 {
645 copy( input, output, DEFAULT_BUFFER_SIZE );
646 }
647
648 /**
649 * Copy and convert bytes from a <code>byte[]</code> to chars on a
650 * <code>Writer</code>.
651 * The platform's default encoding is used for the byte-to-char conversion.
652 *
653 * @param input the InputStream to read from
654 * @param output the output to write to
655 * @param bufferSize size of internal buffer
656 * @throws IOException in case of failure
657 * @deprecated always specify a character encoding
658 */
659 @Deprecated
660 public static void copy( @Nonnull final byte[] input, @Nonnull final Writer output, final int bufferSize )
661 throws IOException
662 {
663 final ByteArrayInputStream in = new ByteArrayInputStream( input );
664 copy( in, output, bufferSize );
665 }
666
667 /**
668 * Copy and convert bytes from a <code>byte[]</code> to chars on a
669 * <code>Writer</code>, using the specified encoding.
670 *
671 * @param encoding the name of a supported character encoding. See the
672 * <a href="https://www.iana.org/assignments/character-sets">IANA
673 * Charset Registry</a> for a list of valid encoding types.
674 * @param input the data to write
675 * @param output the writer to write to
676 * @throws IOException in case of failure
677 * @deprecated use {@code org.apache.commons.io.IOUtils.write()}.
678 */
679 @Deprecated
680 public static void copy( @Nonnull final byte[] input, @Nonnull final Writer output, final String encoding )
681 throws IOException
682 {
683 final ByteArrayInputStream in = new ByteArrayInputStream( input );
684 copy( in, output, encoding );
685 }
686
687 /**
688 * Copy and convert bytes from a <code>byte[]</code> to chars on a
689 * <code>Writer</code>, using the specified encoding.
690 *
691 * @param encoding the name of a supported character encoding. See the
692 * <a href="https://www.iana.org/assignments/character-sets">IANA
693 * Charset Registry</a> for a list of valid encoding types.
694 * @param input the input bytes
695 * @param output The output buffer {@link Writer}
696 * @param bufferSize size of internal buffer
697 * @throws IOException in case of failure
698 * @deprecated use {@code org.apache.commons.io.IOUtils.write()}.
699 */
700 @Deprecated
701 public static void copy( @Nonnull final byte[] input, @Nonnull final Writer output, @Nonnull final String encoding,
702 final int bufferSize )
703 throws IOException
704 {
705 final ByteArrayInputStream in = new ByteArrayInputStream( input );
706 copy( in, output, encoding, bufferSize );
707 }
708
709 ///////////////////////////////////////////////////////////////
710 // byte[] -> String
711
712 /**
713 * Get the contents of a <code>byte[]</code> as a String.
714 * The platform's default encoding is used for the byte-to-char conversion.
715 * @param input the input bytes
716 * @return the resulting string
717 * @throws IOException in case of failure
718 * @deprecated always specify a character encoding
719 */
720 @Deprecated
721 @Nonnull public static String toString( @Nonnull final byte[] input )
722 throws IOException
723 {
724 return toString( input, DEFAULT_BUFFER_SIZE );
725 }
726
727 /**
728 * Get the contents of a <code>byte[]</code> as a String.
729 * The platform's default encoding is used for the byte-to-char conversion.
730 *
731 * @param bufferSize size of internal buffer
732 * @param input the input bytes
733 * @return the created string
734 * @throws IOException in case of failure
735 * @deprecated always specify a character encoding
736 */
737 @Deprecated
738 @Nonnull public static String toString( @Nonnull final byte[] input, final int bufferSize )
739 throws IOException
740 {
741 final StringWriter sw = new StringWriter();
742 copy( input, sw, bufferSize );
743 return sw.toString();
744 }
745
746 /**
747 * Get the contents of a <code>byte[]</code> as a String.
748 *
749 * @param encoding the name of a supported character encoding. See the
750 * <a href="https://www.iana.org/assignments/character-sets">IANA
751 * Charset Registry</a> for a list of valid encoding types.
752 * @param input the input bytes
753 * @return the resulting string
754 * @throws IOException in case of failure
755 * @deprecated use {@code new String(input, encoding)}
756 */
757 @Deprecated
758 @Nonnull public static String toString( @Nonnull final byte[] input, @Nonnull final String encoding )
759 throws IOException
760 {
761 return toString( input, encoding, DEFAULT_BUFFER_SIZE );
762 }
763
764 /**
765 * Get the contents of a <code>byte[]</code> as a String.
766 *
767 * @param encoding the name of a supported character encoding. See the
768 * <a href="https://www.iana.org/assignments/character-sets">IANA
769 * Charset Registry</a> for a list of valid encoding types.
770 * @param bufferSize size of internal buffer
771 * @param input input bytes
772 * @return the resulting string
773 * @throws IOException in case of failure
774 * @deprecated use {@code new String(input, encoding)}
775 */
776 @Deprecated
777 @Nonnull public static String toString( @Nonnull final byte[] input, @Nonnull final String encoding,
778 final int bufferSize )
779 throws IOException
780 {
781 final StringWriter sw = new StringWriter();
782 copy( input, sw, encoding, bufferSize );
783 return sw.toString();
784 }
785
786 ///////////////////////////////////////////////////////////////
787 // byte[] -> OutputStream
788
789 /**
790 * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
791 *
792 * @param input Input byte array.
793 * @param output output stream {@link OutputStream}
794 * @throws IOException in case of failure
795 * @deprecated inline this method
796 */
797 @Deprecated
798 public static void copy( @Nonnull final byte[] input, @Nonnull final OutputStream output )
799 throws IOException
800 {
801 output.write( input );
802 }
803
804 /**
805 * Compare the contents of two streams to determine if they are equal or not.
806 *
807 * @param input1 the first stream
808 * @param input2 the second stream
809 * @return true if the content of the streams are equal or they both don't exist, false otherwise
810 * @throws IOException in case of failure
811 * @deprecated use {@code org.apache.commons.io.IOUtils.contentEquals()}
812 */
813 @Deprecated
814 public static boolean contentEquals( @Nonnull final InputStream input1, @Nonnull final InputStream input2 )
815 throws IOException
816 {
817 final InputStream bufferedInput1 = new BufferedInputStream( input1 );
818 final InputStream bufferedInput2 = new BufferedInputStream( input2 );
819
820 int ch = bufferedInput1.read();
821 while ( -1 != ch )
822 {
823 final int ch2 = bufferedInput2.read();
824 if ( ch != ch2 )
825 {
826 return false;
827 }
828 ch = bufferedInput1.read();
829 }
830
831 final int ch2 = bufferedInput2.read();
832 return -1 == ch2;
833 }
834
835 // ----------------------------------------------------------------------
836 // closeXXX()
837 // ----------------------------------------------------------------------
838
839 /**
840 * <p>Closes a {@code Channel} suppressing any {@code IOException}.</p>
841 * <p>
842 * <b>Note:</b> The use case justifying this method is a shortcoming of the Java language up to but not including
843 * Java 7. For any code targeting Java 7 or later use of this method is highly discouraged and the
844 * {@code try-with-resources} statement should be used instead. Care must be taken to not use this method in a way
845 * {@code IOException}s get suppressed incorrectly.
846 * <strong>You must close all resources in use inside the {@code try} block to not suppress exceptions in the
847 * {@code finally} block incorrectly by using this method.</strong>
848 * </p>
849 * <p>
850 * <b>Example:</b>
851 * </p>
852 * <pre>
853 * // Introduce variables for the resources and initialize them to null. This cannot throw an exception.
854 * Closeable resource1 = null;
855 * Closeable resource2 = null;
856 * try
857 * {
858 * // Obtain a resource object and assign it to variable resource1. This may throw an exception.
859 * // If successful, resource1 != null.
860 * resource1 = ...
861 *
862 * // Obtain a resource object and assign it to variable resource2. This may throw an exception.
863 * // If successful, resource2 != null. Not reached if an exception has been thrown above.
864 * resource2 = ...
865 *
866 * // Perform operations on the resources. This may throw an exception. Not reached if an exception has been
867 * // thrown above. Note: Treat the variables resource1 and resource2 the same way as if they would have been
868 * // declared with the final modifier - that is - do NOT write anyting like resource1 = something else or
869 * // resource2 = something else here.
870 * resource1 ...
871 * resource2 ...
872 *
873 * // Finally, close the resources and set the variables to null indicating successful completion.
874 * // This may throw an exception. Not reached if an exception has been thrown above.
875 * resource1.close();
876 * resource1 = null;
877 * // Not reached if an exception has been thrown above.
878 * resource2.close();
879 * resource2 = null;
880 *
881 * // All resources are closed at this point and all operations (up to here) completed successfully without
882 * // throwing an exception we would need to handle (by letting it propagate or by catching and handling it).
883 * }
884 * finally
885 * {
886 * // Cleanup any resource not closed in the try block due to an exception having been thrown and suppress any
887 * // exception this may produce to not stop the exception from the try block to be propagated. If the try
888 * // block completed successfully, all variables will have been set to null there and this will not do
889 * // anything. This is just to cleanup properly in case of an exception.
890 *
891 * IOUtil.close( resource1 );
892 * IOUtil.close( resource2 );
893 *
894 * // Without that utility method you would need to write the following:
895 * //
896 * // try
897 * // {
898 * // if ( resource1 != null )
899 * // {
900 * // resource1.close();
901 * // }
902 * // }
903 * // catch( IOException e )
904 * // {
905 * // Suppressed. If resource1 != null, an exception has already been thrown in the try block we need to
906 * // propagate instead of this one.
907 * // }
908 * // finally
909 * // {
910 * // try
911 * // {
912 * // if ( resource2 != null )
913 * // {
914 * // resource2.close();
915 * // }
916 * // }
917 * // catch ( IOException e )
918 * // {
919 * // Suppressed. If resource2 != null, an exception has already been thrown in the try block we need to
920 * // propagate instead of this one.
921 * // }
922 * // }
923 * }
924 * </pre>
925 *
926 * @param channel The channel to close or {@code null}.
927 * @deprecated use try-with-resources
928 */
929 @Deprecated
930 public static void close( @Nullable Channel channel )
931 {
932 try
933 {
934 if ( channel != null )
935 {
936 channel.close();
937 }
938 }
939 catch ( IOException ex )
940 {
941 // Suppressed
942 }
943 }
944
945 /**
946 * <p>Closes an {@code InputStream} suppressing any {@code IOException}.</p>
947 * <p>
948 * <b>Note:</b> The use case justifying this method is a shortcoming of the Java language up to but not including
949 * Java 7. For any code targeting Java 7 or later use of this method is highly discouraged and the
950 * {@code try-with-resources} statement should be used instead. Care must be taken to not use this method in a way
951 * {@code IOException}s get suppressed incorrectly.
952 * <strong>You must close all resources in use inside the {@code try} block to not suppress exceptions in the
953 * {@code finally} block incorrectly by using this method.</strong>
954 * </p>
955 * <p>
956 * <b>Example:</b>
957 * </p>
958 * <pre>
959 * // Introduce variables for the resources and initialize them to null. This cannot throw an exception.
960 * Closeable resource1 = null;
961 * Closeable resource2 = null;
962 * try
963 * {
964 * // Obtain a resource object and assign it to variable resource1. This may throw an exception.
965 * // If successful, resource1 != null.
966 * resource1 = ...
967 *
968 * // Obtain a resource object and assign it to variable resource2. This may throw an exception.
969 * // If successful, resource2 != null. Not reached if an exception has been thrown above.
970 * resource2 = ...
971 *
972 * // Perform operations on the resources. This may throw an exception. Not reached if an exception has been
973 * // thrown above. Note: Treat the variables resource1 and resource2 the same way as if they would have been
974 * // declared with the final modifier - that is - do NOT write anyting like resource1 = something else or
975 * // resource2 = something else here.
976 * resource1 ...
977 * resource2 ...
978 *
979 * // Finally, close the resources and set the variables to null indicating successful completion.
980 * // This may throw an exception. Not reached if an exception has been thrown above.
981 * resource1.close();
982 * resource1 = null;
983 * // This may throw an exception. Not reached if an exception has been thrown above.
984 * resource2.close();
985 * resource2 = null;
986 *
987 * // All resources are closed at this point and all operations (up to here) completed successfully without
988 * // throwing an exception we would need to handle (by letting it propagate or by catching and handling it).
989 * }
990 * finally
991 * {
992 * // Cleanup any resource not closed in the try block due to an exception having been thrown and suppress any
993 * // exception this may produce to not stop the exception from the try block to be propagated. If the try
994 * // block completed successfully, all variables will have been set to null there and this will not do
995 * // anything. This is just to cleanup properly in case of an exception.
996 *
997 * IOUtil.close( resource1 );
998 * IOUtil.close( resource2 );
999 *
1000 * // Without that utility method you would need to write the following:
1001 * //
1002 * // try
1003 * // {
1004 * // if ( resource1 != null )
1005 * // {
1006 * // resource1.close();
1007 * // }
1008 * // }
1009 * // catch( IOException e )
1010 * // {
1011 * // Suppressed. If resource1 != null, an exception has already been thrown in the try block we need to
1012 * // propagate instead of this one.
1013 * // }
1014 * // finally
1015 * // {
1016 * // try
1017 * // {
1018 * // if ( resource2 != null )
1019 * // {
1020 * // resource2.close();
1021 * // }
1022 * // }
1023 * // catch ( IOException e )
1024 * // {
1025 * // Suppressed. If resource2 != null, an exception has already been thrown in the try block we need to
1026 * // propagate instead of this one.
1027 * // }
1028 * // }
1029 * }
1030 * </pre>
1031 *
1032 * @param inputStream The stream to close or {@code null}.
1033 * @deprecated use try-with-resources
1034 */
1035 @Deprecated
1036 public static void close( @Nullable InputStream inputStream )
1037 {
1038 try
1039 {
1040 if ( inputStream != null )
1041 {
1042 inputStream.close();
1043 }
1044 }
1045 catch ( IOException ex )
1046 {
1047 // Suppressed
1048 }
1049 }
1050
1051 /**
1052 * <p>Closes an {@code OutputStream} suppressing any {@code IOException}.</p>
1053 * <p>
1054 * <b>Note:</b> The use case justifying this method is a shortcoming of the Java language up to but not including
1055 * Java 7. For any code targeting Java 7 or later use of this method is highly discouraged and the
1056 * {@code try-with-resources} statement should be used instead. Care must be taken to not use this method in a way
1057 * {@code IOException}s get suppressed incorrectly.
1058 * <strong>You must close all resources in use inside the {@code try} block to not suppress exceptions in the
1059 * {@code finally} block incorrectly by using this method.</strong>
1060 * </p>
1061 * <p>
1062 * <b>Example:</b>
1063 * </p>
1064 * <pre>
1065 * // Introduce variables for the resources and initialize them to null. This cannot throw an exception.
1066 * Closeable resource1 = null;
1067 * Closeable resource2 = null;
1068 * try
1069 * {
1070 * // Obtain a resource object and assign it to variable resource1. This may throw an exception.
1071 * // If successful, resource1 != null.
1072 * resource1 = ...
1073 *
1074 * // Obtain a resource object and assign it to variable resource2. This may throw an exception.
1075 * // If successful, resource2 != null. Not reached if an exception has been thrown above.
1076 * resource2 = ...
1077 *
1078 * // Perform operations on the resources. This may throw an exception. Not reached if an exception has been
1079 * // thrown above. Note: Treat the variables resource1 and resource2 the same way as if they would have been
1080 * // declared with the final modifier - that is - do NOT write anyting like resource1 = something else or
1081 * // resource2 = something else here.
1082 * resource1 ...
1083 * resource2 ...
1084 *
1085 * // Finally, close the resources and set the variables to null indicating successful completion.
1086 * // This may throw an exception. Not reached if an exception has been thrown above.
1087 * resource1.close();
1088 * resource1 = null;
1089 * // This may throw an exception. Not reached if an exception has been thrown above.
1090 * resource2.close();
1091 * resource2 = null;
1092 *
1093 * // All resources are closed at this point and all operations (up to here) completed successfully without
1094 * // throwing an exception we would need to handle (by letting it propagate or by catching and handling it).
1095 * }
1096 * finally
1097 * {
1098 * // Cleanup any resource not closed in the try block due to an exception having been thrown and suppress any
1099 * // exception this may produce to not stop the exception from the try block to be propagated. If the try
1100 * // block completed successfully, all variables will have been set to null there and this will not do
1101 * // anything. This is just to cleanup properly in case of an exception.
1102 *
1103 * IOUtil.close( resource1 );
1104 * IOUtil.close( resource2 );
1105 *
1106 * // Without that utility method you would need to write the following:
1107 * //
1108 * // try
1109 * // {
1110 * // if ( resource1 != null )
1111 * // {
1112 * // resource1.close();
1113 * // }
1114 * // }
1115 * // catch( IOException e )
1116 * // {
1117 * // Suppressed. If resource1 != null, an exception has already been thrown in the try block we need to
1118 * // propagate instead of this one.
1119 * // }
1120 * // finally
1121 * // {
1122 * // try
1123 * // {
1124 * // if ( resource2 != null )
1125 * // {
1126 * // resource2.close();
1127 * // }
1128 * // }
1129 * // catch ( IOException e )
1130 * // {
1131 * // Suppressed. If resource2 != null, an exception has already been thrown in the try block we need to
1132 * // propagate instead of this one.
1133 * // }
1134 * // }
1135 * }
1136 * </pre>
1137 *
1138 * @param outputStream The stream to close or {@code null}.
1139 * @deprecated use try-with-resources
1140 */
1141 @Deprecated
1142 public static void close( @Nullable OutputStream outputStream )
1143 {
1144 try
1145 {
1146 if ( outputStream != null )
1147 {
1148 outputStream.close();
1149 }
1150 }
1151 catch ( IOException ex )
1152 {
1153 // Suppressed
1154 }
1155 }
1156
1157 /**
1158 * <p>Closes a {@code Reader} suppressing any {@code IOException}.</p>
1159 * <p>
1160 * <b>Note:</b> The use case justifying this method is a shortcoming of the Java language up to but not including
1161 * Java 7. For any code targeting Java 7 or later use of this method is highly discouraged and the
1162 * {@code try-with-resources} statement should be used instead. Care must be taken to not use this method in a way
1163 * {@code IOException}s get suppressed incorrectly.
1164 * <strong>You must close all resources in use inside the {@code try} block to not suppress exceptions in the
1165 * {@code finally} block incorrectly by using this method.</strong>
1166 * </p>
1167 * <p>
1168 * <b>Example:</b>
1169 * </p>
1170 * <pre>
1171 * // Introduce variables for the resources and initialize them to null. This cannot throw an exception.
1172 * Closeable resource1 = null;
1173 * Closeable resource2 = null;
1174 * try
1175 * {
1176 * // Obtain a resource object and assign it to variable resource1. This may throw an exception.
1177 * // If successful, resource1 != null.
1178 * resource1 = ...
1179 *
1180 * // Obtain a resource object and assign it to variable resource2. This may throw an exception.
1181 * // If successful, resource2 != null. Not reached if an exception has been thrown above.
1182 * resource2 = ...
1183 *
1184 * // Perform operations on the resources. This may throw an exception. Not reached if an exception has been
1185 * // thrown above. Note: Treat the variables resource1 and resource2 the same way as if they would have been
1186 * // declared with the final modifier - that is - do NOT write anyting like resource1 = something else or
1187 * // resource2 = something else here.
1188 * resource1 ...
1189 * resource2 ...
1190 *
1191 * // Finally, close the resources and set the variables to null indicating successful completion.
1192 * // This may throw an exception. Not reached if an exception has been thrown above.
1193 * resource1.close();
1194 * resource1 = null;
1195 * // This may throw an exception. Not reached if an exception has been thrown above.
1196 * resource2.close();
1197 * resource2 = null;
1198 *
1199 * // All resources are closed at this point and all operations (up to here) completed successfully without
1200 * // throwing an exception we would need to handle (by letting it propagate or by catching and handling it).
1201 * }
1202 * finally
1203 * {
1204 * // Cleanup any resource not closed in the try block due to an exception having been thrown and suppress any
1205 * // exception this may produce to not stop the exception from the try block to be propagated. If the try
1206 * // block completed successfully, all variables will have been set to null there and this will not do
1207 * // anything. This is just to cleanup properly in case of an exception.
1208 *
1209 * IOUtil.close( resource1 );
1210 * IOUtil.close( resource2 );
1211 *
1212 * // Without that utility method you would need to write the following:
1213 * //
1214 * // try
1215 * // {
1216 * // if ( resource1 != null )
1217 * // {
1218 * // resource1.close();
1219 * // }
1220 * // }
1221 * // catch( IOException e )
1222 * // {
1223 * // Suppressed. If resource1 != null, an exception has already been thrown in the try block we need to
1224 * // propagate instead of this one.
1225 * // }
1226 * // finally
1227 * // {
1228 * // try
1229 * // {
1230 * // if ( resource2 != null )
1231 * // {
1232 * // resource2.close();
1233 * // }
1234 * // }
1235 * // catch ( IOException e )
1236 * // {
1237 * // Suppressed. If resource2 != null, an exception has already been thrown in the try block we need to
1238 * // propagate instead of this one.
1239 * // }
1240 * // }
1241 * }
1242 * </pre>
1243 *
1244 * @param reader The reader to close or {@code null}.
1245 * @deprecated use try-with-resources
1246 */
1247 @Deprecated
1248 public static void close( @Nullable Reader reader )
1249 {
1250 try
1251 {
1252 if ( reader != null )
1253 {
1254 reader.close();
1255 }
1256 }
1257 catch ( IOException ex )
1258 {
1259 // Suppressed
1260 }
1261 }
1262
1263 /**
1264 * <p>Closes a {@code Writer} suppressing any {@code IOException}.</p>
1265 * <p>
1266 * <b>Note:</b> The use case justifying this method is a shortcoming of the Java language up to but not including
1267 * Java 7. For any code targeting Java 7 or later use of this method is highly discouraged and the
1268 * {@code try-with-resources} statement should be used instead. Care must be taken to not use this method in a way
1269 * {@code IOException}s get suppressed incorrectly.
1270 * <strong>You must close all resources in use inside the {@code try} block to not suppress exceptions in the
1271 * {@code finally} block incorrectly by using this method.</strong>
1272 * </p>
1273 * <p>
1274 * <b>Example:</b>
1275 * </p>
1276 * <pre>
1277 * // Introduce variables for the resources and initialize them to null. This cannot throw an exception.
1278 * Closeable resource1 = null;
1279 * Closeable resource2 = null;
1280 * try
1281 * {
1282 * // Obtain a resource object and assign it to variable resource1. This may throw an exception.
1283 * // If successful, resource1 != null.
1284 * resource1 = ...
1285 *
1286 * // Obtain a resource object and assign it to variable resource2. This may throw an exception.
1287 * // If successful, resource2 != null. Not reached if an exception has been thrown above.
1288 * resource2 = ...
1289 *
1290 * // Perform operations on the resources. This may throw an exception. Not reached if an exception has been
1291 * // thrown above. Note: Treat the variables resource1 and resource2 the same way as if they would have been
1292 * // declared with the final modifier - that is - do NOT write anyting like resource1 = something else or
1293 * // resource2 = something else here.
1294 * resource1 ...
1295 * resource2 ...
1296 *
1297 * // Finally, close the resources and set the variables to null indicating successful completion.
1298 * // This may throw an exception. Not reached if an exception has been thrown above.
1299 * resource1.close();
1300 * resource1 = null;
1301 * // This may throw an exception. Not reached if an exception has been thrown above.
1302 * resource2.close();
1303 * resource2 = null;
1304 *
1305 * // All resources are closed at this point and all operations (up to here) completed successfully without
1306 * // throwing an exception we would need to handle (by letting it propagate or by catching and handling it).
1307 * }
1308 * finally
1309 * {
1310 * // Cleanup any resource not closed in the try block due to an exception having been thrown and suppress any
1311 * // exception this may produce to not stop the exception from the try block to be propagated. If the try
1312 * // block completed successfully, all variables will have been set to null there and this will not do
1313 * // anything. This is just to cleanup properly in case of an exception.
1314 *
1315 * IOUtil.close( resource1 );
1316 * IOUtil.close( resource2 );
1317 *
1318 * // Without that utility method you would need to write the following:
1319 * //
1320 * // try
1321 * // {
1322 * // if ( resource1 != null )
1323 * // {
1324 * // resource1.close();
1325 * // }
1326 * // }
1327 * // catch( IOException e )
1328 * // {
1329 * // Suppressed. If resource1 != null, an exception has already been thrown in the try block we need to
1330 * // propagate instead of this one.
1331 * // }
1332 * // finally
1333 * // {
1334 * // try
1335 * // {
1336 * // if ( resource2 != null )
1337 * // {
1338 * // resource2.close();
1339 * // }
1340 * // }
1341 * // catch ( IOException e )
1342 * // {
1343 * // Suppressed. If resource2 != null, an exception has already been thrown in the try block we need to
1344 * // propagate instead of this one.
1345 * // }
1346 * // }
1347 * }
1348 * </pre>
1349 *
1350 * @param writer The writer to close or {@code null}.
1351 * @deprecated use try-with-resources
1352 */
1353 @Deprecated
1354 public static void close( @Nullable Writer writer )
1355 {
1356 try
1357 {
1358 if ( writer != null )
1359 {
1360 writer.close();
1361 }
1362 }
1363 catch ( IOException ex )
1364 {
1365 // Suppressed
1366 }
1367 }
1368 }