1 33 34 package com.icesoft.util.encoding; 35 36 49 public class Base64 { 50 private static final byte[] BASE64_ALPHABET_ARRAY = { 51 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 52 'N', 53 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 54 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 55 'n', 56 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 57 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 58 '+', '/' 59 }; 60 private static final byte[] BASE64_FOR_URL_ALPHABET_ARRAY = { 61 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 62 'N', 63 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 64 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 65 'n', 66 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 67 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 68 '-', '_' 69 }; 70 private static final byte PAD = '='; 71 72 79 public static byte[] encode(byte[] bytes) { 80 return encode(bytes, BASE64_ALPHABET_ARRAY, true); 81 } 82 83 90 public static String encode(String string) { 91 if (string == null) { 92 return null; 93 } 94 return new String (encode(string.getBytes())); 95 } 96 97 104 public static byte[] encodeForURL(byte[] bytes) { 105 return encode(bytes, BASE64_FOR_URL_ALPHABET_ARRAY, false); 106 } 107 108 private static byte[] encode( 109 byte[] bytes, byte[] alphabetArray, boolean usePadding) { 110 111 if (bytes == null) { 112 return null; 113 } else if (bytes.length == 0) { 114 return bytes; 115 } 116 int _length = bytes.length; 117 int _remainder = _length % 3; 118 byte[] _bytes; 119 if (usePadding) { 120 _bytes = new byte[(_length + 2) / 3 * 4]; 121 } else { 122 _bytes = 123 new byte[ 124 ((_length + 2) / 3 * 4) - 125 (_remainder != 0 ? 3 - _remainder : 0)]; 126 } 127 _length -= _remainder; 128 int _group; 129 int _i; 130 int _index = 0; 131 for (_i = 0; _i < _length;) { 132 _group = 133 (bytes[_i++] & 0xFF) << 16 | 134 (bytes[_i++] & 0xFF) << 8 | 135 bytes[_i++] & 0xFF; 136 _bytes[_index++] = alphabetArray[_group >>> 18]; 137 _bytes[_index++] = alphabetArray[_group >>> 12 & 0x3F]; 138 _bytes[_index++] = alphabetArray[_group >>> 6 & 0x3F]; 139 _bytes[_index++] = alphabetArray[_group & 0x3F]; 140 } 141 switch (_remainder) { 142 case 0: 143 break; 144 case 1: 145 _group = (bytes[_i] & 0xFF) << 4; 146 _bytes[_index++] = alphabetArray[_group >>> 6]; 147 if (usePadding) { 148 _bytes[_index++] = alphabetArray[_group & 0x3F]; 149 _bytes[_index++] = PAD; 150 _bytes[_index] = PAD; 151 } else { 152 _bytes[_index] = alphabetArray[_group & 0x3F]; 153 } 154 break; 155 case 2: 156 _group = ((bytes[_i++] & 0xFF) << 8 | (bytes[_i] & 0xFF)) << 2; 157 _bytes[_index++] = alphabetArray[_group >>> 12]; 158 _bytes[_index++] = alphabetArray[_group >>> 6 & 0x3F]; 159 if (usePadding) { 160 _bytes[_index++] = alphabetArray[_group & 0x3F]; 161 _bytes[_index] = PAD; 162 } else { 163 _bytes[_index] = alphabetArray[_group & 0x3F]; 164 } 165 break; 166 default: 167 } 169 return _bytes; 170 } 171 } 172 | Popular Tags |