Skip to content

Remove unnecessary copy #1888

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
merged 2 commits into from
Jun 11, 2020
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
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-9e5b030.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"description": "Avoid unnecessary copying in `AsyncRequestBody.fromBytes()`"
}
5 changes: 5 additions & 0 deletions core/sdk-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
Expand Down Expand Up @@ -138,7 +137,7 @@ static AsyncRequestBody fromString(String string) {
* @return AsyncRequestBody instance.
*/
static AsyncRequestBody fromBytes(byte[] bytes) {
return new ByteArrayAsyncRequestBody(Arrays.copyOf(bytes, bytes.length));
return new ByteArrayAsyncRequestBody(bytes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import io.reactivex.Flowable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand All @@ -31,6 +32,7 @@
import org.junit.runners.Parameterized;
import org.reactivestreams.Subscriber;
import software.amazon.awssdk.http.async.SimpleSubscriber;
import software.amazon.awssdk.utils.BinaryUtils;

@RunWith(Parameterized.class)
public class AsyncRequestBodyTest {
Expand Down Expand Up @@ -93,4 +95,17 @@ public void onComplete() {
done.await();
assertThat(sb.toString()).isEqualTo(testString);
}

@Test
public void fromBytes_byteArrayNotNull_createsCopy() {
byte[] original = {0x1, 0x2, 0x3, 0x4};
byte[] toModify = new byte[original.length];
System.arraycopy(original, 0, toModify, 0, original.length);
AsyncRequestBody body = AsyncRequestBody.fromBytes(toModify);
for (int i = 0; i < toModify.length; ++i) {
toModify[i]++;
}
ByteBuffer publishedBb = Flowable.fromPublisher(body).toList().blockingGet().get(0);
assertThat(BinaryUtils.copyAllBytesFrom(publishedBb)).isEqualTo(original);
}
}