Fix resource leaks

(cherry picked from commit 537221a0cad28cc0df49a9d90a15e241b297d37f)
This commit is contained in:
Glavo 2023-02-05 04:25:40 +01:00 committed by Nassim Jahnke
parent d80ab5b7bd
commit 04c0131935
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 21 additions and 14 deletions

View File

@ -5,7 +5,7 @@
<groupId>com.viaversion</groupId>
<artifactId>opennbt</artifactId>
<version>2.1.1</version>
<version>2.1.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>OpenNBT</name>

View File

@ -70,16 +70,20 @@ public class NBTIO {
*/
public static CompoundTag readFile(File file, boolean compressed, boolean littleEndian) throws IOException {
InputStream in = new FileInputStream(file);
if(compressed) {
in = new GZIPInputStream(in);
}
try {
if (compressed) {
in = new GZIPInputStream(in);
}
CompoundTag tag = readTag(in, littleEndian);
if(!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a CompoundTag!");
}
CompoundTag tag = readTag(in, littleEndian);
if (!(tag instanceof CompoundTag)) {
throw new IOException("Root tag is not a CompoundTag!");
}
return tag;
return tag;
} finally {
in.close();
}
}
/**
@ -136,12 +140,15 @@ public class NBTIO {
}
OutputStream out = new FileOutputStream(file);
if(compressed) {
out = new GZIPOutputStream(out);
}
try {
if (compressed) {
out = new GZIPOutputStream(out);
}
writeTag(out, tag, littleEndian);
out.close();
writeTag(out, tag, littleEndian);
} finally {
out.close();
}
}
/**