Debian Codegraph @

src/main/java/org/freehep/util/io/Base64InputStream.java

raw
1 // Copyright 2003, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.IOException;
5 import java.io.InputStream;
6
7 /**
8 * The Base64InputStream decodes binary data according to RFC 2045.
9 *
10 * IMPORTANT: inherits from InputStream rather than FilterInputStream so that
11 * the correct read(byte[], int, int) method is used.
12 *
13 * @author Mark Donszelmann
14 * @version $Id: Base64InputStream.java 8584 2006-08-10 23:06:37Z duns $
15 */
16 public class Base64InputStream extends InputStream {
17
18 private int b[] = new int[3];
19
20 private int bIndex;
21
22 private int bLength;
23
24 private boolean endReached;
25
26 private int lineNo;
27
28 private InputStream in;
29
30 private static final int ILLEGAL = -1;
31
32 // private static final int WHITESPACE = -2;
33 private static final int LINEFEED = -3;
34
35 private static final int CARRIAGERETURN = -4;
36
37 private static final int EQUALS = -5;
38
39 private static final byte base64toInt[] = { -1, -1, -1, -1, -1, -1, -1, -1,
40 -1, -2, -3, -1, -1, -4,
41 -1,
42 -1, // Tab, LineFeed, CarriageReturn
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1,
45 63, // Space, Plus, Slash
46 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -5, -1,
47 -1, // 0 - 9, =
48 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
49 14, // A-Z
50 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,
51 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // a-z
52 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };
53
54 /**
55 * Creates a Base64 input stream for given input
56 *
57 * @param input to be read from
58 */
59 public Base64InputStream(InputStream input) {
60 super();
61 in = input;
62 endReached = false;
63 bIndex = 0;
64 bLength = 0;
65 lineNo = 1;
66 }
67
68 public int read() throws IOException {
69
70 if (bIndex >= bLength) {
71 if (endReached)
72 return -1;
73 bLength = readTuple();
74 if (bLength <= 0)
75 return -1;
76 bIndex = 0;
77 }
78 int a = b[bIndex];
79 bIndex++;
80
81 return a;
82 }
83
84 /**
85 * @return current line number
86 */
87 public int getLineNo() {
88 return lineNo;
89 }
90
91 private int readTuple() throws IOException, EncodingException {
92 byte c[] = new byte[4];
93 int cIndex = 0;
94 int encoding = 0;
95 int prevEncoding = 0;
96 int i = 0;
97
98 if (endReached)
99 return 0;
100
101 while (i < c.length) {
102 int ch = in.read();
103 if (ch < 0) {
104 endReached = true;
105 if (cIndex == 0) {
106 // still a proper end.
107 return 0;
108 } else {
109 throw new EncodingException(
110 "Improperly padded Base64 Input.");
111 }
112 }
113
114 prevEncoding = encoding;
115 encoding = base64toInt[ch & 0x7f];
116 switch (encoding) {
117 case ILLEGAL:
118 if (ch < 0)
119 throw new EncodingException(
120 "Illegal character in Base64 encoding '" + ch
121 + "'.");
122 case EQUALS:
123 // ignore, but keep reading padding
124 i++;
125 break;
126 case CARRIAGERETURN:
127 lineNo++;
128 break;
129 case LINEFEED:
130 if (prevEncoding != CARRIAGERETURN) {
131 lineNo++;
132 }
133 break;
134 default:
135 c[cIndex] = (byte) (encoding & 0xFF);
136 cIndex++;
137 i++;
138 break;
139 }
140 }
141
142 int data;
143 switch (cIndex) {
144 case 2:
145 data = (c[0] << 18) | (c[1] << 12);
146
147 b[0] = data >>> 16;
148 return 1;
149
150 case 3:
151 data = (c[0] << 18) | (c[1] << 12) | (c[2] << 6);
152
153 b[0] = data >>> 16;
154 b[1] = data >>> 8;
155 return 2;
156
157 case 4:
158 data = (c[0] << 18) | (c[1] << 12) | (c[2] << 6) | (c[3]);
159
160 b[0] = data >>> 16;
161 b[1] = data >>> 8;
162 b[2] = data;
163 return 3;
164
165 default:
166 throw new EncodingException("Base64InputStream: internal error.");
167 }
168 }
169 }