org.mortbay.util
Class LineInput

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--org.mortbay.util.LineInput

public class LineInput
extends java.io.FilterInputStream

Fast LineInput InputStream. This buffered InputStream provides methods for reading lines of bytes. The lines can be converted to String or character arrays either using the default encoding or a user supplied encoding. Buffering and data copying are highly optimized, making this an ideal class for protocols that mix character encoding lines with arbitrary byte data (eg HTTP). The buffer size is also the maximum line length in bytes and/or characters. If the byte length of a line is less than the max, but the character length is greater, than then trailing characters are lost. Line termination is forgiving and accepts CR, LF, CRLF or EOF. Line input uses the mark/reset mechanism, so any marks set prior to a readLine call are lost.

Version:
$Id: LineInput.java,v 1.10.2.1 2004/02/08 22:34:48 gregwilkins Exp $
Author:
Greg Wilkins (gregw)

Nested Class Summary
static class LineInput.LineBuffer
          Reusable LineBuffer.
 
Constructor Summary
LineInput(java.io.InputStream in)
          Constructor.
LineInput(java.io.InputStream in, int bufferSize)
          Constructor.
LineInput(java.io.InputStream in, int bufferSize, java.lang.String encoding)
          Constructor.
 
Method Summary
 int available()
           
 void destroy()
           
 int getByteLimit()
          Get the byte limit.
 java.io.InputStream getInputStream()
           
 void mark(int limit)
           
 boolean markSupported()
           
 int read()
           
 int read(byte[] b, int off, int len)
           
 java.lang.String readLine()
          Read a line ended by CR, LF or CRLF.
 int readLine(byte[] b, int off, int len)
          Read a line ended by CR, LF or CRLF.
 int readLine(char[] c, int off, int len)
          Read a line ended by CR, LF or CRLF.
 LineInput.LineBuffer readLineBuffer()
          Read a Line ended by CR, LF or CRLF.
 LineInput.LineBuffer readLineBuffer(int len)
          Read a Line ended by CR, LF or CRLF.
 void reset()
           
 void setByteLimit(int bytes)
          Set the byte limit.
 long skip(long n)
           
 
Methods inherited from class java.io.FilterInputStream
close, read
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LineInput

public LineInput(java.io.InputStream in)
Constructor. Default buffer and maximum line size is 2048.

Parameters:
in - The underlying input stream.

LineInput

public LineInput(java.io.InputStream in,
                 int bufferSize)
Constructor.

Parameters:
in - The underlying input stream.
bufferSize - The buffer size and maximum line length.

LineInput

public LineInput(java.io.InputStream in,
                 int bufferSize,
                 java.lang.String encoding)
          throws java.io.UnsupportedEncodingException
Constructor.

Parameters:
in - The underlying input stream.
bufferSize - The buffer size and maximum line length.
encoding - the character encoding to use for readLine methods.
Throws:
java.io.UnsupportedEncodingException
Method Detail

getInputStream

public java.io.InputStream getInputStream()

setByteLimit

public void setByteLimit(int bytes)
Set the byte limit. If set, only this number of bytes are read before EOF.

Parameters:
bytes - Limit number of bytes, or -1 for no limit.

getByteLimit

public int getByteLimit()
Get the byte limit.

Returns:
Number of bytes until EOF is returned or -1 for no limit.

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Read a line ended by CR, LF or CRLF. The default or supplied encoding is used to convert bytes to characters.

Returns:
The line as a String or null for EOF.
Throws:
java.io.IOException

readLine

public int readLine(char[] c,
                    int off,
                    int len)
             throws java.io.IOException
Read a line ended by CR, LF or CRLF. The default or supplied encoding is used to convert bytes to characters.

Parameters:
c - Character buffer to place the line into.
off - Offset into the buffer.
len - Maximum length of line.
Returns:
The length of the line or -1 for EOF.
Throws:
java.io.IOException

readLine

public int readLine(byte[] b,
                    int off,
                    int len)
             throws java.io.IOException
Read a line ended by CR, LF or CRLF.

Parameters:
b - Byte array to place the line into.
off - Offset into the buffer.
len - Maximum length of line.
Returns:
The length of the line or -1 for EOF.
Throws:
java.io.IOException

readLineBuffer

public LineInput.LineBuffer readLineBuffer()
                                    throws java.io.IOException
Read a Line ended by CR, LF or CRLF. Read a line into a shared LineBuffer instance. The LineBuffer is resused between calls and should not be held by the caller. The default or supplied encoding is used to convert bytes to characters.

Returns:
LineBuffer instance or null for EOF.
Throws:
java.io.IOException

readLineBuffer

public LineInput.LineBuffer readLineBuffer(int len)
                                    throws java.io.IOException
Read a Line ended by CR, LF or CRLF. Read a line into a shared LineBuffer instance. The LineBuffer is resused between calls and should not be held by the caller. The default or supplied encoding is used to convert bytes to characters.

Parameters:
len - Maximum length of a line, or 0 for default
Returns:
LineBuffer instance or null for EOF.
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.FilterInputStream
java.io.IOException

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.FilterInputStream
java.io.IOException

mark

public void mark(int limit)
          throws java.lang.IllegalArgumentException
Overrides:
mark in class java.io.FilterInputStream
java.lang.IllegalArgumentException

reset

public void reset()
           throws java.io.IOException
Overrides:
reset in class java.io.FilterInputStream
java.io.IOException

markSupported

public boolean markSupported()
Overrides:
markSupported in class java.io.FilterInputStream

destroy

public void destroy()


Copyright ? 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.