KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > util > Base64


1 /* Encodes and decodes to and from Base64 notation.
2  * Copyright (C) 2003 "Eric Glass" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.util;
20
21 public class Base64 {
22
23     private static final String JavaDoc ALPHABET =
24             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
25
26     /**
27      * Base-64 encodes the supplied block of data. Line wrapping is not
28      * applied on output.
29      *
30      * @param bytes The block of data that is to be Base-64 encoded.
31      * @return A <code>String</code> containing the encoded data.
32      */

33     public static String JavaDoc encode(byte[] bytes) {
34         int length = bytes.length;
35         if (length == 0) return "";
36         StringBuffer JavaDoc buffer =
37                 new StringBuffer JavaDoc((int) Math.ceil((double) length / 3d) * 4);
38         int remainder = length % 3;
39         length -= remainder;
40         int block;
41         int i = 0;
42         while (i < length) {
43             block = ((bytes[i++] & 0xff) << 16) | ((bytes[i++] & 0xff) << 8) |
44                     (bytes[i++] & 0xff);
45             buffer.append(ALPHABET.charAt(block >>> 18));
46             buffer.append(ALPHABET.charAt((block >>> 12) & 0x3f));
47             buffer.append(ALPHABET.charAt((block >>> 6) & 0x3f));
48             buffer.append(ALPHABET.charAt(block & 0x3f));
49         }
50         if (remainder == 0) return buffer.toString();
51         if (remainder == 1) {
52             block = (bytes[i] & 0xff) << 4;
53             buffer.append(ALPHABET.charAt(block >>> 6));
54             buffer.append(ALPHABET.charAt(block & 0x3f));
55             buffer.append("==");
56             return buffer.toString();
57         }
58         block = (((bytes[i++] & 0xff) << 8) | ((bytes[i]) & 0xff)) << 2;
59         buffer.append(ALPHABET.charAt(block >>> 12));
60         buffer.append(ALPHABET.charAt((block >>> 6) & 0x3f));
61         buffer.append(ALPHABET.charAt(block & 0x3f));
62         buffer.append("=");
63         return buffer.toString();
64     }
65
66     /**
67      * Decodes the supplied Base-64 encoded string.
68      *
69      * @param string The Base-64 encoded string that is to be decoded.
70      * @return A <code>byte[]</code> containing the decoded data block.
71      */

72     public static byte[] decode(String JavaDoc string) {
73         int length = string.length();
74         if (length == 0) return new byte[0];
75         int pad = (string.charAt(length - 2) == '=') ? 2 :
76                 (string.charAt(length - 1) == '=') ? 1 : 0;
77         int size = length * 3 / 4 - pad;
78         byte[] buffer = new byte[size];
79         int block;
80         int i = 0;
81         int index = 0;
82         while (i < length) {
83             block = (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 18 |
84                     (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 12 |
85                     (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 6 |
86                     (ALPHABET.indexOf(string.charAt(i++)) & 0xff);
87             buffer[index++] = (byte) (block >>> 16);
88             if (index < size) buffer[index++] = (byte) ((block >>> 8) & 0xff);
89             if (index < size) buffer[index++] = (byte) (block & 0xff);
90         }
91         return buffer;
92     }
93
94 }
95
Popular Tags