001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.InputStream;
042: import java.io.IOException;
043:
044: /**
045: *
046: *
047: * @author Stan Bailes
048: */
049: public class Base64InputStream extends InputStream {
050: InputStream in;
051: int accum = 0;
052: int cnt = -1;
053: boolean eof = false;
054:
055: static int[] base64Map = new int[256];
056: static {
057: byte[] b = Base64OutputStream.base64;
058: for (int i = 0; i < base64Map.length; i++)
059: base64Map[i] = -1;
060: for (int i = 0; i < b.length; i++) {
061: base64Map[b[i]] = i;
062: }
063: base64Map['='] = 0;
064: }
065:
066: public Base64InputStream(InputStream in) {
067: this .in = in;
068: }
069:
070: final int readChar() throws IOException {
071: int c = eof ? -1 : in.read();
072: while (c == '\r' || c == '\n')
073: c = in.read();
074: eof = c < 0;
075: return c;
076: }
077:
078: public int read() throws IOException {
079: if (eof)
080: return -1;
081: if (cnt < 0) {
082: int c1 = readChar();
083: int c2 = readChar();
084: int c3 = readChar();
085: int c4 = readChar();
086: // System.out.println("[" + (char)c1 + "] [" + (char)c2 + "] [" + (char)c3 +
087: // "] [" + (char)c4 + "]");
088: if (eof)
089: return -1;
090:
091: int b1 = base64Map[c1];
092: int b2 = base64Map[c2];
093: int b3 = base64Map[c3];
094: int b4 = base64Map[c4];
095:
096: // System.out.println("(" + b1 + ") (" + b2 + ") (" + b3 + ") (" + b4 + ")");
097: if (b1 < 0 || b2 < 0 || b3 < 0 || b4 < 0) {
098: throw new IOException("Bad base64 character");
099: }
100: accum = (b1 << 18) | (b2 << 12) | (b3 << 6) | b4;
101: // System.out.println(Integer.toHexString(accum));
102:
103: if (c3 == '=') {
104: cnt = 0;
105: accum >>= 16;
106: } else if (c4 == '=') {
107: cnt = 8;
108: accum >>= 8;
109: } else {
110: cnt = 16;
111: }
112:
113: }
114: int ret = (accum >> cnt) & 0xff;
115: cnt -= 8;
116: return ret;
117: }
118:
119: public void close() throws IOException {
120: in.close();
121: }
122: }
|