Skip to content

Commit

Permalink
BinaryRequestBody and ContentBody use InputStream.transferToOutputStream
Browse files Browse the repository at this point in the history
Allow for optimization when underlying input stream (such as
ByteArrayInputStream, ChannelInputStream) overrides
transferTo(OutputStream) to avoid extra array allocations.

See
* https://bugs.openjdk.org/browse/JDK-8067661
* https://bugs.openjdk.org/browse/JDK-8265891
* https://bugs.openjdk.org/browse/JDK-8273038
* https://bugs.openjdk.org/browse/JDK-8279283
* https://bugs.openjdk.org/browse/JDK-8296431
  • Loading branch information
schlosna committed Jul 28, 2023
1 parent 73102d0 commit f74d3c9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.palantir.dialogue.annotations;

import com.google.common.io.ByteStreams;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.logger.SafeLogger;
Expand All @@ -39,7 +38,7 @@ final class InputStreamContentBody implements ContentBody {

@Override
public void writeTo(OutputStream output) throws IOException {
ByteStreams.copy(inputStream, output);
inputStream.transferTo(output);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ public void stream_3_gigabytes() throws IOException {

undertowHandler = exchange -> {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/octet-stream");
InputStream bigInputStream = repeat(sample, limit);
ByteStreams.copy(bigInputStream, exchange.getOutputStream());
try (InputStream bigInputStream = repeat(sample, limit)) {
bigInputStream.transferTo(exchange.getOutputStream());
}
};

Stopwatch sw = Stopwatch.createStarted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.palantir.dialogue;

import com.google.common.io.ByteStreams;
import com.palantir.logsafe.Preconditions;
import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -49,7 +48,7 @@ static BinaryRequestBody of(InputStream inputStream) {
public void write(OutputStream requestBody) throws IOException {
Preconditions.checkState(!invoked, "Write has already been called");
invoked = true;
ByteStreams.copy(inputStream, requestBody);
inputStream.transferTo(requestBody);
}

@Override
Expand Down

0 comments on commit f74d3c9

Please sign in to comment.