Skip to content

Commit f58b7ac

Browse files
Swaps isBase64 with internal implementation for Android compatibility (#1079)
* Adding animal sniffer to display all issues related to android compatibility. * Duplicate isBase64 internally Co-authored-by: Vitor Pamplona <vitor@vitorpamplona.com> Co-authored-by: dotasek <david.otasek@smilecdr.com>
1 parent 2c60f5b commit f58b7ac

File tree

8 files changed

+73
-6
lines changed

8 files changed

+73
-6
lines changed

org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/model/Base64BinaryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public String fhirType() {
9999
* @throws DataFormatException
100100
*/
101101
public void checkValidBase64(String toCheck) throws DataFormatException {
102-
if (!Base64.isBase64(toCheck.getBytes())) {
102+
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
103103
throw new DataFormatException("");
104104
}
105105
}

org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/Base64BinaryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public String fhirType() {
100100
* @throws DataFormatException
101101
*/
102102
public void checkValidBase64(String toCheck) throws DataFormatException {
103-
if (!Base64.isBase64(toCheck.getBytes())) {
103+
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
104104
throw new DataFormatException("");
105105
}
106106
}

org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/Base64BinaryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public boolean isEmpty() {
152152
* @throws DataFormatException
153153
*/
154154
public void checkValidBase64(String toCheck) throws DataFormatException {
155-
if (!Base64.isBase64(toCheck.getBytes())) {
155+
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
156156
throw new DataFormatException("");
157157
}
158158
}

org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/Base64BinaryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public boolean isEmpty() {
152152
* @throws DataFormatException
153153
*/
154154
public void checkValidBase64(String toCheck) throws DataFormatException {
155-
if (!Base64.isBase64(toCheck.getBytes())) {
155+
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
156156
throw new DataFormatException("");
157157
}
158158
}

org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/model/Base64BinaryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public String primitiveValue() {
156156
* @throws DataFormatException
157157
*/
158158
public void checkValidBase64(String toCheck) throws DataFormatException {
159-
if (!Base64.isBase64(toCheck.getBytes())) {
159+
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
160160
throw new DataFormatException("");
161161
}
162162
}

org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Base64BinaryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public String primitiveValue() {
156156
* @throws DataFormatException
157157
*/
158158
public void checkValidBase64(String toCheck) throws DataFormatException {
159-
if (!Base64.isBase64(toCheck.getBytes())) {
159+
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
160160
throw new DataFormatException("");
161161
}
162162
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.hl7.fhir.utilities;
2+
3+
/**
4+
* This is a partial duplication of org.apache.commons.codec.binary.Base64
5+
*
6+
* It exists because Android compatibility only supports version 1.2 of that
7+
* library, which only has the deprecated isArrayByteBase64. The use of
8+
* isBase64 from this class will allow us to avoid using a deprecated method
9+
* or hacking a solution that involves catching exceptions on decoding.
10+
*/
11+
public class Base64 {
12+
13+
private static final byte[] DECODE_TABLE = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
14+
15+
public static boolean isBase64(byte octet) {
16+
return octet == 61 || octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1;
17+
}
18+
19+
public static boolean isBase64(byte[] arrayOctet) {
20+
for(int i = 0; i < arrayOctet.length; ++i) {
21+
if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
}
28+
29+
protected static boolean isWhiteSpace(byte byteToCheck) {
30+
switch (byteToCheck) {
31+
case 9:
32+
case 10:
33+
case 13:
34+
case 32:
35+
return true;
36+
default:
37+
return false;
38+
}
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
import org.hl7.fhir.utilities.Base64;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class Base64Tests {
9+
@Test
10+
public void testIsArrayByteBase64() {
11+
assertFalse(Base64.isBase64(new byte[] { Byte.MIN_VALUE }));
12+
assertFalse(Base64.isBase64(new byte[] { -125 }));
13+
assertFalse(Base64.isBase64(new byte[] { -10 }));
14+
assertFalse(Base64.isBase64(new byte[] { 0 }));
15+
assertFalse(Base64.isBase64(new byte[] { 64, Byte.MAX_VALUE }));
16+
assertFalse(Base64.isBase64(new byte[] { Byte.MAX_VALUE }));
17+
18+
assertTrue(Base64.isBase64(new byte[] { 'A' }));
19+
20+
assertFalse(Base64.isBase64(new byte[] { 'A', Byte.MIN_VALUE }));
21+
22+
assertTrue(Base64.isBase64(new byte[] { 'A', 'Z', 'a' }));
23+
assertTrue(Base64.isBase64(new byte[] { '/', '=', '+' }));
24+
25+
assertFalse(Base64.isBase64(new byte[] { '$' }));
26+
}
27+
}

0 commit comments

Comments
 (0)