1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/QPEncoderStream.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,199 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * @(#)QPEncoderStream.java 1.6 02/03/27 1.31 + */ 1.32 + 1.33 + 1.34 + 1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.util; 1.36 + 1.37 +import java.io.*; 1.38 + 1.39 +/** 1.40 + * This class implements a Quoted Printable Encoder. It is implemented as 1.41 + * a FilterOutputStream, so one can just wrap this class around 1.42 + * any output stream and write bytes into this filter. The Encoding 1.43 + * is done as the bytes are written out. 1.44 + * 1.45 + * @author John Mani 1.46 + */ 1.47 + 1.48 +public class QPEncoderStream extends FilterOutputStream { 1.49 + private int count = 0; // number of bytes that have been output 1.50 + private int bytesPerLine; // number of bytes per line 1.51 + private boolean gotSpace = false; 1.52 + private boolean gotCR = false; 1.53 + 1.54 + /** 1.55 + * Create a QP encoder that encodes the specified input stream 1.56 + * @param out the output stream 1.57 + * @param bytesPerLine the number of bytes per line. The encoder 1.58 + * inserts a CRLF sequence after this many number 1.59 + * of bytes. 1.60 + */ 1.61 + public QPEncoderStream(OutputStream out, int bytesPerLine) { 1.62 + super(out); 1.63 + // Subtract 1 to account for the '=' in the soft-return 1.64 + // at the end of a line 1.65 + this.bytesPerLine = bytesPerLine - 1; 1.66 + } 1.67 + 1.68 + /** 1.69 + * Create a QP encoder that encodes the specified input stream. 1.70 + * Inserts the CRLF sequence after outputting 76 bytes. 1.71 + * @param out the output stream 1.72 + */ 1.73 + public QPEncoderStream(OutputStream out) { 1.74 + this(out, 76); 1.75 + } 1.76 + 1.77 + /** 1.78 + * Encodes <code>len</code> bytes from the specified 1.79 + * <code>byte</code> array starting at offset <code>off</code> to 1.80 + * this output stream. 1.81 + * 1.82 + * @param b the data. 1.83 + * @param off the start offset in the data. 1.84 + * @param len the number of bytes to write. 1.85 + * @exception IOException if an I/O error occurs. 1.86 + */ 1.87 + public void write(byte[] b, int off, int len) throws IOException { 1.88 + for (int i = 0; i < len; i++) 1.89 + write(b[off + i]); 1.90 + } 1.91 + 1.92 + /** 1.93 + * Encodes <code>b.length</code> bytes to this output stream. 1.94 + * @param b the data to be written. 1.95 + * @exception IOException if an I/O error occurs. 1.96 + */ 1.97 + public void write(byte[] b) throws IOException { 1.98 + write(b, 0, b.length); 1.99 + } 1.100 + 1.101 + /** 1.102 + * Encodes the specified <code>byte</code> to this output stream. 1.103 + * @param c the <code>byte</code>. 1.104 + * @exception IOException if an I/O error occurs. 1.105 + */ 1.106 + public void write(int c) throws IOException { 1.107 + c = c & 0xff; // Turn off the MSB. 1.108 + if (gotSpace) { // previous character was <SPACE> 1.109 + if (c == '\r' || c == '\n') 1.110 + // if CR/LF, we need to encode the <SPACE> char 1.111 + output(' ', true); 1.112 + else // no encoding required, just output the char 1.113 + output(' ', false); 1.114 + gotSpace = false; 1.115 + } 1.116 + 1.117 + if (c == '\r') { 1.118 + gotCR = true; 1.119 + outputCRLF(); 1.120 + } else { 1.121 + if (c == '\n') { 1.122 + if (gotCR) 1.123 + // This is a CRLF sequence, we already output the 1.124 + // corresponding CRLF when we got the CR, so ignore this 1.125 + ; 1.126 + else 1.127 + outputCRLF(); 1.128 + } else if (c == ' ') { 1.129 + gotSpace = true; 1.130 + } else if (c < 040 || c >= 0177 || c == '=') 1.131 + // Encoding required. 1.132 + output(c, true); 1.133 + else // No encoding required 1.134 + output(c, false); 1.135 + // whatever it was, it wasn't a CR 1.136 + gotCR = false; 1.137 + } 1.138 + } 1.139 + 1.140 + /** 1.141 + * Flushes this output stream and forces any buffered output bytes 1.142 + * to be encoded out to the stream. 1.143 + * @exception IOException if an I/O error occurs. 1.144 + */ 1.145 + public void flush() throws IOException { 1.146 + out.flush(); 1.147 + } 1.148 + 1.149 + /** 1.150 + * Forces any buffered output bytes to be encoded out to the stream 1.151 + * and closes this output stream 1.152 + */ 1.153 + public void close() throws IOException { 1.154 + out.close(); 1.155 + } 1.156 + 1.157 + private void outputCRLF() throws IOException { 1.158 + out.write('\r'); 1.159 + out.write('\n'); 1.160 + count = 0; 1.161 + } 1.162 + 1.163 + // The encoding table 1.164 + private final static char hex[] = { 1.165 + '0','1', '2', '3', '4', '5', '6', '7', 1.166 + '8','9', 'A', 'B', 'C', 'D', 'E', 'F' 1.167 + }; 1.168 + 1.169 + protected void output(int c, boolean encode) throws IOException { 1.170 + if (encode) { 1.171 + if ((count += 3) > bytesPerLine) { 1.172 + out.write('='); 1.173 + out.write('\r'); 1.174 + out.write('\n'); 1.175 + count = 3; // set the next line's length 1.176 + } 1.177 + out.write('='); 1.178 + out.write(hex[c >> 4]); 1.179 + out.write(hex[c & 0xf]); 1.180 + } else { 1.181 + if (++count > bytesPerLine) { 1.182 + out.write('='); 1.183 + out.write('\r'); 1.184 + out.write('\n'); 1.185 + count = 1; // set the next line's length 1.186 + } 1.187 + out.write(c); 1.188 + } 1.189 + } 1.190 + 1.191 + /**** begin TEST program *** 1.192 + public static void main(String argv[]) throws Exception { 1.193 + FileInputStream infile = new FileInputStream(argv[0]); 1.194 + QPEncoderStream encoder = new QPEncoderStream(System.out); 1.195 + int c; 1.196 + 1.197 + while ((c = infile.read()) != -1) 1.198 + encoder.write(c); 1.199 + encoder.close(); 1.200 + } 1.201 + *** end TEST program ***/ 1.202 +}