franta-hg@115: /*
franta-hg@115: * SONEWS News Server
franta-hg@115: * see AUTHORS for the list of contributors
franta-hg@115: *
franta-hg@115: * This program is free software: you can redistribute it and/or modify
franta-hg@115: * it under the terms of the GNU General Public License as published by
franta-hg@115: * the Free Software Foundation, either version 3 of the License, or
franta-hg@115: * (at your option) any later version.
franta-hg@115: *
franta-hg@115: * This program is distributed in the hope that it will be useful,
franta-hg@115: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@115: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@115: * GNU General Public License for more details.
franta-hg@115: *
franta-hg@115: * You should have received a copy of the GNU General Public License
franta-hg@115: * along with this program. If not, see .
franta-hg@115: */
franta-hg@115: package org.sonews.util.io;
franta-hg@115:
franta-hg@115: import java.io.FilterInputStream;
franta-hg@115: import java.io.IOException;
franta-hg@115: import java.io.InputStream;
franta-hg@115:
franta-hg@115: /**
franta-hg@115: * Filter input stream for reading from SMTP (or NNTP or similar) socket
franta-hg@115: * where lines containing single dot have special meaning – end of message.
franta-hg@115: *
franta-hg@115: * @author František Kučera (frantovo.cz)
franta-hg@115: */
franta-hg@115: public class SMTPInputStream extends FilterInputStream {
franta-hg@115:
franta-hg@115: public static final int CR = 0x0d;
franta-hg@115: public static final int LF = 0x0a;
franta-hg@115: public static final int DOT = 0x2e;
franta-hg@115: protected int last;
franta-hg@115:
franta-hg@115: public SMTPInputStream(InputStream in) {
franta-hg@115: super(in);
franta-hg@115: }
franta-hg@115:
franta-hg@115: /**
franta-hg@115: * @return one byte as expected
franta-hg@115: * or -2 if there was line with single dot (which means end of message)
franta-hg@115: * @throws IOException
franta-hg@115: */
franta-hg@115: @Override
franta-hg@115: public int read() throws IOException {
franta-hg@115: // read current character
franta-hg@115: int ch = super.read();
franta-hg@115:
franta-hg@115: if (ch == DOT) {
franta-hg@115: if (last == LF) {
franta-hg@115: int next = super.read();
franta-hg@115:
franta-hg@115: 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?
franta-hg@115: // . → end of current message
franta-hg@115: ch = -2;
franta-hg@115: } else {
franta-hg@115: // .… → eat one dot and return next character
franta-hg@115: ch = next;
franta-hg@115: }
franta-hg@115: }
franta-hg@115: }
franta-hg@115:
franta-hg@115: last = ch;
franta-hg@115: return ch;
franta-hg@115: }
franta-hg@115:
franta-hg@115: /**
franta-hg@115: * @param buffer
franta-hg@115: * @param offset
franta-hg@115: * @param length
franta-hg@115: * @return See {@link FilterInputStream#read(byte[], int, int)} or -2 (then see {@link #read(byte[])})
franta-hg@115: * @throws IOException
franta-hg@115: */
franta-hg@115: @Override
franta-hg@115: public int read(byte[] buffer, int offset, int length) throws IOException {
franta-hg@115: if (buffer == null) {
franta-hg@115: throw new NullPointerException("Byte array should not be null.");
franta-hg@115: } else if ((offset < 0) || (offset > buffer.length) || (length < 0) || ((offset + length) > buffer.length) || ((offset + length) < 0)) {
franta-hg@115: throw new IndexOutOfBoundsException("Invalid offset or length.");
franta-hg@115: } else if (length == 0) {
franta-hg@115: return 0;
franta-hg@115: }
franta-hg@115:
franta-hg@115: int ch = read();
franta-hg@115:
franta-hg@115: if (ch == -1 || ch == -2) {
franta-hg@115: return ch;
franta-hg@115: }
franta-hg@115:
franta-hg@115: buffer[offset] = (byte) ch;
franta-hg@115:
franta-hg@115: int readCounter = 1;
franta-hg@115:
franta-hg@115: for (; readCounter < length; readCounter++) {
franta-hg@115: ch = read();
franta-hg@115:
franta-hg@115: if (ch == -1 || ch == -2) {
franta-hg@115: break;
franta-hg@115: }
franta-hg@115:
franta-hg@115: if (buffer != null) {
franta-hg@115: buffer[offset + readCounter] = (byte) ch;
franta-hg@115: }
franta-hg@115: }
franta-hg@115:
franta-hg@115: return readCounter;
franta-hg@115: }
franta-hg@115: }