01: /* Encodes and decodes to and from Base64 notation.
02: * Copyright (C) 2003 "Eric Glass" <jcifs at samba dot org>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * 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 ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24:
25: /**
26: * Base-64 encodes the supplied block of data. Line wrapping is not
27: * applied on output.
28: *
29: * @param bytes The block of data that is to be Base-64 encoded.
30: * @return A <code>String</code> containing the encoded data.
31: */
32: public static String encode(byte[] bytes) {
33: int length = bytes.length;
34: if (length == 0)
35: return "";
36: StringBuffer buffer = new StringBuffer((int) Math
37: .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)
44: | ((bytes[i++] & 0xff) << 8) | (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)
51: return buffer.toString();
52: if (remainder == 1) {
53: block = (bytes[i] & 0xff) << 4;
54: buffer.append(ALPHABET.charAt(block >>> 6));
55: buffer.append(ALPHABET.charAt(block & 0x3f));
56: buffer.append("==");
57: return buffer.toString();
58: }
59: block = (((bytes[i++] & 0xff) << 8) | ((bytes[i]) & 0xff)) << 2;
60: buffer.append(ALPHABET.charAt(block >>> 12));
61: buffer.append(ALPHABET.charAt((block >>> 6) & 0x3f));
62: buffer.append(ALPHABET.charAt(block & 0x3f));
63: buffer.append("=");
64: return buffer.toString();
65: }
66:
67: /**
68: * Decodes the supplied Base-64 encoded string.
69: *
70: * @param string The Base-64 encoded string that is to be decoded.
71: * @return A <code>byte[]</code> containing the decoded data block.
72: */
73: public static byte[] decode(String string) {
74: int length = string.length();
75: if (length == 0)
76: return new byte[0];
77: int pad = (string.charAt(length - 2) == '=') ? 2 : (string
78: .charAt(length - 1) == '=') ? 1 : 0;
79: int size = length * 3 / 4 - pad;
80: byte[] buffer = new byte[size];
81: int block;
82: int i = 0;
83: int index = 0;
84: while (i < length) {
85: block = (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 18
86: | (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 12
87: | (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 6
88: | (ALPHABET.indexOf(string.charAt(i++)) & 0xff);
89: buffer[index++] = (byte) (block >>> 16);
90: if (index < size)
91: buffer[index++] = (byte) ((block >>> 8) & 0xff);
92: if (index < size)
93: buffer[index++] = (byte) (block & 0xff);
94: }
95: return buffer;
96: }
97:
98: }
|