1 32 94 95 package net.myvietnam.mvncore.misc; 96 97 107 public final class Base64 108 { 109 static private final int BASELENGTH = 255; 110 static private final int LOOKUPLENGTH = 64; 111 static private final int TWENTYFOURBITGROUP = 24; 112 static private final int EIGHTBIT = 8; 113 static private final int SIXTEENBIT = 16; 114 static private final int SIXBIT = 6; 115 static private final int FOURBYTE = 4; 116 static private final int SIGN = -128; 117 static private final byte PAD = (byte) '='; 118 static private byte [] base64Alphabet = new byte[BASELENGTH]; 119 static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH]; 120 122 static 123 { 124 for (int i = 0; i < BASELENGTH; i++ ) 125 { 126 base64Alphabet[i] = -1; 127 } 128 for (int i = 'Z'; i >= 'A'; i--) 129 { 130 base64Alphabet[i] = (byte) (i - 'A'); 131 } 132 for (int i = 'z'; i>= 'a'; i--) 133 { 134 base64Alphabet[i] = (byte) (i - 'a' + 26); 135 } 136 for (int i = '9'; i >= '0'; i--) 137 { 138 base64Alphabet[i] = (byte) (i - '0' + 52); 139 } 140 141 base64Alphabet['+'] = 62; 142 base64Alphabet['/'] = 63; 143 144 for (int i = 0; i <= 25; i++ ) 145 lookUpBase64Alphabet[i] = (byte) ('A' + i); 146 147 for (int i = 26, j = 0; i <= 51; i++, j++ ) 148 lookUpBase64Alphabet[i] = (byte) ('a'+ j); 149 150 for (int i = 52, j = 0; i <= 61; i++, j++ ) 151 lookUpBase64Alphabet[i] = (byte) ('0' + j); 152 153 lookUpBase64Alphabet[62] = (byte) '+'; 154 lookUpBase64Alphabet[63] = (byte) '/'; 155 } 156 157 public static boolean isBase64( String isValidString ) 158 { 159 return isArrayByteBase64(isValidString.getBytes()); 160 } 161 162 public static boolean isBase64( byte octect ) 163 { 164 return (octect == PAD || base64Alphabet[octect] != -1); 166 } 167 168 public static boolean isArrayByteBase64( byte[] arrayOctect ) 169 { 170 int length = arrayOctect.length; 171 if (length == 0) 172 { 173 return true; 176 } 177 for (int i=0; i < length; i++) 178 { 179 if ( !Base64.isBase64(arrayOctect[i]) ) 180 return false; 181 } 182 return true; 183 } 184 185 191 public static byte[] encode( byte[] binaryData ) 192 { 193 int lengthDataBits = binaryData.length*EIGHTBIT; 194 int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP; 195 int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP; 196 byte encodedData[] = null; 197 198 199 if (fewerThan24bits != 0) 200 { 201 encodedData = new byte[ (numberTriplets + 1 ) * 4 ]; 203 } 204 else 205 { 206 encodedData = new byte[ numberTriplets * 4 ]; 208 } 209 210 byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; 211 212 int encodedIndex = 0; 213 int dataIndex = 0; 214 int i = 0; 215 for ( i = 0; i<numberTriplets; i++ ) 217 { 218 dataIndex = i*3; 219 b1 = binaryData[dataIndex]; 220 b2 = binaryData[dataIndex + 1]; 221 b3 = binaryData[dataIndex + 2]; 222 223 225 l = (byte)(b2 & 0x0f); 226 k = (byte)(b1 & 0x03); 227 228 encodedIndex = i * 4; 229 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 230 byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 231 byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc); 232 233 encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ]; 234 encodedData[encodedIndex+1] = 238 lookUpBase64Alphabet[ val2 | ( k<<4 )]; 239 encodedData[encodedIndex+2] = 240 lookUpBase64Alphabet[ (l <<2 ) | val3 ]; 241 encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ]; 242 } 243 244 dataIndex = i*3; 246 encodedIndex = i*4; 247 if (fewerThan24bits == EIGHTBIT ) 248 { 249 b1 = binaryData[dataIndex]; 250 k = (byte) ( b1 &0x03 ); 251 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 254 encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ]; 255 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ]; 256 encodedData[encodedIndex + 2] = PAD; 257 encodedData[encodedIndex + 3] = PAD; 258 } 259 else if (fewerThan24bits == SIXTEENBIT) 260 { 261 262 b1 = binaryData[dataIndex]; 263 b2 = binaryData[dataIndex +1 ]; 264 l = (byte) (b2 & 0x0f); 265 k = (byte) (b1 & 0x03); 266 267 byte val1 = ((b1 & SIGN) == 0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 268 byte val2 = ((b2 & SIGN) == 0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 269 270 encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ]; 271 encodedData[encodedIndex + 1] = 272 lookUpBase64Alphabet[ val2 | ( k<<4 )]; 273 encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ]; 274 encodedData[encodedIndex + 3] = PAD; 275 } 276 277 return encodedData; 278 } 279 280 286 public static byte[] decode( byte[] base64Data ) 287 { 288 if(base64Data.length == 0) { return new byte[0]; } 290 291 int numberQuadruple = base64Data.length/FOURBYTE; 292 byte decodedData[] = null; 293 byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0; 294 295 297 int encodedIndex = 0; 298 int dataIndex = 0; 299 { 300 int lastData = base64Data.length; 302 while (base64Data[lastData-1] == PAD) 304 { 305 if (--lastData == 0) 306 { 307 return new byte[0]; 308 } 309 } 310 decodedData = new byte[ lastData - numberQuadruple ]; 311 } 312 313 for (int i = 0; i < numberQuadruple; i++) 314 { 315 dataIndex = i * 4; 316 marker0 = base64Data[dataIndex + 2]; 317 marker1 = base64Data[dataIndex + 3]; 318 319 b1 = base64Alphabet[base64Data[dataIndex]]; 320 b2 = base64Alphabet[base64Data[dataIndex +1]]; 321 322 if (marker0 != PAD && marker1 != PAD) 323 { 324 b3 = base64Alphabet[ marker0 ]; 326 b4 = base64Alphabet[ marker1 ]; 327 328 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ; 329 decodedData[encodedIndex + 1] = 330 (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 331 decodedData[encodedIndex + 2] = (byte)( b3<<6 | b4 ); 332 } 333 else if (marker0 == PAD) 334 { 335 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ; 337 } 338 else if (marker1 == PAD) 339 { 340 b3 = base64Alphabet[ marker0 ]; 342 343 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ); 344 decodedData[encodedIndex + 1] = 345 (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 346 } 347 encodedIndex += 3; 348 } 349 return decodedData; 350 } 351 352 353 } 354 | Popular Tags |