@@ -562,12 +562,17 @@ public static void writeList(Collection<?> lines, OutputStream output, Charset e
562562 lineSeparator = LINE_SEPARATOR ;
563563 }
564564 Charset cs = Charsets .toCharset (encoding );
565- for (Object line : lines ) {
566- if (line != null ) {
567- output .write (line .toString ().getBytes (cs ));
565+ try {
566+ for (Object line : lines ) {
567+ if (line != null ) {
568+ output .write (line .toString ().getBytes (cs ));
569+ }
570+ output .write (lineSeparator .getBytes (cs ));
568571 }
569- output .write (lineSeparator .getBytes (cs ));
572+ } finally {
573+ IOUtils .closeQuietly (output );
570574 }
575+
571576 }
572577
573578 public static void writeList (Collection <?> lines ,
@@ -583,12 +588,17 @@ public static void writeList(Collection<?> lines, String lineSeparator,
583588 if (lineSeparator == null ) {
584589 lineSeparator = LINE_SEPARATOR ;
585590 }
586- for (Object line : lines ) {
587- if (line != null ) {
588- writer .write (line .toString ());
591+ try {
592+ for (Object line : lines ) {
593+ if (line != null ) {
594+ writer .write (line .toString ());
595+ }
596+ writer .write (lineSeparator );
589597 }
590- writer .write (lineSeparator );
598+ } finally {
599+ IOUtils .closeQuietly (writer );
591600 }
601+
592602 }
593603
594604 public static void copyLegacy (File source , File dest )
@@ -604,8 +614,8 @@ public static void copyLegacy(File source, File dest)
604614 output .write (buf , 0 , bytesRead );
605615 }
606616 } finally {
607- input . close ( );
608- output . close ( );
617+ IOUtils . closeQuietly ( input );
618+ IOUtils . closeQuietly ( output );
609619 }
610620 }
611621
@@ -650,9 +660,14 @@ public static long copyLarge(InputStream input, OutputStream output, byte[] buff
650660 throws IOException {
651661 long count = 0 ;
652662 int n = 0 ;
653- while (EOF != (n = input .read (buffer ))) {
654- output .write (buffer , 0 , n );
655- count += n ;
663+ try {
664+ while (EOF != (n = input .read (buffer ))) {
665+ output .write (buffer , 0 , n );
666+ count += n ;
667+ }
668+ } finally {
669+ IOUtils .closeQuietly (input );
670+ IOUtils .closeQuietly (output );
656671 }
657672 return count ;
658673 }
@@ -677,13 +692,18 @@ public static long copyLarge(InputStream input, OutputStream output,
677692 }
678693 int read ;
679694 long totalRead = 0 ;
680- while (bytesToRead > 0 && EOF != (read = input .read (buffer , 0 , bytesToRead ))) {
681- output .write (buffer , 0 , read );
682- totalRead += read ;
683- if (length > 0 ) { // only adjust length if not reading to the end
684- // Note the cast must work because buffer.length is an integer
685- bytesToRead = (int ) Math .min (length - totalRead , bufferLength );
695+ try {
696+ while (bytesToRead > 0 && EOF != (read = input .read (buffer , 0 , bytesToRead ))) {
697+ output .write (buffer , 0 , read );
698+ totalRead += read ;
699+ if (length > 0 ) { // only adjust length if not reading to the end
700+ // Note the cast must work because buffer.length is an integer
701+ bytesToRead = (int ) Math .min (length - totalRead , bufferLength );
702+ }
686703 }
704+ } finally {
705+ IOUtils .closeQuietly (input );
706+ IOUtils .closeQuietly (output );
687707 }
688708 return totalRead ;
689709 }
@@ -743,13 +763,18 @@ public static long copyLarge(Reader input, Writer output, final long inputOffset
743763 }
744764 int read ;
745765 long totalRead = 0 ;
746- while (bytesToRead > 0 && EOF != (read = input .read (buffer , 0 , bytesToRead ))) {
747- output .write (buffer , 0 , read );
748- totalRead += read ;
749- if (length > 0 ) { // only adjust length if not reading to the end
750- // Note the cast must work because buffer.length is an integer
751- bytesToRead = (int ) Math .min (length - totalRead , buffer .length );
766+ try {
767+ while (bytesToRead > 0 && EOF != (read = input .read (buffer , 0 , bytesToRead ))) {
768+ output .write (buffer , 0 , read );
769+ totalRead += read ;
770+ if (length > 0 ) { // only adjust length if not reading to the end
771+ // Note the cast must work because buffer.length is an integer
772+ bytesToRead = (int ) Math .min (length - totalRead , buffer .length );
773+ }
752774 }
775+ } finally {
776+ IOUtils .closeQuietly (input );
777+ IOUtils .closeQuietly (output );
753778 }
754779 return totalRead ;
755780 }
@@ -835,9 +860,9 @@ public static void skipFully(InputStream input, long toSkip) throws IOException
835860 *
836861 * @param input stream to skip
837862 * @param toSkip the number of characters to skip
838- * @throws IOException if there is a problem reading the file
863+ * @throws IOException if there is a problem reading the file
839864 * @throws IllegalArgumentException if toSkip is negative
840- * @throws EOFException if the number of characters skipped was incorrect
865+ * @throws EOFException if the number of characters skipped was incorrect
841866 * @see Reader#skip(long)
842867 * @since 2.0
843868 */
@@ -867,13 +892,17 @@ public static int read(Reader input, char[] buffer, int offset, int length) thro
867892 throw new IllegalArgumentException ("Length must not be negative: " + length );
868893 }
869894 int remaining = length ;
870- while (remaining > 0 ) {
871- int location = length - remaining ;
872- int count = input .read (buffer , offset + location , remaining );
873- if (EOF == count ) { // EOF
874- break ;
895+ try {
896+ while (remaining > 0 ) {
897+ int location = length - remaining ;
898+ int count = input .read (buffer , offset + location , remaining );
899+ if (EOF == count ) { // EOF
900+ break ;
901+ }
902+ remaining -= count ;
875903 }
876- remaining -= count ;
904+ } finally {
905+ IOUtils .closeQuietly (input );
877906 }
878907 return length - remaining ;
879908 }
@@ -913,13 +942,17 @@ public static int read(InputStream input, byte[] buffer, int offset, int length)
913942 throw new IllegalArgumentException ("Length must not be negative: " + length );
914943 }
915944 int remaining = length ;
916- while (remaining > 0 ) {
917- int location = length - remaining ;
918- int count = input .read (buffer , offset + location , remaining );
919- if (EOF == count ) { // EOF
920- break ;
945+ try {
946+ while (remaining > 0 ) {
947+ int location = length - remaining ;
948+ int count = input .read (buffer , offset + location , remaining );
949+ if (EOF == count ) { // EOF
950+ break ;
951+ }
952+ remaining -= count ;
921953 }
922- remaining -= count ;
954+ } finally {
955+ IOUtils .closeQuietly (input );
923956 }
924957 return length - remaining ;
925958 }
@@ -950,9 +983,9 @@ public static int read(InputStream input, byte[] buffer) throws IOException {
950983 * @param buffer destination
951984 * @param offset inital offset into buffer
952985 * @param length length to read, must be >= 0
953- * @throws IOException if there is a problem reading the file
986+ * @throws IOException if there is a problem reading the file
954987 * @throws IllegalArgumentException if length is negative
955- * @throws EOFException if the number of characters read was incorrect
988+ * @throws EOFException if the number of characters read was incorrect
956989 * @since 2.2
957990 */
958991 public static void readFully (Reader input , char [] buffer , int offset , int length ) throws IOException {
@@ -970,9 +1003,9 @@ public static void readFully(Reader input, char[] buffer, int offset, int length
9701003 *
9711004 * @param input where to read input from
9721005 * @param buffer destination
973- * @throws IOException if there is a problem reading the file
1006+ * @throws IOException if there is a problem reading the file
9741007 * @throws IllegalArgumentException if length is negative
975- * @throws EOFException if the number of characters read was incorrect
1008+ * @throws EOFException if the number of characters read was incorrect
9761009 * @since 2.2
9771010 */
9781011 public static void readFully (Reader input , char [] buffer ) throws IOException {
@@ -989,9 +1022,9 @@ public static void readFully(Reader input, char[] buffer) throws IOException {
9891022 * @param buffer destination
9901023 * @param offset inital offset into buffer
9911024 * @param length length to read, must be >= 0
992- * @throws IOException if there is a problem reading the file
1025+ * @throws IOException if there is a problem reading the file
9931026 * @throws IllegalArgumentException if length is negative
994- * @throws EOFException if the number of bytes read was incorrect
1027+ * @throws EOFException if the number of bytes read was incorrect
9951028 * @since 2.2
9961029 */
9971030 public static void readFully (InputStream input , byte [] buffer , int offset , int length ) throws IOException {
@@ -1009,9 +1042,9 @@ public static void readFully(InputStream input, byte[] buffer, int offset, int l
10091042 *
10101043 * @param input where to read input from
10111044 * @param buffer destination
1012- * @throws IOException if there is a problem reading the file
1045+ * @throws IOException if there is a problem reading the file
10131046 * @throws IllegalArgumentException if length is negative
1014- * @throws EOFException if the number of bytes read was incorrect
1047+ * @throws EOFException if the number of bytes read was incorrect
10151048 * @since 2.2
10161049 */
10171050 public static void readFully (InputStream input , byte [] buffer ) throws IOException {
0 commit comments