@@ -135,11 +135,11 @@ public class IOUtils {
135135
136136 static {
137137 // avoid security issues
138- final StringBuilderWriter buf = new StringBuilderWriter (4 );
139- final PrintWriter out = new PrintWriter (buf );
140- out .println ();
141- LINE_SEPARATOR = buf .toString ();
142- out . close ();
138+ try ( final StringBuilderWriter buf = new StringBuilderWriter (4 );
139+ final PrintWriter out = new PrintWriter (buf )) {
140+ out .println ();
141+ LINE_SEPARATOR = buf .toString ();
142+ }
143143 }
144144
145145 /**
@@ -762,9 +762,10 @@ public static BufferedInputStream buffer(final InputStream inputStream, int size
762762 * @throws IOException if an I/O error occurs
763763 */
764764 public static byte [] toByteArray (final InputStream input ) throws IOException {
765- final ByteArrayOutputStream output = new ByteArrayOutputStream ();
766- copy (input , output );
767- return output .toByteArray ();
765+ try (final ByteArrayOutputStream output = new ByteArrayOutputStream ()) {
766+ copy (input , output );
767+ return output .toByteArray ();
768+ }
768769 }
769770
770771 /**
@@ -864,9 +865,10 @@ public static byte[] toByteArray(final Reader input) throws IOException {
864865 * @since 2.3
865866 */
866867 public static byte [] toByteArray (final Reader input , final Charset encoding ) throws IOException {
867- final ByteArrayOutputStream output = new ByteArrayOutputStream ();
868- copy (input , output , encoding );
869- return output .toByteArray ();
868+ try (final ByteArrayOutputStream output = new ByteArrayOutputStream ()) {
869+ copy (input , output , encoding );
870+ return output .toByteArray ();
871+ }
870872 }
871873
872874 /**
@@ -1079,9 +1081,10 @@ public static String toString(final InputStream input) throws IOException {
10791081 * @since 2.3
10801082 */
10811083 public static String toString (final InputStream input , final Charset encoding ) throws IOException {
1082- final StringBuilderWriter sw = new StringBuilderWriter ();
1083- copy (input , sw , encoding );
1084- return sw .toString ();
1084+ try (final StringBuilderWriter sw = new StringBuilderWriter ()) {
1085+ copy (input , sw , encoding );
1086+ return sw .toString ();
1087+ }
10851088 }
10861089
10871090 /**
@@ -1120,9 +1123,10 @@ public static String toString(final InputStream input, final String encoding)
11201123 * @throws IOException if an I/O error occurs
11211124 */
11221125 public static String toString (final Reader input ) throws IOException {
1123- final StringBuilderWriter sw = new StringBuilderWriter ();
1124- copy (input , sw );
1125- return sw .toString ();
1126+ try (final StringBuilderWriter sw = new StringBuilderWriter ()) {
1127+ copy (input , sw );
1128+ return sw .toString ();
1129+ }
11261130 }
11271131
11281132 /**
0 commit comments