Skip to content

Commit b932b28

Browse files
committed
Add appendNullSafe and append string with encoding
1 parent 807db89 commit b932b28

File tree

9 files changed

+129
-87
lines changed

9 files changed

+129
-87
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* better resource handling for compression
55
* add nullSafe from() constructor
66
* rename `toObjectArray()` to `toBoxedArray()` (will be removed in 1.0)
7+
* add appendNullSafe and append string with encoding
78

89
## v0.4.6
910

checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</module>
3333

3434
<module name="FileLength"><!-- max line length for single file: http://checkstyle.sourceforge.net/config_sizes.html#FileLength -->
35-
<property name="max" value="1500"/>
35+
<property name="max" value="3500"/>
3636
</module>
3737

3838
<module name="TreeWalker">

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,15 +652,39 @@ public Bytes append(byte[] secondArray) {
652652
return transform(new BytesTransformer.ConcatTransformer(secondArray));
653653
}
654654

655+
/**
656+
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
657+
* <p>
658+
* If given array is null, the nothing will be appended.
659+
*
660+
* @param secondArrayNullable to append, may be null
661+
* @return appended instance or same if passed array is null
662+
*/
663+
public Bytes appendNullSafe(byte[] secondArrayNullable) {
664+
return secondArrayNullable == null ? this : append(secondArrayNullable);
665+
}
666+
655667
/**
656668
* Creates a new instance with the current array appended to the provided utf-8 encoded representation of this string
657669
*
658-
* @param string string used to get utf-8 bytes from
670+
* @param stringUtf8 string used to get utf-8 bytes from
671+
* @return appended instance
672+
*/
673+
public Bytes append(CharSequence stringUtf8) {
674+
return append(stringUtf8, StandardCharsets.UTF_8);
675+
}
676+
677+
/**
678+
* Creates a new instance with the current array appended to the provided string with provided encoding
679+
*
680+
* @param string string used to get bytes from
681+
* @param charset encoding of provided string
659682
* @return appended instance
660683
*/
661-
public Bytes append(CharSequence string) {
684+
public Bytes append(CharSequence string, Charset charset) {
685+
Objects.requireNonNull(charset);
662686
Objects.requireNonNull(string);
663-
return transform(new BytesTransformer.ConcatTransformer(string.toString().getBytes(StandardCharsets.UTF_8)));
687+
return transform(new BytesTransformer.ConcatTransformer(string.toString().getBytes(charset)));
664688
}
665689

666690
/**
@@ -1393,6 +1417,7 @@ public List<Byte> toList() {
13931417

13941418
/**
13951419
* @deprecated renamed API, use {@link #toBoxedArray()} instead - will be removed in v1.0+
1420+
* @return see {@link #toBoxedArray()}
13961421
*/
13971422
@Deprecated
13981423
public Byte[] toObjectArray() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public abstract class ABytesTest {
5353

5454
byte[] example_bytes_twentyfour;
5555
String example_hex_twentyfour;
56+
5657
@Before
5758
public void setUp() throws Exception {
5859
example_bytes_empty = new byte[0];

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class Base64Test {
3232

3333
@Test
34-
public void decode() throws Exception {
34+
public void decode() {
3535
assertArrayEquals("".getBytes(), Base64.decode(""));
3636
assertArrayEquals("f".getBytes(), Base64.decode("Zg=="));
3737
assertArrayEquals("fo".getBytes(), Base64.decode("Zm8="));
@@ -47,7 +47,7 @@ public void decode() throws Exception {
4747
}
4848

4949
@Test
50-
public void encode() throws Exception {
50+
public void encode() {
5151
assertEquals("", Base64.encode("".getBytes()));
5252
assertEquals("Zg==", Base64.encode("f".getBytes()));
5353
assertEquals("Zm8=", Base64.encode("fo".getBytes()));
@@ -60,4 +60,4 @@ public void encode() throws Exception {
6060

6161
}
6262

63-
}
63+
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@
3030

3131
public class BinaryToTextEncodingTest {
3232
@Test
33-
public void encodeHex() throws Exception {
33+
public void encodeHex() {
3434
assertEquals("010203", new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
3535
assertEquals("030201", new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN));
3636
assertNotEquals(new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
3737
}
3838

3939
@Test(expected = IllegalArgumentException.class)
40-
public void decodeHexShouldFail() throws Exception {
40+
public void decodeHexShouldFail() {
4141
new BinaryToTextEncoding.Hex(false).decode("AAI=");
4242
}
4343

4444
@Test
45-
public void encodeBaseRadix() throws Exception {
45+
public void encodeBaseRadix() {
4646
assertEquals("100211", new BinaryToTextEncoding.BaseRadix(16).encode(new byte[]{16, 2, 17}, ByteOrder.BIG_ENDIAN));
4747
assertEquals("110210", new BinaryToTextEncoding.BaseRadix(16).encode(new byte[]{16, 2, 17}, ByteOrder.LITTLE_ENDIAN));
4848
assertNotEquals(new BinaryToTextEncoding.BaseRadix(2).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.BaseRadix(2).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
4949
}
5050

5151
@Test
52-
public void encodeDecodeRadix() throws Exception {
52+
public void encodeDecodeRadix() {
5353
for (int i = 0; i < 32; i++) {
5454
Bytes rnd = Bytes.random(i);
5555
System.out.println("\n\nNEW TEST: " + i + " bytes\n");
@@ -67,7 +67,7 @@ public void encodeDecodeRadix() throws Exception {
6767

6868
@Test
6969
@Ignore("should fix")
70-
public void encodeDecodeRadixZeros() throws Exception {
70+
public void encodeDecodeRadixZeros() {
7171
Bytes bytes = Bytes.wrap(new byte[]{0, 0, 0, 0});
7272
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.BaseRadix(36);
7373
String encodedBigEndian = encoding.encode(bytes.array(), ByteOrder.BIG_ENDIAN);
@@ -80,7 +80,7 @@ public void encodeDecodeRadixZeros() throws Exception {
8080
}
8181

8282
@Test
83-
public void encodeDecodeBase64() throws Exception {
83+
public void encodeDecodeBase64() {
8484
for (int i = 4; i < 32; i += 4) {
8585
Bytes rnd = Bytes.random(i);
8686
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.Base64Encoding();
@@ -91,7 +91,7 @@ public void encodeDecodeBase64() throws Exception {
9191
}
9292

9393
@Test
94-
public void encodeDecodeHex() throws Exception {
94+
public void encodeDecodeHex() {
9595
for (int i = 4; i < 32; i += 4) {
9696
Bytes rnd = Bytes.random(i);
9797
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.Hex();
@@ -102,30 +102,30 @@ public void encodeDecodeHex() throws Exception {
102102
}
103103

104104
@Test(expected = IllegalArgumentException.class)
105-
public void decodeInvalidRadix16() throws Exception {
105+
public void decodeInvalidRadix16() {
106106
new BinaryToTextEncoding.BaseRadix(16).decode("AAI=");
107107
}
108108

109109
@Test(expected = IllegalArgumentException.class)
110-
public void decodeInvalidRadix36() throws Exception {
110+
public void decodeInvalidRadix36() {
111111
new BinaryToTextEncoding.BaseRadix(36).decode("AAI=");
112112
}
113113

114114
@Test
115-
public void encodeBase64() throws Exception {
115+
public void encodeBase64() {
116116
assertEquals("EAIR", new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{16, 2, 17}, ByteOrder.BIG_ENDIAN));
117117
assertEquals("EQIQ", new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{17, 2, 16}, ByteOrder.BIG_ENDIAN));
118118
assertEquals("EQIQ", new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{16, 2, 17}, ByteOrder.LITTLE_ENDIAN));
119119
assertNotEquals(new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
120120
}
121121

122122
@Test(expected = IllegalArgumentException.class)
123-
public void decodeInvalidBase64() throws Exception {
123+
public void decodeInvalidBase64() {
124124
new BinaryToTextEncoding.Base64Encoding().decode("(&´´");
125125
}
126126

127127
@Test(expected = IllegalArgumentException.class)
128-
public void decodeHalfInvalidBase64() throws Exception {
128+
public void decodeHalfInvalidBase64() {
129129
new BinaryToTextEncoding.Base64Encoding().decode("EAI`");
130130
}
131-
}
131+
}

0 commit comments

Comments
 (0)