Skip to content

Commit be961a4

Browse files
committed
Add some more tests and small fixes
1 parent 063a918 commit be961a4

File tree

7 files changed

+104
-25
lines changed

7 files changed

+104
-25
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
*
3131
* @see <a href="https://en.wikipedia.org/wiki/Binary-to-text_encoding">Binary To Text Encoding</a>
3232
*/
33-
public final class BinaryToTextEncoding {
33+
public interface BinaryToTextEncoding {
3434

3535
/**
3636
* Interface for encoding bytes
3737
*/
38-
public interface Encoder {
38+
interface Encoder {
3939

4040
/**
4141
* Encodes given array with given byte order to a string
@@ -50,7 +50,7 @@ public interface Encoder {
5050
/**
5151
* Interface for decoding encoded strings
5252
*/
53-
public interface Decoder {
53+
interface Decoder {
5454

5555
/**
5656
* Decodes given encoded string
@@ -64,7 +64,7 @@ public interface Decoder {
6464
/**
6565
* Hex or Base16
6666
*/
67-
public static class Hex implements Encoder, Decoder {
67+
class Hex implements Encoder, Decoder {
6868
private final boolean upperCase;
6969

7070
public Hex() {
@@ -111,7 +111,7 @@ public byte[] decode(String hexString) {
111111
}
112112
}
113113

114-
public static class Base64Encoding implements Encoder, Decoder {
114+
class Base64Encoding implements Encoder, Decoder {
115115
@Override
116116
public String encode(byte[] array, ByteOrder byteOrder) {
117117
return Base64.encode(array);
@@ -123,7 +123,7 @@ public byte[] decode(String encoded) {
123123
}
124124
}
125125

126-
public static class BaseRadix implements Encoder, Decoder {
126+
class BaseRadix implements Encoder, Decoder {
127127
private final int radix;
128128

129129
BaseRadix(int radix) {

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ public static Bytes allocate(int length, byte defaultValue) {
8585
return wrap(array);
8686
}
8787

88+
/**
89+
* Creates a new reference backed by the same byte array.
90+
* Inherits all attributes (readonly, etc.)
91+
*
92+
* @param bytes to use as template
93+
* @return new instance
94+
*/
95+
public static Bytes wrap(Bytes bytes) {
96+
return new Bytes(bytes.internalArray(), bytes.byteOrder, bytes.mutable, bytes.readonly);
97+
}
98+
8899
/**
89100
* Creates a new instance with given byte array.
90101
* <p>
@@ -374,22 +385,23 @@ public static Bytes random(int length, Random random) {
374385
private final boolean readonly;
375386

376387
/**
377-
* Creates a new immutable instance with Big-Endian ordering
388+
* Creates a new immutable instance
378389
*
379390
* @param byteArray internal byte array
391+
* @param byteOrder the internal byte order - this is used to interpret given array, not to change it
380392
*/
381-
Bytes(byte[] byteArray) {
382-
this(byteArray, ByteOrder.BIG_ENDIAN, false, false);
393+
Bytes(byte[] byteArray, ByteOrder byteOrder) {
394+
this(byteArray, byteOrder, false, false);
383395
}
384396

385397
/**
386-
* Creates a new immutable instance
398+
* Creates a new instance with given array and copies all attributes from old instance
387399
*
388-
* @param byteArray internal byte array
389-
* @param byteOrder the internal byte order - this is used to interpret given array, not to change it
400+
* @param byteArray internal byte array
401+
* @param oldInstance old instance to copy all internal attributes
390402
*/
391-
Bytes(byte[] byteArray, ByteOrder byteOrder) {
392-
this(byteArray, byteOrder, false, false);
403+
Bytes(byte[] byteArray, Bytes oldInstance) {
404+
this(byteArray, oldInstance.byteOrder(), oldInstance.mutable, oldInstance.readonly);
393405
}
394406

395407
/**
@@ -706,16 +718,17 @@ public Bytes resize(int newByteLength) {
706718
}
707719

708720
if (newByteLength == 0) {
709-
return wrap(new byte[0]);
721+
return new Bytes(new byte[0], this);
710722
}
711723

712-
byte[] newSize = new byte[newByteLength];
724+
byte[] resizedArray = new byte[newByteLength];
713725
if (newByteLength > length()) {
714-
System.arraycopy(internalArray(), 0, newSize, Math.max(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
726+
System.arraycopy(internalArray(), 0, resizedArray, Math.max(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
715727
} else {
716-
System.arraycopy(internalArray(), Math.max(0, Math.abs(newByteLength - length())), newSize, Math.min(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
728+
System.arraycopy(internalArray(), Math.max(0, Math.abs(newByteLength - length())), resizedArray, Math.min(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
717729
}
718-
return wrap(newSize);
730+
731+
return new Bytes(resizedArray, this);
719732
}
720733

721734

@@ -864,9 +877,10 @@ public double entropy() {
864877
* @return new instance backed by the same data
865878
*/
866879
public Bytes duplicate() {
867-
return wrap(internalArray());
880+
return wrap(this);
868881
}
869882

883+
870884
/**
871885
* Set the byte order or endianness of this instance. Default in Java is {@link ByteOrder#BIG_ENDIAN}.
872886
* <p>
@@ -878,7 +892,7 @@ public Bytes duplicate() {
878892
*/
879893
public Bytes byteOrder(ByteOrder byteOrder) {
880894
if (byteOrder != this.byteOrder) {
881-
return new Bytes(internalArray(), byteOrder);
895+
return new Bytes(internalArray(), byteOrder, mutable, readonly);
882896
}
883897
return this;
884898
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ static void shuffle(byte[] array, Random random) {
194194

195195
private static final int BUF_SIZE = 0x1000; // 4K
196196

197+
/**
198+
* Read all bytes, buffered, from given input stream
199+
*
200+
* @param inputStream to read from
201+
* @return all bytes from the stream
202+
*/
197203
static byte[] readFromStream(InputStream inputStream) {
198204
try {
199205
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -211,6 +217,13 @@ static byte[] readFromStream(InputStream inputStream) {
211217
}
212218
}
213219

220+
/**
221+
* Combines a single argument with a vararg to a single array
222+
*
223+
* @param firstByte first arg
224+
* @param moreBytes varargs
225+
* @return array containing all args
226+
*/
214227
static byte[] concatVararg(byte firstByte, byte[] moreBytes) {
215228
if (moreBytes == null) {
216229
return new byte[]{firstByte};
@@ -262,9 +275,5 @@ public double entropy() {
262275
}
263276
return entropy;
264277
}
265-
266-
public double perplexity() {
267-
return Math.pow(2, entropy());
268-
}
269278
}
270279
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package at.favre.lib.bytes;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertArrayEquals;
6+
import static org.junit.Assert.assertEquals;
7+
8+
/**
9+
* Test cases from rfc4648
10+
*/
11+
public class Base64Test {
12+
13+
@Test
14+
public void decode() throws Exception {
15+
assertArrayEquals("".getBytes(), Base64.decode(""));
16+
assertArrayEquals("f".getBytes(), Base64.decode("Zg=="));
17+
assertArrayEquals("fo".getBytes(), Base64.decode("Zm8="));
18+
assertArrayEquals("foo".getBytes(), Base64.decode("Zm9v"));
19+
assertArrayEquals("foob".getBytes(), Base64.decode("Zm9vYg=="));
20+
assertArrayEquals("fooba".getBytes(), Base64.decode("Zm9vYmE="));
21+
assertArrayEquals("foobar".getBytes(), Base64.decode("Zm9vYmFy"));
22+
}
23+
24+
@Test
25+
public void encode() throws Exception {
26+
assertEquals("", Base64.encode("".getBytes()));
27+
assertEquals("Zg==", Base64.encode("f".getBytes()));
28+
assertEquals("Zm8=", Base64.encode("fo".getBytes()));
29+
assertEquals("Zm9v", Base64.encode("foo".getBytes()));
30+
assertEquals("Zm9vYg==", Base64.encode("foob".getBytes()));
31+
assertEquals("Zm9vYmE=", Base64.encode("fooba".getBytes()));
32+
assertEquals("Zm9vYmFy", Base64.encode("foobar".getBytes()));
33+
}
34+
35+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public void wrapTest() throws Exception {
4444

4545
assertSame(example_bytes_seven, b.array());
4646
assertArrayNotEquals(copy, example_bytes_seven);
47+
48+
Bytes b2 = Bytes.wrap(b);
49+
assertSame(b.array(), b2.array());
4750
}
4851

4952
@Test

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void indexOfByte() throws Exception {
110110
@Test
111111
public void indexOfArray() throws Exception {
112112
assertEquals(-1, Bytes.allocate(0).indexOf(new byte[]{(byte) 0xFD}));
113+
assertEquals(0, Bytes.allocate(1).indexOf(new byte[0]));
113114
assertEquals(2, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0xFF}));
114115
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0x00}));
115116
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package at.favre.lib.bytes;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertArrayEquals;
6+
7+
public class UtilTest {
8+
@Test(expected = IllegalStateException.class)
9+
public void readFromStream() throws Exception {
10+
Util.readFromStream(null);
11+
}
12+
13+
@Test
14+
public void concatVararg() throws Exception {
15+
assertArrayEquals(new byte[]{1}, Util.concatVararg((byte) 1, null));
16+
}
17+
}

0 commit comments

Comments
 (0)