Skip to content

Commit 0b4f0c4

Browse files
authored
Merge pull request #35 from hydrogen2/master
Add a new API fromByteArray() to avoid the base64 decoding if the cli…
2 parents f5b2dfb + 8906e63 commit 0b4f0c4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/java/com/iab/gdpr/consent/VendorConsentDecoder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ public static VendorConsent fromBase64String(String consentString) {
2020
if (isNullOrEmpty(consentString))
2121
throw new IllegalArgumentException("Null or empty consent string passed as an argument");
2222

23-
final Bits bits = new Bits(BASE64_DECODER.decode(consentString));
23+
return fromByteArray(BASE64_DECODER.decode(consentString));
24+
}
25+
26+
public static VendorConsent fromByteArray(byte[] bytes) {
27+
if (bytes == null || bytes.length == 0)
28+
throw new IllegalArgumentException("Null or empty consent bytes passed as an argument");
29+
30+
final Bits bits = new Bits(bytes);
2431
final int version = getVersion(bits);
2532
switch (version) {
2633
case 1:

src/test/java/com/iab/gdpr/consent/VendorConsentDecoderTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public void testNullConsentString() {
2424
// Then IllegalArgumentException exception is thrown
2525
}
2626

27+
@Test(expected = IllegalArgumentException.class)
28+
public void testNullConsentBytes() {
29+
// Given: null consent string
30+
byte[] consentBytes = null;
31+
32+
// When: decoder is called
33+
final VendorConsent vendorConsent = VendorConsentDecoder.fromByteArray(consentBytes);
34+
35+
// Then IllegalArgumentException exception is thrown
36+
}
37+
2738
@Test(expected = IllegalArgumentException.class)
2839
public void testEmptyConsentString() {
2940
// Given: empty consent string
@@ -36,6 +47,18 @@ public void testEmptyConsentString() {
3647

3748
}
3849

50+
@Test(expected = IllegalArgumentException.class)
51+
public void testEmptyConsentBytes() {
52+
// Given: empty consent string
53+
byte[] consentBytes = new byte[0];
54+
55+
// When: decoder is called
56+
final VendorConsent vendorConsent = VendorConsentDecoder.fromByteArray(consentBytes);
57+
58+
// Then IllegalArgumentException exception is thrown
59+
60+
}
61+
3962
@Test(expected = IllegalStateException.class)
4063
public void testUnknownVersion() {
4164
// Given: unknown version number in consent string

0 commit comments

Comments
 (0)