001package org.hl7.fhir.utilities; 002 003/** 004 * This is a partial duplication of org.apache.commons.codec.binary.Base64 005 * 006 * It exists because Android compatibility only supports version 1.2 of that 007 * library, which only has the deprecated isArrayByteBase64. The use of 008 * isBase64 from this class will allow us to avoid using a deprecated method 009 * or hacking a solution that involves catching exceptions on decoding. 010 */ 011public class Base64 { 012 013 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}; 014 015 public static boolean isBase64(byte octet) { 016 return octet == 61 || octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1; 017 } 018 019 public static boolean isBase64(byte[] arrayOctet) { 020 for(int i = 0; i < arrayOctet.length; ++i) { 021 if (!isBase64Char(arrayOctet[i])) { 022 return false; 023 } 024 } 025 026 return true; 027 } 028 029 private static boolean isBase64Char(byte b) { 030 return isBase64(b) || isWhiteSpace(b); 031 } 032 033 public static boolean isBase64(String content) { 034 for (char ch : content.toCharArray()) { 035 if (!isBase64Char((byte) ch)) { 036 return false; 037 } 038 } 039 040 return true; 041 } 042 043 protected static boolean isWhiteSpace(byte byteToCheck) { 044 switch (byteToCheck) { 045 case 9: 046 case 10: 047 case 13: 048 case 32: 049 return true; 050 default: 051 return false; 052 } 053 } 054}