Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: mindbright/util/Base64.java


1   /******************************************************************************
2    *
3    * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4    *                 www.mindbright.se, info@mindbright.se
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   *****************************************************************************
17   * $Author: nallen $
18   * $Date: 2001/11/12 16:31:29 $
19   * $Name:  $
20   *****************************************************************************/
21  package mindbright.util;
22  
23  public final class Base64 {
24  
25      private final static int[] fromBase64 = {
26    -1, -1, -1, -1, -1, -1, -1, -1,
27    -1, -1, -1, -1, -1, -1, -1, -1,
28    -1, -1, -1, -1, -1, -1, -1, -1,
29    -1, -1, -1, -1, -1, -1, -1, -1,
30    -1, -1, -1, -1, -1, -1, -1, -1,
31    -1, -1, -1, 62, -1, -1, -1, 63,
32    52, 53, 54, 55, 56, 57, 58, 59,
33    60, 61, -1, -1, -1, -1, -1, -1,
34    -1,  0,  1,  2,  3,  4,  5,  6,
35    7,  8,  9, 10, 11, 12, 13, 14,
36    15, 16, 17, 18, 19, 20, 21, 22,
37    23, 24, 25, -1, -1, -1, -1, -1,
38    -1, 26, 27, 28, 29, 30, 31, 32,
39    33, 34, 35, 36, 37, 38, 39, 40,
40    41, 42, 43, 44, 45, 46, 47, 48,
41    49, 50, 51, -1, -1, -1, -1, -1
42      };
43  
44      private final static byte[] toBase64 = {
45    // 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
46       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
47    // 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
48       81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99,100,101,102,
49    // 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
50       103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,
51    // 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
52       119,120,121,122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, (byte)'='
53      };
54  
55      private final static int PAD_CHAR = 64;
56  
57      public static byte[] encode(byte[] data) {
58    return encode(data, 0, data.length);
59      }
60  
61      public static byte[] encode(byte[] data, int offset, int length) {
62    byte[] encoded;
63    int x1, x2, x3, x4;
64    int i, j, r, n;
65  
66    r       = length % 3;
67    n       = offset + (length - r);
68    encoded = new byte[((length / 3) * 4) + (r != 0 ? 4 : 0)];
69  
70    for(i = offset, j = 0; i < n; i += 3, j += 4) {
71        x1 = (data[i] & 0xfc) >> 2;
72        x2 = (((data[i    ] & 0x03) << 4) | ((data[i + 1] & 0xf0) >> 4));
73        x3 = (((data[i + 1] & 0x0f) << 2) | ((data[i + 2] & 0xc0) >> 6));
74        x4 = (data[i + 2] & 0x3f);
75        encoded[j    ] = toBase64[x1];
76        encoded[j + 1] = toBase64[x2];
77        encoded[j + 2] = toBase64[x3];
78        encoded[j + 3] = toBase64[x4];
79    }
80  
81    if(r != 0) {
82        x1 = (data[i] & 0xfc) >> 2;
83        x2 = (data[i] & 0x03) << 4;
84        x3 = PAD_CHAR;
85        x4 = PAD_CHAR;
86        if(r == 2) {
87      x2 |= ((data[i + 1] & 0xf0) >> 4);
88      x3 = (data[i + 1] & 0x0f) << 2;
89        }
90        encoded[j++] = toBase64[x1];
91        encoded[j++] = toBase64[x2];
92        encoded[j++] = toBase64[x3];
93        encoded[j++] = toBase64[x4];
94    }
95  
96    return encoded;
97      }
98  
99      public static byte[] decode(byte[] data) {
100   return decode(data, 0, data.length);
101     }
102 
103     public static byte[] decode(byte[] encoded, int offset, int length) {
104   byte[] data;
105   int i, j, n, v = 0, c, bits = 0;
106 
107   n    = offset + length;
108   data = new byte[(length * 3) / 4];
109 
110   for(i = offset, j = 0; i < n; i++) {
111       c = fromBase64[encoded[i]];
112       if(c < 0) {
113     if(encoded[i] == toBase64[PAD_CHAR])
114         break;
115     continue;
116       }
117       v = (v << 6) | c;
118       bits += 6;
119       if(bits >= 8) {
120     bits -= 8;
121     data[j++] = (byte) ((v >> bits) & 0xff);
122       }
123   }
124 
125   if(data.length > j) {
126       byte[] tmp = new byte[j];
127       System.arraycopy(data, 0, tmp, 0, j);
128       data = tmp;
129   }
130 
131   return data;
132     }
133 
134 }