01: /*
02: * JavuX - Java Component Set
03: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
04: * e-mail: ksadlocha@programics.com
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library 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 GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: package com.javujavu.javux.util.code;
22:
23: public class CodeBase64 {
24: public static byte[] decode(String s) {
25: int len = s.length();
26: while (len > 0 && s.charAt(len - 1) == '=')
27: len--;
28: int rsize = (len * 3) / 4;
29: byte[] r = new byte[rsize];
30: char c;
31: int v, k = 0;
32: for (int i = 0, sh = 0; i < len && k < rsize; i++) {
33: c = s.charAt(i);
34: if (c >= 'A' && c <= 'Z')
35: v = c - 'A';
36: else if (c >= 'a' && c <= 'z')
37: v = c - 'a' + 26;
38: else if (c >= '0' && c <= '9')
39: v = c - '0' + 52;
40: else if (c == '+')
41: v = 62;
42: else if (c == '/')
43: v = 63;
44: else
45: continue;
46:
47: if (sh == 0) {
48: r[k] = (byte) (v << 2);
49: sh = 1;
50: } else if (sh == 1) {
51: r[k] |= (byte) (v >> 4);
52: k++;
53: if (k == rsize)
54: break;
55: r[k] = (byte) (v << 4);
56: sh = 2;
57: } else if (sh == 2) {
58: r[k] |= (byte) (v >> 2);
59: k++;
60: if (k == rsize)
61: break;
62: r[k] = (byte) (v << 6);
63: sh = 3;
64: } else if (sh == 3) {
65: r[k] |= v;
66: k++;
67: sh = 0;
68: }
69: }
70: if (k < rsize) {
71: byte[] r2 = new byte[k];
72: System.arraycopy(r, 0, r2, 0, k);
73: r = r2;
74: }
75: return r;
76: }
77: }
|