Skip to content

Enable zero-copy ByteBuffer publishing in AsyncRequestBody "unsafe" constructors #4096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-5e523a4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "StephenFlavin",
"description": "Enable zero-copy ByteBuffer publishing in AsyncRequestBody via \"unsafe\" constructors"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.internal.util.Mimetype;
import software.amazon.awssdk.utils.BinaryUtils;
import software.amazon.awssdk.utils.Logger;

/**
Expand Down Expand Up @@ -96,11 +95,6 @@ public void request(long n) {
do {
ByteBuffer buffer = buffers[i];

// Pending discussions on https://github.com/aws/aws-sdk-java-v2/issues/3928
if (buffer.isDirect()) {
buffer = BinaryUtils.toNonDirectBuffer(buffer);
}

s.onNext(buffer.asReadOnlyBuffer());
remaining--;
} while (remaining > 0 && (i = index.getAndIncrement()) < buffers.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,6 @@ public void canceledSubscriberDoesNotReturnNewResults() {
assertTrue(subscriber.publishedResults.isEmpty());
}

// Pending discussions on https://github.com/aws/aws-sdk-java-v2/issues/3928
@Test
public void directBuffersAreCoppiedToNonDirectBuffers() {
byte[] bytes = "Hello World!".getBytes(StandardCharsets.UTF_8);
ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length)
.put(bytes);
buffer.flip();
AsyncRequestBody requestBody = ByteBuffersAsyncRequestBody.of(buffer);

TestSubscriber subscriber = new TestSubscriber();
requestBody.subscribe(subscriber);
subscriber.request(1);

ByteBuffer publishedBuffer = subscriber.publishedResults.get(0);
assertFalse(publishedBuffer.isDirect());
byte[] publishedBytes = new byte[publishedBuffer.remaining()];
publishedBuffer.get(publishedBytes);
assertArrayEquals(bytes, publishedBytes);
}

@Test
public void staticOfByteBufferConstructorSetsLengthBasedOnBufferRemaining() {
ByteBuffer bb1 = ByteBuffer.allocate(2);
Expand Down