Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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: 674365 $
   26    */
   27   public final class Base64Encoder {
   28       private final static char[] pem_array = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
   29               83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
   30               112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 };
   31   
   32       private final static char eq = 61;
   33   
   34       /**
   35        * Private constructor to prevent instantiation.
   36        */
   37       private Base64Encoder() {
   38       }
   39   
   40       public final static String encode(String s) {
   41           return encode(s.getBytes());
   42       }
   43   
   44       public final static String encode(byte[] bs) {
   45           StringBuffer out = new StringBuffer("");
   46           int bl = bs.length;
   47           for (int i = 0; i < bl; i += 3) {
   48               out.append(encodeAtom(bs, i, (bl - i)));
   49           }
   50           return out.toString();
   51       }
   52   
   53       public final static String encodeAtom(byte[] b, int strt, int left) {
   54           StringBuffer out = new StringBuffer("");
   55           if (left == 1) {
   56               byte b1 = b[strt];
   57               int k = 0;
   58               out.append(String.valueOf(pem_array[b1 >>> 2 & 63]));
   59               out.append(String.valueOf(pem_array[(b1 << 4 & 48) + (k >>> 4 & 15)]));
   60               out.append(String.valueOf(eq));
   61               out.append(String.valueOf(eq));
   62               return out.toString();
   63           }
   64           if (left == 2) {
   65               byte b2 = b[strt];
   66               byte b4 = b[strt + 1];
   67               int l = 0;
   68               out.append(String.valueOf(pem_array[b2 >>> 2 & 63]));
   69               out.append(String.valueOf(pem_array[(b2 << 4 & 48) + (b4 >>> 4 & 15)]));
   70               out.append(String.valueOf(pem_array[(b4 << 2 & 60) + (l >>> 6 & 3)]));
   71               out.append(String.valueOf(eq));
   72               return out.toString();
   73           }
   74           byte b3 = b[strt];
   75           byte b5 = b[strt + 1];
   76           byte b6 = b[strt + 2];
   77           out.append(String.valueOf(pem_array[b3 >>> 2 & 63]));
   78           out.append(String.valueOf(pem_array[(b3 << 4 & 48) + (b5 >>> 4 & 15)]));
   79           out.append(String.valueOf(pem_array[(b5 << 2 & 60) + (b6 >>> 6 & 3)]));
   80           out.append(String.valueOf(pem_array[b6 & 63]));
   81           return out.toString();
   82       }
   83   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]