From 1c3c581f52db95cf4d88bc6dcea9e420b3015b5f Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Thu, 14 Apr 2022 11:34:54 +0400 Subject: [PATCH] chore(perf): Tune decompression performance. --- .../http/ext/DecompressingSubscriber.java | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java b/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java index 9e445b0..8fc8560 100644 --- a/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java +++ b/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java @@ -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; @@ -103,20 +102,14 @@ public void onNext(List item) { private List decompress(InputStream decStream) throws IOException { List 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); @@ -140,16 +133,6 @@ private void pushNext(List item) { } } - private ByteBuffer newBuffer() { - return ByteBuffer.allocate(bufferSize); - } - - private void add(Collection decompressed, ByteBuffer buf) { - if (buf.position() > 0) { - decompressed.add(buf.flip()); - } - } - private InputStream initDecompressingStream() { try { is.mark(32);