[6785] | 1 | /**************************************************************** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
| 3 | * or more contributor license agreements. See the NOTICE file * |
---|
| 4 | * distributed with this work for additional information * |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file * |
---|
| 6 | * to you under the Apache License, Version 2.0 (the * |
---|
| 7 | * "License"); you may not use this file except in compliance * |
---|
| 8 | * with the License. You may obtain a copy of the License at * |
---|
| 9 | * * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
| 11 | * * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, * |
---|
| 13 | * software distributed under the License is distributed on an * |
---|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
| 15 | * KIND, either express or implied. See the License for the * |
---|
| 16 | * specific language governing permissions and limitations * |
---|
| 17 | * under the License. * |
---|
| 18 | ****************************************************************/ |
---|
| 19 | |
---|
| 20 | package org.apache.james.mime4j.codec; |
---|
| 21 | |
---|
| 22 | import java.io.FilterOutputStream; |
---|
| 23 | import java.io.IOException; |
---|
| 24 | import java.io.OutputStream; |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Performs Quoted-Printable encoding on an underlying stream. |
---|
| 28 | */ |
---|
| 29 | public class QuotedPrintableOutputStream extends FilterOutputStream { |
---|
| 30 | |
---|
| 31 | private static final int DEFAULT_BUFFER_SIZE = 1024 * 3; |
---|
| 32 | |
---|
| 33 | private static final byte TB = 0x09; |
---|
| 34 | private static final byte SP = 0x20; |
---|
| 35 | private static final byte EQ = 0x3D; |
---|
| 36 | private static final byte CR = 0x0D; |
---|
| 37 | private static final byte LF = 0x0A; |
---|
| 38 | private static final byte QUOTED_PRINTABLE_LAST_PLAIN = 0x7E; |
---|
| 39 | private static final int QUOTED_PRINTABLE_MAX_LINE_LENGTH = 76; |
---|
| 40 | private static final int QUOTED_PRINTABLE_OCTETS_PER_ESCAPE = 3; |
---|
| 41 | private static final byte[] HEX_DIGITS = { |
---|
| 42 | '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; |
---|
| 43 | |
---|
| 44 | private final byte[] outBuffer; |
---|
| 45 | private final boolean binary; |
---|
| 46 | |
---|
| 47 | private boolean pendingSpace; |
---|
| 48 | private boolean pendingTab; |
---|
| 49 | private boolean pendingCR; |
---|
| 50 | private int nextSoftBreak; |
---|
| 51 | private int outputIndex; |
---|
| 52 | |
---|
| 53 | private boolean closed = false; |
---|
| 54 | |
---|
| 55 | private byte[] singleByte = new byte[1]; |
---|
| 56 | |
---|
| 57 | public QuotedPrintableOutputStream(int bufsize, OutputStream out, boolean binary) { |
---|
| 58 | super(out); |
---|
| 59 | this.outBuffer = new byte[bufsize]; |
---|
| 60 | this.binary = binary; |
---|
| 61 | this.pendingSpace = false; |
---|
| 62 | this.pendingTab = false; |
---|
| 63 | this.pendingCR = false; |
---|
| 64 | this.outputIndex = 0; |
---|
| 65 | this.nextSoftBreak = QUOTED_PRINTABLE_MAX_LINE_LENGTH + 1; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public QuotedPrintableOutputStream(OutputStream out, boolean binary) { |
---|
| 69 | this(DEFAULT_BUFFER_SIZE, out, binary); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | private void encodeChunk(byte[] buffer, int off, int len) throws IOException { |
---|
| 73 | for (int inputIndex = off; inputIndex < len + off; inputIndex++) { |
---|
| 74 | encode(buffer[inputIndex]); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | private void completeEncoding() throws IOException { |
---|
| 79 | writePending(); |
---|
| 80 | flushOutput(); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | private void writePending() throws IOException { |
---|
| 84 | if (pendingSpace) { |
---|
| 85 | plain(SP); |
---|
| 86 | } else if (pendingTab) { |
---|
| 87 | plain(TB); |
---|
| 88 | } else if (pendingCR) { |
---|
| 89 | plain(CR); |
---|
| 90 | } |
---|
| 91 | clearPending(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | private void clearPending() throws IOException { |
---|
| 95 | pendingSpace = false; |
---|
| 96 | pendingTab = false; |
---|
| 97 | pendingCR = false; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | private void encode(byte next) throws IOException { |
---|
| 101 | if (next == LF) { |
---|
| 102 | if (binary) { |
---|
| 103 | writePending(); |
---|
| 104 | escape(next); |
---|
| 105 | } else { |
---|
| 106 | if (pendingCR) { |
---|
| 107 | // Expect either space or tab pending |
---|
| 108 | // but not both |
---|
| 109 | if (pendingSpace) { |
---|
| 110 | escape(SP); |
---|
| 111 | } else if (pendingTab) { |
---|
| 112 | escape(TB); |
---|
| 113 | } |
---|
| 114 | lineBreak(); |
---|
| 115 | clearPending(); |
---|
| 116 | } else { |
---|
| 117 | writePending(); |
---|
| 118 | plain(next); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } else if (next == CR) { |
---|
| 122 | if (binary) { |
---|
| 123 | escape(next); |
---|
| 124 | } else { |
---|
| 125 | pendingCR = true; |
---|
| 126 | } |
---|
| 127 | } else { |
---|
| 128 | writePending(); |
---|
| 129 | if (next == SP) { |
---|
| 130 | if (binary) { |
---|
| 131 | escape(next); |
---|
| 132 | } else { |
---|
| 133 | pendingSpace = true; |
---|
| 134 | } |
---|
| 135 | } else if (next == TB) { |
---|
| 136 | if (binary) { |
---|
| 137 | escape(next); |
---|
| 138 | } else { |
---|
| 139 | pendingTab = true; |
---|
| 140 | } |
---|
| 141 | } else if (next < SP) { |
---|
| 142 | escape(next); |
---|
| 143 | } else if (next > QUOTED_PRINTABLE_LAST_PLAIN) { |
---|
| 144 | escape(next); |
---|
| 145 | } else if (next == EQ) { |
---|
| 146 | escape(next); |
---|
| 147 | } else { |
---|
| 148 | plain(next); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | private void plain(byte next) throws IOException { |
---|
| 154 | if (--nextSoftBreak <= 1) { |
---|
| 155 | softBreak(); |
---|
| 156 | } |
---|
| 157 | write(next); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | private void escape(byte next) throws IOException { |
---|
| 161 | if (--nextSoftBreak <= QUOTED_PRINTABLE_OCTETS_PER_ESCAPE) { |
---|
| 162 | softBreak(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | int nextUnsigned = next & 0xff; |
---|
| 166 | |
---|
| 167 | write(EQ); |
---|
| 168 | --nextSoftBreak; |
---|
| 169 | write(HEX_DIGITS[nextUnsigned >> 4]); |
---|
| 170 | --nextSoftBreak; |
---|
| 171 | write(HEX_DIGITS[nextUnsigned % 0x10]); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | private void write(byte next) throws IOException { |
---|
| 175 | outBuffer[outputIndex++] = next; |
---|
| 176 | if (outputIndex >= outBuffer.length) { |
---|
| 177 | flushOutput(); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | private void softBreak() throws IOException { |
---|
| 182 | write(EQ); |
---|
| 183 | lineBreak(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | private void lineBreak() throws IOException { |
---|
| 187 | write(CR); |
---|
| 188 | write(LF); |
---|
| 189 | nextSoftBreak = QUOTED_PRINTABLE_MAX_LINE_LENGTH; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void flushOutput() throws IOException { |
---|
| 193 | if (outputIndex < outBuffer.length) { |
---|
| 194 | out.write(outBuffer, 0, outputIndex); |
---|
| 195 | } else { |
---|
| 196 | out.write(outBuffer); |
---|
| 197 | } |
---|
| 198 | outputIndex = 0; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | @Override |
---|
| 202 | public void close() throws IOException { |
---|
| 203 | if (closed) |
---|
| 204 | return; |
---|
| 205 | |
---|
| 206 | try { |
---|
| 207 | completeEncoding(); |
---|
| 208 | // do not close the wrapped stream |
---|
| 209 | } finally { |
---|
| 210 | closed = true; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | @Override |
---|
| 215 | public void flush() throws IOException { |
---|
| 216 | flushOutput(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | @Override |
---|
| 220 | public void write(int b) throws IOException { |
---|
| 221 | singleByte[0] = (byte) b; |
---|
| 222 | this.write(singleByte, 0, 1); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | @Override |
---|
| 226 | public void write(byte[] b, int off, int len) throws IOException { |
---|
| 227 | if (closed) { |
---|
| 228 | throw new IOException("Stream has been closed"); |
---|
| 229 | } |
---|
| 230 | encodeChunk(b, off, len); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | } |
---|