Skip to content

Commit 0ccbe23

Browse files
committed
Read DataInput with buffer
1 parent 8982cfe commit 0ccbe23

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,13 @@ static byte[] readFromStream(InputStream inputStream) {
278278
static byte[] readFromDataInput(DataInput dataInput, int length) {
279279
ByteArrayOutputStream out = new ByteArrayOutputStream();
280280
try {
281+
byte[] buf;
282+
int remaining = length;
281283
for (int i = 0; i < length; i++) {
282-
out.write(dataInput.readUnsignedByte());
284+
buf = new byte[Math.min(remaining, BUF_SIZE)];
285+
dataInput.readFully(buf);
286+
out.write(buf);
287+
remaining -= buf.length;
283288
}
284289
return out.toByteArray();
285290
} catch (Exception e) {

src/test/java/at/favre/lib/bytes/BytesConstructorTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public void fromInputStream() throws Exception {
229229
checkInputStream(example_bytes_seven);
230230
checkInputStream(example_bytes_eight);
231231
checkInputStream(example_bytes_sixteen);
232+
checkInputStream(Bytes.random(32 * 987).array());
232233
}
233234

234235
private void checkInputStream(byte[] array) {
@@ -243,6 +244,7 @@ public void fromDataInput() throws Exception {
243244
checkDataInput(example_bytes_seven);
244245
checkDataInput(example_bytes_eight);
245246
checkDataInput(example_bytes_sixteen);
247+
checkDataInput(Bytes.random(32 * 987).array());
246248
}
247249

248250
private void checkDataInput(byte[] array) {
@@ -266,7 +268,7 @@ public void fromList() throws Exception {
266268

267269
private void checkList(byte[] array) {
268270
Bytes bList = Bytes.from(Util.toList(array));
269-
Bytes bLinkedList = Bytes.from(new LinkedList<Byte>(Util.toList(array)));
271+
Bytes bLinkedList = Bytes.from(new LinkedList<>(Util.toList(array)));
270272
assertArrayEquals(array, bList.array());
271273
assertArrayEquals(array, bLinkedList.array());
272274
}

0 commit comments

Comments
 (0)