KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > coder > Base64DecoderInputStream


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.coder;
37
38 import java.io.FilterInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41
42 /**
43  * FilterInputStream that decodes a base64 encoded stream.
44  *
45  * <br>
46  * <b>RFC(s):</b> 2045
47  *
48  * @author Timo Stich <tstich@users.sourceforge.net>
49  */

50 public class Base64DecoderInputStream extends FilterInputStream JavaDoc {
51
52     // US-ASCII to Base64 Table
53

54     private static byte[] table =
55         {
56             000,
57             000,
58             000,
59             000,
60             000,
61             000,
62             000,
63             000,
64             000,
65             000,
66             000,
67             000,
68             000,
69             000,
70             000,
71             000,
72             000,
73             000,
74             000,
75             000,
76             000,
77             000,
78             000,
79             000,
80             000,
81             000,
82             000,
83             000,
84             000,
85             000,
86             000,
87             000,
88             000,
89             000,
90             000,
91             000,
92             000,
93             000,
94             000,
95             000,
96             000,
97             000,
98             000,
99             62,
100             000,
101             000,
102             000,
103             63,
104             52,
105             53,
106         // + / 0..
107
54, 55, 56, 57, 58, 59, 60, 61, 000, 000, // ..9
108
000, 0, 000, 000, 000, 0, 1, 2, 3, 4, // = A..
109
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // ..
110
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //
111
25, 000, 000, 000, 000, 000, 000, 26, 27, 28, // ..Z a..
112
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // ..
113
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // ..
114
49, 50, 51, 000, 000, 000, 000, 000 }; // ..z
115

116     private int[] outBytes;
117     private byte[] inBytes;
118     private int pos;
119     private int available;
120
121     /**
122      * Constructs a Base64DecoderInputStream.
123      *
124      * @param in the base64 encoded inputstream
125      */

126     public Base64DecoderInputStream(InputStream JavaDoc in) {
127         super(new CRLFFilterInputStream(in));
128         outBytes = new int[3];
129         inBytes = new byte[4];
130     }
131
132     /**
133      * Read 4 characters = 1 base64 pack and decode it into
134      * the ouBytes buffer.
135      *
136      * @return number of characters read
137      * @throws IOException
138      */

139     private int readNextPack() throws IOException JavaDoc {
140         int[] lookedUp = new int[4];
141
142         //int block;
143
int read = in.read(inBytes);
144
145         if (read != 4)
146             return -1;
147
148         lookedUp[0] = table[inBytes[0]];
149         lookedUp[1] = table[inBytes[1]];
150         lookedUp[2] = table[inBytes[2]];
151         lookedUp[3] = table[inBytes[3]];
152
153         outBytes[0] = 0x0ff & ((lookedUp[0] << 2) | (lookedUp[1] >> 4));
154         outBytes[1] = 0x0ff & ((lookedUp[1] << 4) | (lookedUp[2] >> 2));
155         outBytes[2] = 0x0ff & ((lookedUp[2] << 6) | (lookedUp[3]));
156
157
158         // check if this pack is padded
159
if (inBytes[2] == 61) {
160             return 1;
161         }
162         if (inBytes[3] == 61) {
163             return 2;
164         }
165                 
166         return 3;
167     }
168
169     /**
170      * @see java.io.InputStream#read()
171      */

172     public int read() throws IOException JavaDoc {
173         // Do we need to read the next pack from the inputstream?
174
if( pos == available ) {
175             available = readNextPack();
176             pos = 0;
177         }
178         
179         // Are we at the end of the input
180
if( available == -1) return -1;
181         
182         // return next byte
183
return outBytes[pos++];
184     }
185
186     /**
187      * @see java.io.InputStream#read(byte[], int, int)
188      */

189     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
190         int next;
191         for( int i=0; i<arg2; i++) {
192             next = read();
193             if( next == -1 ) {
194                 if( i == 0 ) {
195                     return -1;
196                 } else {
197                     return i;
198                 }
199             }
200             arg0[arg1+i] = (byte) next;
201         }
202         return arg2;
203     }
204     
205 }
Popular Tags