Skip to content

Commit 3ca2191

Browse files
mp911dechristophstrobl
authored andcommitted
Respect ByteBuffer position and limits in ByteUtils.getBytes(ByteBuffer).
We now properly extract the byte array from a ByteBuffer by copying its content respecting the read position and limits. Closes #2204 Original Pull Request: #2213
1 parent 9a56c38 commit 3ca2191

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

src/main/java/org/springframework/data/redis/util/ByteUtils.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ public static byte[][] mergeArrays(byte[] firstArray, byte[]... additionalArrays
129129
}
130130

131131
/**
132-
* Extract a byte array from {@link ByteBuffer} without consuming it.
132+
* Extract a byte array from {@link ByteBuffer} without consuming it. The resulting {@code byte[]} is a copy of the
133+
* buffer's contents and not updated upon changes within the buffer.
133134
*
134135
* @param byteBuffer must not be {@literal null}.
135136
* @return
@@ -139,10 +140,6 @@ public static byte[] getBytes(ByteBuffer byteBuffer) {
139140

140141
Assert.notNull(byteBuffer, "ByteBuffer must not be null!");
141142

142-
if (byteBuffer.hasArray()) {
143-
return byteBuffer.array();
144-
}
145-
146143
ByteBuffer duplicate = byteBuffer.duplicate();
147144
byte[] bytes = new byte[duplicate.remaining()];
148145
duplicate.get(bytes);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.util;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.nio.ByteBuffer;
21+
import java.nio.charset.StandardCharsets;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
* Unit tests for {@link ByteUtils}.
27+
*
28+
* @author Mark Paluch
29+
*/
30+
class ByteUtilsUnitTests {
31+
32+
@Test // GH-2204
33+
void getBytesShouldUseCorrectHeapBufferSpace() {
34+
35+
ByteBuffer buffer = ByteBuffer.allocate(16);
36+
buffer.put("hello".getBytes(StandardCharsets.US_ASCII));
37+
buffer.flip();
38+
39+
byte[] bytes = ByteUtils.getBytes(buffer);
40+
41+
assertThat(bytes).hasSize(5);
42+
}
43+
44+
@Test // GH-2204
45+
void getBytesShouldUseCorrectDirectBufferSpace() {
46+
47+
ByteBuffer buffer = ByteBuffer.allocateDirect(16);
48+
buffer.put("hello".getBytes(StandardCharsets.US_ASCII));
49+
buffer.flip();
50+
51+
byte[] bytes = ByteUtils.getBytes(buffer);
52+
53+
assertThat(bytes).hasSize(5);
54+
}
55+
}

0 commit comments

Comments
 (0)