Skip to content

Commit 153c872

Browse files
committed
Add utility and conversation section in readme
1 parent 3e997a5 commit 153c872

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

README.md

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ providing a lot of additional features.
1616

1717
It's main features include:
1818

19-
* **Creation** from a wide variety of sources: multiple arrays, integers, streams, random, files, ...
19+
* **Creation** from a wide variety of sources: multiple arrays, integers, streams, random, strings, files, ...
2020
* **Transformation** with many built-in: append, xor, and, or, shifts, shuffle, reverse, sort, ...
2121
* **Validators** with the ability to arbitrarily combine multiple ones
2222
* **Parsing and Encoding** in most common binary-to-text-encodings: [hex](https://en.wikipedia.org/wiki/Hexadecimal), [base64](https://en.wikipedia.org/wiki/Base64), ...
@@ -222,6 +222,31 @@ Bytes.from(array).encodeBase36(); //5qpdvuwjvu5
222222

223223
### Utility Methods
224224

225+
Methods that return additional information about the instance.
226+
227+
Finding occurrence of specific bytes:
228+
229+
```java
230+
Bytes.wrap(array).indexOf((byte) 0xFD);
231+
Bytes.wrap(array).lastIndexOf((byte) 0xAE);
232+
```
233+
234+
Length checks:
235+
236+
```java
237+
Bytes.wrap(array).length();
238+
Bytes.wrap(array).lengthBit(); //8 * array.length
239+
Bytes.wrap(array).isEmpty();
240+
```
241+
242+
And others:
243+
244+
```java
245+
Bytes.wrap(array).byteAt(14);
246+
Bytes.wrap(array).count(0x01); //occurrence of 0x01
247+
Bytes.wrap(array).entropy();
248+
```
249+
225250
### Validation
226251

227252
A simple validation framework which can be used to check the internal byte array:
@@ -250,11 +275,64 @@ Nesting is also possible:
250275
assertTrue(Bytes.allocate(16).validate(
251276
or(
252277
and(atLeast(8),not(onlyOf(((byte) 0)))),
253-
or(exactLength(16), exactLength(12)))));
278+
or(exactLength(16), exactLength(12))))); // true
254279
```
255280

256281
### Converting
257282

283+
The internal byte array can be converted or exported into many different formats.
284+
There are 2 different kinds of converters:
285+
286+
* Ones that create a new type which reuses the same shared memory
287+
* Ones that create a copy of the internal array, which start with `to*`
288+
289+
#### Shared Memory Conversion
290+
291+
Not technically a conversation, but it is of course possible to access the internal array:
292+
293+
```java
294+
Bytes.wrap(array).array();
295+
```
296+
297+
Conversion to `InputStream` and `ByteBuffer`:
298+
299+
```java
300+
Bytes.wrap(array).inputStream();
301+
Bytes.wrap(array).byteBuffer();
302+
```
303+
304+
If you just want a duplicated instance, sharing the same array:
305+
306+
```java
307+
Bytes.wrap(array).duplicate();
308+
```
309+
310+
For the conversion to read-only and mutability, see below.
311+
312+
#### Copy Conversion
313+
314+
To primitives (if the internal array is not too long)
315+
316+
```java
317+
Bytes.wrap(array).toByte();
318+
Bytes.wrap(array).toInt();
319+
Bytes.wrap(array).toLong();
320+
```
321+
322+
To other collections
323+
324+
```java
325+
Bytes.wrap(array).toList(); // of type List<Byte>
326+
Bytes.wrap(array).toObjectArray(); // of type Byte[Byte]
327+
Bytes.wrap(array).toBitSet();
328+
```
329+
330+
and to BigInteger of course:
331+
332+
```java
333+
Bytes.wrap(array).toBigInteger();
334+
```
335+
258336
### Mutable and Read-Only
259337

260338
## Digital Signatures

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,6 @@ private ByteBuffer internalBuffer() {
10261026
return ByteBuffer.wrap(internalArray()).order(byteOrder);
10271027
}
10281028

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

0 commit comments

Comments
 (0)