KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > util > Base64Encoder


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/util/Base64Encoder.java,v 1.4 2004/02/12 00:29:50 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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: 1.4 $
26  */

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     /**
40      * Private constructor to prevent instantiation.
41      */

42     private Base64Encoder()
43     {
44     }
45
46     public final static String JavaDoc encode(String JavaDoc s)
47     {
48         return encode(s.getBytes());
49     }
50
51     public final static String JavaDoc encode(byte[] bs)
52     {
53         StringBuffer JavaDoc out = new StringBuffer JavaDoc("");
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 JavaDoc encodeAtom(byte[] b, int strt, int left)
63     {
64         StringBuffer JavaDoc out = new StringBuffer JavaDoc("");
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