01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jmeter.protocol.http.util;
20:
21: /**
22: * This class provides an implementation of Base64 encoding without relying on
23: * the the sun.* packages.
24: *
25: * @version $Revision: 493789 $
26: */
27: public final class Base64Encoder {
28: private final static char[] pem_array = { 65, 66, 67, 68, 69, 70,
29: 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
30: 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104,
31: 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
32: 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54,
33: 55, 56, 57, 43, 47 };
34:
35: private final static char eq = 61;
36:
37: /**
38: * Private constructor to prevent instantiation.
39: */
40: private Base64Encoder() {
41: }
42:
43: public final static String encode(String s) {
44: return encode(s.getBytes());
45: }
46:
47: public final static String encode(byte[] bs) {
48: StringBuffer out = new StringBuffer("");
49: int bl = bs.length;
50: for (int i = 0; i < bl; i += 3) {
51: out.append(encodeAtom(bs, i, (bl - i)));
52: }
53: return out.toString();
54: }
55:
56: public final static String encodeAtom(byte[] b, int strt, int left) {
57: StringBuffer out = new StringBuffer("");
58: if (left == 1) {
59: byte b1 = b[strt];
60: int k = 0;
61: out.append(String.valueOf(pem_array[b1 >>> 2 & 63]));
62: out.append(String.valueOf(pem_array[(b1 << 4 & 48)
63: + (k >>> 4 & 15)]));
64: out.append(String.valueOf(eq));
65: out.append(String.valueOf(eq));
66: return out.toString();
67: }
68: if (left == 2) {
69: byte b2 = b[strt];
70: byte b4 = b[strt + 1];
71: int l = 0;
72: out.append(String.valueOf(pem_array[b2 >>> 2 & 63]));
73: out.append(String.valueOf(pem_array[(b2 << 4 & 48)
74: + (b4 >>> 4 & 15)]));
75: out.append(String.valueOf(pem_array[(b4 << 2 & 60)
76: + (l >>> 6 & 3)]));
77: out.append(String.valueOf(eq));
78: return out.toString();
79: }
80: byte b3 = b[strt];
81: byte b5 = b[strt + 1];
82: byte b6 = b[strt + 2];
83: out.append(String.valueOf(pem_array[b3 >>> 2 & 63]));
84: out.append(String.valueOf(pem_array[(b3 << 4 & 48)
85: + (b5 >>> 4 & 15)]));
86: out.append(String.valueOf(pem_array[(b5 << 2 & 60)
87: + (b6 >>> 6 & 3)]));
88: out.append(String.valueOf(pem_array[b6 & 63]));
89: return out.toString();
90: }
91: }
|