src/org/sonews/util/io/SMTPInputStream.java
changeset 119 f5b57e221e38
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/util/io/SMTPInputStream.java	Thu Jul 05 13:19:19 2012 +0200
     1.3 @@ -0,0 +1,110 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package org.sonews.util.io;
    1.22 +
    1.23 +import java.io.FilterInputStream;
    1.24 +import java.io.IOException;
    1.25 +import java.io.InputStream;
    1.26 +
    1.27 +/**
    1.28 + * Filter input stream for reading from SMTP (or NNTP or similar) socket
    1.29 + * where lines containing single dot have special meaning – end of message.
    1.30 + * 
    1.31 + * @author František Kučera (frantovo.cz)
    1.32 + */
    1.33 +public class SMTPInputStream extends FilterInputStream {
    1.34 +
    1.35 +	public static final int CR = 0x0d;
    1.36 +	public static final int LF = 0x0a;
    1.37 +	public static final int DOT = 0x2e;
    1.38 +	protected int last;
    1.39 +
    1.40 +	public SMTPInputStream(InputStream in) {
    1.41 +		super(in);
    1.42 +	}
    1.43 +
    1.44 +	/**
    1.45 +	 * @return one byte as expected 
    1.46 +	 * or -2 if there was line with single dot (which means end of message)
    1.47 +	 * @throws IOException 
    1.48 +	 */
    1.49 +	@Override
    1.50 +	public int read() throws IOException {
    1.51 +		// read current character
    1.52 +		int ch = super.read();
    1.53 +
    1.54 +		if (ch == DOT) {
    1.55 +			if (last == LF) {
    1.56 +				int next = super.read();
    1.57 +
    1.58 +				if (next == CR || next == LF) { // There should be CRLF, but we may accept also just LF or CR with missing LF. Or should we be more strict?
    1.59 +					// <CRLF>.<CRLF> → end of current message
    1.60 +					ch = -2;
    1.61 +				} else {
    1.62 +					// <CRLF>.… → eat one dot and return next character
    1.63 +					ch = next;
    1.64 +				}
    1.65 +			}
    1.66 +		}
    1.67 +
    1.68 +		last = ch;
    1.69 +		return ch;
    1.70 +	}
    1.71 +
    1.72 +	/**
    1.73 +	 * @param buffer
    1.74 +	 * @param offset
    1.75 +	 * @param length
    1.76 +	 * @return See {@link FilterInputStream#read(byte[], int, int)} or -2 (then see {@link #read(byte[])})
    1.77 +	 * @throws IOException 
    1.78 +	 */
    1.79 +	@Override
    1.80 +	public int read(byte[] buffer, int offset, int length) throws IOException {
    1.81 +		if (buffer == null) {
    1.82 +			throw new NullPointerException("Byte array should not be null.");
    1.83 +		} else if ((offset < 0) || (offset > buffer.length) || (length < 0) || ((offset + length) > buffer.length) || ((offset + length) < 0)) {
    1.84 +			throw new IndexOutOfBoundsException("Invalid offset or length.");
    1.85 +		} else if (length == 0) {
    1.86 +			return 0;
    1.87 +		}
    1.88 +
    1.89 +		int ch = read();
    1.90 +
    1.91 +		if (ch == -1 || ch == -2) {
    1.92 +			return ch;
    1.93 +		}
    1.94 +
    1.95 +		buffer[offset] = (byte) ch;
    1.96 +
    1.97 +		int readCounter = 1;
    1.98 +
    1.99 +		for (; readCounter < length; readCounter++) {
   1.100 +			ch = read();
   1.101 +
   1.102 +			if (ch == -1 || ch == -2) {
   1.103 +				break;
   1.104 +			}
   1.105 +
   1.106 +			if (buffer != null) {
   1.107 +				buffer[offset + readCounter] = (byte) ch;
   1.108 +			}
   1.109 +		}
   1.110 +
   1.111 +		return readCounter;
   1.112 +	}
   1.113 +}