Skip to content

Commit

Permalink
chore(perf): Tune decompression performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Apr 14, 2022
1 parent f4a5b80 commit 1c3c581
Showing 1 changed file with 4 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.net.http.HttpResponse.BodySubscriber;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -103,20 +102,14 @@ public void onNext(List<ByteBuffer> item) {

private List<ByteBuffer> decompress(InputStream decStream) throws IOException {
List<ByteBuffer> dec = new ArrayList<>(1);
ByteBuffer buf = newBuffer();

try (var stream = decStream) {
byte[] buf = new byte[bufferSize];
int r;
while ((r = stream.read()) != -1) {
if (!buf.hasRemaining()) {
add(dec, buf);
buf = newBuffer();
}

buf.put((byte) r);
while ((r = stream.read(buf)) > 0) {
ByteBuffer bb = ByteBuffer.allocate(r).put(buf, 0, r).flip();
dec.add(bb);
}

add(dec, buf);
}

return Collections.unmodifiableList(dec);
Expand All @@ -140,16 +133,6 @@ private void pushNext(List<ByteBuffer> item) {
}
}

private ByteBuffer newBuffer() {
return ByteBuffer.allocate(bufferSize);
}

private void add(Collection<ByteBuffer> decompressed, ByteBuffer buf) {
if (buf.position() > 0) {
decompressed.add(buf.flip());
}
}

private InputStream initDecompressingStream() {
try {
is.mark(32);
Expand Down

0 comments on commit 1c3c581

Please sign in to comment.