1 /* Base64InputStream.java -- base-64 input stream.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.io
.ByteArrayInputStream
;
42 import java
.io
.ByteArrayOutputStream
;
43 import java
.io
.FilterInputStream
;
44 import java
.io
.IOException
;
45 import java
.io
.InputStream
;
48 * A filter input stream that decodes data encoded in the Base-64
51 * @author Casey Marshall (rsdio@metastatic.org)
53 public class Base64InputStream
extends FilterInputStream
56 // Constants and fields.
57 // ------------------------------------------------------------------------
59 /** Base-64 digits. */
60 private static final String BASE_64
=
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
63 /** Base-64 padding character. */
64 private static final char BASE_64_PAD
= '=';
66 /** Decoding state. */
69 /** Intermediate decoded value. */
75 private final byte[] one
= new byte[1];
78 // ------------------------------------------------------------------------
81 * Create a new Base-64 input stream. The input bytes must be the
82 * ASCII characters A-Z, a-z, 0-9, + and /, with optional whitespace,
83 * and will be decoded into a byte stream.
85 * @param in The source of Base-64 input.
87 public Base64InputStream(InputStream in
)
96 // ------------------------------------------------------------------------
99 * Decode a single Base-64 string to a byte array.
101 * @param base64 The Base-64 encoded data.
102 * @return The decoded bytes.
103 * @throws IOException If the given data do not compose a valid Base-64
106 public static byte[] decode(String base64
) throws IOException
108 Base64InputStream in
=
109 new Base64InputStream(new ByteArrayInputStream(base64
.getBytes()));
110 ByteArrayOutputStream out
=
111 new ByteArrayOutputStream((int) (base64
.length() / 0.666));
112 byte[] buf
= new byte[1024];
114 while ((len
= in
.read(buf
)) != -1)
115 out
.write(buf
, 0, len
);
116 return out
.toByteArray();
120 // ------------------------------------------------------------------------
122 public int available()
127 public int read() throws IOException
134 public int read(byte[] buf
, int off
, int len
) throws IOException
142 while (Character
.isWhitespace((char) (i
= in
.read())));
143 int pos
= BASE_64
.indexOf((char) i
);
153 buf
[count
++] = (byte) (temp
| (pos
>>> 4));
154 temp
= (pos
& 0x0F) << 4;
158 buf
[count
++] = (byte) (temp
| (pos
>>> 2));
159 temp
= (pos
& 0x03) << 6;
163 buf
[count
++] = (byte) (temp
| pos
);
168 else if (i
== BASE_64_PAD
)
174 throw new IOException("malformed Base-64 input");
176 while (Character
.isWhitespace((char) (i
= in
.read())));
177 if (i
!= BASE_64_PAD
)
178 throw new IOException("malformed Base-64 input");
180 while (Character
.isWhitespace((char) (i
= in
.read())));
185 else // First non-Base-64 character, consider it end-of-stream.
188 throw new IOException("malformed Base-64 input");
196 public boolean markSupported()
201 public void mark(int markLimit
) { }
203 public void reset() throws IOException
205 throw new IOException("reset not supported");
208 public long skip(long n
) throws IOException
211 for (skipped
= 0; skipped
< n
; skipped
++)