Skip to content

Commit 4d6e2e7

Browse files
authored
Improve ByteBufPrimitiveCodec readBytes (#1617)
1 parent f5605ea commit 4d6e2e7

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/protocol/ByteBufPrimitiveCodec.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ public int readUnsignedShort(ByteBuf source) {
114114
public ByteBuffer readBytes(ByteBuf source) {
115115
int length = readInt(source);
116116
if (length < 0) return null;
117-
ByteBuf slice = source.readSlice(length);
118-
return ByteBuffer.wrap(readRawBytes(slice));
117+
byte[] bytes = new byte[length];
118+
source.readBytes(bytes);
119+
return ByteBuffer.wrap(bytes);
119120
}
120121

121122
@Override
@@ -220,22 +221,6 @@ public void writeShortBytes(byte[] bytes, ByteBuf dest) {
220221
dest.writeBytes(bytes);
221222
}
222223

223-
// Reads *all* readable bytes from a buffer and return them.
224-
// If the buffer is backed by an array, this will return the underlying array directly, without
225-
// copy.
226-
private static byte[] readRawBytes(ByteBuf buffer) {
227-
if (buffer.hasArray() && buffer.readableBytes() == buffer.array().length) {
228-
// Move the readerIndex just so we consistently consume the input
229-
buffer.readerIndex(buffer.writerIndex());
230-
return buffer.array();
231-
}
232-
233-
// Otherwise, just read the bytes in a new array
234-
byte[] bytes = new byte[buffer.readableBytes()];
235-
buffer.readBytes(bytes);
236-
return bytes;
237-
}
238-
239224
private static String readString(ByteBuf source, int length) {
240225
try {
241226
String str = source.toString(source.readerIndex(), length, CharsetUtil.UTF_8);

core/src/test/java/com/datastax/oss/driver/internal/core/protocol/ByteBufPrimitiveCodecTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,65 @@ public void should_read_bytes() {
164164
assertThat(Bytes.toHexString(bytes)).isEqualTo("0xcafebabe");
165165
}
166166

167+
@Test
168+
public void should_read_bytes_when_extra_data() {
169+
ByteBuf source =
170+
ByteBufs.wrap(
171+
// length (as an int)
172+
0x00,
173+
0x00,
174+
0x00,
175+
0x04,
176+
// contents
177+
0xca,
178+
0xfe,
179+
0xba,
180+
0xbe,
181+
0xde,
182+
0xda,
183+
0xdd);
184+
ByteBuffer bytes = codec.readBytes(source);
185+
assertThat(Bytes.toHexString(bytes)).isEqualTo("0xcafebabe");
186+
}
187+
188+
@Test
189+
public void read_bytes_should_udpate_reader_index() {
190+
ByteBuf source =
191+
ByteBufs.wrap(
192+
// length (as an int)
193+
0x00,
194+
0x00,
195+
0x00,
196+
0x04,
197+
// contents
198+
0xca,
199+
0xfe,
200+
0xba,
201+
0xbe,
202+
0xde,
203+
0xda,
204+
0xdd);
205+
codec.readBytes(source);
206+
207+
assertThat(source.readerIndex()).isEqualTo(8);
208+
}
209+
210+
@Test
211+
public void read_bytes_should_throw_when_not_enough_content() {
212+
ByteBuf source =
213+
ByteBufs.wrap(
214+
// length (as an int) : 4 bytes
215+
0x00,
216+
0x00,
217+
0x00,
218+
0x04,
219+
// contents : only 2 bytes
220+
0xca,
221+
0xfe);
222+
assertThatThrownBy(() -> codec.readBytes(source))
223+
.isInstanceOf(IndexOutOfBoundsException.class);
224+
}
225+
167226
@Test
168227
public void should_read_null_bytes() {
169228
ByteBuf source = ByteBufs.wrap(0xFF, 0xFF, 0xFF, 0xFF); // -1 (as an int)

0 commit comments

Comments
 (0)