@@ -27,54 +27,48 @@ public class JsonIO {
2727
2828 /**
2929 * Converts an input stream to an UTF-8 encoded JSON object.
30+ *
3031 * @param inputStream The input stream.
3132 * @return A UTF-8 encoded JSON object.
3233 * @throws IOException If the conversion failed.
3334 */
3435 public static JSONObject toJSON (InputStream inputStream ) throws IOException {
3536
36- // Create output stream.
37- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
38- byte [] buffer = new byte [1024 ];
37+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
3938
40- // Read input stream.
41- int length = inputStream .read (buffer );
42- while (length != -1 ) {
43- outputStream .write (buffer , 0 , length );
44- length = inputStream .read (buffer );
45- }
46-
47- // Convert output stream to JSON.
48- JSONObject json = new JSONObject (outputStream .toString ("UTF-8" ));
39+ // Create buffer.
40+ byte [] buffer = new byte [1024 ];
4941
50- // Close the output stream.
51- outputStream .close ();
42+ // Convert input stream to byte array output stream.
43+ int length = inputStream .read (buffer );
44+ while (length != -1 ) {
45+ outputStream .write (buffer , 0 , length );
46+ length = inputStream .read (buffer );
47+ }
5248
53- return json ;
49+ // Convert output stream to JSON.
50+ return new JSONObject (outputStream .toString ("UTF-8" ));
51+ }
5452 }
5553
5654 /**
5755 * Converts a file to a UTF-8 encoded JSON object.
56+ *
5857 * @param file The file to convert.
5958 * @return A UTF-8 encoded JSON object.
6059 * @throws IOException If the conversion failed.
6160 */
6261 public static JSONObject toJSON (File file ) throws IOException {
6362
64- // Create file input stream.
65- FileInputStream fileInputStream = new FileInputStream (file );
63+ try (FileInputStream fileInputStream = new FileInputStream (file )) {
6664
67- // Convert input stream to JSON.
68- JSONObject json = toJSON (fileInputStream );
69-
70- // Close the input stream.
71- fileInputStream .close ();
72-
73- return json ;
65+ return toJSON (fileInputStream );
66+ }
7467 }
7568
7669 /**
7770 * Converts a JSON string to a UTF-8 encoded JSON object.
71+ *
7872 * @param string The JSON as a string.
7973 * @return A UTF-8 encoded JSON object.
8074 * @throws IOException If the conversion failed.
@@ -84,21 +78,19 @@ public static JSONObject toJSON(String string) throws IOException {
8478 // Get raw JSON.
8579 byte [] rawJSON = string .getBytes ();
8680
87- // Convert bytes to output stream.
88- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
89- outputStream .write (rawJSON );
81+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
9082
91- // Convert output stream to JSON .
92- JSONObject json = new JSONObject ( outputStream .toString ( "UTF-8" ) );
83+ // Convert bytes to output stream .
84+ outputStream .write ( rawJSON );
9385
94- // Close the output stream.
95- outputStream .close ();
96-
97- return json ;
86+ // Convert output stream to JSON.
87+ return new JSONObject (outputStream .toString ("UTF-8" ));
88+ }
9889 }
9990
10091 /**
10192 * Outputs a JSON object to a UTF-8 encoded file.
93+ *
10294 * @param json The JSON object to output.
10395 * @param file The file to create.
10496 * @throws IOException If the output failed.
@@ -110,8 +102,9 @@ public static void toFile(JSONObject json, File file) throws IOException {
110102
111103 /**
112104 * Outputs a JSON object to a UTF-8 encoded file.
113- * @param json The JSON object to output.
114- * @param file The file to create or to append to.
105+ *
106+ * @param json The JSON object to output.
107+ * @param file The file to create or to append to.
115108 * @param append Whether to append the JSON contents to a file.
116109 * @throws IOException If the output failed.
117110 */
@@ -120,15 +113,16 @@ public static void toFile(JSONObject json, File file, boolean append) throws IOE
120113 // Get raw JSON.
121114 byte [] rawJSON = json .toString ().getBytes ("UTF-8" );
122115
123- // Create output stream.
124- FileOutputStream fileOutputStream = new FileOutputStream (file , append );
116+ try (FileOutputStream fileOutputStream = new FileOutputStream (file , append )) {
125117
126- // Write the raw JSON.
127- fileOutputStream .write (rawJSON );
118+ // Write the raw JSON to file.
119+ fileOutputStream .write (rawJSON );
120+ }
128121 }
129122
130123 /**
131124 * Converts a JSON object to a UTF-8 encoded string.
125+ *
132126 * @param json The JSON object to convert.
133127 * @return A UTF-8 encoded string.
134128 */
@@ -137,17 +131,13 @@ public static String toString(JSONObject json) throws IOException {
137131 // Get raw JSON.
138132 byte [] rawJSON = json .toString ().getBytes ();
139133
140- // Convert bytes to output stream.
141- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
142- outputStream .write (rawJSON );
143-
144- // Convert output stream to string.
145- String jsonString = outputStream .toString ("UTF-8" );
134+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
146135
147- // Close output stream.
148- outputStream .close ();
136+ outputStream .write (rawJSON );
149137
150- return jsonString ;
138+ // Convert output stream to string.
139+ return outputStream .toString ("UTF-8" );
140+ }
151141 }
152142
153- }
143+ }
0 commit comments