[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Classpathx-javamail] gnu.mail.util.DotTerminatedInputStream.java
From: |
Doug Porter |
Subject: |
[Classpathx-javamail] gnu.mail.util.DotTerminatedInputStream.java |
Date: |
Fri, 19 Sep 2003 03:24:05 -0800 |
/*
* DotTerminatedInputStream.java
* Copyright (C) 2003 Doug Porter
*
* This file is part of GNU JavaMail, a library.
*
* GNU JavaMail is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNU JavaMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*
* As a special exception, if you link this library with other files to
* produce an executable, this library does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* This exception does not however invalidate any other reasons why the
* executable file might be covered by the GNU General Public License.
*/
package gnu.mail.util;
import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import javax.mail.Session;
/**
* An input stream that returns end of stream on the sequence "CRLF .
CRLF".
* <p>
* If you want to read until a line with a single dot, wrap your stream
in
* this class before passing it to LineInputStream.
* <p>
* This implementation is dependent on methods ultimately calling read()
* for input.
* <p>
* @author Doug Porter
*/
public class DotTerminatedInputStream
extends ReadFilterInputStream
{
static final int MarkReadLimit = 5;
static final int EOS = -1;
private boolean isEOS = false;
/**
* Constructor.
* @param in the inner input stream
*/
public DotTerminatedInputStream (InputStream in) {
// we need mark/reset
super (in.markSupported () ? in : new BufferedInputStream (in));
}
/**
* Returns the number of bytes available.
* <p>
* If a dot line has been seen, returns zero.
* @return the number of bytes available
*/
public int available ()
throws IOException
{
int n;
if (isEOS) {
n = 0;
}
else {
n = super.available ();
}
return n;
}
/**
* Returns the next byte from this stream.
* <p>
* If a dot line has been seen, returns -1.
* @return The next byte from this stream, or -1 on dot line or end
of stream
*/
public int read ()
throws IOException
{
final int CR = 13;
final int LF = 10;
final int DOT = '.';
int b;
if (isEOS) {
b = EOS;
}
else {
b = super.read ();
mark (MarkReadLimit);
if (b == CR &&
super.read () == LF &&
super.read () == DOT &&
super.read () == CR &&
super.read () == LF) {
isEOS = true;
}
else {
// our streams often have stripped CRs (CRLFInputStream)
reset ();
mark (MarkReadLimit);
if (b == LF &&
super.read () == DOT &&
super.read () == LF) {
isEOS = true;
}
}
if (isEOS) {
// end of stream
b = EOS;
}
else {
reset ();
}
}
return b;
}
}
--
Doug Porter
address@hidden
--
http://www.fastmail.fm - Same, same, but differentÂ…
- [Classpathx-javamail] gnu.mail.util.DotTerminatedInputStream.java,
Doug Porter <=