Add try-with-resources to close streams in
DirectorySerializer.
this closes
@@ -49,13 +49,14 @@ public class DirectorySerializer {
Map<String, byte[]> data = new HashMap<>();
serialize(data, path, file);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(baos);
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(data);
oos.flush();
return baos.toByteArray();
}
+ }
/**
* Deserializes directory content.
@@ -67,8 +68,8 @@ public class DirectorySerializer {
*/
@SuppressWarnings("unchecked")
public static void deserialize(Path path, byte[] data) throws IOException, ClassNotFoundException {
- ByteArrayInputStream bais = new ByteArrayInputStream(data);
- ObjectInputStream ois = new ObjectInputStream(bais);
+ try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
+ ObjectInputStream ois = new ObjectInputStream(bais)) {
Map<String, byte[]> files = (Map<String, byte[]>)ois.readObject();
for (Map.Entry<String, byte[]> file : files.entrySet()) {
@@ -76,11 +77,13 @@ public class DirectorySerializer {
File dstFile = dst.toFile();
Files.createDirectories(dstFile.getParentFile().toPath());
Files.createFile(dstFile.toPath());
- FileOutputStream fos = new FileOutputStream(dstFile);
+ try (FileOutputStream fos = new FileOutputStream(dstFile)) {
fos.write(file.getValue());
fos.flush();
}
}
+ }
+ }
/**
* Removes the specified directory.
