1 19 20 package org.netbeans.modules.proxy; 21 22 27 class Base64Encoder { 28 29 private static final char [] characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); 30 31 private Base64Encoder() { 32 } 33 34 public static String encode(byte [] data) { 35 int length = data.length; 36 StringBuffer sb = new StringBuffer (data.length * 3 / 2); 37 38 int end = length - 3; 39 int i = 0; 40 41 while (i <= end) { 42 int d = ((((int) data[i]) & 0xFF) << 16) | ((((int) data[i + 1]) & 0xFF) << 8) | (((int) data[i + 2]) & 0xFF); 43 sb.append(characters[(d >> 18) & 0x3F]); 44 sb.append(characters[(d >> 12) & 0x3F]); 45 sb.append(characters[(d >> 6) & 0x3F]); 46 sb.append(characters[d & 0x3F]); 47 i += 3; 48 } 49 50 if (i == length - 2) { 51 int d = ((((int) data[i]) & 0xFF) << 16) | ((((int) data[i + 1]) & 0xFF) << 8); 52 sb.append(characters[(d >> 18) & 0x3F]); 53 sb.append(characters[(d >> 12) & 0x3F]); 54 sb.append(characters[(d >> 6) & 0x3F]); 55 sb.append("="); 56 } else if (i == length - 1) { 57 int d = (((int) data[i]) & 0xFF) << 16; 58 sb.append(characters[(d >> 18) & 0x3F]); 59 sb.append(characters[(d >> 12) & 0x3F]); 60 sb.append("=="); 61 } 62 return sb.toString(); 63 } 64 } 65 | Popular Tags |