ccs.utils
Class LineInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by ccs.utils.LineInputStream
All Implemented Interfaces:
java.io.Closeable

public class LineInputStream
extends java.io.FilterInputStream

This filter returns its underlying stream a single "line" at a time. Specifically, the bytestring returned from the read(byte, int, int) method is guaranteed to obey one of these criteria:


Field Summary
static int BUFLEN
          The length of the internal buffer.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
LineInputStream(java.io.InputStream in)
          Create a LineInputStream for the specified InputStream, without anal CRLF checking.
LineInputStream(java.io.InputStream in, boolean isAnalCRLF)
          Create a LineInputStream for the InputStream with settable "anal CRLF Checking".
 
Method Summary
 boolean isEOF()
          Whether there is an EOF condition.
 int read()
          This method is incompatible with the contract of the stream - it's supposed to return a line at a time, not a byte at a time.
 int read(byte[] wad, int off, int len)
          Returns the next "line" - as defined in the preamble - or as much of it as fits in the method's internal buffer.
 java.lang.String readAscii()
          Returns the next line as a String, assuming that the data is ASCII.
 void setDam(int damleft)
          Sets a dam.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFLEN

public static final int BUFLEN
The length of the internal buffer. Length currently dictated by the requirements of SMTP.

See Also:
read(byte[], int, int), Constant Field Values
Constructor Detail

LineInputStream

public LineInputStream(java.io.InputStream in)
Create a LineInputStream for the specified InputStream, without anal CRLF checking.


LineInputStream

public LineInputStream(java.io.InputStream in,
                       boolean isAnalCRLF)
Create a LineInputStream for the InputStream with settable "anal CRLF Checking". Anal CRLF checking only affects the behaviour of readAscii. If enabled, a line must end with CRLF; non-compliant lines generate UTFDataFormatException (which is slightly wonky, but what the heck.)

Method Detail

setDam

public void setDam(int damleft)
Sets a dam. Starting from the next read, when damleft bytes have been returned, the stream will report EOF. This is used to e.g. not make socket streams block. NB. Due to the internal buffering used by this stream, it is generally not sufficient to feed a DammedInputStream from this stream, hence this apparent duplication of functionality. (The opposite configuration - feeding this from a DammedInputStream - is OK.) To disable the dam, set it to < 0.


read

public int read()
         throws java.io.IOException
This method is incompatible with the contract of the stream - it's supposed to return a line at a time, not a byte at a time. It therefore always throws.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] wad,
                int off,
                int len)
         throws java.io.IOException
Returns the next "line" - as defined in the preamble - or as much of it as fits in the method's internal buffer. It is an error to request fewer bytes than the stream wishes to send back(!). To avoid this error, always request at least BUFLEN bytes.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

readAscii

public java.lang.String readAscii()
                           throws java.io.IOException
Returns the next line as a String, assuming that the data is ASCII. The terminating CRLF is trimmed off. Returns null on EOF.

Throws:
java.io.UTFDataFormatException - If anal CRLF checking is enabled and the line does not comply.
java.io.IOException

isEOF

public boolean isEOF()
Whether there is an EOF condition. If true, at least one read attempt has already failed, and further reads will also fail. EOF is not set until the first attempted read that fails.