Skip to content

Commit f3ff704

Browse files
committed
Close streams ater reading and writing
1 parent efa74cc commit f3ff704

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

client-lib/src/main/java/com/ibm/ws/repository/transport/client/SingleFileClient.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)