Skip to content

Commit 9d42b6b

Browse files
committed
Add more to readme
1 parent 153c872 commit 9d42b6b

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

README.md

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@ as well as **immutability** and **mutability**, so the caller may decide to favo
77
performance. This can be seen as combination of the features provided by
88
[`BigInteger`](https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html),
99
[`ByteBuffer`](https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html) but
10-
providing a lot of additional features.
10+
providing a lot of additional features. The main goal is to minimize the need
11+
to blindly paste code snippets from
12+
[s](https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java)
13+
[t](https://stackoverflow.com/questions/12893758/how-to-reverse-the-byte-array-in-java)
14+
[a](https://stackoverflow.com/questions/3329163/is-there-an-equivalent-to-memcpy-in-java)
15+
[c](https://stackoverflow.com/questions/5513152/easy-way-to-concatenate-two-byte-arrays)
16+
[k](https://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java)
17+
[o](https://stackoverflow.com/questions/14243922/java-xor-over-two-arrays)
18+
[v](https://stackoverflow.com/questions/28997781/bit-shift-operations-on-a-byte-array-in-java)
19+
[e](https://stackoverflow.com/questions/13109588/base64-encoding-in-java)
20+
[r](https://stackoverflow.com/questions/2091454/byte-to-inputstream-or-outputstream)
21+
[f](https://stackoverflow.com/questions/3736058/java-object-to-byte-and-byte-to-object-converter-for-tokyo-cabinet)
22+
[l](https://stackoverflow.com/questions/4231674/converting-an-array-of-bytes-to-listbyte)
23+
[o](https://stackoverflow.com/questions/28703273/sorting-byte-arrays-in-numeric-order)
24+
[w](https://stackoverflow.com/questions/4385623/bytes-of-a-string-in-java)
25+
[.com](https://stackoverflow.com/questions/23360692/byte-position-in-java)
1126

1227
[![Download](https://api.bintray.com/packages/patrickfav/maven/bytes-java/images/download.svg)](https://bintray.com/patrickfav/maven/bytes-java/_latestVersion)
1328
[![Build Status](https://travis-ci.org/patrickfav/bytes-java.svg?branch=master)](https://travis-ci.org/patrickfav/bytes-java)
@@ -247,6 +262,16 @@ Bytes.wrap(array).count(0x01); //occurrence of 0x01
247262
Bytes.wrap(array).entropy();
248263
```
249264

265+
Of course all standard Java Object methods are implemented including:
266+
`hashCode()`, `equals()`, `toString()` as well as it being
267+
[`Comparable`](https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html).
268+
269+
The `toString()` methods only shows the length and a preview of maximal 8 bytes:
270+
271+
```
272+
16 bytes (0x7ed1fdaa...12af000a)
273+
```
274+
250275
### Validation
251276

252277
A simple validation framework which can be used to check the internal byte array:
@@ -283,8 +308,8 @@ assertTrue(Bytes.allocate(16).validate(
283308
The internal byte array can be converted or exported into many different formats.
284309
There are 2 different kinds of converters:
285310

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*`
311+
* Ones that create a new type which **reuses the same shared memory**
312+
* Ones that create a **copy** of the internal array, which start with `to*`
288313

289314
#### Shared Memory Conversion
290315

@@ -294,7 +319,8 @@ Not technically a conversation, but it is of course possible to access the inter
294319
Bytes.wrap(array).array();
295320
```
296321

297-
Conversion to `InputStream` and `ByteBuffer`:
322+
Conversion to [`InputStream`](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html)
323+
and [`ByteBuffer`](https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html):
298324

299325
```java
300326
Bytes.wrap(array).inputStream();
@@ -323,18 +349,62 @@ To other collections
323349

324350
```java
325351
Bytes.wrap(array).toList(); // of type List<Byte>
326-
Bytes.wrap(array).toObjectArray(); // of type Byte[Byte]
352+
Bytes.wrap(array).toObjectArray(); // of type Byte[]
327353
Bytes.wrap(array).toBitSet();
328354
```
329355

330-
and to BigInteger of course:
356+
and to [`BigInteger`](https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html) of course:
331357

332358
```java
333359
Bytes.wrap(array).toBigInteger();
334360
```
335361

336362
### Mutable and Read-Only
337363

364+
Per default the instance is immutable, i.e. every transformation will create a
365+
a new internal byte array (very similar to the API of `BigInteger`). While
366+
this is usually the default way to design such a construct because it shows
367+
[various advantages](https://softwareengineering.stackexchange.com/questions/151733/if-immutable-objects-are-good-why-do-people-keep-creating-mutable-objects)
368+
this can introduce a major performance issue when handling big arrays
369+
or many transformations.
370+
371+
#### Mutable Bytes
372+
373+
All transformers (if possible) reuse or overwrite the same internal memory
374+
to avoid unneeded array creation to minimize time and space complexity.
375+
To create a mutable instance just do:
376+
377+
```java
378+
MutableBytes b = Bytes.from(array).mutable();
379+
```
380+
381+
Mutable classes also enable further APIs for directly modify the internal array:
382+
383+
```java
384+
b.overwrite(anotherArray) //directly overwrite given array
385+
b.fill(0x03) // fills with e.g. 3
386+
b.wipe() //fills with zeros
387+
b.secureWipe() //fills with random data
388+
```
389+
390+
#### Readonly Bytes
391+
392+
On the other hand, if you want a export a instance with limited access,
393+
especially no easy way to alter the internal byte array, read-only instances
394+
may be created by:
395+
396+
```java
397+
Bytes b = Bytes.from(array).readOnly();
398+
```
399+
400+
Every call to the following conversation methods will throw a `ReadOnlyBufferException`:
401+
402+
```java
403+
readOnlyBytes.array();
404+
readOnlyBytes.byteBuffer();
405+
readOnlyBytes.inputStream();
406+
```
407+
338408
## Digital Signatures
339409

340410
### Signed Jar

0 commit comments

Comments
 (0)