Skip to content

Commit 04e18dd

Browse files
committed
Change parse() methods to now expect more flexible CharSequence instead of String
1 parent 9639fc3 commit 04e18dd

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* add radix encoding/parsing and fix radix tests #6, #20
66
* add support for Base32 RFC4648 non-hex alphabet encoding/parsing #21
77
* add constructor for `IntBuffer` and `CharBuffer`
8+
* `parse()` methods now expect more flexible `CharSequence` instead of `String` #23
9+
10+
### Breaking
11+
12+
* interface `BinaryToTextEncoding.decode()` changed param to `CharSequence` from `String` #23
813

914
### Deprecations (will be removed in v1.0+)
1015

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private Base64() {
4747
'5', '6', '7', '8', '9', '-', '_'
4848
};
4949

50-
static byte[] decode(String in) {
50+
static byte[] decode(CharSequence in) {
5151
// Ignore trailing '=' padding and whitespace from the input.
5252
int limit = in.length();
5353
for (; limit > 0; limit--) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private String trimTrailingPadding(CharSequence chars) {
115115
}
116116

117117
@Override
118-
public byte[] decode(String encoded) {
118+
public byte[] decode(CharSequence encoded) {
119119
encoded = trimTrailingPadding(encoded);
120120
byte[] tmp = new byte[maxDecodedSize(encoded.length())];
121121
int len = decodeTo(tmp, encoded);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ interface Decoder {
5959
* @param encoded string
6060
* @return byte array represented by given encoded string
6161
*/
62-
byte[] decode(String encoded);
62+
byte[] decode(CharSequence encoded);
6363
}
6464

6565
/**
@@ -101,13 +101,13 @@ public String encode(byte[] byteArray, ByteOrder byteOrder) {
101101
}
102102

103103
@Override
104-
public byte[] decode(String hexString) {
104+
public byte[] decode(CharSequence hexString) {
105105
Objects.requireNonNull(hexString, "hex input must not be null");
106106
if (hexString.length() % 2 != 0)
107107
throw new IllegalArgumentException("invalid hex string, must be mod 2 == 0");
108108

109109
int start;
110-
if (hexString.startsWith("0x")) {
110+
if (hexString.toString().startsWith("0x")) {
111111
start = 2;
112112
} else {
113113
start = 0;
@@ -151,7 +151,7 @@ public String encode(byte[] array, ByteOrder byteOrder) {
151151
}
152152

153153
@Override
154-
public byte[] decode(String encoded) {
154+
public byte[] decode(CharSequence encoded) {
155155
return Base64.decode(encoded);
156156
}
157157
}
@@ -175,8 +175,8 @@ public String encode(byte[] array, ByteOrder byteOrder) {
175175
}
176176

177177
@Override
178-
public byte[] decode(String encoded) {
179-
byte[] array = new BigInteger(encoded, radix).toByteArray();
178+
public byte[] decode(CharSequence encoded) {
179+
byte[] array = new BigInteger(encoded.toString(), radix).toByteArray();
180180
if (array[0] == 0) {
181181
byte[] tmp = new byte[array.length - 1];
182182
System.arraycopy(array, 1, tmp, 0, tmp.length);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public static Bytes from(UUID uuid) {
543543
* @param binaryString the encoded string
544544
* @return decoded instance
545545
*/
546-
public static Bytes parseBinary(String binaryString) {
546+
public static Bytes parseBinary(CharSequence binaryString) {
547547
return parseRadix(binaryString, 2);
548548
}
549549

@@ -553,7 +553,7 @@ public static Bytes parseBinary(String binaryString) {
553553
* @param octalString the encoded string
554554
* @return decoded instance
555555
*/
556-
public static Bytes parseOctal(String octalString) {
556+
public static Bytes parseOctal(CharSequence octalString) {
557557
return parseRadix(octalString, 8);
558558
}
559559

@@ -563,7 +563,7 @@ public static Bytes parseOctal(String octalString) {
563563
* @param decString the encoded string
564564
* @return decoded instance
565565
*/
566-
public static Bytes parseDec(String decString) {
566+
public static Bytes parseDec(CharSequence decString) {
567567
return parseRadix(decString, 10);
568568
}
569569

@@ -578,7 +578,7 @@ public static Bytes parseDec(String decString) {
578578
* @param radix radix of the String representation (supported are 2-36)
579579
* @return decoded instance
580580
*/
581-
public static Bytes parseRadix(String radixNumberString, int radix) {
581+
public static Bytes parseRadix(CharSequence radixNumberString, int radix) {
582582
return parse(radixNumberString, new BinaryToTextEncoding.BaseRadixNumber(radix));
583583
}
584584

@@ -589,7 +589,7 @@ public static Bytes parseRadix(String radixNumberString, int radix) {
589589
* @param hexString the encoded string
590590
* @return decoded instance
591591
*/
592-
public static Bytes parseHex(String hexString) {
592+
public static Bytes parseHex(CharSequence hexString) {
593593
return parse(hexString, new BinaryToTextEncoding.Hex());
594594
}
595595

@@ -601,7 +601,7 @@ public static Bytes parseHex(String hexString) {
601601
* @param base32Rfc4648String the encoded string
602602
* @return decoded instance
603603
*/
604-
public static Bytes parseBase32(String base32Rfc4648String) {
604+
public static Bytes parseBase32(CharSequence base32Rfc4648String) {
605605
return parse(base32Rfc4648String, new BaseEncoding(BaseEncoding.BASE32_RFC4848, BaseEncoding.BASE32_RFC4848_PADDING));
606606
}
607607

@@ -613,10 +613,10 @@ public static Bytes parseBase32(String base32Rfc4648String) {
613613
*
614614
* @param base36String the encoded string
615615
* @return decoded instance
616-
* @deprecated use {@link #parseRadix(String, int)} with 36 instead; will be removed in v1.0+
616+
* @deprecated use {@link #parseRadix(CharSequence, int)} with 36 instead; will be removed in v1.0+
617617
*/
618618
@Deprecated
619-
public static Bytes parseBase36(String base36String) {
619+
public static Bytes parseBase36(CharSequence base36String) {
620620
return parse(base36String, new BinaryToTextEncoding.BaseRadixNumber(36));
621621
}
622622

@@ -626,7 +626,7 @@ public static Bytes parseBase36(String base36String) {
626626
* @param base64String the encoded string
627627
* @return decoded instance
628628
*/
629-
public static Bytes parseBase64(String base64String) {
629+
public static Bytes parseBase64(CharSequence base64String) {
630630
return parse(base64String, new BinaryToTextEncoding.Base64Encoding());
631631
}
632632

@@ -637,7 +637,7 @@ public static Bytes parseBase64(String base64String) {
637637
* @param decoder the decoder used to decode the string
638638
* @return decoded instance
639639
*/
640-
public static Bytes parse(String encoded, BinaryToTextEncoding.Decoder decoder) {
640+
public static Bytes parse(CharSequence encoded, BinaryToTextEncoding.Decoder decoder) {
641641
return wrap(Objects.requireNonNull(decoder, "passed decoder instance must no be null").decode(Objects.requireNonNull(encoded, "encoded data must not be null")));
642642
}
643643

0 commit comments

Comments
 (0)