src/org/sonews/util/io/SMTPOutputStream.java
changeset 107 b723308e1359
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/util/io/SMTPOutputStream.java	Tue Oct 25 10:16:13 2011 +0200
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 +GNU-Classpath Extensions: javamail
     1.6 +Copyright (C) 2002 Chris Burdess
     1.7 +
     1.8 +For more information on the classpathx please mail:
     1.9 +nferrier@tapsellferrier.co.uk
    1.10 +
    1.11 +This program is free software; you can redistribute it and/or
    1.12 +modify it under the terms of the GNU Lesser General Public License
    1.13 +as published by the Free Software Foundation; either version 2
    1.14 +of the License, or (at your option) any later version.
    1.15 +
    1.16 +This program is distributed in the hope that it will be useful,
    1.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.19 +GNU General Public License for more details.
    1.20 +
    1.21 +You should have received a copy of the GNU General Public License
    1.22 +along with this program; if not, write to the Free Software
    1.23 +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.24 + */
    1.25 +package org.sonews.util.io; // original package: gnu.mail.providers.smtp
    1.26 +
    1.27 +import java.io.FilterOutputStream;
    1.28 +import java.io.IOException;
    1.29 +
    1.30 +/**
    1.31 + * An output stream implementing the SMTP specification for dot escaping.
    1.32 + * Objects of this class are intended to be chained with CRLFOutputStream
    1.33 + * instances as all SMTP output is intended to be CRLF-escaped.
    1.34 + *
    1.35 + * @author dog@gnu.org
    1.36 + * @version 0.1
    1.37 + */
    1.38 +public class SMTPOutputStream extends FilterOutputStream {
    1.39 +
    1.40 +	/**
    1.41 +	 * The LF octet.
    1.42 +	 */
    1.43 +	public static final int LF = 0x0a;
    1.44 +	/**
    1.45 +	 * The dot octet.
    1.46 +	 */
    1.47 +	public static final int DOT = 0x2e;
    1.48 +	/**
    1.49 +	 * The last octet read.
    1.50 +	 */
    1.51 +	protected int last;
    1.52 +
    1.53 +	/**
    1.54 +	 * Constructs an SMTP output stream connected to the specified output stream.
    1.55 +	 * The underlying output stream should coordinate proper CRLF pairs at
    1.56 +	 * line ends.
    1.57 +	 * @param out a CRLFOutputStream
    1.58 +	 */
    1.59 +	public SMTPOutputStream(CRLFOutputStream out) {
    1.60 +		super(out);
    1.61 +	}
    1.62 +
    1.63 +	/**
    1.64 +	 * Writes a character to the underlying stream.
    1.65 +	 * @exception IOException if an I/O error occurred
    1.66 +	 */
    1.67 +	@Override
    1.68 +	public void write(int ch) throws IOException {
    1.69 +		if (ch == DOT) {
    1.70 +			if (last == LF) {
    1.71 +				out.write(DOT);
    1.72 +			}
    1.73 +		}
    1.74 +		out.write(ch);
    1.75 +		last = ch;
    1.76 +	}
    1.77 +
    1.78 +	/**
    1.79 +	 * Writes a byte array to the underlying stream.
    1.80 +	 * @exception IOException if an I/O error occurred
    1.81 +	 */
    1.82 +	@Override
    1.83 +	public void write(byte b[]) throws IOException {
    1.84 +		write(b, 0, b.length);
    1.85 +	}
    1.86 +
    1.87 +	/**
    1.88 +	 * Writes a portion of a byte array to the underlying stream.
    1.89 +	 * @exception IOException if an I/O error occurred
    1.90 +	 */
    1.91 +	@Override
    1.92 +	public void write(byte b[], int off, int len) throws IOException {
    1.93 +		int d = off;
    1.94 +		len += off;
    1.95 +		for (int i = off; i < len; i++) {
    1.96 +			switch (b[i]) {
    1.97 +				case DOT:
    1.98 +					int l = (i - d);
    1.99 +					if (l > 0) {
   1.100 +						out.write(b, d, l);
   1.101 +					}
   1.102 +					d = i;
   1.103 +					if (last == LF) {
   1.104 +						out.write(DOT);
   1.105 +					}
   1.106 +					break;
   1.107 +			}
   1.108 +			last = b[i];
   1.109 +		}
   1.110 +		if (len - d > 0) {
   1.111 +			out.write(b, d, len - d);
   1.112 +		}
   1.113 +	}
   1.114 +}