-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: dotasek <[email protected]>
- Loading branch information
1 parent
2c60f5b
commit f58b7ac
Showing
8 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Base64.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.hl7.fhir.utilities; | ||
|
||
/** | ||
* This is a partial duplication of org.apache.commons.codec.binary.Base64 | ||
* | ||
* It exists because Android compatibility only supports version 1.2 of that | ||
* library, which only has the deprecated isArrayByteBase64. The use of | ||
* isBase64 from this class will allow us to avoid using a deprecated method | ||
* or hacking a solution that involves catching exceptions on decoding. | ||
*/ | ||
public class Base64 { | ||
|
||
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}; | ||
|
||
public static boolean isBase64(byte octet) { | ||
return octet == 61 || octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1; | ||
} | ||
|
||
public static boolean isBase64(byte[] arrayOctet) { | ||
for(int i = 0; i < arrayOctet.length; ++i) { | ||
if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected static boolean isWhiteSpace(byte byteToCheck) { | ||
switch (byteToCheck) { | ||
case 9: | ||
case 10: | ||
case 13: | ||
case 32: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.hl7.fhir.utilities.Base64; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class Base64Tests { | ||
@Test | ||
public void testIsArrayByteBase64() { | ||
assertFalse(Base64.isBase64(new byte[] { Byte.MIN_VALUE })); | ||
assertFalse(Base64.isBase64(new byte[] { -125 })); | ||
assertFalse(Base64.isBase64(new byte[] { -10 })); | ||
assertFalse(Base64.isBase64(new byte[] { 0 })); | ||
assertFalse(Base64.isBase64(new byte[] { 64, Byte.MAX_VALUE })); | ||
assertFalse(Base64.isBase64(new byte[] { Byte.MAX_VALUE })); | ||
|
||
assertTrue(Base64.isBase64(new byte[] { 'A' })); | ||
|
||
assertFalse(Base64.isBase64(new byte[] { 'A', Byte.MIN_VALUE })); | ||
|
||
assertTrue(Base64.isBase64(new byte[] { 'A', 'Z', 'a' })); | ||
assertTrue(Base64.isBase64(new byte[] { '/', '=', '+' })); | ||
|
||
assertFalse(Base64.isBase64(new byte[] { '$' })); | ||
} | ||
} |