Skip to content

Fix immutableCopyOf bug #4266

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
Aug 9, 2023
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/bugfix-AWSSDKforJavav2-5ae7899.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"description": "Fixed bug where limit was not copied over when cloning ByteBuffer using immutableCopyOf()"
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ public static ByteBuffer immutableCopyOf(ByteBuffer bb) {
if (bb == null) {
return null;
}
int sourceBufferPosition = bb.position();
ByteBuffer readOnlyCopy = bb.asReadOnlyBuffer();
readOnlyCopy.rewind();
ByteBuffer cloned = ByteBuffer.allocate(readOnlyCopy.capacity())
.put(readOnlyCopy);
cloned.position(sourceBufferPosition);
cloned.position(bb.position());
cloned.limit(bb.limit());
return cloned.asReadOnlyBuffer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ public void testImmutableCopyOfByteBuffer() {
assertArrayEquals(bytesInSourceAfterCopy, fromSource);
}

@Test
public void immutableCopyOf_retainsOriginalLimit() {
ByteBuffer sourceBuffer = ByteBuffer.allocate(10);
byte[] bytes = {1, 2, 3, 4};
sourceBuffer.put(bytes);
sourceBuffer.rewind();
sourceBuffer.limit(bytes.length);
ByteBuffer copy = BinaryUtils.immutableCopyOf(sourceBuffer);
assertThat(copy.limit()).isEqualTo(sourceBuffer.limit());
}

@Test
public void testImmutableCopyOfByteBuffer_nullBuffer() {
assertNull(BinaryUtils.immutableCopyOf(null));
Expand Down