Skip to content

Package: QPDecoderStream

QPDecoderStream

nameinstructionbranchcomplexitylinemethod
QPDecoderStream(InputStream)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
available()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 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: 122 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
read(byte[], int, int)
M: 3 C: 24
89%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 5
83%
M: 0 C: 1
100%

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: * @(#)QPDecoderStream.java 1.9 02/04/02
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 QP 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: */
29:
30: public class QPDecoderStream extends FilterInputStream {
31: protected byte[] ba = new byte[2];
32: protected int spaces = 0;
33:
34: /**
35: * Create a Quoted Printable decoder that decodes the specified
36: * input stream.
37: * @param in the input stream
38: */
39: public QPDecoderStream(InputStream in) {
40:         super(new PushbackInputStream(in, 2)); // pushback of size=2
41: }
42:
43: /**
44: * Read the next decoded byte from this input stream. The byte
45: * is returned as an <code>int</code> in the range <code>0</code>
46: * to <code>255</code>. If no byte is available because the end of
47: * the stream has been reached, the value <code>-1</code> is returned.
48: * This method blocks until input data is available, the end of the
49: * stream is detected, or an exception is thrown.
50: *
51: * @return the next byte of data, or <code>-1</code> if the end of the
52: * stream is reached.
53: * @exception IOException if an I/O error occurs.
54: */
55: @Override
56: public int read() throws IOException {
57:•        if (spaces > 0) {
58:          // We have cached space characters, return one
59:          spaces--;
60:          return ' ';
61:         }
62:         
63:         int c = in.read();
64:
65:•        if (c == ' ') {
66:          // Got space, keep reading till we get a non-space char
67:•         while ((c = in.read()) == ' ')
68:                 spaces++;
69:
70:•         if (c == '\r' || c == '\n' || c == -1)
71:                 // If the non-space char is CR/LF/EOF, the spaces we got
72:                  // so far is junk introduced during transport. Junk 'em.
73:                 spaces = 0;
74:          else {
75:                 // The non-space char is NOT CR/LF, the spaces are valid.
76:                 ((PushbackInputStream)in).unread(c);
77:                 c = ' ';
78:          }
79:          return c; // return either <SPACE> or <CR/LF>
80:         }
81:•        else if (c == '=') {
82:          // QP Encoded atom. Decode the next two bytes
83:          int a = in.read();
84:
85:•         if (a == '\n') {
86:                 /* Hmm ... not really confirming QP encoding, but lets
87:                  * allow this as a LF terminated encoded line .. and
88:                  * consider this a soft linebreak and recurse to fetch
89:                  * the next char.
90:                  */
91:                 return read();
92:•         } else if (a == '\r') {
93:                 // Expecting LF. This forms a soft linebreak to be ignored.
94:                 int b = in.read();
95:•                if (b != '\n')
96:                  /* Not really confirming QP encoding, but
97:                  * lets allow this as well.
98:                  */
99:                  ((PushbackInputStream)in).unread(b);
100:                 return read();
101:•         } else if (a == -1) {
102:                  // Not valid QP encoding, but we be nice and tolerant here !
103:                 return -1;
104:          } else {
105:                 ba[0] = (byte)a;
106:                 ba[1] = (byte)in.read();
107:                 try {
108:                  return ASCIIUtility.parseInt(ba, 0, 2, 16);
109:                 } catch (NumberFormatException nex) {
110:                  /*
111:                  System.err.println(
112:                          "Illegal characters in QP encoded stream: " +
113:                          ASCIIUtility.toString(ba, 0, 2)
114:                  );
115:                  */
116:
117:                  ((PushbackInputStream)in).unread(ba);
118:                  return c;
119:                 }
120:          }
121:         }
122:         return c;
123: }
124:
125: /**
126: * Reads up to <code>len</code> decoded bytes of data from this input stream
127: * into an array of bytes. This method blocks until some input is
128: * available.
129: * <p>
130: *
131: * @param buf the buffer into which the data is read.
132: * @param off the start offset of the data.
133: * @param len the maximum number of bytes read.
134: * @return the total number of bytes read into the buffer, or
135: * <code>-1</code> if there is no more data because the end of
136: * the stream has been reached.
137: * @exception IOException if an I/O error occurs.
138: */
139: @Override
140: public int read(byte[] buf, int off, int len) throws IOException {
141:         int i, c;
142:•        for (i = 0; i < len; i++) {
143:•         if ((c = read()) == -1) {
144:•                if (i == 0) // At end of stream, so we should
145:                  i = -1; // return -1 , NOT 0.
146:                 break;
147:          }
148:          buf[off+i] = (byte)c;
149:         }
150: return i;
151: }
152:
153: /**
154: * Tests if this input stream supports marks. Currently this class
155: * does not support marks
156: */
157: @Override
158: public boolean markSupported() {
159:         return false;
160: }
161:
162: /**
163: * Returns the number of bytes that can be read from this input
164: * stream without blocking. The QP algorithm does not permit
165: * a priori knowledge of the number of bytes after decoding, so
166: * this method just invokes the <code>available</code> method
167: * of the original input stream.
168: */
169: @Override
170: public int available() throws IOException {
171:         // This is bogus ! We don't really know how much
172:         // bytes are available *after* decoding
173:         return in.available();
174: }
175:
176: /**** begin TEST program
177: public static void main(String argv[]) throws Exception {
178: FileInputStream infile = new FileInputStream(argv[0]);
179: QPDecoderStream decoder = new QPDecoderStream(infile);
180: int c;
181:
182: while ((c = decoder.read()) != -1)
183: System.out.print((char)c);
184: System.out.println();
185: }
186: *** end TEST program ****/
187: }