Skip to content

Commit a5f5e16

Browse files
committed
Add readme and bigInteger constructor method
1 parent cf146d6 commit a5f5e16

File tree

9 files changed

+206
-58
lines changed

9 files changed

+206
-58
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v0.3.1
44

55
* better validation and more creation possibilities
6+
* create from bigInteger
67

78
## v0.3.0
89

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,124 @@
66
[![Coverage Status](https://coveralls.io/repos/github/patrickfav/bytes-java/badge.svg?branch=master)](https://coveralls.io/github/patrickfav/bytes-java?branch=master)
77

88

9+
## API Description
10+
11+
### Constructors
12+
13+
There are 2 basic constructors:
14+
15+
* `wrap()` which reuses the given array reference; this is equivalent to `ByteBuffer.wrap()`
16+
* `from()` which always creates a new internal array reference (i.e. a copy of the passed reference)
17+
18+
Here is a simple example to show the difference
19+
20+
byte[] myArray = ...
21+
Bytes bWrap = Bytes.wrap(myArray);
22+
assertSame(myArray, bWrap.array());
23+
24+
byte[] myArray2 = ...
25+
Bytes bFrom = Bytes.from(myArray2);
26+
assertNotSame(myArray2, bFrom.array());
27+
assertArrayEquals(myArray2, bFrom.array());
28+
29+
The following code is equivalent:
30+
31+
Bytes.wrap(myArray).copy() ~ Bytes.from(myArray)
32+
33+
### More Use Cases
34+
35+
Concatenating of multiple byte arrays or bytes:
36+
37+
Bytes.from(array1, array2, array3);
38+
Bytes.from((byte) 0x01, (byte) 0x02, (byte) 0x03);
39+
40+
Creating byte arrays from primitive integer types:
41+
42+
Bytes.from(8); //00000000 00000000 00000000 00001000
43+
Bytes.from(1897621543227L);
44+
45+
Initializing empty arrays of arbitrary length:
46+
47+
Bytes.allocate(16);
48+
Bytes.allocate(4,(byte) 1); //fill with 0x01
49+
50+
Creating random byte arrays for e.g. testing:
51+
52+
Bytes.random(12);
53+
54+
Reading byte content of encoded `String`s:
55+
56+
Bytes.from(utf8String)
57+
Bytes.from(utf8StringToNormalize, Normalizer.Form.NFKD) //normalizes unicode
58+
Bytes.from(asciiString, StandardCharset.US_ASCII) //any charset
59+
60+
And other types:
61+
62+
Bytes.from(byteInputStream); //any inputStream
63+
Bytes.from(byteList); //List<Byte> byteList = ...
64+
Bytes.from(myBitSet); //BitSet myBitSet = ...
65+
Bytes.from(bigInteger);
66+
67+
For parsing binary-text-encoded strings, see below.
68+
69+
### Transformers
70+
71+
Transformer transform the internal byte array. It is possible to create
72+
a custom transformer if a specific use case is required (see `BytesTransformer`).
73+
74+
Bytes result = Bytes.wrap(array1).transform(myCustomTransformer);
75+
76+
#### Built-In Transformers
77+
78+
For **appending** byte arrays or primitive integer types to current instances.
79+
Note however, that this will create new copies of byte arrays every time.
80+
For dynamic growing byte arrays see `ByteArrayOutputStream`
81+
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);
85+
86+
Bitwise operations: XOR, OR, AND, NOT as well as left and right shifts and switching bits:
87+
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);
95+
96+
Copy operations, which copies the internal byte array to a new instance:
97+
98+
Bytes copy = Bytes.wrap(array).copy();
99+
Bytes copy = Bytes.wrap(array).copy(3, 17); //copy partial array
100+
101+
Resizing the internal byte array:
102+
103+
Bytes resized = Bytes.wrap(array).resize(3); //from {3, 9, 2, 1} to {9, 2, 1}
104+
105+
Other transformers:
106+
107+
Bytes result = Bytes.wrap(array).shuffle();
108+
Bytes result = Bytes.wrap(array).sort(myComparator);
109+
Bytes result = Bytes.wrap(array).reverse();
110+
111+
### Parser and Encoder
112+
113+
### Validation
114+
115+
A simple validation framework which can be used to check the internal byte array:
116+
117+
Bytes.wrap(new byte[]{8, 3, 9}.validate(BytesValidators.atLeast(3)); // true
118+
119+
This is especially convenient when combining validators:
120+
121+
Bytes.wrap(new byte[]{0, 1}.validate(BytesValidators.atMost(2), BytesValidators.notOnlyOf((byte) 0)); // true
122+
123+
### Converting
124+
125+
### Mutable and Read-Only
126+
9127
## Digital Signatures
10128

11129
### Signed Jar

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

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* <strong>Example:</strong>
5454
* <pre>
5555
* Bytes b = Bytes.from(array);
56-
* b.negate();
56+
* b.not();
5757
* System.out.println(b.encodeHex());
5858
* </pre>
5959
*/
@@ -265,7 +265,7 @@ public static Bytes from(ByteBuffer buffer) {
265265
}
266266

267267
/**
268-
* Creates a new instance from given BitSet.
268+
* Creates a new instance from given {@link BitSet}.
269269
*
270270
* @param set to get the byte array from
271271
* @return new instance
@@ -274,6 +274,17 @@ public static Bytes from(BitSet set) {
274274
return wrap(set.toByteArray());
275275
}
276276

277+
/**
278+
* /**
279+
* Creates a new instance from given {@link BigInteger}.
280+
*
281+
* @param bigInteger to get the byte array from
282+
* @return new instance
283+
*/
284+
public static Bytes from(BigInteger bigInteger) {
285+
return wrap(bigInteger.toByteArray());
286+
}
287+
277288
/**
278289
* Reads given input stream and creates a new instance from read data
279290
*
@@ -574,13 +585,13 @@ public Bytes or(byte[] secondArray) {
574585
}
575586

576587
/**
577-
* Bitwise negate operation on the whole internal byte array.
588+
* Bitwise not operation on the whole internal byte array.
578589
* See the considerations about possible in-place operation in {@link #transform(BytesTransformer)}.
579590
*
580591
* @return negated instance
581592
* @see <a href="https://en.wikipedia.org/wiki/Bitwise_operation#NOT">Bitwise operators: NOT</a>
582593
*/
583-
public Bytes negate() {
594+
public Bytes not() {
584595
return transform(new BytesTransformer.NegateTransformer());
585596
}
586597

@@ -610,6 +621,27 @@ public Bytes rightShift(int shiftCount) {
610621
return transform(new BytesTransformer.ShiftTransformer(shiftCount, BytesTransformer.ShiftTransformer.Type.RIGHT_SHIFT));
611622
}
612623

624+
/**
625+
* Returns a Byte whose value is equivalent to this Byte with the designated bit set to newBitValue. Bits start to count from the LSB (ie. Bytes.from(0).switchBit(0,true) == 1)
626+
*
627+
* @param bitPosition not to confuse with byte position
628+
* @param newBitValue if true set to 1, 0 otherwise
629+
* @return instance with bit switched
630+
*/
631+
public Bytes switchBit(int bitPosition, boolean newBitValue) {
632+
return transform(new BytesTransformer.BitSwitchTransformer(bitPosition, newBitValue));
633+
}
634+
635+
/**
636+
* Returns a Byte whose value is equivalent to this Byte with the designated bit switched.
637+
*
638+
* @param bitPosition not to confuse with byte position
639+
* @return instance with bit switched
640+
*/
641+
public Bytes switchBit(int bitPosition) {
642+
return transform(new BytesTransformer.BitSwitchTransformer(bitPosition, null));
643+
}
644+
613645
/**
614646
* Creates a new instance with a copy of the internal byte array and all other attributes.
615647
*
@@ -704,27 +736,6 @@ public Bytes resize(int newByteLength) {
704736
return transform(new BytesTransformer.ResizeTransformer(newByteLength));
705737
}
706738

707-
/**
708-
* Returns a Byte whose value is equivalent to this Byte with the designated bit set to newBitValue. Bits start to count from the LSB (ie. Bytes.from(0).switchBit(0,true) == 1)
709-
*
710-
* @param bitPosition not to confuse with byte position
711-
* @param newBitValue if true set to 1, 0 otherwise
712-
* @return instance with bit switched
713-
*/
714-
public Bytes switchBit(int bitPosition, boolean newBitValue) {
715-
return transform(new BytesTransformer.BitSwitchTransformer(bitPosition, newBitValue));
716-
}
717-
718-
/**
719-
* Returns a Byte whose value is equivalent to this Byte with the designated bit switched.
720-
*
721-
* @param bitPosition not to confuse with byte position
722-
* @return instance with bit switched
723-
*/
724-
public Bytes switchBit(int bitPosition) {
725-
return transform(new BytesTransformer.BitSwitchTransformer(bitPosition, null));
726-
}
727-
728739
/**
729740
* Generic transformation of this instance.
730741
* <p>
@@ -973,23 +984,7 @@ private ByteBuffer internalBuffer() {
973984
return ByteBuffer.wrap(internalArray()).order(byteOrder);
974985
}
975986

976-
/**
977-
* The internal byte array wrapped in a {@link BigInteger} instance.
978-
* <p>
979-
* If the internal byte order is {@link ByteOrder#LITTLE_ENDIAN}, a copy of the internal
980-
* array will be reversed and used as backing array with the big integer. Otherwise the internal
981-
* array will be used directly.
982-
*
983-
* @return big integer
984-
* @throws ReadOnlyBufferException if this is a read-only instance
985-
*/
986-
public BigInteger bigInteger() {
987-
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
988-
return new BigInteger(new BytesTransformer.ReverseTransformer().transform(array(), false));
989-
} else {
990-
return new BigInteger(array());
991-
}
992-
}
987+
993988

994989
/**
995990
* Returns a mutable version of this instance with sharing the same underlying byte-array.
@@ -1182,6 +1177,23 @@ public BitSet toBitSet() {
11821177
return BitSet.valueOf(internalArray());
11831178
}
11841179

1180+
/**
1181+
* The internal byte array wrapped in a {@link BigInteger} instance.
1182+
* <p>
1183+
* If the internal byte order is {@link ByteOrder#LITTLE_ENDIAN}, a copy of the internal
1184+
* array will be reversed and used as backing array with the big integer. Otherwise the internal
1185+
* array will be used directly.
1186+
*
1187+
* @return big integer
1188+
*/
1189+
public BigInteger toBigInteger() {
1190+
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
1191+
return new BigInteger(new BytesTransformer.ReverseTransformer().transform(internalArray(), false));
1192+
} else {
1193+
return new BigInteger(internalArray());
1194+
}
1195+
}
1196+
11851197
/**
11861198
* If the underlying byte array is smaller than or equal to 1 byte / 8 bit returns unsigned two-complement
11871199
* representation for a Java byte value.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ public void toLong() throws Exception {
126126
@Test
127127
public void bigInteger() throws Exception {
128128
Bytes b = Bytes.from(example_bytes_four);
129-
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).bigInteger(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).bigInteger());
130-
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).bigInteger(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().bigInteger());
129+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toBigInteger(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).toBigInteger());
130+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toBigInteger(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().toBigInteger());
131131
}
132132

133133
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Test;
2525

2626
import java.io.ByteArrayInputStream;
27+
import java.math.BigInteger;
2728
import java.nio.ByteBuffer;
2829
import java.nio.charset.Charset;
2930
import java.nio.charset.StandardCharsets;
@@ -79,6 +80,7 @@ public void fromBitSet() throws Exception {
7980
checkBitSet(example_bytes_two);
8081
checkBitSet(example_bytes_seven);
8182
checkBitSet(example_bytes_eight);
83+
checkBitSet(example_bytes_twentyfour);
8284
}
8385

8486
private void checkBitSet(byte[] array) {
@@ -87,6 +89,21 @@ private void checkBitSet(byte[] array) {
8789
assertEquals(bitSet, Bytes.from(bitSet).toBitSet());
8890
}
8991

92+
@Test
93+
public void fromBigInteger() throws Exception {
94+
checkBigInteger(example_bytes_one);
95+
checkBigInteger(example_bytes_two);
96+
checkBigInteger(example_bytes_seven);
97+
checkBigInteger(example_bytes_eight);
98+
checkBigInteger(example_bytes_twentyfour);
99+
}
100+
101+
private void checkBigInteger(byte[] array) {
102+
BigInteger bigInteger = new BigInteger(array);
103+
assertArrayEquals(array, Bytes.from(bigInteger).array());
104+
assertEquals(bigInteger, Bytes.from(bigInteger).toBigInteger());
105+
}
106+
90107
@Test
91108
public void fromByte() throws Exception {
92109
byte test = 0x4E;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void readOnlyShouldKeepProperty() throws Exception {
192192
assertTrue(b.resize(6).isReadOnly());
193193
assertTrue(b.sort().isReadOnly());
194194
assertTrue(b.shuffle().isReadOnly());
195-
assertTrue(b.negate().isReadOnly());
195+
assertTrue(b.not().isReadOnly());
196196
assertTrue(b.leftShift(1).isReadOnly());
197197
assertTrue(b.rightShift(1).isReadOnly());
198198
assertTrue(b.and(Bytes.random(b.length())).isReadOnly());

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public void array() throws Exception {
4242

4343
@Test
4444
public void bigInteger() throws Exception {
45-
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).bigInteger().toByteArray());
46-
assertArrayEquals(example_bytes_two, Bytes.from(example_bytes_two).bigInteger().toByteArray());
47-
assertArrayEquals(example_bytes_four, Bytes.from(example_bytes_four).bigInteger().toByteArray());
48-
assertArrayEquals(example_bytes_seven, Bytes.from(example_bytes_seven).bigInteger().toByteArray());
49-
assertArrayEquals(example_bytes_eight, Bytes.from(example_bytes_eight).bigInteger().toByteArray());
50-
assertArrayEquals(example_bytes_sixteen, Bytes.from(example_bytes_sixteen).bigInteger().toByteArray());
45+
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).toBigInteger().toByteArray());
46+
assertArrayEquals(example_bytes_two, Bytes.from(example_bytes_two).toBigInteger().toByteArray());
47+
assertArrayEquals(example_bytes_four, Bytes.from(example_bytes_four).toBigInteger().toByteArray());
48+
assertArrayEquals(example_bytes_seven, Bytes.from(example_bytes_seven).toBigInteger().toByteArray());
49+
assertArrayEquals(example_bytes_eight, Bytes.from(example_bytes_eight).toBigInteger().toByteArray());
50+
assertArrayEquals(example_bytes_sixteen, Bytes.from(example_bytes_sixteen).toBigInteger().toByteArray());
5151
}
5252

5353
@Test

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ public void and() throws Exception {
164164

165165
@Test
166166
public void negate() throws Exception {
167-
assertArrayEquals(new byte[0], Bytes.from(new byte[0]).negate().array());
168-
assertArrayEquals(new byte[]{(byte) 0xFF}, Bytes.from(new byte[1]).negate().array());
167+
assertArrayEquals(new byte[0], Bytes.from(new byte[0]).not().array());
168+
assertArrayEquals(new byte[]{(byte) 0xFF}, Bytes.from(new byte[1]).not().array());
169169

170-
assertArrayEquals(new byte[]{-104}, Bytes.from(example_bytes_one).negate().array());
171-
assertArrayEquals(new byte[]{-27, -112}, Bytes.from(example_bytes_two).negate().array());
172-
assertArrayEquals(new byte[]{81, -31}, Bytes.from(new byte[]{(byte) 0xAE, (byte) 0x1E}).negate().array());
170+
assertArrayEquals(new byte[]{-104}, Bytes.from(example_bytes_one).not().array());
171+
assertArrayEquals(new byte[]{-27, -112}, Bytes.from(example_bytes_two).not().array());
172+
assertArrayEquals(new byte[]{81, -31}, Bytes.from(new byte[]{(byte) 0xAE, (byte) 0x1E}).not().array());
173173

174-
assertArrayNotEquals(new byte[0], Bytes.from(example_bytes_one).negate().array());
174+
assertArrayNotEquals(new byte[0], Bytes.from(example_bytes_one).not().array());
175175
}
176176

177177
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void testTransformerShouldBeMutable() throws Exception {
117117
assertTrue(b.resize(6).isMutable());
118118
assertTrue(b.sort().isMutable());
119119
assertTrue(b.shuffle().isMutable());
120-
assertTrue(b.negate().isMutable());
120+
assertTrue(b.not().isMutable());
121121
assertTrue(b.leftShift(1).isMutable());
122122
assertTrue(b.rightShift(1).isMutable());
123123
assertTrue(b.and(Bytes.random(b.length())).isMutable());

0 commit comments

Comments
 (0)