Skip to content

Commit

Permalink
Enable zero-copy ByteBuffer publishing in AsyncRequestBody "unsafe" c…
Browse files Browse the repository at this point in the history
…onstructors (#4096)
  • Loading branch information
StephenFlavin committed Jul 10, 2023
1 parent 48a356c commit 55383d3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
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

0 comments on commit 55383d3

Please sign in to comment.