Skip to content

Commit 79a9317

Browse files
committed
Add more to readme and read from file
1 parent 5066fa6 commit 79a9317

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Initializing **empty arrays** of arbitrary length:
6161

6262
```java
6363
Bytes.allocate(16);
64-
Bytes.allocate(4,(byte) 1); //fill with 0x01
64+
Bytes.allocate(4, (byte) 1); //fill with 0x01
6565
```
6666

6767
Creating **random** byte arrays for e.g. testing:
@@ -85,6 +85,7 @@ Bytes.from(byteInputStream); //any inputStream
8585
Bytes.from(byteList); //List<Byte> byteList = ...
8686
Bytes.from(myBitSet); //BitSet myBitSet = ...
8787
Bytes.from(bigInteger);
88+
Bytes.from(file); //reads bytes from any java.io.File
8889
```
8990

9091
For parsing binary-text-encoded strings, see below.
@@ -180,6 +181,8 @@ Bytes.from(array).encodeOctal(); //1124517677707527755
180181
Bytes.from(array).encodeBase36(); //5qpdvuwjvu5
181182
```
182183

184+
### Utility Methods
185+
183186
### Validation
184187

185188
A simple validation framework which can be used to check the internal byte array:

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package at.favre.lib.bytes;
2323

2424
import java.io.ByteArrayInputStream;
25+
import java.io.File;
2526
import java.io.InputStream;
2627
import java.math.BigInteger;
2728
import java.nio.ByteBuffer;
@@ -295,6 +296,19 @@ public static Bytes from(InputStream stream) {
295296
return wrap(Util.readFromStream(stream));
296297
}
297298

299+
/**
300+
* Reads given file and returns the byte content. Be aware that the whole file content will be loaded to
301+
* memory, so be careful what to read in.
302+
*
303+
* @param file to read from
304+
* @return new instance
305+
* @throws IllegalArgumentException if file does not exist
306+
* @throws IllegalStateException if file could not be read
307+
*/
308+
public static Bytes from(File file) {
309+
return wrap(Util.readFromFile(file));
310+
}
311+
298312
/**
299313
* Creates a new instance from given utf-8 encoded string
300314
*
@@ -985,7 +999,6 @@ private ByteBuffer internalBuffer() {
985999
}
9861000

9871001

988-
9891002
/**
9901003
* Returns a mutable version of this instance with sharing the same underlying byte-array.
9911004
* If you want the mutable version to be a copy, call {@link #copy()} first.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
package at.favre.lib.bytes;
2323

2424
import java.io.ByteArrayOutputStream;
25+
import java.io.File;
26+
import java.io.IOException;
2527
import java.io.InputStream;
28+
import java.nio.file.Files;
2629
import java.util.*;
2730

2831
/**
@@ -232,6 +235,25 @@ static byte[] concatVararg(byte firstByte, byte[] moreBytes) {
232235
}
233236
}
234237

238+
/**
239+
* Reads all bytes from a file
240+
*
241+
* @param file the file to read
242+
* @return byte content
243+
*/
244+
static byte[] readFromFile(File file) {
245+
if (file == null || !file.exists() || !file.isFile()) {
246+
throw new IllegalArgumentException("file must not be null, has to exist and must be a file (not a directory) " + file);
247+
}
248+
249+
try {
250+
return Files.readAllBytes(file.toPath());
251+
} catch (IOException e) {
252+
throw new IllegalStateException("could not read from file", e);
253+
}
254+
255+
}
256+
235257
/*
236258
=================================================================================================
237259
Copyright 2011 Twitter, Inc.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121

2222
package at.favre.lib.bytes;
2323

24+
import org.junit.Rule;
2425
import org.junit.Test;
26+
import org.junit.rules.TemporaryFolder;
2527

2628
import java.io.ByteArrayInputStream;
29+
import java.io.File;
30+
import java.io.FileOutputStream;
2731
import java.math.BigInteger;
2832
import java.nio.ByteBuffer;
2933
import java.nio.charset.Charset;
@@ -36,6 +40,8 @@
3640
import static org.junit.Assert.*;
3741

3842
public class BytesConstructorTests extends ABytesTest {
43+
@Rule
44+
public TemporaryFolder testFolder = new TemporaryFolder();
3945

4046
@Test
4147
public void wrapTest() throws Exception {
@@ -255,4 +261,16 @@ public void fromPartByte() throws Exception {
255261
assertArrayEquals(new byte[]{example_bytes_four[1]}, Bytes.from(example_bytes_four, 1, 1).array());
256262
assertArrayEquals(new byte[]{example_bytes_eight[4], example_bytes_eight[5], example_bytes_eight[6]}, Bytes.from(example_bytes_eight, 4, 3).array());
257263
}
264+
265+
@Test
266+
public void fromFile() throws Exception {
267+
File tempFile = testFolder.newFile("out-test.txt");
268+
Bytes randomBytes = Bytes.random(500);
269+
270+
try (FileOutputStream stream = new FileOutputStream(tempFile)) {
271+
stream.write(randomBytes.array());
272+
}
273+
274+
assertArrayEquals(randomBytes.array(), Bytes.from(tempFile).array());
275+
}
258276
}

0 commit comments

Comments
 (0)