Skip to content

Commit 063a918

Browse files
committed
Add more tests
1 parent 4a97126 commit 063a918

File tree

5 files changed

+110
-32
lines changed

5 files changed

+110
-32
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,10 @@ public static byte[] decode(String in) {
120120
'5', '6', '7', '8', '9', '+', '/'
121121
};
122122

123-
private static final byte[] URL_MAP = new byte[]{
124-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
125-
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
126-
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
127-
'5', '6', '7', '8', '9', '-', '_'
128-
};
129-
130123
public static String encode(byte[] in) {
131124
return encode(in, MAP);
132125
}
133126

134-
public static String encodeUrl(byte[] in) {
135-
return encode(in, URL_MAP);
136-
}
137-
138127
private static String encode(byte[] in, byte[] map) {
139128
int length = (in.length + 2) / 3 * 4;
140129
byte[] out = new byte[length];

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
package at.favre.lib.bytes;
2323

24+
import java.io.ByteArrayInputStream;
2425
import java.io.InputStream;
25-
import java.io.OutputStream;
2626
import java.math.BigInteger;
2727
import java.nio.ByteBuffer;
2828
import java.nio.ByteOrder;
@@ -589,16 +589,16 @@ public Bytes rightShift(int shiftCount) {
589589
}
590590

591591
/**
592-
* Creates a new instance with a copy of the internal byte array.
592+
* Creates a new instance with a copy of the internal byte array and all other attributes.
593593
*
594594
* @return copied instance
595595
*/
596596
public Bytes copy() {
597-
return wrap(Arrays.copyOf(internalArray(), length()));
597+
return new Bytes(Arrays.copyOf(internalArray(), length()), byteOrder, mutable, readonly);
598598
}
599599

600600
/**
601-
* Creates a new instance with a copy of the internal byte array.
601+
* Creates a new instance with a copy of the internal byte array and all other attributes.
602602
*
603603
* @param offset starting position in the source array
604604
* @param length of the new instance
@@ -607,7 +607,7 @@ public Bytes copy() {
607607
public Bytes copy(int offset, int length) {
608608
byte[] copy = new byte[length];
609609
System.arraycopy(internalArray(), offset, copy, 0, copy.length);
610-
return wrap(copy);
610+
return new Bytes(copy, byteOrder, mutable, readonly);
611611
}
612612

613613
/**
@@ -758,6 +758,15 @@ public ByteOrder byteOrder() {
758758
return byteOrder;
759759
}
760760

761+
/**
762+
* Check if this instance is read only
763+
*
764+
* @return true if read only
765+
*/
766+
public boolean isReadOnly() {
767+
return readonly;
768+
}
769+
761770
/**
762771
* Returns the index of the first appearance of the value {@code target} in
763772
* {@code array}.
@@ -939,12 +948,12 @@ public MutableBytes mutable() {
939948
}
940949

941950
/**
942-
* Creates an output stream with the same backing data as the intern array of this instance
951+
* Creates an input stream with the same backing data as the intern array of this instance
943952
*
944-
* @return new output stream
953+
* @return new input stream
945954
*/
946-
public OutputStream outputStream() {
947-
return Util.createOutputStream(array());
955+
public InputStream inputStream() {
956+
return new ByteArrayInputStream(internalArray());
948957
}
949958

950959
/**

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.io.ByteArrayOutputStream;
2525
import java.io.InputStream;
26-
import java.io.OutputStream;
2726
import java.util.*;
2827

2928
/**
@@ -183,7 +182,6 @@ static Byte[] toObjectArray(byte[] array) {
183182
*
184183
* @param array
185184
* @param random
186-
* @return shuffled array
187185
*/
188186
static void shuffle(byte[] array, Random random) {
189187
for (int i = array.length - 1; i > 0; i--) {
@@ -213,16 +211,6 @@ static byte[] readFromStream(InputStream inputStream) {
213211
}
214212
}
215213

216-
static OutputStream createOutputStream(byte[] array) {
217-
try {
218-
OutputStream outputStream = new ByteArrayOutputStream(array.length);
219-
outputStream.write(array);
220-
return outputStream;
221-
} catch (Exception e) {
222-
throw new IllegalStateException("could not write to output stream", e);
223-
}
224-
}
225-
226214
static byte[] concatVararg(byte firstByte, byte[] moreBytes) {
227215
if (moreBytes == null) {
228216
return new byte[]{firstByte};

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public void byteAt() throws Exception {
131131
for (int i = 0; i < example_bytes_twentyfour.length; i++) {
132132
assertEquals(example_bytes_twentyfour[i], Bytes.wrap(example_bytes_twentyfour).byteAt(i));
133133
}
134+
135+
try {
136+
assertEquals(0, Bytes.allocate(1).byteAt(1));
137+
fail();
138+
} catch (IndexOutOfBoundsException e) {
139+
}
134140
}
135141

136142
@Test
@@ -152,4 +158,11 @@ public void entropy() throws Exception {
152158
assertTrue(Bytes.from(example_bytes_seven).entropy() > 2.5);
153159
assertTrue(Bytes.from(example_bytes_two).entropy() > 0.5);
154160
}
161+
162+
@Test
163+
public void readOnly() throws Exception {
164+
assertFalse(Bytes.from(example_bytes_twentyfour).isReadOnly());
165+
assertTrue(Bytes.from(example_bytes_twentyfour).readOnly().isReadOnly());
166+
assertTrue(Bytes.from(example_bytes_twentyfour).readOnly().copy().isReadOnly());
167+
}
155168
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2017 Patrick Favre-Bulle
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package at.favre.lib.bytes;
23+
24+
import org.junit.Test;
25+
26+
import java.nio.ByteBuffer;
27+
28+
import static org.junit.Assert.*;
29+
30+
public class BytesSharedDataConverterTest extends ABytesTest {
31+
32+
@Test
33+
public void array() throws Exception {
34+
assertArrayEquals(new byte[0], Bytes.from(new byte[0]).array());
35+
assertArrayEquals(example_bytes_one, Bytes.from(example_bytes_one).array());
36+
assertArrayEquals(example_bytes_two, Bytes.from(example_bytes_two).array());
37+
assertArrayEquals(example_bytes_four, Bytes.from(example_bytes_four).array());
38+
assertArrayEquals(example_bytes_seven, Bytes.from(example_bytes_seven).array());
39+
assertArrayEquals(example_bytes_eight, Bytes.from(example_bytes_eight).array());
40+
assertArrayEquals(example_bytes_sixteen, Bytes.from(example_bytes_sixteen).array());
41+
}
42+
43+
@Test
44+
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());
51+
}
52+
53+
@Test
54+
public void buffer() throws Exception {
55+
assertEquals(ByteBuffer.wrap(new byte[0]), Bytes.from(new byte[0]).buffer());
56+
assertEquals(ByteBuffer.wrap(example_bytes_one), Bytes.from(example_bytes_one).buffer());
57+
assertEquals(ByteBuffer.wrap(example_bytes_two), Bytes.from(example_bytes_two).buffer());
58+
assertEquals(ByteBuffer.wrap(example_bytes_four), Bytes.from(example_bytes_four).buffer());
59+
assertEquals(ByteBuffer.wrap(example_bytes_seven), Bytes.from(example_bytes_seven).buffer());
60+
assertEquals(ByteBuffer.wrap(example_bytes_eight), Bytes.from(example_bytes_eight).buffer());
61+
assertEquals(ByteBuffer.wrap(example_bytes_sixteen), Bytes.from(example_bytes_sixteen).buffer());
62+
}
63+
64+
@Test
65+
public void duplicate() throws Exception {
66+
Bytes b = Bytes.from(example_bytes_sixteen);
67+
Bytes b2 = b.duplicate();
68+
assertNotSame(b, b2);
69+
assertSame(b.array(), b2.array());
70+
}
71+
72+
@Test
73+
public void inputStream() throws Exception {
74+
assertArrayEquals(example_bytes_one, Util.readFromStream(Bytes.from(example_bytes_one).inputStream()));
75+
assertArrayEquals(example_bytes_two, Util.readFromStream(Bytes.from(example_bytes_two).inputStream()));
76+
assertArrayEquals(example_bytes_four, Util.readFromStream(Bytes.from(example_bytes_four).inputStream()));
77+
assertArrayEquals(example_bytes_sixteen, Util.readFromStream(Bytes.from(example_bytes_sixteen).inputStream()));
78+
}
79+
}

0 commit comments

Comments
 (0)