1 18 19 package org.apache.jmeter.protocol.http.util; 20 21 27 public final class Base64Encoder 28 { 29 private final static char[] pem_array = { 30 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 31 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 32 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 33 101, 102, 103, 104, 105, 106, 107, 108, 34 109, 110, 111, 112, 113, 114, 115, 116, 35 117, 118, 119, 120, 121, 122, 48, 49, 50, 36 51, 52, 53, 54, 55, 56, 57, 43, 47}; 37 private final static char eq = 61; 38 39 42 private Base64Encoder() 43 { 44 } 45 46 public final static String encode(String s) 47 { 48 return encode(s.getBytes()); 49 } 50 51 public final static String encode(byte[] bs) 52 { 53 StringBuffer out = new StringBuffer (""); 54 int bl = bs.length; 55 for (int i = 0; i < bl; i += 3) 56 { 57 out.append(encodeAtom(bs, i, (bl - i))); 58 } 59 return out.toString(); 60 } 61 62 public final static String encodeAtom(byte[] b, int strt, int left) 63 { 64 StringBuffer out = new StringBuffer (""); 65 if (left == 1) 66 { 67 byte b1 = b[strt]; 68 int k = 0; 69 out.append(String.valueOf(pem_array[b1 >>> 2 & 63])); 70 out.append( 71 String.valueOf(pem_array[(b1 << 4 & 48) + (k >>> 4 & 15)])); 72 out.append(String.valueOf(eq)); 73 out.append(String.valueOf(eq)); 74 return out.toString(); 75 } 76 if (left == 2) 77 { 78 byte b2 = b[strt]; 79 byte b4 = b[strt + 1]; 80 int l = 0; 81 out.append(String.valueOf(pem_array[b2 >>> 2 & 63])); 82 out.append( 83 String.valueOf(pem_array[(b2 << 4 & 48) + (b4 >>> 4 & 15)])); 84 out.append( 85 String.valueOf(pem_array[(b4 << 2 & 60) + (l >>> 6 & 3)])); 86 out.append(String.valueOf(eq)); 87 return out.toString(); 88 } 89 byte b3 = b[strt]; 90 byte b5 = b[strt + 1]; 91 byte b6 = b[strt + 2]; 92 out.append(String.valueOf(pem_array[b3 >>> 2 & 63])); 93 out.append(String.valueOf(pem_array[(b3 << 4 & 48) + (b5 >>> 4 & 15)])); 94 out.append(String.valueOf(pem_array[(b5 << 2 & 60) + (b6 >>> 6 & 3)])); 95 out.append(String.valueOf(pem_array[b6 & 63])); 96 return out.toString(); 97 } 98 } 99 100 101 | Popular Tags |