1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
64
65 /**
66 * Create a QP encoder that encodes the specified input stream.
67 * Inserts the CRLF sequence after outputting 76 bytes.
68 * @param out the output stream
69 */
70 public QPEncoderStream(OutputStream out) {
71 this(out, 76);
72 }
73
74 /**
75 * Encodes <code>len</code> bytes from the specified
76 * <code>byte</code> array starting at offset <code>off</code> to
77 * this output stream.
78 *
79 * @param b the data.
80 * @param off the start offset in the data.
81 * @param len the number of bytes to write.
82 * @exception IOException if an I/O error occurs.
83 */
84 public void write(byte[] b, int off, int len) throws IOException {
85 for (int i = 0; i < len; i++)
86 write(b[off + i]);
87 }
88
89 /**
90 * Encodes <code>b.length</code> bytes to this output stream.
91 * @param b the data to be written.
92 * @exception IOException if an I/O error occurs.
93 */
94 public void write(byte[] b) throws IOException {
95 write(b, 0, b.length);
96 }
97
98 /**
99 * Encodes the specified <code>byte</code> to this output stream.
100 * @param c the <code>byte</code>.
101 * @exception IOException if an I/O error occurs.
102 */
103 public void write(int c) throws IOException {
104 c = c & 0xff; // Turn off the MSB.
105 if (gotSpace) { // previous character was <SPACE>
106 if (c == '\r' || c == '\n')
107 // if CR/LF, we need to encode the <SPACE> char
108 output(' ', true);
109 else // no encoding required, just output the char
110 output(' ', false);
111 gotSpace = false;
112 }
113
114 if (c == '\r') {
115 gotCR = true;
116 outputCRLF();
117 } else {
118 if (c == '\n') {
119 if (gotCR)
120 // This is a CRLF sequence, we already output the
121 // corresponding CRLF when we got the CR, so ignore this
122 ;
123 else
124 outputCRLF();
125 } else if (c == ' ') {
126 gotSpace = true;
127 } else if (c < 040 || c >= 0177 || c == '=')
128 // Encoding required.
129 output(c, true);
130 else // No encoding required
131 output(c, false);
132 // whatever it was, it wasn't a CR
133 gotCR = false;
134 }
135 }
136
137 /**
138 * Flushes this output stream and forces any buffered output bytes
139 * to be encoded out to the stream.
140 * @exception IOException if an I/O error occurs.
141 */
142 public void flush() throws IOException {
143 out.flush();
144 }
145
146 /**
147 * Forces any buffered output bytes to be encoded out to the stream
148 * and closes this output stream
149 */
150 public void close() throws IOException {
151 out.close();
152 }
153
154 private void outputCRLF() throws IOException {
155 out.write('\r');
156 out.write('\n');
157 count = 0;
158 }
159
160 // The encoding table
161 private final static char hex[] = {
162 '0','1', '2', '3', '4', '5', '6', '7',
163 '8','9', 'A', 'B', 'C', 'D', 'E', 'F'
164 };
165
166 protected void output(int c, boolean encode) throws IOException {
167 if (encode) {
168 if ((count += 3) > bytesPerLine) {
169 out.write('=');
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
64
65 /**
66 * Create a QP encoder that encodes the specified input stream.
67 * Inserts the CRLF sequence after outputting 76 bytes.
68 * @param out the output stream
69 */
70 public QPEncoderStream(OutputStream out) {
71 this(out, 76);
72 }
73
74 /**
75 * Encodes <code>len</code> bytes from the specified
76 * <code>byte</code> array starting at offset <code>off</code> to
77 * this output stream.
78 *
79 * @param b the data.
80 * @param off the start offset in the data.
81 * @param len the number of bytes to write.
82 * @exception IOException if an I/O error occurs.
83 */
84 @Override
85 public void write(byte[] b, int off, int len) throws IOException {
86 for (int i = 0; i < len; i++)
87 write(b[off + i]);
88 }
89
90 /**
91 * Encodes <code>b.length</code> bytes to this output stream.
92 * @param b the data to be written.
93 * @exception IOException if an I/O error occurs.
94 */
95 @Override
96 public void write(byte[] b) throws IOException {
97 write(b, 0, b.length);
98 }
99
100 /**
101 * Encodes the specified <code>byte</code> to this output stream.
102 * @param c the <code>byte</code>.
103 * @exception IOException if an I/O error occurs.
104 */
105 @Override
106 public void write(int c) throws IOException {
107 c = c & 0xff; // Turn off the MSB.
108 if (gotSpace) { // previous character was <SPACE>
109 if (c == '\r' || c == '\n')
110 // if CR/LF, we need to encode the <SPACE> char
111 output(' ', true);
112 else // no encoding required, just output the char
113 output(' ', false);
114 gotSpace = false;
115 }
116
117 if (c == '\r') {
118 gotCR = true;
119 outputCRLF();
120 } else {
121 if (c == '\n') {
122 if (gotCR)
123 // This is a CRLF sequence, we already output the
124 // corresponding CRLF when we got the CR, so ignore this
125 ;
126 else
127 outputCRLF();
128 } else if (c == ' ') {
129 gotSpace = true;
130 } else if (c < 040 || c >= 0177 || c == '=')
131 // Encoding required.
132 output(c, true);
133 else // No encoding required
134 output(c, false);
135 // whatever it was, it wasn't a CR
136 gotCR = false;
137 }
138 }
139
140 /**
141 * Flushes this output stream and forces any buffered output bytes
142 * to be encoded out to the stream.
143 * @exception IOException if an I/O error occurs.
144 */
145 @Override
146 public void flush() throws IOException {
147 out.flush();
148 }
149
150 /**
151 * Forces any buffered output bytes to be encoded out to the stream
152 * and closes this output stream
153 */
154 @Override
155 public void close() throws IOException {
156 out.close();
157 }
158
159 private void outputCRLF() throws IOException {
160 out.write('\r');
161 out.write('\n');
162 count = 0;
163 }
164
165 // The encoding table
166 private final static char hex[] = {
167 '0','1', '2', '3', '4', '5', '6', '7',
168 '8','9', 'A', 'B', 'C', 'D', 'E', 'F'
169 };
170
171 protected void output(int c, boolean encode) throws IOException {
172 if (encode) {
173 if ((count += 3) > bytesPerLine) {
174 out.write('=');
|