Skip to content

Commit 1ba3e7b

Browse files
authored
Merge pull request #1888 from dagnir/1877
Remove unnecessary copy
2 parents b679524 + b5adbd9 commit 1ba3e7b

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"description": "Avoid unnecessary copying in `AsyncRequestBody.fromBytes()`"
5+
}

core/sdk-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@
172172
<version>${awsjavasdk.version}</version>
173173
<scope>test</scope>
174174
</dependency>
175+
<dependency>
176+
<groupId>io.reactivex.rxjava2</groupId>
177+
<artifactId>rxjava</artifactId>
178+
<scope>test</scope>
179+
</dependency>
175180
</dependencies>
176181
<build>
177182
<plugins>

core/sdk-core/src/main/java/software/amazon/awssdk/core/async/AsyncRequestBody.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.Charset;
2121
import java.nio.charset.StandardCharsets;
2222
import java.nio.file.Path;
23-
import java.util.Arrays;
2423
import java.util.Optional;
2524
import org.reactivestreams.Publisher;
2625
import org.reactivestreams.Subscriber;
@@ -138,7 +137,7 @@ static AsyncRequestBody fromString(String string) {
138137
* @return AsyncRequestBody instance.
139138
*/
140139
static AsyncRequestBody fromBytes(byte[] bytes) {
141-
return new ByteArrayAsyncRequestBody(Arrays.copyOf(bytes, bytes.length));
140+
return new ByteArrayAsyncRequestBody(bytes);
142141
}
143142

144143
/**

core/sdk-core/src/test/java/software/amazon/awssdk/core/async/AsyncRequestBodyTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.jimfs.Configuration;
2121
import com.google.common.jimfs.Jimfs;
22+
import io.reactivex.Flowable;
2223
import java.io.IOException;
2324
import java.nio.ByteBuffer;
2425
import java.nio.charset.StandardCharsets;
@@ -31,6 +32,7 @@
3132
import org.junit.runners.Parameterized;
3233
import org.reactivestreams.Subscriber;
3334
import software.amazon.awssdk.http.async.SimpleSubscriber;
35+
import software.amazon.awssdk.utils.BinaryUtils;
3436

3537
@RunWith(Parameterized.class)
3638
public class AsyncRequestBodyTest {
@@ -93,4 +95,17 @@ public void onComplete() {
9395
done.await();
9496
assertThat(sb.toString()).isEqualTo(testString);
9597
}
98+
99+
@Test
100+
public void fromBytes_byteArrayNotNull_createsCopy() {
101+
byte[] original = {0x1, 0x2, 0x3, 0x4};
102+
byte[] toModify = new byte[original.length];
103+
System.arraycopy(original, 0, toModify, 0, original.length);
104+
AsyncRequestBody body = AsyncRequestBody.fromBytes(toModify);
105+
for (int i = 0; i < toModify.length; ++i) {
106+
toModify[i]++;
107+
}
108+
ByteBuffer publishedBb = Flowable.fromPublisher(body).toList().blockingGet().get(0);
109+
assertThat(BinaryUtils.copyAllBytesFrom(publishedBb)).isEqualTo(original);
110+
}
96111
}

0 commit comments

Comments
 (0)