org.mortbay.util

Class LineInput


public class LineInput
extends 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.17 2005/10/05 11:32:40 gregwilkins Exp $
Author:
Greg Wilkins (gregw)

Nested Class Summary

static class
LineInput.LineBuffer
Reusable LineBuffer.

Constructor Summary

LineInput(InputStream in)
Constructor.
LineInput(InputStream in, int bufferSize)
Constructor.
LineInput(InputStream in, int bufferSize, String encoding)
Constructor.

Method Summary

int
available()
void
destroy()
int
getByteLimit()
Get the byte limit.
InputStream
getInputStream()
void
mark(int limit)
boolean
markSupported()
int
read()
int
read(b[] , int off, int len)
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)

Constructor Details

LineInput

public LineInput(InputStream in)
Constructor. Default buffer and maximum line size is 2048.
Parameters:
in - The underlying input stream.

LineInput

public LineInput(InputStream in,
                 int bufferSize)
Constructor.
Parameters:
in - The underlying input stream.
bufferSize - The buffer size and maximum line length.

LineInput

public LineInput(InputStream in,
                 int bufferSize,
                 String encoding)
            throws 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.

Method Details

available

public int available()
            throws IOException

destroy

public void destroy()

getByteLimit

public int getByteLimit()
Get the byte limit.
Returns:
Number of bytes until EOF is returned or -1 for no limit.

getInputStream

public InputStream getInputStream()

mark

public void mark(int limit)
            throws IllegalArgumentException

markSupported

public boolean markSupported()

read

public int read()
            throws IOException

read

public int read(b[] ,
                int off,
                int len)
            throws IOException

readLine

public String readLine()
            throws 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.

readLine

public int readLine(byte[] b,
                    int off,
                    int len)
            throws 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.

readLine

public int readLine(char[] c,
                    int off,
                    int len)
            throws 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.

readLineBuffer

public LineInput.LineBuffer readLineBuffer()
            throws 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.

readLineBuffer

public LineInput.LineBuffer readLineBuffer(int len)
            throws 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.

reset

public void reset()
            throws IOException

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.

skip

public long skip(long n)
            throws IOException

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.