File tree Expand file tree Collapse file tree 4 files changed +55
-10
lines changed
main/java/at/favre/lib/bytes
test/java/at/favre/lib/bytes Expand file tree Collapse file tree 4 files changed +55
-10
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 2121
2222package 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 .*;
2825import java .math .BigInteger ;
2926import java .nio .ByteBuffer ;
3027import 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.
Original file line number Diff line number Diff line change 2121
2222package 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 .*;
2825import java .nio .ByteBuffer ;
2926import java .nio .file .Files ;
3027import 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 *
Original file line number Diff line number Diff line change 2727import org .junit .rules .TemporaryFolder ;
2828
2929import java .io .ByteArrayInputStream ;
30+ import java .io .DataInputStream ;
3031import java .io .File ;
3132import java .io .FileOutputStream ;
3233import 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 );
You can’t perform that action at this time.
0 commit comments