Skip to content

Commit 86598e7

Browse files
committed
Refactor ZlibDataReader with try-finally for cleanup
1 parent f1d82df commit 86598e7

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

‎src/main/java/org/glavo/nbt/internal/input/ZlibDataReader.java‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void close(Inflater value) {
3434
value.end();
3535
}
3636
};
37+
3738
private final Inflater inflater;
3839

3940
ZlibDataReader(InputContext context, long compressedSize) {
@@ -56,28 +57,30 @@ public void ensureBufferRemaining(int required) throws IOException {
5657
ByteBuffer output = this.buffer.getByteBuffer();
5758
output.compact();
5859

59-
do {
60-
if (inflater.finished() || inflater.needsDictionary()) {
61-
throw new EOFException();
62-
}
63-
64-
if (inflater.needsInput()) {
65-
if (context.rawReader.buffer.remaining() == 0) {
66-
context.rawReader.ensureBufferRemaining(1);
60+
try {
61+
do {
62+
if (inflater.finished() || inflater.needsDictionary()) {
63+
throw new EOFException();
6764
}
68-
inflater.setInput(context.rawReader.buffer.getByteBuffer());
69-
}
7065

71-
try {
72-
inflater.inflate(output);
73-
} catch (DataFormatException exception) {
74-
throw new IOException(exception);
75-
}
66+
if (inflater.needsInput()) {
67+
if (context.rawReader.buffer.remaining() == 0) {
68+
context.rawReader.ensureBufferRemaining(1);
69+
}
70+
inflater.setInput(context.rawReader.buffer.getByteBuffer());
71+
}
7672

77-
} while (output.position() < required);
73+
try {
74+
inflater.inflate(output);
75+
} catch (DataFormatException exception) {
76+
throw new IOException(exception);
77+
}
7878

79-
inflater.setInput(EMPTY_BYTE_ARRAY);
80-
output.flip();
79+
} while (output.position() < required);
80+
} finally {
81+
inflater.setInput(EMPTY_BYTE_ARRAY);
82+
output.flip();
83+
}
8184
}
8285

8386
@Override

0 commit comments

Comments
 (0)