001: /*
002: * Copyright (C) 2007 Jared Alexander Spigner
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * jspigner@openjx.org
019: *
020: * Base64.java
021: *
022: * Created on June 9, 2007, 11:08 PM
023: *
024: */
025:
026: package org.openjx.util;
027:
028: import java.io.StringReader;
029: import java.io.IOException;
030: import java.io.ByteArrayOutputStream;
031:
032: /**
033: * This class simply handles encoding and decoding data to and from base 64.
034: *
035: * @author Jared Spigner
036: */
037: public class Base64 {
038:
039: /**
040: * This is the constructor for the Base64 class. It creates a new
041: * instance of Base64.
042: */
043: public Base64() {
044: }
045:
046: /**
047: * This method encodes the data in the StringReader from base 64.
048: *
049: * @param in is a stream of data we want to encode.
050: *
051: * @return a byte array fo teh encoded data.
052: */
053: public byte[] encodeBase64(StringReader in) {
054: byte code[] = new byte[64];
055:
056: ByteArrayOutputStream out = new ByteArrayOutputStream();
057:
058: for (int i = 0; i < 26; i++)
059: code[i] = (byte) ('A' + i);
060: for (int i = 0; i < 26; i++)
061: code[26 + i] = (byte) ('a' + i);
062: for (int i = 0; i < 10; i++)
063: code[52 + i] = (byte) ('0' + i);
064: code[62] = (byte) '+';
065: code[63] = (byte) '/';
066:
067: int c, d, e, k = 0, end = 0;
068: byte u, v, w, x;
069:
070: while (end == 0) {
071: try {
072: if ((c = in.read()) == -1) {
073: c = 0;
074: end = 1;
075: }
076: if ((d = in.read()) == -1) {
077: d = 0;
078: end += 1;
079: }
080: if ((e = in.read()) == -1) {
081: e = 0;
082: end += 1;
083: }
084:
085: u = code[c >> 2];
086: v = code[(0x00000003 & c) << 4 | d >> 4];
087: w = code[(0x0000000F & d) << 2 | e >> 6];
088: x = code[e & 0x0000003F];
089:
090: if (k == 76) {
091: k = 0;
092: }
093: if (end >= 1)
094: x = (byte) '=';
095: if (end == 2)
096: w = (byte) '=';
097:
098: if (end < 3) {
099: out.write(u);
100: out.write(v);
101: out.write(w);
102: out.write(x);
103: }
104:
105: k += 4;
106: } catch (IOException eio) {
107: }
108: }
109:
110: return out.toByteArray();
111: }
112:
113: /**
114: * This method decodes the data in the StringReader from base 64.
115: *
116: * @param in is a stream of data we want to decode.
117: *
118: * @return a byte array fo teh decoded data.
119: */
120: public byte[] decodeBase64(StringReader in) {
121: byte code[] = new byte[64];
122: int decode[] = new int[128];
123:
124: ByteArrayOutputStream out = new ByteArrayOutputStream();
125:
126: for (int i = 0; i < 26; i++)
127: code[i] = (byte) ('A' + i);
128: for (int i = 0; i < 26; i++)
129: code[26 + i] = (byte) ('a' + i);
130: for (int i = 0; i < 10; i++)
131: code[52 + i] = (byte) ('0' + i);
132: code[62] = (byte) '+';
133: code[63] = (byte) '/';
134:
135: for (int i = 0; i < 128; i++)
136: decode[i] = -1;
137: for (int i = 0; i < 64; i++)
138: decode[(int) code[i]] = i;
139:
140: int c = 0, d = 0, e = 0, f = 0, i = 0, n = 0;
141:
142: do {
143: try {
144: f = in.read();
145:
146: if (f >= 0 && f < 128 && (i = decode[f]) != -1) {
147: if (n % 4 == 0) {
148: c = i << 2;
149: } else if (n % 4 == 1) {
150: c = c | (i >> 4);
151: d = (i & 0x0000000f) << 4;
152: } else if (n % 4 == 2) {
153: d = d | (i >> 2);
154: e = (i & 0x00000003) << 6;
155: } else
156: e = e | i;
157:
158: n++;
159:
160: if (n % 4 == 0) {
161: out.write(c);
162: out.write(d);
163: out.write(e);
164: }
165: }
166: } catch (IOException eio) {
167: }
168:
169: } while (f != -1);
170:
171: if (n % 4 == 3) {
172: out.write(c);
173: out.write(d);
174: } else if (n % 4 == 2) {
175: out.write(c);
176: }
177:
178: return out.toByteArray();
179: }
180:
181: }
|