88
99## API Description
1010
11+ Per default the instance is ** immutable** , which means any transformation will
12+ create a copy of the internal array (it is, however, possible to get and
13+ modify the internal array). There is a ** mutable** version which supports
14+ in-place modification for better performance and a ** read-only** version which
15+ restricts the access to the internal array.
16+
1117### Constructors
1218
13- There are 2 basic constructors:
19+ There are 3 basic constructors:
1420
1521 * ` wrap() ` which reuses the given array reference; this is equivalent to ` ByteBuffer.wrap() `
1622 * ` from() ` which always creates a new internal array reference (i.e. a copy of the passed reference)
23+ * ` parse() ` which parses from binary-text-encoded strings (see other section)
1724
1825Here is a simple example to show the difference
1926
20- byte[] myArray = ...
21- Bytes bWrap = Bytes.wrap(myArray);
22- assertSame(myArray, bWrap.array());
27+ ``` java
28+ byte [] myArray = ...
29+ Bytes bWrap = Bytes . wrap(myArray);
30+ assertSame(myArray, bWrap. array());
2331
24- byte[] myArray2 = ...
25- Bytes bFrom = Bytes.from(myArray2);
26- assertNotSame(myArray2, bFrom.array());
27- assertArrayEquals(myArray2, bFrom.array());
32+ byte [] myArray2 = ...
33+ Bytes bFrom = Bytes . from(myArray2);
34+ assertNotSame(myArray2, bFrom. array());
35+ assertArrayEquals(myArray2, bFrom. array());
36+ ```
2837
2938The following code is equivalent:
3039
31- Bytes.wrap(myArray).copy() ~ Bytes.from(myArray)
40+ ``` java
41+ Bytes . wrap(myArray). copy() ~ Bytes . from(myArray)
42+ ```
3243
33- ### More Use Cases
44+ ### More Constructors
3445
35- Concatenating of multiple byte arrays or bytes:
46+ ** Concatenating** of multiple byte arrays or bytes:
3647
37- Bytes.from(array1, array2, array3);
38- Bytes.from((byte) 0x01, (byte) 0x02, (byte) 0x03);
48+ ``` java
49+ Bytes . from(array1, array2, array3);
50+ Bytes . from((byte ) 0x01 , (byte ) 0x02 , (byte ) 0x03 );
51+ ```
3952
40- Creating byte arrays from primitive integer types:
53+ Creating byte arrays from ** primitive integer** types:
4154
42- Bytes.from(8); //00000000 00000000 00000000 00001000
43- Bytes.from(1897621543227L);
55+ ``` java
56+ Bytes . from(8 ); // 00000000 00000000 00000000 00001000
57+ Bytes . from(1897621543227L );
58+ ```
4459
45- Initializing empty arrays of arbitrary length:
60+ Initializing ** empty arrays** of arbitrary length:
4661
47- Bytes.allocate(16);
48- Bytes.allocate(4,(byte) 1); //fill with 0x01
62+ ``` java
63+ Bytes . allocate(16 );
64+ Bytes . allocate(4 ,(byte ) 1 ); // fill with 0x01
65+ ```
4966
50- Creating random byte arrays for e.g. testing:
67+ Creating ** random** byte arrays for e.g. testing:
5168
52- Bytes.random(12);
69+ ``` java
70+ Bytes . random(12 );
71+ ```
5372
5473Reading byte content of encoded ` String ` s:
5574
56- Bytes.from(utf8String)
57- Bytes.from(utf8StringToNormalize, Normalizer.Form.NFKD) //normalizes unicode
58- Bytes.from(asciiString, StandardCharset.US_ASCII) //any charset
75+ ``` java
76+ Bytes . from(utf8String)
77+ Bytes . from(utf8StringToNormalize, Normalizer . Form . NFKD ) // normalizes unicode
78+ Bytes . from(asciiString, StandardCharset . US_ASCII ) // any charset
79+ ```
5980
6081And other types:
6182
62- Bytes.from(byteInputStream); //any inputStream
63- Bytes.from(byteList); //List<Byte> byteList = ...
64- Bytes.from(myBitSet); //BitSet myBitSet = ...
65- Bytes.from(bigInteger);
83+ ``` java
84+ Bytes . from(byteInputStream); // any inputStream
85+ Bytes . from(byteList); // List<Byte> byteList = ...
86+ Bytes . from(myBitSet); // BitSet myBitSet = ...
87+ Bytes . from(bigInteger);
88+ ```
6689
6790For parsing binary-text-encoded strings, see below.
6891
@@ -71,54 +94,105 @@ For parsing binary-text-encoded strings, see below.
7194Transformer transform the internal byte array. It is possible to create
7295a custom transformer if a specific use case is required (see ` BytesTransformer ` ).
7396
74- Bytes result = Bytes.wrap(array1).transform(myCustomTransformer);
97+ ``` java
98+ Bytes result = Bytes . wrap(array1). transform(myCustomTransformer);
99+ ```
75100
76101#### Built-In Transformers
77102
78103For ** appending** byte arrays or primitive integer types to current instances.
79104Note however, that this will create new copies of byte arrays every time.
80105For dynamic growing byte arrays see ` ByteArrayOutputStream `
81106
82- Bytes result = Bytes.wrap(array1).append(array2);
83- Bytes result = Bytes.wrap(array1).append(1341);
84- Bytes result = Bytes.wrap(array1).append((byte) 3);
107+ ``` java
108+ Bytes result = Bytes . wrap(array1). append(array2);
109+ Bytes result = Bytes . wrap(array1). append(1341 );
110+ Bytes result = Bytes . wrap(array1). append((byte ) 3 );
111+ ```
85112
86- Bitwise operations: XOR, OR, AND, NOT as well as left and right shifts and switching bits:
113+ ** Bitwise operations** : XOR, OR, AND, NOT as well as left and right shifts and switching bits:
87114
88- Bytes result = Bytes.wrap(array).xor(array2);
89- Bytes result = Bytes.wrap(array).or(array2);
90- Bytes result = Bytes.wrap(array).and(array2);
91- Bytes result = Bytes.wrap(array).negate();
92- Bytes result = Bytes.wrap(array).leftShift(8);
93- Bytes result = Bytes.wrap(array).rightShift(8);
94- Bytes result = Bytes.wrap(array).switchBit(3, true);
115+ ``` java
116+ Bytes result = Bytes . wrap(array). xor(array2);
117+ Bytes result = Bytes . wrap(array). or(array2);
118+ Bytes result = Bytes . wrap(array). and(array2);
119+ Bytes result = Bytes . wrap(array). negate();
120+ Bytes result = Bytes . wrap(array). leftShift(8 );
121+ Bytes result = Bytes . wrap(array). rightShift(8 );
122+ Bytes result = Bytes . wrap(array). switchBit(3 , true );
123+ ```
95124
96- Copy operations, which copies the internal byte array to a new instance:
125+ ** Copy** operations, which copies the internal byte array to a new instance:
97126
98- Bytes copy = Bytes.wrap(array).copy();
99- Bytes copy = Bytes.wrap(array).copy(3, 17); //copy partial array
127+ ``` java
128+ Bytes copy = Bytes . wrap(array). copy();
129+ Bytes copy = Bytes . wrap(array). copy(3 , 17 ); // copy partial array
130+ ```
100131
101- Resizing the internal byte array:
132+ ** Resizing** the internal byte array:
102133
103- Bytes resized = Bytes.wrap(array).resize(3); //from {3, 9, 2, 1} to {9, 2, 1}
134+ ``` java
135+ Bytes resized = Bytes . wrap(array). resize(3 ); // from {3, 9, 2, 1} to {9, 2, 1}
136+ ```
104137
105138Other transformers:
106139
107- Bytes result = Bytes.wrap(array).shuffle();
108- Bytes result = Bytes.wrap(array).sort(myComparator);
109- Bytes result = Bytes.wrap(array).reverse();
140+ ``` java
141+ Bytes result = Bytes . wrap(array). shuffle();
142+ Bytes result = Bytes . wrap(array). sort(myComparator);
143+ Bytes result = Bytes . wrap(array). reverse();
144+ ```
145+
146+ ### Parser and Encoder for Binary-Text-Encodings
147+
148+ This library can parse and encode a variety of encodings: binary, decimal, ocatal,
149+ hex, base36 and base64. Additionally custom parsers are supported by providing your own
150+ implementation:
151+
152+ ``` java
153+ Bytes . parse(" 8sK;S*j=r" , base85Decoder);
154+ Bytes . encode(base85Encoder);
155+ ```
156+
157+ ** Hex** can be upper and lowercase and also supports ` 0x ` prefix for parsing
158+
159+ ``` java
160+ Bytes . parseHex(" a0e13eaa1a" )
161+ Bytes . parseHex(" 0xA0E1" )
162+
163+ Bytes . from(array). encodeHex() // a0e13eaa1a
164+ ```
165+
166+ This lib has it's own build in ** Base64** encoder:
167+
168+ ``` java
169+ Bytes . parseBase64(" SpT9/x6v7Q==" );
170+
171+ Bytes . from(array). encodeBase64(); // "SpT9/x6v7Q=="
172+ ```
173+
174+ Additionally the following encodings are supported:
110175
111- ### Parser and Encoder
176+ ``` java
177+ Bytes . from(array). encodeBinary(); // 1110110110101111
178+ Bytes . from(array). encodeDec(); // 20992966904426477
179+ Bytes . from(array). encodeOctal(); // 1124517677707527755
180+ Bytes . from(array). encodeBase36(); // 5qpdvuwjvu5
181+ ```
112182
113183### Validation
114184
115185A simple validation framework which can be used to check the internal byte array:
116186
117- Bytes.wrap(new byte[]{8, 3, 9}.validate(BytesValidators.atLeast(3)); // true
187+ ``` java
188+ Bytes . wrap(new byte []{8 , 3 , 9 }. validate(BytesValidators . atLeast(3 )); // true
189+ ```
118190
119191This is especially convenient when combining validators:
120192
121- Bytes.wrap(new byte[]{0, 1}.validate(BytesValidators.atMost(2), BytesValidators.notOnlyOf((byte) 0)); // true
193+ ```java
194+ Bytes . wrap(new byte []{0 , 1 }. validate(BytesValidators . atMost(2 ), BytesValidators . notOnlyOf((byte ) 0 )); // true
195+ ```
122196
123197### Converting
124198
0 commit comments