@@ -100,8 +100,16 @@ private synchronized Map<String, JsonObject> getAssetMap() throws IOException {
100100
101101 idCounter = new AtomicInteger (1 );
102102 assets = new HashMap <String , JsonObject >();
103- JsonReader reader = Json .createReader (new FileInputStream (file ));
104- JsonArray assetList = reader .readArray ();
103+ JsonReader reader = null ;
104+ JsonArray assetList = null ;
105+ try {
106+ reader = Json .createReader (new FileInputStream (file ));
107+ assetList = reader .readArray ();
108+ } finally {
109+ if (reader != null ) {
110+ reader .close ();
111+ }
112+ }
105113 for (JsonValue val : assetList ) {
106114 String id = Integer .toString (idCounter .getAndIncrement ());
107115 if (val .getValueType () == ValueType .OBJECT ) {
@@ -245,17 +253,21 @@ private synchronized void rewriteFile() throws FileNotFoundException, IOExceptio
245253
246254 // Write the assets back to the file
247255 FileOutputStream out = null ;
256+ JsonWriter streamWriter = null ;
248257 try {
249258 Map <String , Object > config = new HashMap <String , Object >();
250259 config .put (JsonGenerator .PRETTY_PRINTING , true );
251260 JsonWriterFactory writerFactory = Json .createWriterFactory (config );
252261 out = new FileOutputStream (file );
253- JsonWriter streamWriter = writerFactory .createWriter (out );
262+ streamWriter = writerFactory .createWriter (out );
254263 streamWriter .write (jsonToStore .build ());
255264 } finally {
256265 if (out != null ) {
257266 out .close ();
258267 }
268+ if (streamWriter != null ) {
269+ streamWriter .close ();
270+ }
259271 }
260272 }
261273
0 commit comments