Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | /* |
aoqi@0 | 27 | * @(#)QPEncoderStream.java 1.6 02/03/27 |
aoqi@0 | 28 | */ |
aoqi@0 | 29 | |
aoqi@0 | 30 | |
aoqi@0 | 31 | |
aoqi@0 | 32 | package com.sun.xml.internal.messaging.saaj.packaging.mime.util; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import java.io.*; |
aoqi@0 | 35 | |
aoqi@0 | 36 | /** |
aoqi@0 | 37 | * This class implements a Quoted Printable Encoder. It is implemented as |
aoqi@0 | 38 | * a FilterOutputStream, so one can just wrap this class around |
aoqi@0 | 39 | * any output stream and write bytes into this filter. The Encoding |
aoqi@0 | 40 | * is done as the bytes are written out. |
aoqi@0 | 41 | * |
aoqi@0 | 42 | * @author John Mani |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | |
aoqi@0 | 45 | public class QPEncoderStream extends FilterOutputStream { |
aoqi@0 | 46 | private int count = 0; // number of bytes that have been output |
aoqi@0 | 47 | private int bytesPerLine; // number of bytes per line |
aoqi@0 | 48 | private boolean gotSpace = false; |
aoqi@0 | 49 | private boolean gotCR = false; |
aoqi@0 | 50 | |
aoqi@0 | 51 | /** |
aoqi@0 | 52 | * Create a QP encoder that encodes the specified input stream |
aoqi@0 | 53 | * @param out the output stream |
aoqi@0 | 54 | * @param bytesPerLine the number of bytes per line. The encoder |
aoqi@0 | 55 | * inserts a CRLF sequence after this many number |
aoqi@0 | 56 | * of bytes. |
aoqi@0 | 57 | */ |
aoqi@0 | 58 | public QPEncoderStream(OutputStream out, int bytesPerLine) { |
aoqi@0 | 59 | super(out); |
aoqi@0 | 60 | // Subtract 1 to account for the '=' in the soft-return |
aoqi@0 | 61 | // at the end of a line |
aoqi@0 | 62 | this.bytesPerLine = bytesPerLine - 1; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | /** |
aoqi@0 | 66 | * Create a QP encoder that encodes the specified input stream. |
aoqi@0 | 67 | * Inserts the CRLF sequence after outputting 76 bytes. |
aoqi@0 | 68 | * @param out the output stream |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | public QPEncoderStream(OutputStream out) { |
aoqi@0 | 71 | this(out, 76); |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | /** |
aoqi@0 | 75 | * Encodes <code>len</code> bytes from the specified |
aoqi@0 | 76 | * <code>byte</code> array starting at offset <code>off</code> to |
aoqi@0 | 77 | * this output stream. |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * @param b the data. |
aoqi@0 | 80 | * @param off the start offset in the data. |
aoqi@0 | 81 | * @param len the number of bytes to write. |
aoqi@0 | 82 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 83 | */ |
aoqi@0 | 84 | public void write(byte[] b, int off, int len) throws IOException { |
aoqi@0 | 85 | for (int i = 0; i < len; i++) |
aoqi@0 | 86 | write(b[off + i]); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Encodes <code>b.length</code> bytes to this output stream. |
aoqi@0 | 91 | * @param b the data to be written. |
aoqi@0 | 92 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | public void write(byte[] b) throws IOException { |
aoqi@0 | 95 | write(b, 0, b.length); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** |
aoqi@0 | 99 | * Encodes the specified <code>byte</code> to this output stream. |
aoqi@0 | 100 | * @param c the <code>byte</code>. |
aoqi@0 | 101 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 102 | */ |
aoqi@0 | 103 | public void write(int c) throws IOException { |
aoqi@0 | 104 | c = c & 0xff; // Turn off the MSB. |
aoqi@0 | 105 | if (gotSpace) { // previous character was <SPACE> |
aoqi@0 | 106 | if (c == '\r' || c == '\n') |
aoqi@0 | 107 | // if CR/LF, we need to encode the <SPACE> char |
aoqi@0 | 108 | output(' ', true); |
aoqi@0 | 109 | else // no encoding required, just output the char |
aoqi@0 | 110 | output(' ', false); |
aoqi@0 | 111 | gotSpace = false; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | if (c == '\r') { |
aoqi@0 | 115 | gotCR = true; |
aoqi@0 | 116 | outputCRLF(); |
aoqi@0 | 117 | } else { |
aoqi@0 | 118 | if (c == '\n') { |
aoqi@0 | 119 | if (gotCR) |
aoqi@0 | 120 | // This is a CRLF sequence, we already output the |
aoqi@0 | 121 | // corresponding CRLF when we got the CR, so ignore this |
aoqi@0 | 122 | ; |
aoqi@0 | 123 | else |
aoqi@0 | 124 | outputCRLF(); |
aoqi@0 | 125 | } else if (c == ' ') { |
aoqi@0 | 126 | gotSpace = true; |
aoqi@0 | 127 | } else if (c < 040 || c >= 0177 || c == '=') |
aoqi@0 | 128 | // Encoding required. |
aoqi@0 | 129 | output(c, true); |
aoqi@0 | 130 | else // No encoding required |
aoqi@0 | 131 | output(c, false); |
aoqi@0 | 132 | // whatever it was, it wasn't a CR |
aoqi@0 | 133 | gotCR = false; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | /** |
aoqi@0 | 138 | * Flushes this output stream and forces any buffered output bytes |
aoqi@0 | 139 | * to be encoded out to the stream. |
aoqi@0 | 140 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 141 | */ |
aoqi@0 | 142 | public void flush() throws IOException { |
aoqi@0 | 143 | out.flush(); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | /** |
aoqi@0 | 147 | * Forces any buffered output bytes to be encoded out to the stream |
aoqi@0 | 148 | * and closes this output stream |
aoqi@0 | 149 | */ |
aoqi@0 | 150 | public void close() throws IOException { |
aoqi@0 | 151 | out.close(); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | private void outputCRLF() throws IOException { |
aoqi@0 | 155 | out.write('\r'); |
aoqi@0 | 156 | out.write('\n'); |
aoqi@0 | 157 | count = 0; |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | // The encoding table |
aoqi@0 | 161 | private final static char hex[] = { |
aoqi@0 | 162 | '0','1', '2', '3', '4', '5', '6', '7', |
aoqi@0 | 163 | '8','9', 'A', 'B', 'C', 'D', 'E', 'F' |
aoqi@0 | 164 | }; |
aoqi@0 | 165 | |
aoqi@0 | 166 | protected void output(int c, boolean encode) throws IOException { |
aoqi@0 | 167 | if (encode) { |
aoqi@0 | 168 | if ((count += 3) > bytesPerLine) { |
aoqi@0 | 169 | out.write('='); |
aoqi@0 | 170 | out.write('\r'); |
aoqi@0 | 171 | out.write('\n'); |
aoqi@0 | 172 | count = 3; // set the next line's length |
aoqi@0 | 173 | } |
aoqi@0 | 174 | out.write('='); |
aoqi@0 | 175 | out.write(hex[c >> 4]); |
aoqi@0 | 176 | out.write(hex[c & 0xf]); |
aoqi@0 | 177 | } else { |
aoqi@0 | 178 | if (++count > bytesPerLine) { |
aoqi@0 | 179 | out.write('='); |
aoqi@0 | 180 | out.write('\r'); |
aoqi@0 | 181 | out.write('\n'); |
aoqi@0 | 182 | count = 1; // set the next line's length |
aoqi@0 | 183 | } |
aoqi@0 | 184 | out.write(c); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | /**** begin TEST program *** |
aoqi@0 | 189 | public static void main(String argv[]) throws Exception { |
aoqi@0 | 190 | FileInputStream infile = new FileInputStream(argv[0]); |
aoqi@0 | 191 | QPEncoderStream encoder = new QPEncoderStream(System.out); |
aoqi@0 | 192 | int c; |
aoqi@0 | 193 | |
aoqi@0 | 194 | while ((c = infile.read()) != -1) |
aoqi@0 | 195 | encoder.write(c); |
aoqi@0 | 196 | encoder.close(); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | *** end TEST program ***/ |
aoqi@0 | 199 | } |