You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jl...@apache.org on 2022/05/03 12:22:12 UTC
svn commit: r1900504 [16/22] - in /geronimo/specs/trunk: ./ geronimo-activation_2.0_spec/ geronimo-activation_2.0_spec/src/ geronimo-activation_2.0_spec/src/main/ geronimo-activation_2.0_spec/src/main/java/ geronimo-activation_2.0_spec/src/main/java/ja...
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64Encoder.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64Encoder.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64Encoder.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64Encoder.java Tue May 3 12:22:08 2022
@@ -0,0 +1,654 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+public class Base64Encoder
+ implements Encoder
+{
+ protected final byte[] encodingTable =
+ {
+ (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
+ (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
+ (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
+ (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
+ (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
+ (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
+ (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
+ (byte)'v',
+ (byte)'w', (byte)'x', (byte)'y', (byte)'z',
+ (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6',
+ (byte)'7', (byte)'8', (byte)'9',
+ (byte)'+', (byte)'/'
+ };
+
+ protected byte padding = (byte)'=';
+
+ /*
+ * set up the decoding table.
+ */
+ protected final byte[] decodingTable = new byte[256];
+
+ protected void initialiseDecodingTable()
+ {
+ for (int i = 0; i < encodingTable.length; i++)
+ {
+ decodingTable[encodingTable[i]] = (byte)i;
+ }
+ }
+
+ public Base64Encoder()
+ {
+ initialiseDecodingTable();
+ }
+
+ /**
+ * encode the input data producing a base 64 output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(
+ final byte[] data,
+ final int off,
+ final int length,
+ final OutputStream out)
+ throws IOException
+ {
+ final int modulus = length % 3;
+ final int dataLength = (length - modulus);
+ int a1, a2, a3;
+
+ for (int i = off; i < off + dataLength; i += 3)
+ {
+ a1 = data[i] & 0xff;
+ a2 = data[i + 1] & 0xff;
+ a3 = data[i + 2] & 0xff;
+
+ out.write(encodingTable[(a1 >>> 2) & 0x3f]);
+ out.write(encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]);
+ out.write(encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]);
+ out.write(encodingTable[a3 & 0x3f]);
+ }
+
+ /*
+ * process the tail end.
+ */
+ int b1, b2, b3;
+ int d1, d2;
+
+ switch (modulus)
+ {
+ case 0: /* nothing left to do */
+ break;
+ case 1:
+ d1 = data[off + dataLength] & 0xff;
+ b1 = (d1 >>> 2) & 0x3f;
+ b2 = (d1 << 4) & 0x3f;
+
+ out.write(encodingTable[b1]);
+ out.write(encodingTable[b2]);
+ out.write(padding);
+ out.write(padding);
+ break;
+ case 2:
+ d1 = data[off + dataLength] & 0xff;
+ d2 = data[off + dataLength + 1] & 0xff;
+
+ b1 = (d1 >>> 2) & 0x3f;
+ b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
+ b3 = (d2 << 2) & 0x3f;
+
+ out.write(encodingTable[b1]);
+ out.write(encodingTable[b2]);
+ out.write(encodingTable[b3]);
+ out.write(padding);
+ break;
+ }
+
+ return (dataLength / 3) * 4 + ((modulus == 0) ? 0 : 4);
+ }
+
+ private boolean ignore(
+ final char c)
+ {
+ return (c == '\n' || c =='\r' || c == '\t' || c == ' ');
+ }
+
+ /**
+ * decode the base 64 encoded byte data writing it to the given output stream,
+ * whitespace characters will be ignored.
+ *
+ * @return the number of bytes produced.
+ */
+ public int decode(
+ final byte[] data,
+ final int off,
+ final int length,
+ final OutputStream out)
+ throws IOException
+ {
+ byte b1, b2, b3, b4;
+ int outLen = 0;
+
+ int end = off + length;
+
+ while (end > 0)
+ {
+ if (!ignore((char)data[end - 1]))
+ {
+ break;
+ }
+
+ end--;
+ }
+
+ int i = off;
+ final int finish = end - 4;
+
+ while (i < finish)
+ {
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b1 = decodingTable[data[i++]];
+
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b2 = decodingTable[data[i++]];
+
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b3 = decodingTable[data[i++]];
+
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b4 = decodingTable[data[i++]];
+
+ out.write((b1 << 2) | (b2 >> 4));
+ out.write((b2 << 4) | (b3 >> 2));
+ out.write((b3 << 6) | b4);
+
+ outLen += 3;
+ }
+
+ if (data[end - 2] == padding)
+ {
+ b1 = decodingTable[data[end - 4]];
+ b2 = decodingTable[data[end - 3]];
+
+ out.write((b1 << 2) | (b2 >> 4));
+
+ outLen += 1;
+ }
+ else if (data[end - 1] == padding)
+ {
+ b1 = decodingTable[data[end - 4]];
+ b2 = decodingTable[data[end - 3]];
+ b3 = decodingTable[data[end - 2]];
+
+ out.write((b1 << 2) | (b2 >> 4));
+ out.write((b2 << 4) | (b3 >> 2));
+
+ outLen += 2;
+ }
+ else
+ {
+ b1 = decodingTable[data[end - 4]];
+ b2 = decodingTable[data[end - 3]];
+ b3 = decodingTable[data[end - 2]];
+ b4 = decodingTable[data[end - 1]];
+
+ out.write((b1 << 2) | (b2 >> 4));
+ out.write((b2 << 4) | (b3 >> 2));
+ out.write((b3 << 6) | b4);
+
+ outLen += 3;
+ }
+
+ return outLen;
+ }
+
+ /**
+ * decode the base 64 encoded String data writing it to the given output stream,
+ * whitespace characters will be ignored.
+ *
+ * @return the number of bytes produced.
+ */
+ public int decode(
+ final String data,
+ final OutputStream out)
+ throws IOException
+ {
+ byte b1, b2, b3, b4;
+ int length = 0;
+
+ int end = data.length();
+
+ while (end > 0)
+ {
+ if (!ignore(data.charAt(end - 1)))
+ {
+ break;
+ }
+
+ end--;
+ }
+
+ int i = 0;
+ final int finish = end - 4;
+
+ while (i < finish)
+ {
+ while ((i < finish) && ignore(data.charAt(i)))
+ {
+ i++;
+ }
+
+ b1 = decodingTable[data.charAt(i++)];
+
+ while ((i < finish) && ignore(data.charAt(i)))
+ {
+ i++;
+ }
+ b2 = decodingTable[data.charAt(i++)];
+
+ while ((i < finish) && ignore(data.charAt(i)))
+ {
+ i++;
+ }
+ b3 = decodingTable[data.charAt(i++)];
+
+ while ((i < finish) && ignore(data.charAt(i)))
+ {
+ i++;
+ }
+ b4 = decodingTable[data.charAt(i++)];
+
+ out.write((b1 << 2) | (b2 >> 4));
+ out.write((b2 << 4) | (b3 >> 2));
+ out.write((b3 << 6) | b4);
+
+ length += 3;
+ }
+
+ if (data.charAt(end - 2) == padding)
+ {
+ b1 = decodingTable[data.charAt(end - 4)];
+ b2 = decodingTable[data.charAt(end - 3)];
+
+ out.write((b1 << 2) | (b2 >> 4));
+
+ length += 1;
+ }
+ else if (data.charAt(end - 1) == padding)
+ {
+ b1 = decodingTable[data.charAt(end - 4)];
+ b2 = decodingTable[data.charAt(end - 3)];
+ b3 = decodingTable[data.charAt(end - 2)];
+
+ out.write((b1 << 2) | (b2 >> 4));
+ out.write((b2 << 4) | (b3 >> 2));
+
+ length += 2;
+ }
+ else
+ {
+ b1 = decodingTable[data.charAt(end - 4)];
+ b2 = decodingTable[data.charAt(end - 3)];
+ b3 = decodingTable[data.charAt(end - 2)];
+ b4 = decodingTable[data.charAt(end - 1)];
+
+ out.write((b1 << 2) | (b2 >> 4));
+ out.write((b2 << 4) | (b3 >> 2));
+ out.write((b3 << 6) | b4);
+
+ length += 3;
+ }
+
+ return length;
+ }
+
+ /**
+ * decode the base 64 encoded byte data writing it to the provided byte array buffer.
+ *
+ * @return the number of bytes produced.
+ */
+ public int decode(final byte[] data, final int off, final int length, final byte[] out) throws IOException
+ {
+ byte b1, b2, b3, b4;
+ int outLen = 0;
+
+ int end = off + length;
+
+ while (end > 0)
+ {
+ if (!ignore((char)data[end - 1]))
+ {
+ break;
+ }
+
+ end--;
+ }
+
+ int i = off;
+ final int finish = end - 4;
+
+ while (i < finish)
+ {
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b1 = decodingTable[data[i++]];
+
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b2 = decodingTable[data[i++]];
+
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b3 = decodingTable[data[i++]];
+
+ while ((i < finish) && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b4 = decodingTable[data[i++]];
+
+ out[outLen++] = (byte)((b1 << 2) | (b2 >> 4));
+ out[outLen++] = (byte)((b2 << 4) | (b3 >> 2));
+ out[outLen++] = (byte)((b3 << 6) | b4);
+ }
+
+ if (data[end - 2] == padding)
+ {
+ b1 = decodingTable[data[end - 4]];
+ b2 = decodingTable[data[end - 3]];
+
+ out[outLen++] = (byte)((b1 << 2) | (b2 >> 4));
+ }
+ else if (data[end - 1] == padding)
+ {
+ b1 = decodingTable[data[end - 4]];
+ b2 = decodingTable[data[end - 3]];
+ b3 = decodingTable[data[end - 2]];
+
+ out[outLen++] = (byte)((b1 << 2) | (b2 >> 4));
+ out[outLen++] = (byte)((b2 << 4) | (b3 >> 2));
+ }
+ else
+ {
+ b1 = decodingTable[data[end - 4]];
+ b2 = decodingTable[data[end - 3]];
+ b3 = decodingTable[data[end - 2]];
+ b4 = decodingTable[data[end - 1]];
+
+ out[outLen++] = (byte)((b1 << 2) | (b2 >> 4));
+ out[outLen++] = (byte)((b2 << 4) | (b3 >> 2));
+ out[outLen++] = (byte)((b3 << 6) | b4);
+ }
+
+ return outLen;
+ }
+
+ /**
+ * Test if a character is a valid Base64 encoding character. This
+ * must be either a valid digit or the padding character ("=").
+ *
+ * @param ch The test character.
+ *
+ * @return true if this is valid in Base64 encoded data, false otherwise.
+ */
+ public boolean isValidBase64(final int ch) {
+ // 'A' has the value 0 in the decoding table, so we need a special one for that
+ return ch == padding || ch == 'A' || decodingTable[ch] != 0;
+ }
+
+
+ /**
+ * Perform RFC-2047 word encoding using Base64 data encoding.
+ *
+ * @param in The source for the encoded data.
+ * @param charset The charset tag to be added to each encoded data section.
+ * @param out The output stream where the encoded data is to be written.
+ * @param fold Controls whether separate sections of encoded data are separated by
+ * linebreaks or whitespace.
+ *
+ * @exception IOException
+ */
+ public void encodeWord(final InputStream in, final String charset, final OutputStream out, final boolean fold) throws IOException
+ {
+ final PrintStream writer = new PrintStream(out);
+
+ // encoded words are restricted to 76 bytes, including the control adornments.
+ final int limit = 75 - 7 - charset.length();
+ boolean firstLine = true;
+ final StringBuffer encodedString = new StringBuffer(76);
+
+ while (true) {
+ // encode the next segment.
+ encode(in, encodedString, limit);
+ // if we're out of data, nothing will be encoded.
+ if (encodedString.length() == 0) {
+ break;
+ }
+
+ // if we have more than one segment, we need to insert separators. Depending on whether folding
+ // was requested, this is either a blank or a linebreak.
+ if (!firstLine) {
+ if (fold) {
+ writer.print("\r\n");
+ }
+ else {
+ writer.print(" ");
+ }
+ }
+
+ // add the encoded word header
+ writer.print("=?");
+ writer.print(charset);
+ writer.print("?B?");
+ // the data
+ writer.print(encodedString.toString());
+ // and the word terminator.
+ writer.print("?=");
+ writer.flush();
+
+ // reset our string buffer for the next segment.
+ encodedString.setLength(0);
+ // we need a delimiter after this
+ firstLine = false;
+ }
+ }
+
+
+ /**
+ * Perform RFC-2047 word encoding using Base64 data encoding.
+ *
+ * @param in The source for the encoded data.
+ * @param charset The charset tag to be added to each encoded data section.
+ * @param out The output stream where the encoded data is to be written.
+ * @param fold Controls whether separate sections of encoded data are separated by
+ * linebreaks or whitespace.
+ *
+ * @exception IOException
+ */
+ public void encodeWord(final byte[] data, final StringBuffer out, final String charset) throws IOException
+ {
+ // append the word header
+ out.append("=?");
+ out.append(charset);
+ out.append("?B?");
+ // add on the encodeded data
+ encodeWordData(data, out);
+ // the end of the encoding marker
+ out.append("?=");
+ }
+
+ /**
+ * encode the input data producing a base 64 output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public void encodeWordData(final byte[] data, final StringBuffer out)
+ {
+ final int modulus = data.length % 3;
+ final int dataLength = (data.length - modulus);
+ int a1, a2, a3;
+
+ for (int i = 0; i < dataLength; i += 3)
+ {
+ a1 = data[i] & 0xff;
+ a2 = data[i + 1] & 0xff;
+ a3 = data[i + 2] & 0xff;
+
+ out.append((char)encodingTable[(a1 >>> 2) & 0x3f]);
+ out.append((char)encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]);
+ out.append((char)encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]);
+ out.append((char)encodingTable[a3 & 0x3f]);
+ }
+
+ /*
+ * process the tail end.
+ */
+ int b1, b2, b3;
+ int d1, d2;
+
+ switch (modulus)
+ {
+ case 0: /* nothing left to do */
+ break;
+ case 1:
+ d1 = data[dataLength] & 0xff;
+ b1 = (d1 >>> 2) & 0x3f;
+ b2 = (d1 << 4) & 0x3f;
+
+ out.append((char)encodingTable[b1]);
+ out.append((char)encodingTable[b2]);
+ out.append((char)padding);
+ out.append((char)padding);
+ break;
+ case 2:
+ d1 = data[dataLength] & 0xff;
+ d2 = data[dataLength + 1] & 0xff;
+
+ b1 = (d1 >>> 2) & 0x3f;
+ b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
+ b3 = (d2 << 2) & 0x3f;
+
+ out.append((char)encodingTable[b1]);
+ out.append((char)encodingTable[b2]);
+ out.append((char)encodingTable[b3]);
+ out.append((char)padding);
+ break;
+ }
+ }
+
+
+ /**
+ * encode the input data producing a base 64 output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public void encode(final InputStream in, final StringBuffer out, final int limit) throws IOException
+ {
+ int count = limit / 4;
+ final byte [] inBuffer = new byte[3];
+
+ while (count-- > 0) {
+
+ final int readCount = in.read(inBuffer);
+ // did we get a full triplet? that's an easy encoding.
+ if (readCount == 3) {
+ final int a1 = inBuffer[0] & 0xff;
+ final int a2 = inBuffer[1] & 0xff;
+ final int a3 = inBuffer[2] & 0xff;
+
+ out.append((char)encodingTable[(a1 >>> 2) & 0x3f]);
+ out.append((char)encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]);
+ out.append((char)encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]);
+ out.append((char)encodingTable[a3 & 0x3f]);
+
+ }
+ else if (readCount <= 0) {
+ // eof condition, don'e entirely.
+ return;
+ }
+ else if (readCount == 1) {
+ final int a1 = inBuffer[0] & 0xff;
+ out.append((char)encodingTable[(a1 >>> 2) & 0x3f]);
+ out.append((char)encodingTable[(a1 << 4) & 0x3f]);
+ out.append((char)padding);
+ out.append((char)padding);
+ return;
+ }
+ else if (readCount == 2) {
+ final int a1 = inBuffer[0] & 0xff;
+ final int a2 = inBuffer[1] & 0xff;
+
+ out.append((char)encodingTable[(a1 >>> 2) & 0x3f]);
+ out.append((char)encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]);
+ out.append((char)encodingTable[(a2 << 2) & 0x3f]);
+ out.append((char)padding);
+ return;
+ }
+ }
+ }
+
+
+ /**
+ * Estimate the final encoded size of a segment of data.
+ * This is used to ensure that the encoded blocks do
+ * not get split across a unicode character boundary and
+ * that the encoding will fit within the bounds of
+ * a mail header line.
+ *
+ * @param data The data we're anticipating encoding.
+ *
+ * @return The size of the byte data in encoded form.
+ */
+ public int estimateEncodedLength(final byte[] data)
+ {
+ return ((data.length + 2) / 3) * 4;
+ }
+}
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64EncoderStream.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64EncoderStream.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64EncoderStream.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Base64EncoderStream.java Tue May 3 12:22:08 2022
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * An implementation of a FilterOutputStream that encodes the
+ * stream data in BASE64 encoding format. This version does the
+ * encoding "on the fly" rather than encoding a single block of
+ * data. Since this version is intended for use by the MimeUtilty class,
+ * it also handles line breaks in the encoded data.
+ */
+public class Base64EncoderStream extends FilterOutputStream {
+
+ // our Filtered stream writes everything out as byte data. This allows the CRLF sequence to
+ // be written with a single call.
+ protected static final byte[] CRLF = { '\r', '\n' };
+
+ // our hex encoder utility class.
+ protected Base64Encoder encoder = new Base64Encoder();
+
+ // our default for line breaks
+ protected static final int DEFAULT_LINEBREAK = 76;
+
+ // Data can only be written out in complete units of 3 bytes encoded as 4. Therefore, we need to buffer
+ // as many as 2 bytes to fill out an encoding unit.
+
+ // the buffered byte count
+ protected int bufferedBytes = 0;
+
+ // we'll encode this part once it is filled up.
+ protected byte[] buffer = new byte[3];
+
+
+ // the size we process line breaks at. If this is Integer.MAX_VALUE, no line breaks are handled.
+ protected int lineBreak;
+
+ // the number of encoded characters we've written to the stream, which determines where we
+ // insert line breaks.
+ protected int outputCount;
+
+ /**
+ * Create a Base64 encoder stream that wraps a specifed stream
+ * using the default line break size.
+ *
+ * @param out The wrapped output stream.
+ */
+ public Base64EncoderStream(final OutputStream out) {
+ this(out, DEFAULT_LINEBREAK);
+ }
+
+
+ public Base64EncoderStream(final OutputStream out, final int lineBreak) {
+ super(out);
+ // lines are processed only in multiple of 4, so round this down.
+ this.lineBreak = (lineBreak / 4) * 4 ;
+ }
+
+ // in order for this to work, we need to override the 3 different signatures for write
+
+ @Override
+ public void write(final int ch) throws IOException {
+ // store this in the buffer.
+ buffer[bufferedBytes++] = (byte)ch;
+ // if the buffer is filled, encode these bytes
+ if (bufferedBytes == 3) {
+ // check for room in the current line for this character
+ checkEOL(4);
+ // write these directly to the stream.
+ encoder.encode(buffer, 0, 3, out);
+ bufferedBytes = 0;
+ // and update the line length checkers
+ updateLineCount(4);
+ }
+ }
+
+ @Override
+ public void write(final byte [] data) throws IOException {
+ write(data, 0, data.length);
+ }
+
+ @Override
+ public void write(final byte [] data, int offset, int length) throws IOException {
+ // if we have something in the buffer, we need to write enough bytes out to flush
+ // those into the output stream AND continue on to finish off a line. Once we're done there
+ // we can write additional data out in complete blocks.
+ while ((bufferedBytes > 0 || outputCount > 0) && length > 0) {
+ write(data[offset++]);
+ length--;
+ }
+
+ if (length > 0) {
+ // no linebreaks requested? YES!!!!!, we can just dispose of the lot with one call.
+ if (lineBreak == Integer.MAX_VALUE) {
+ encoder.encode(data, offset, length, out);
+ }
+ else {
+ // calculate the size of a segment we can encode directly as a line.
+ final int segmentSize = (lineBreak / 4) * 3;
+
+ // write this out a block at a time, with separators between.
+ while (length > segmentSize) {
+ // encode a segment
+ encoder.encode(data, offset, segmentSize, out);
+ // write an EOL marker
+ out.write(CRLF);
+ offset += segmentSize;
+ length -= segmentSize;
+ }
+
+ // any remainder we write out a byte at a time to manage the groupings and
+ // the line count appropriately.
+ if (length > 0) {
+ while (length > 0) {
+ write(data[offset++]);
+ length--;
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ flush();
+ out.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ if (bufferedBytes > 0) {
+ encoder.encode(buffer, 0, bufferedBytes, out);
+ bufferedBytes = 0;
+ }
+ }
+
+
+ /**
+ * Check for whether we're about the reach the end of our
+ * line limit for an update that's about to occur. If we will
+ * overflow, then a line break is inserted.
+ *
+ * @param required The space required for this pending write.
+ *
+ * @exception IOException
+ */
+ private void checkEOL(final int required) throws IOException {
+ if (lineBreak != Integer.MAX_VALUE) {
+ // if this write would exceed the line maximum, add a linebreak to the stream.
+ if (outputCount + required > lineBreak) {
+ out.write(CRLF);
+ outputCount = 0;
+ }
+ }
+ }
+
+ /**
+ * Update the counter of characters on the current working line.
+ * This is conditional if we're not working with a line limit.
+ *
+ * @param added The number of characters just added.
+ */
+ private void updateLineCount(final int added) {
+ if (lineBreak != Integer.MAX_VALUE) {
+ outputCount += added;
+ }
+ }
+}
+
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Encoder.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Encoder.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Encoder.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Encoder.java Tue May 3 12:22:08 2022
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * Encode and decode byte arrays (typically from binary to 7-bit ASCII
+ * encodings).
+ */
+public interface Encoder
+{
+ int encode(byte[] data, int off, int length, OutputStream out) throws IOException;
+
+ int decode(byte[] data, int off, int length, OutputStream out) throws IOException;
+
+ int decode(String data, OutputStream out) throws IOException;
+}
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Hex.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Hex.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Hex.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/Hex.java Tue May 3 12:22:08 2022
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class Hex
+{
+ private static final Encoder encoder = new HexEncoder();
+
+ /**
+ * encode the input data producing a Hex encoded byte array.
+ *
+ * @return a byte array containing the Hex encoded data.
+ */
+ public static byte[] encode(
+ final byte[] data)
+ {
+ return encode(data, 0, data.length);
+ }
+
+ /**
+ * encode the input data producing a Hex encoded byte array.
+ *
+ * @return a byte array containing the Hex encoded data.
+ */
+ public static byte[] encode(
+ final byte[] data,
+ final int off,
+ final int length)
+ {
+ final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ try
+ {
+ encoder.encode(data, off, length, bOut);
+ }
+ catch (final IOException e)
+ {
+ throw new RuntimeException("exception encoding Hex string: " + e);
+ }
+
+ return bOut.toByteArray();
+ }
+
+ /**
+ * Hex encode the byte data writing it to the given output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public static int encode(
+ final byte[] data,
+ final OutputStream out)
+ throws IOException
+ {
+ return encoder.encode(data, 0, data.length, out);
+ }
+
+ /**
+ * Hex encode the byte data writing it to the given output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public static int encode(
+ final byte[] data,
+ final int off,
+ final int length,
+ final OutputStream out)
+ throws IOException
+ {
+ return encoder.encode(data, 0, data.length, out);
+ }
+
+ /**
+ * decode the Hex encoded input data. It is assumed the input data is valid.
+ *
+ * @return a byte array representing the decoded data.
+ */
+ public static byte[] decode(
+ final byte[] data)
+ {
+ final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ try
+ {
+ encoder.decode(data, 0, data.length, bOut);
+ }
+ catch (final IOException e)
+ {
+ throw new RuntimeException("exception decoding Hex string: " + e);
+ }
+
+ return bOut.toByteArray();
+ }
+
+ /**
+ * decode the Hex encoded String data - whitespace will be ignored.
+ *
+ * @return a byte array representing the decoded data.
+ */
+ public static byte[] decode(
+ final String data)
+ {
+ final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ try
+ {
+ encoder.decode(data, bOut);
+ }
+ catch (final IOException e)
+ {
+ throw new RuntimeException("exception decoding Hex string: " + e);
+ }
+
+ return bOut.toByteArray();
+ }
+
+ /**
+ * decode the Hex encoded String data writing it to the given output stream,
+ * whitespace characters will be ignored.
+ *
+ * @return the number of bytes produced.
+ */
+ public static int decode(
+ final String data,
+ final OutputStream out)
+ throws IOException
+ {
+ return encoder.decode(data, out);
+ }
+}
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/HexEncoder.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/HexEncoder.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/HexEncoder.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/HexEncoder.java Tue May 3 12:22:08 2022
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class HexEncoder
+ implements Encoder
+{
+ protected final byte[] encodingTable =
+ {
+ (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
+ (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
+ };
+
+ /*
+ * set up the decoding table.
+ */
+ protected final byte[] decodingTable = new byte[128];
+
+ protected void initialiseDecodingTable()
+ {
+ for (int i = 0; i < encodingTable.length; i++)
+ {
+ decodingTable[encodingTable[i]] = (byte)i;
+ }
+
+ decodingTable['A'] = decodingTable['a'];
+ decodingTable['B'] = decodingTable['b'];
+ decodingTable['C'] = decodingTable['c'];
+ decodingTable['D'] = decodingTable['d'];
+ decodingTable['E'] = decodingTable['e'];
+ decodingTable['F'] = decodingTable['f'];
+ }
+
+ public HexEncoder()
+ {
+ initialiseDecodingTable();
+ }
+
+ /**
+ * encode the input data producing a Hex output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(
+ final byte[] data,
+ final int off,
+ final int length,
+ final OutputStream out)
+ throws IOException
+ {
+ for (int i = off; i < (off + length); i++)
+ {
+ final int v = data[i] & 0xff;
+
+ out.write(encodingTable[(v >>> 4)]);
+ out.write(encodingTable[v & 0xf]);
+ }
+
+ return length * 2;
+ }
+
+ private boolean ignore(
+ final char c)
+ {
+ return (c == '\n' || c =='\r' || c == '\t' || c == ' ');
+ }
+
+ /**
+ * decode the Hex encoded byte data writing it to the given output stream,
+ * whitespace characters will be ignored.
+ *
+ * @return the number of bytes produced.
+ */
+ public int decode(
+ final byte[] data,
+ final int off,
+ final int length,
+ final OutputStream out)
+ throws IOException
+ {
+ byte b1, b2;
+ int outLen = 0;
+
+ int end = off + length;
+
+ while (end > 0)
+ {
+ if (!ignore((char)data[end - 1]))
+ {
+ break;
+ }
+
+ end--;
+ }
+
+ int i = off;
+ while (i < end)
+ {
+ while (i < end && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b1 = decodingTable[data[i++]];
+
+ while (i < end && ignore((char)data[i]))
+ {
+ i++;
+ }
+
+ b2 = decodingTable[data[i++]];
+
+ out.write((b1 << 4) | b2);
+
+ outLen++;
+ }
+
+ return outLen;
+ }
+
+ /**
+ * decode the Hex encoded String data writing it to the given output stream,
+ * whitespace characters will be ignored.
+ *
+ * @return the number of bytes produced.
+ */
+ public int decode(
+ final String data,
+ final OutputStream out)
+ throws IOException
+ {
+ byte b1, b2;
+ int length = 0;
+
+ int end = data.length();
+
+ while (end > 0)
+ {
+ if (!ignore(data.charAt(end - 1)))
+ {
+ break;
+ }
+
+ end--;
+ }
+
+ int i = 0;
+ while (i < end)
+ {
+ while (i < end && ignore(data.charAt(i)))
+ {
+ i++;
+ }
+
+ b1 = decodingTable[data.charAt(i++)];
+
+ while (i < end && ignore(data.charAt(i)))
+ {
+ i++;
+ }
+
+ b2 = decodingTable[data.charAt(i++)];
+
+ out.write((b1 << 4) | b2);
+
+ length++;
+ }
+
+ return length;
+ }
+}
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintable.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintable.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintable.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintable.java Tue May 3 12:22:08 2022
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class QuotedPrintable {
+ // NOTE: the QuotedPrintableEncoder class needs to keep some active state about what's going on with
+ // respect to line breaks and significant white spaces. This makes it difficult to keep one static
+ // instance of the decode around for reuse.
+
+
+ /**
+ * encode the input data producing a Q-P encoded byte array.
+ *
+ * @return a byte array containing the Q-P encoded data.
+ */
+ public static byte[] encode(
+ final byte[] data)
+ {
+ return encode(data, 0, data.length);
+ }
+
+ /**
+ * encode the input data producing a Q-P encoded byte array.
+ *
+ * @return a byte array containing the Q-P encoded data.
+ */
+ public static byte[] encode(
+ final byte[] data,
+ final int off,
+ final int length)
+ {
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+
+ final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ try
+ {
+ encoder.encode(data, off, length, bOut);
+ }
+ catch (final IOException e)
+ {
+ throw new RuntimeException("exception encoding Q-P encoded string: " + e);
+ }
+
+ return bOut.toByteArray();
+ }
+
+ /**
+ * Q-P encode the byte data writing it to the given output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public static int encode(
+ final byte[] data,
+ final OutputStream out)
+ throws IOException
+ {
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+
+ return encoder.encode(data, 0, data.length, out);
+ }
+
+ /**
+ * Q-P encode the byte data writing it to the given output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public static int encode(
+ final byte[] data,
+ final int off,
+ final int length,
+ final OutputStream out)
+ throws IOException
+ {
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+ return encoder.encode(data, 0, data.length, out);
+ }
+
+ /**
+ * decode the Q-P encoded input data. It is assumed the input data is valid.
+ *
+ * @return a byte array representing the decoded data.
+ */
+ public static byte[] decode(
+ final byte[] data)
+ {
+ final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+ try
+ {
+ encoder.decode(data, 0, data.length, bOut);
+ }
+ catch (final IOException e)
+ {
+ throw new RuntimeException("exception decoding Q-P encoded string: " + e);
+ }
+
+ return bOut.toByteArray();
+ }
+
+ /**
+ * decode the UUEncided String data.
+ *
+ * @return a byte array representing the decoded data.
+ */
+ public static byte[] decode(
+ final String data)
+ {
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+ final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ try
+ {
+ encoder.decode(data, bOut);
+ }
+ catch (final IOException e)
+ {
+ throw new RuntimeException("exception decoding Q-P encoded string: " + e);
+ }
+
+ return bOut.toByteArray();
+ }
+
+ /**
+ * decode the Q-P encoded encoded String data writing it to the given output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public static int decode(
+ final String data,
+ final OutputStream out)
+ throws IOException
+ {
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+ return encoder.decode(data, out);
+ }
+
+ /**
+ * decode the base Q-P encoded String data writing it to the given output stream,
+ * whitespace characters will be ignored.
+ *
+ * @param data The array data to decode.
+ * @param out The output stream for the data.
+ *
+ * @return the number of bytes produced.
+ * @exception IOException
+ */
+ public static int decode(final byte [] data, final OutputStream out) throws IOException
+ {
+ final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
+ return encoder.decode(data, 0, data.length, out);
+ }
+}
+
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableDecoderStream.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableDecoderStream.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableDecoderStream.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableDecoderStream.java Tue May 3 12:22:08 2022
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * An implementation of a FilterOutputStream that decodes the
+ * stream data in Q-P encoding format. This version does the
+ * decoding "on the fly" rather than decoding a single block of
+ * data. Since this version is intended for use by the MimeUtilty class,
+ * it also handles line breaks in the encoded data.
+ */
+public class QuotedPrintableDecoderStream extends FilterInputStream {
+ // our decoder for processing the data
+ protected QuotedPrintableEncoder decoder;
+
+
+ /**
+ * Stream constructor.
+ *
+ * @param in The InputStream this stream is filtering.
+ */
+ public QuotedPrintableDecoderStream(final InputStream in) {
+ super(in);
+ decoder = new QuotedPrintableEncoder();
+ }
+
+ // in order to function as a filter, these streams need to override the different
+ // read() signatures.
+
+
+ /**
+ * Read a single byte from the stream.
+ *
+ * @return The next byte of the stream. Returns -1 for an EOF condition.
+ * @exception IOException
+ */
+ @Override
+ public int read() throws IOException
+ {
+ // just get a single byte from the decoder
+ return decoder.decode(in);
+ }
+
+
+ /**
+ * Read a buffer of data from the input stream.
+ *
+ * @param buffer The target byte array the data is placed into.
+ * @param offset The starting offset for the read data.
+ * @param length How much data is requested.
+ *
+ * @return The number of bytes of data read.
+ * @exception IOException
+ */
+ @Override
+ public int read(final byte [] buffer, final int offset, final int length) throws IOException {
+
+ for (int i = 0; i < length; i++) {
+ final int ch = decoder.decode(in);
+ if (ch == -1) {
+ return i == 0 ? -1 : i;
+ }
+ buffer[offset + i] = (byte)ch;
+ }
+
+ return length;
+ }
+
+
+ /**
+ * Indicate whether this stream supports the mark() operation.
+ *
+ * @return Always returns false.
+ */
+ @Override
+ public boolean markSupported() {
+ return false;
+ }
+
+
+ /**
+ * Give an estimate of how much additional data is available
+ * from this stream.
+ *
+ * @return Always returns -1.
+ * @exception IOException
+ */
+ @Override
+ public int available() throws IOException {
+ // this is almost impossible to determine at this point
+ return -1;
+ }
+}
+
+
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoder.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoder.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoder.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoder.java Tue May 3 12:22:08 2022
@@ -0,0 +1,776 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.PushbackInputStream;
+import java.io.UnsupportedEncodingException;
+
+public class QuotedPrintableEncoder implements Encoder {
+
+ static protected final byte[] encodingTable =
+ {
+ (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
+ (byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F'
+ };
+
+ /*
+ * set up the decoding table.
+ */
+ static protected final byte[] decodingTable = new byte[128];
+
+ static {
+ // initialize the decoding table
+ for (int i = 0; i < encodingTable.length; i++)
+ {
+ decodingTable[encodingTable[i]] = (byte)i;
+ }
+ }
+
+
+ // default number of characters we will write per line.
+ static private final int DEFAULT_CHARS_PER_LINE = 76;
+
+ // the output stream we're wrapped around
+ protected OutputStream out;
+ // the number of bytes written;
+ protected int bytesWritten = 0;
+ // number of bytes written on the current line
+ protected int lineCount = 0;
+ // line length we're dealing with
+ protected int lineLength;
+ // number of deferred whitespace characters in decode mode.
+ protected int deferredWhitespace = 0;
+
+ protected int cachedCharacter = -1;
+
+ // indicates whether the last character was a '\r', potentially part of a CRLF sequence.
+ protected boolean lastCR = false;
+ // remember whether last character was a white space.
+ protected boolean lastWhitespace = false;
+
+ public QuotedPrintableEncoder() {
+ this(null, DEFAULT_CHARS_PER_LINE);
+ }
+
+ public QuotedPrintableEncoder(final OutputStream out) {
+ this(out, DEFAULT_CHARS_PER_LINE);
+ }
+
+ public QuotedPrintableEncoder(final OutputStream out, final int lineLength) {
+ this.out = out;
+ this.lineLength = lineLength;
+ }
+
+ private void checkDeferred(final int ch) throws IOException {
+ // was the last character we looked at a whitespace? Try to decide what to do with it now.
+ if (lastWhitespace) {
+ // if this whitespace is at the end of the line, write it out encoded
+ if (ch == '\r' || ch == '\n') {
+ writeEncodedCharacter(' ');
+ }
+ else {
+ // we can write this out without encoding.
+ writeCharacter(' ');
+ }
+ // we always turn this off.
+ lastWhitespace = false;
+ }
+ // deferred carriage return?
+ else if (lastCR) {
+ // if the char following the CR was not a new line, write an EOL now.
+ if (ch != '\n') {
+ writeEOL();
+ }
+ // we always turn this off too
+ lastCR = false;
+ }
+ }
+
+
+ /**
+ * encode the input data producing a UUEncoded output stream.
+ *
+ * @param data The array of byte data.
+ * @param off The starting offset within the data.
+ * @param length Length of the data to encode.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(final byte[] data, int off, final int length) throws IOException {
+ final int endOffset = off + length;
+
+ while (off < endOffset) {
+ // get the character
+ final byte ch = data[off++];
+
+ // handle the encoding of this character.
+ encode(ch);
+ }
+
+ return bytesWritten;
+ }
+
+
+ public void encode(int ch) throws IOException {
+ // make sure this is just a single byte value.
+ ch = ch &0xFF;
+
+ // see if we had to defer handling of a whitespace or '\r' character, and handle it if necessary.
+ checkDeferred(ch);
+ // different characters require special handling.
+ switch (ch) {
+ // spaces require special handling. If the next character is a line terminator, then
+ // the space needs to be encoded.
+ case ' ':
+ {
+ // at this point, we don't know whether this needs encoding or not. If the next
+ // character is a linend, it gets encoded. If anything else, we just write it as is.
+ lastWhitespace = true;
+ // turn off any CR flags.
+ lastCR = false;
+ break;
+ }
+
+ // carriage return, which may be part of a CRLF sequence.
+ case '\r':
+ {
+ // just flag this until we see the next character.
+ lastCR = true;
+ break;
+ }
+
+ // a new line character...we need to check to see if it was paired up with a '\r' char.
+ case '\n':
+ {
+ // we always write this out for a newline. We defer CRs until we see if the LF follows.
+ writeEOL();
+ break;
+ }
+
+ // an '=' is the escape character for an encoded character, so it must also
+ // be written encoded.
+ case '=':
+ {
+ writeEncodedCharacter(ch);
+ break;
+ }
+
+ // all other characters. If outside the printable character range, write it encoded.
+ default:
+ {
+ if (ch < 32 || ch >= 127) {
+ writeEncodedCharacter(ch);
+ }
+ else {
+ writeCharacter(ch);
+ }
+ break;
+ }
+ }
+ }
+
+
+ /**
+ * encode the input data producing a UUEncoded output stream.
+ *
+ * @param data The array of byte data.
+ * @param off The starting offset within the data.
+ * @param length Length of the data to encode.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(final byte[] data, int off, final int length, final String specials) throws IOException {
+ final int endOffset = off + length;
+
+ while (off < endOffset) {
+ // get the character
+ final byte ch = data[off++];
+
+ // handle the encoding of this character.
+ encode(ch, specials);
+ }
+
+ return bytesWritten;
+ }
+
+
+ /**
+ * encode the input data producing a UUEncoded output stream.
+ *
+ * @param data The array of byte data.
+ * @param off The starting offset within the data.
+ * @param length Length of the data to encode.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(final PushbackInputStream in, final StringBuffer out, final String specials, final int limit) throws IOException {
+ int count = 0;
+
+ while (count < limit) {
+ int ch = in.read();
+
+ if (ch == -1) {
+ return count;
+ }
+ // make sure this is just a single byte value.
+ ch = ch &0xFF;
+
+ // spaces require special handling. If the next character is a line terminator, then
+ // the space needs to be encoded.
+ if (ch == ' ') {
+ // blanks get translated into underscores, because the encoded tokens can't have embedded blanks.
+ out.append('_');
+ count++;
+ }
+ // non-ascii chars and the designated specials all get encoded.
+ else if (ch < 32 || ch >= 127 || specials.indexOf(ch) != -1) {
+ // we need at least 3 characters to write this out, so we need to
+ // forget we saw this one and try in the next segment.
+ if (count + 3 > limit) {
+ in.unread(ch);
+ return count;
+ }
+ out.append('=');
+ out.append((char)encodingTable[ch >> 4]);
+ out.append((char)encodingTable[ch & 0x0F]);
+ count += 3;
+ }
+ else {
+ // good character, just use unchanged.
+ out.append((char)ch);
+ count++;
+ }
+ }
+ return count;
+ }
+
+
+ /**
+ * Specialized version of the decoder that handles encoding of
+ * RFC 2047 encoded word values. This has special handling for
+ * certain characters, but less special handling for blanks and
+ * linebreaks.
+ *
+ * @param ch
+ * @param specials
+ *
+ * @exception IOException
+ */
+ public void encode(int ch, final String specials) throws IOException {
+ // make sure this is just a single byte value.
+ ch = ch &0xFF;
+
+ // spaces require special handling. If the next character is a line terminator, then
+ // the space needs to be encoded.
+ if (ch == ' ') {
+ // blanks get translated into underscores, because the encoded tokens can't have embedded blanks.
+ writeCharacter('_');
+ }
+ // non-ascii chars and the designated specials all get encoded.
+ else if (ch < 32 || ch >= 127 || specials.indexOf(ch) != -1) {
+ writeEncodedCharacter(ch);
+ }
+ else {
+ // good character, just use unchanged.
+ writeCharacter(ch);
+ }
+ }
+
+
+ /**
+ * encode the input data producing a UUEncoded output stream.
+ *
+ * @param data The array of byte data.
+ * @param off The starting offset within the data.
+ * @param length Length of the data to encode.
+ * @param out The output stream the encoded data is written to.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(final byte[] data, final int off, final int length, final OutputStream out) throws IOException {
+ // make sure we're writing to the correct stream
+ this.out = out;
+ bytesWritten = 0;
+
+ // do the actual encoding
+ return encode(data, off, length);
+ }
+
+
+ /**
+ * decode the uuencoded byte data writing it to the given output stream
+ *
+ * @param data The array of byte data to decode.
+ * @param off Starting offset within the array.
+ * @param length The length of data to encode.
+ * @param out The output stream used to return the decoded data.
+ *
+ * @return the number of bytes produced.
+ * @exception IOException
+ */
+ public int decode(final byte[] data, int off, final int length, final OutputStream out) throws IOException {
+ // make sure we're writing to the correct stream
+ this.out = out;
+
+ final int endOffset = off + length;
+ int bytesWritten = 0;
+
+ while (off < endOffset) {
+ final byte ch = data[off++];
+
+ // space characters are a pain. We need to scan ahead until we find a non-space character.
+ // if the character is a line terminator, we need to discard the blanks.
+ if (ch == ' ') {
+ int trailingSpaces = 1;
+ // scan forward, counting the characters.
+ while (off < endOffset && data[off] == ' ') {
+ // step forward and count this.
+ off++;
+ trailingSpaces++;
+ }
+ // is this a lineend at the current location?
+ if (off >= endOffset || data[off] == '\r' || data[off] == '\n') {
+ // go to the next one
+ continue;
+ }
+ else {
+ // make sure we account for the spaces in the output count.
+ bytesWritten += trailingSpaces;
+ // write out the blank characters we counted and continue with the non-blank.
+ while (trailingSpaces-- > 0) {
+ out.write(' ');
+ }
+ }
+ }
+ else if (ch == '=') {
+ // we found an encoded character. Reduce the 3 char sequence to one.
+ // but first, make sure we have two characters to work with.
+ if (off + 1 >= endOffset) {
+ throw new IOException("Invalid quoted printable encoding");
+ }
+ // convert the two bytes back from hex.
+ byte b1 = data[off++];
+ byte b2 = data[off++];
+
+ // we've found an encoded carriage return. The next char needs to be a newline
+ if (b1 == '\r') {
+ if (b2 != '\n') {
+ throw new IOException("Invalid quoted printable encoding");
+ }
+ // this was a soft linebreak inserted by the encoding. We just toss this away
+ // on decode.
+ }
+ else {
+ // this is a hex pair we need to convert back to a single byte.
+ b1 = decodingTable[b1];
+ b2 = decodingTable[b2];
+ out.write((b1 << 4) | b2);
+ // 3 bytes in, one byte out
+ bytesWritten++;
+ }
+ }
+ else {
+ // simple character, just write it out.
+ out.write(ch);
+ bytesWritten++;
+ }
+ }
+
+ return bytesWritten;
+ }
+
+ /**
+ * Decode a byte array of data.
+ *
+ * @param data The data array.
+ * @param out The output stream target for the decoded data.
+ *
+ * @return The number of bytes written to the stream.
+ * @exception IOException
+ */
+ public int decodeWord(final byte[] data, final OutputStream out) throws IOException {
+ return decodeWord(data, 0, data.length, out);
+ }
+
+
+ /**
+ * decode the uuencoded byte data writing it to the given output stream
+ *
+ * @param data The array of byte data to decode.
+ * @param off Starting offset within the array.
+ * @param length The length of data to encode.
+ * @param out The output stream used to return the decoded data.
+ *
+ * @return the number of bytes produced.
+ * @exception IOException
+ */
+ public int decodeWord(final byte[] data, int off, final int length, final OutputStream out) throws IOException {
+ // make sure we're writing to the correct stream
+ this.out = out;
+
+ final int endOffset = off + length;
+ int bytesWritten = 0;
+
+ while (off < endOffset) {
+ final byte ch = data[off++];
+
+ // space characters were translated to '_' on encode, so we need to translate them back.
+ if (ch == '_') {
+ out.write(' ');
+ }
+ else if (ch == '=') {
+ // we found an encoded character. Reduce the 3 char sequence to one.
+ // but first, make sure we have two characters to work with.
+ if (off + 1 >= endOffset) {
+ throw new IOException("Invalid quoted printable encoding");
+ }
+ // convert the two bytes back from hex.
+ final byte b1 = data[off++];
+ final byte b2 = data[off++];
+
+ // we've found an encoded carriage return. The next char needs to be a newline
+ if (b1 == '\r') {
+ if (b2 != '\n') {
+ throw new IOException("Invalid quoted printable encoding");
+ }
+ // this was a soft linebreak inserted by the encoding. We just toss this away
+ // on decode.
+ }
+ else {
+ // this is a hex pair we need to convert back to a single byte.
+ final byte c1 = decodingTable[b1];
+ final byte c2 = decodingTable[b2];
+ out.write((c1 << 4) | c2);
+ // 3 bytes in, one byte out
+ bytesWritten++;
+ }
+ }
+ else {
+ // simple character, just write it out.
+ out.write(ch);
+ bytesWritten++;
+ }
+ }
+
+ return bytesWritten;
+ }
+
+
+ /**
+ * decode the UUEncoded String data writing it to the given output stream.
+ *
+ * @param data The String data to decode.
+ * @param out The output stream to write the decoded data to.
+ *
+ * @return the number of bytes produced.
+ * @exception IOException
+ */
+ public int decode(final String data, final OutputStream out) throws IOException {
+ try {
+ // just get the byte data and decode.
+ final byte[] bytes = data.getBytes("US-ASCII");
+ return decode(bytes, 0, bytes.length, out);
+ } catch (final UnsupportedEncodingException e) {
+ throw new IOException("Invalid UUEncoding");
+ }
+ }
+
+ private void checkLineLength(final int required) throws IOException {
+ // if we're at our line length limit, write out a soft line break and reset.
+ if ((lineCount + required) >= lineLength ) {
+ out.write('=');
+ out.write('\r');
+ out.write('\n');
+ bytesWritten += 3;
+ lineCount = 0;
+ }
+ }
+
+
+ public void writeEncodedCharacter(final int ch) throws IOException {
+ // we need 3 characters for an encoded value
+ checkLineLength(3);
+ out.write('=');
+ out.write(encodingTable[ch >> 4]);
+ out.write(encodingTable[ch & 0x0F]);
+ lineCount += 3;
+ bytesWritten += 3;
+ }
+
+
+ public void writeCharacter(final int ch) throws IOException {
+ // we need 3 characters for an encoded value
+ checkLineLength(1);
+ out.write(ch);
+ lineCount++;
+ bytesWritten++;
+ }
+
+
+ public void writeEOL() throws IOException {
+ out.write('\r');
+ out.write('\n');
+ lineCount = 0;
+ bytesWritten += 3;
+ }
+
+
+ public int decode(final InputStream in) throws IOException {
+
+ // we potentially need to scan over spans of whitespace characters to determine if they're real
+ // we just return blanks until the count goes to zero.
+ if (deferredWhitespace > 0) {
+ deferredWhitespace--;
+ return ' ';
+ }
+
+ // we may have needed to scan ahead to find the first non-blank character, which we would store here.
+ // hand that back once we're done with the blanks.
+ if (cachedCharacter != -1) {
+ final int result = cachedCharacter;
+ cachedCharacter = -1;
+ return result;
+ }
+
+ int ch = in.read();
+
+ // reflect back an EOF condition.
+ if (ch == -1) {
+ return -1;
+ }
+
+ // space characters are a pain. We need to scan ahead until we find a non-space character.
+ // if the character is a line terminator, we need to discard the blanks.
+ if (ch == ' ') {
+ // scan forward, counting the characters.
+ while ((ch = in.read()) == ' ') {
+ deferredWhitespace++;
+ }
+
+ // is this a lineend at the current location?
+ if (ch == -1 || ch == '\r' || ch == '\n') {
+ // those blanks we so zealously counted up don't really exist. Clear out the counter.
+ deferredWhitespace = 0;
+ // return the real significant character now.
+ return ch;
+ }
+ // remember this character for later, after we've used up the deferred blanks.
+ cachedCharacter = decodeNonspaceChar(in, ch);
+ // return this space. We did not include this one in the deferred count, so we're right in sync.
+ return ' ';
+ }
+ return decodeNonspaceChar(in, ch);
+ }
+
+ private int decodeNonspaceChar(final InputStream in, final int ch) throws IOException {
+ if (ch == '=') {
+ int b1 = in.read();
+ // we need to get two characters after the quotation marker
+ if (b1 == -1) {
+ throw new IOException("Truncated quoted printable data");
+ }
+ int b2 = in.read();
+ // we need to get two characters after the quotation marker
+ if (b2 == -1) {
+ throw new IOException("Truncated quoted printable data");
+ }
+
+ // we've found an encoded carriage return. The next char needs to be a newline
+ if (b1 == '\r') {
+ if (b2 != '\n') {
+ throw new IOException("Invalid quoted printable encoding");
+ }
+ // this was a soft linebreak inserted by the encoding. We just toss this away
+ // on decode. We need to return something, so recurse and decode the next.
+ return decode(in);
+ }
+ else {
+ // this is a hex pair we need to convert back to a single byte.
+ b1 = decodingTable[b1];
+ b2 = decodingTable[b2];
+ return (b1 << 4) | b2;
+ }
+ }
+ else {
+ return ch;
+ }
+ }
+
+
+ /**
+ * Perform RFC-2047 word encoding using Q-P data encoding.
+ *
+ * @param in The source for the encoded data.
+ * @param charset The charset tag to be added to each encoded data section.
+ * @param specials The set of special characters that we require to encoded.
+ * @param out The output stream where the encoded data is to be written.
+ * @param fold Controls whether separate sections of encoded data are separated by
+ * linebreaks or whitespace.
+ *
+ * @exception IOException
+ */
+ public void encodeWord(final InputStream in, final String charset, final String specials, final OutputStream out, final boolean fold) throws IOException
+ {
+ // we need to scan ahead in a few places, which may require pushing characters back on to the stream.
+ // make sure we have a stream where this is possible.
+ final PushbackInputStream inStream = new PushbackInputStream(in);
+ final PrintStream writer = new PrintStream(out);
+
+ // segments of encoded data are limited to 75 byes, including the control sections.
+ final int limit = 75 - 7 - charset.length();
+ boolean firstLine = true;
+ final StringBuffer encodedString = new StringBuffer(76);
+
+ while (true) {
+
+ // encode another segment of data.
+ encode(inStream, encodedString, specials, limit);
+ // nothing encoded means we've hit the end of the data.
+ if (encodedString.length() == 0) {
+ break;
+ }
+ // if we have more than one segment, we need to insert separators. Depending on whether folding
+ // was requested, this is either a blank or a linebreak.
+ if (!firstLine) {
+ if (fold) {
+ writer.print("\r\n");
+ }
+ else {
+ writer.print(" ");
+ }
+ }
+
+ // add the encoded word header
+ writer.print("=?");
+ writer.print(charset);
+ writer.print("?Q?");
+ // the data
+ writer.print(encodedString.toString());
+ // and the terminator mark
+ writer.print("?=");
+ writer.flush();
+
+ // we reset the string buffer and reuse it.
+ encodedString.setLength(0);
+ // we need a delimiter between sections from this point on.
+ firstLine = false;
+ }
+ }
+
+
+ /**
+ * Perform RFC-2047 word encoding using Base64 data encoding.
+ *
+ * @param in The source for the encoded data.
+ * @param charset The charset tag to be added to each encoded data section.
+ * @param out The output stream where the encoded data is to be written.
+ * @param fold Controls whether separate sections of encoded data are separated by
+ * linebreaks or whitespace.
+ *
+ * @exception IOException
+ */
+ public void encodeWord(final byte[] data, final StringBuffer out, final String charset, final String specials) throws IOException
+ {
+ // append the word header
+ out.append("=?");
+ out.append(charset);
+ out.append("?Q?");
+ // add on the encodeded data
+ encodeWordData(data, out, specials);
+ // the end of the encoding marker
+ out.append("?=");
+ }
+
+
+ /**
+ * Perform RFC-2047 word encoding using Q-P data encoding.
+ *
+ * @param in The source for the encoded data.
+ * @param charset The charset tag to be added to each encoded data section.
+ * @param specials The set of special characters that we require to encoded.
+ * @param out The output stream where the encoded data is to be written.
+ * @param fold Controls whether separate sections of encoded data are separated by
+ * linebreaks or whitespace.
+ *
+ * @exception IOException
+ */
+ public void encodeWordData(final byte[] data, final StringBuffer out, final String specials) throws IOException {
+ for (int i = 0; i < data.length; i++) {
+ final int ch = data[i] & 0xff; ;
+
+ // spaces require special handling. If the next character is a line terminator, then
+ // the space needs to be encoded.
+ if (ch == ' ') {
+ // blanks get translated into underscores, because the encoded tokens can't have embedded blanks.
+ out.append('_');
+ }
+ // non-ascii chars and the designated specials all get encoded.
+ else if (ch < 32 || ch >= 127 || specials.indexOf(ch) != -1) {
+ out.append('=');
+ out.append((char)encodingTable[ch >> 4]);
+ out.append((char)encodingTable[ch & 0x0F]);
+ }
+ else {
+ // good character, just use unchanged.
+ out.append((char)ch);
+ }
+ }
+ }
+
+
+ /**
+ * Estimate the final encoded size of a segment of data.
+ * This is used to ensure that the encoded blocks do
+ * not get split across a unicode character boundary and
+ * that the encoding will fit within the bounds of
+ * a mail header line.
+ *
+ * @param data The data we're anticipating encoding.
+ *
+ * @return The size of the byte data in encoded form.
+ */
+ public int estimateEncodedLength(final byte[] data, final String specials)
+ {
+ int count = 0;
+
+ for (int i = 0; i < data.length; i++) {
+ // make sure this is just a single byte value.
+ final int ch = data[i] & 0xff;
+
+ // non-ascii chars and the designated specials all get encoded.
+ if (ch < 32 || ch >= 127 || specials.indexOf(ch) != -1) {
+ // Q encoding translates a single char into 3 characters
+ count += 3;
+ }
+ else {
+ // non-encoded character
+ count++;
+ }
+ }
+ return count;
+ }
+}
+
+
+
Added: geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoderStream.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoderStream.java?rev=1900504&view=auto
==============================================================================
--- geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoderStream.java (added)
+++ geronimo/specs/trunk/geronimo-jakartamail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/QuotedPrintableEncoderStream.java Tue May 3 12:22:08 2022
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geronimo.mail.util;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * An implementation of a FilterOutputStream that encodes the
+ * stream data in Q-P encoding format. This version does the
+ * encoding "on the fly" rather than encoding a single block of
+ * data. Since this version is intended for use by the MimeUtilty class,
+ * it also handles line breaks in the encoded data.
+ */
+public class QuotedPrintableEncoderStream extends FilterOutputStream {
+ // our hex encoder utility class.
+ protected QuotedPrintableEncoder encoder;
+
+ // our default for line breaks
+ protected static final int DEFAULT_LINEBREAK = 76;
+
+ // the instance line break value
+ protected int lineBreak;
+
+ /**
+ * Create a Base64 encoder stream that wraps a specifed stream
+ * using the default line break size.
+ *
+ * @param out The wrapped output stream.
+ */
+ public QuotedPrintableEncoderStream(final OutputStream out) {
+ this(out, DEFAULT_LINEBREAK);
+ }
+
+
+ public QuotedPrintableEncoderStream(final OutputStream out, final int lineBreak) {
+ super(out);
+ // lines are processed only in multiple of 4, so round this down.
+ this.lineBreak = (lineBreak / 4) * 4 ;
+
+ // create an encoder configured to this amount
+ encoder = new QuotedPrintableEncoder(out, this.lineBreak);
+ }
+
+
+ @Override
+ public void write(final int ch) throws IOException {
+ // have the encoder do the heavy lifting here.
+ encoder.encode(ch);
+ }
+
+ @Override
+ public void write(final byte [] data) throws IOException {
+ write(data, 0, data.length);
+ }
+
+ @Override
+ public void write(final byte [] data, final int offset, final int length) throws IOException {
+ // the encoder does the heavy lifting here.
+ encoder.encode(data, offset, length);
+ }
+
+ @Override
+ public void close() throws IOException {
+ out.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ out.flush();
+ }
+}
+
+