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 (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) {
022        return false;
023      }
024    }
025
026    return true;
027  }
028
029  protected static boolean isWhiteSpace(byte byteToCheck) {
030    switch (byteToCheck) {
031      case 9:
032      case 10:
033      case 13:
034      case 32:
035        return true;
036      default:
037        return false;
038    }
039  }
040}