Skip to content

Package: BASE64DecoderStream

BASE64DecoderStream

nameinstructionbranchcomplexitylinemethod
BASE64DecoderStream(InputStream)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
available()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
decode()
M: 164 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
decode(byte[])
M: 145 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
markSupported()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
read()
M: 28 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
read(byte[], int, int)
M: 27 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 289 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: /*
12: * @(#)BASE64DecoderStream.java 1.8 02/03/27
13: */
14:
15:
16:
17: package com.sun.xml.messaging.saaj.packaging.mime.util;
18:
19: import java.io.*;
20:
21: /**
22: * This class implements a BASE64 Decoder. It is implemented as
23: * a FilterInputStream, so one can just wrap this class around
24: * any input stream and read bytes from this filter. The decoding
25: * is done as the bytes are read out.
26: *
27: * @author John Mani
28: * @author Bill Shannon
29: */
30:
31: public class BASE64DecoderStream extends FilterInputStream {
32: private byte[] buffer;         // cache of decoded bytes
33: private int bufsize = 0;        // size of the cache
34: private int index = 0;        // index into the cache
35:
36: /**
37: * Create a BASE64 decoder that decodes the specified input stream
38: * @param in        the input stream
39: */
40: public BASE64DecoderStream(InputStream in) {
41:         super(in);
42:         buffer = new byte[3];
43: }
44:
45: /**
46: * Read the next decoded byte from this input stream. The byte
47: * is returned as an <code>int</code> in the range <code>0</code>
48: * to <code>255</code>. If no byte is available because the end of
49: * the stream has been reached, the value <code>-1</code> is returned.
50: * This method blocks until input data is available, the end of the
51: * stream is detected, or an exception is thrown.
52: *
53: * @return next byte of data, or <code>-1</code> if the end of the
54: * stream is reached.
55: * @exception IOException if an I/O error occurs.
56: * @see java.io.FilterInputStream#in
57: */
58: @Override
59: public int read() throws IOException {
60:•        if (index >= bufsize) {
61:          decode(); // Fills up buffer
62:•         if (bufsize == 0) // buffer is empty
63:                 return -1;
64:          index = 0; // reset index into buffer
65:         }
66:         return buffer[index++] & 0xff; // Zero off the MSB
67: }
68:
69: /**
70: * Reads up to <code>len</code> decoded bytes of data from this input stream
71: * into an array of bytes. This method blocks until some input is
72: * available.
73: * <p>
74: *
75: * @param buf the buffer into which the data is read.
76: * @param off the start offset of the data.
77: * @param len the maximum number of bytes read.
78: * @return the total number of bytes read into the buffer, or
79: * <code>-1</code> if there is no more data because the end of
80: * the stream has been reached.
81: * @exception IOException if an I/O error occurs.
82: */
83: @Override
84: public int read(byte[] buf, int off, int len) throws IOException {
85:         int i, c;
86:•        for (i = 0; i < len; i++) {
87:•         if ((c = read()) == -1) {
88:•                if (i == 0) // At end of stream, so we should
89:                  i = -1; // return -1 , NOT 0.
90:                 break;
91:          }
92:          buf[off+i] = (byte)c;
93:         }
94:
95:         return i;
96: }
97:
98: /**
99: * Tests if this input stream supports marks. Currently this class
100: * does not support marks
101: */
102: @Override
103: public boolean markSupported() {
104:         return false; // Maybe later ..
105: }
106:
107: /**
108: * Returns the number of bytes that can be read from this input
109: * stream without blocking. However, this figure is only
110: * a close approximation in case the original encoded stream
111: * contains embedded CRLFs; since the CRLFs are discarded, not decoded
112: */
113: @Override
114: public int available() throws IOException {
115:          // This is only an estimate, since in.available()
116:          // might include CRLFs too ..
117:          return ((in.available() * 3)/4 + (bufsize-index));
118: }
119:
120: /**
121: * This character array provides the character to value map
122: * based on RFC1521.
123: */
124: private final static char pem_array[] = {
125:         'A','B','C','D','E','F','G','H', // 0
126:         'I','J','K','L','M','N','O','P', // 1
127:         'Q','R','S','T','U','V','W','X', // 2
128:         'Y','Z','a','b','c','d','e','f', // 3
129:         'g','h','i','j','k','l','m','n', // 4
130:         'o','p','q','r','s','t','u','v', // 5
131:         'w','x','y','z','0','1','2','3', // 6
132:         '4','5','6','7','8','9','+','/' // 7
133: };
134:
135: private final static byte pem_convert_array[] = new byte[256];
136:
137: static {
138:•        for (int i = 0; i < 255; i++)
139:          pem_convert_array[i] = -1;
140:•        for(int i = 0; i < pem_array.length; i++)
141:          pem_convert_array[pem_array[i]] = (byte) i;
142: }
143:
144: /* The decoder algorithm */
145: private byte[] decode_buffer = new byte[4];
146: private void decode() throws IOException {
147:         bufsize = 0;
148:         /*
149:          * We need 4 valid base64 characters before we start decoding.
150:          * We skip anything that's not a valid base64 character (usually
151:          * just CRLF).
152:          */
153:         int got = 0;
154:•        while (got < 4) {
155:          int i = in.read();
156:•         if (i == -1) {
157:•                if (got == 0)
158:                  return;        // EOF before any data is ok
159:                 throw new IOException("Error in encoded stream, got " + got);
160:          }
161:•         if (i >= 0 && i < 256 && i == '=' || pem_convert_array[i] != -1)
162:                 decode_buffer[got++] = (byte)i;
163:         }
164:
165:         byte a, b;
166:         a = pem_convert_array[decode_buffer[0] & 0xff];
167:         b = pem_convert_array[decode_buffer[1] & 0xff];
168:         // The first decoded byte
169:         buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));
170:
171:•        if (decode_buffer[2] == '=') // End of this BASE64 encoding
172:          return;
173:         a = b;
174:         b = pem_convert_array[decode_buffer[2] & 0xff];
175:         // The second decoded byte
176:         buffer[bufsize++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));
177:
178:•        if (decode_buffer[3] == '=') // End of this BASE64 encoding
179:          return;
180:         a = b;
181:         b = pem_convert_array[decode_buffer[3] & 0xff];
182:         // The third decoded byte
183:         buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f));
184: }
185:
186: /**
187: * Base64 decode a byte array. No line breaks are allowed.
188: * This method is suitable for short strings, such as those
189: * in the IMAP AUTHENTICATE protocol, but not to decode the
190: * entire content of a MIME part.
191: *
192: * @param inbuf byte array to decode
193: *
194: * @return decoded byte array
195: *
196: * NOTE: inbuf may only contain valid base64 characters.
197: * Whitespace is not ignored.
198: */
199: public static byte[] decode(byte[] inbuf) {
200:         int size = (inbuf.length / 4) * 3;
201:•        if (size == 0)
202:          return inbuf;
203:
204:•        if (inbuf[inbuf.length - 1] == '=') {
205:          size--;
206:•         if (inbuf[inbuf.length - 2] == '=')
207:                 size--;
208:         }
209:         byte[] outbuf = new byte[size];
210:
211:         int inpos = 0, outpos = 0;
212:         size = inbuf.length;
213:•        while (size > 0) {
214:          byte a, b;
215:          a = pem_convert_array[inbuf[inpos++] & 0xff];
216:          b = pem_convert_array[inbuf[inpos++] & 0xff];
217:          // The first decoded byte
218:          outbuf[outpos++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));
219:
220:•         if (inbuf[inpos] == '=') // End of this BASE64 encoding
221:                 return outbuf;
222:          a = b;
223:          b = pem_convert_array[inbuf[inpos++] & 0xff];
224:          // The second decoded byte
225:          outbuf[outpos++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));
226:
227:•         if (inbuf[inpos] == '=') // End of this BASE64 encoding
228:                 return outbuf;
229:          a = b;
230:          b = pem_convert_array[inbuf[inpos++] & 0xff];
231:          // The third decoded byte
232:          outbuf[outpos++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f));
233:          size -= 4;
234:         }
235:         return outbuf;
236: }
237:
238: /*** begin TEST program ***
239: public static void main(String argv[]) throws Exception {
240:         FileInputStream infile = new FileInputStream(argv[0]);
241:         BASE64DecoderStream decoder = new BASE64DecoderStream(infile);
242:         int c;
243:
244:         while ((c = decoder.read()) != -1)
245:          System.out.print((char)c);
246:         System.out.flush();
247: }
248: *** end TEST program ***/
249: }