Skip to content

Commit 545ecf9

Browse files
committed
Add construction from DataInput
1 parent c79fea0 commit 545ecf9

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

CHANGELOG

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
## v0.4.2
44

55
* add check method if transformer supports inplace
6-
* adds contains method
7-
* adds Iterable interface
6+
* add contains method
7+
* add Iterable interface
8+
* add setByteAt
9+
* add construction from DataInput
810

911
## v0.4.1
1012

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.ByteArrayInputStream;
25-
import java.io.File;
26-
import java.io.InputStream;
27-
import java.io.Serializable;
24+
import java.io.*;
2825
import java.math.BigInteger;
2926
import java.nio.ByteBuffer;
3027
import java.nio.ByteOrder;
@@ -329,6 +326,17 @@ public static Bytes from(InputStream stream) {
329326
return wrap(Util.readFromStream(stream));
330327
}
331328

329+
/**
330+
* Reads given {@link DataInput} and creates a new instance from read data
331+
*
332+
* @param dataInput to read from
333+
* @param length how many bytes should be read
334+
* @return new instance
335+
*/
336+
public static Bytes from(DataInput dataInput, int length) {
337+
return wrap(Util.readFromDataInput(dataInput, length));
338+
}
339+
332340
/**
333341
* Reads given file and returns the byte content. Be aware that the whole file content will be loaded to
334342
* memory, so be careful what to read in.

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.ByteArrayOutputStream;
25-
import java.io.File;
26-
import java.io.IOException;
27-
import java.io.InputStream;
24+
import java.io.*;
2825
import java.nio.ByteBuffer;
2926
import java.nio.file.Files;
3027
import java.util.*;
@@ -272,6 +269,24 @@ static byte[] readFromStream(InputStream inputStream) {
272269
}
273270
}
274271

272+
/**
273+
* Read all bytes until length from given byte array.
274+
*
275+
* @param dataInput to read from
276+
* @return all bytes from the dataInput
277+
*/
278+
static byte[] readFromDataInput(DataInput dataInput, int length) {
279+
ByteArrayOutputStream out = new ByteArrayOutputStream();
280+
try {
281+
for (int i = 0; i < length; i++) {
282+
out.write(dataInput.readUnsignedByte());
283+
}
284+
return out.toByteArray();
285+
} catch (Exception e) {
286+
throw new IllegalStateException("could not read from data input", e);
287+
}
288+
}
289+
275290
/**
276291
* Combines a single argument with a vararg to a single array
277292
*

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.rules.TemporaryFolder;
2828

2929
import java.io.ByteArrayInputStream;
30+
import java.io.DataInputStream;
3031
import java.io.File;
3132
import java.io.FileOutputStream;
3233
import java.math.BigInteger;
@@ -234,6 +235,25 @@ private void checkInputStream(byte[] array) {
234235
assertArrayEquals(array, Bytes.from(new ByteArrayInputStream(array)).array());
235236
}
236237

238+
@Test
239+
public void fromDataInput() throws Exception {
240+
checkDataInput(example_bytes_one);
241+
checkDataInput(example_bytes_two);
242+
checkDataInput(example_bytes_four);
243+
checkDataInput(example_bytes_seven);
244+
checkDataInput(example_bytes_eight);
245+
checkDataInput(example_bytes_sixteen);
246+
}
247+
248+
private void checkDataInput(byte[] array) {
249+
assertArrayEquals(array, Bytes.from(new DataInputStream(new ByteArrayInputStream(array)), array.length).array());
250+
}
251+
252+
@Test(expected = IllegalStateException.class)
253+
public void fromDataInputShouldThrowException() throws Exception {
254+
Bytes.from(new DataInputStream(new ByteArrayInputStream(example_bytes_one)), 2);
255+
}
256+
237257
@Test
238258
public void fromList() throws Exception {
239259
checkList(example_bytes_one);

0 commit comments

Comments
 (0)