Skip to content

Commit

Permalink
Fix compression/decompression performance issue on windows
Browse files Browse the repository at this point in the history
Apparently, we weren't closing the GZip stream.
This shouldn't be an issue as there really isn't any underlying file or other native resource backing it, just an Inflater/Deflater.
Their documentation says that end() (called by stream close) releases the underlying resources. And it says that it's automatically called from finalizer.
Except on windows it isn't because the object ends up being leaked and never GCed.
  • Loading branch information
Barteks2x committed Feb 25, 2022
1 parent 4ae7403 commit fc487fe
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/cubicchunks/converter/lib/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,19 @@ public static CompoundTag readCompressed(InputStream is) throws IOException {
}

public static CompoundTag readCompressedCC(InputStream is) throws IOException {
BufferedInputStream data = new BufferedInputStream(new GZIPInputStream(is));
return (CompoundTag) new NBTInputStream(data, false).readTag();
try (NBTInputStream nbtInputStream = new NBTInputStream(new BufferedInputStream(new GZIPInputStream(is)), false)) {
return (CompoundTag) nbtInputStream.readTag();
}
}

public static ByteBuffer writeCompressed(CompoundTag tag, boolean prefixFormat) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
if (prefixFormat) {
bytes.write(1); // mark as GZIP
}
NBTOutputStream nbtOut = new NBTOutputStream(new BufferedOutputStream(new GZIPOutputStream(bytes)), false);
nbtOut.writeTag(tag);
nbtOut.close();
bytes.flush();
try (NBTOutputStream nbtOut = new NBTOutputStream(new BufferedOutputStream(new GZIPOutputStream(bytes)), false)) {
nbtOut.writeTag(tag);
}
return ByteBuffer.wrap(bytes.toByteArray());
}

Expand Down

0 comments on commit fc487fe

Please sign in to comment.