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 classLineInput.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
intavailable()
voiddestroy()
intgetByteLimit()
Get the byte limit.
InputStreamgetInputStream()
voidmark(int limit)
booleanmarkSupported()
intread()
intread(byte[] b, int off, int len)
StringreadLine()
Read a line ended by CR, LF or CRLF.
intreadLine(char[] c, int off, int len)
Read a line ended by CR, LF or CRLF.
intreadLine(byte[] b, int off, int len)
Read a line ended by CR, LF or CRLF.
LineInput.LineBufferreadLineBuffer()
Read a Line ended by CR, LF or CRLF.
LineInput.LineBufferreadLineBuffer(int len)
Read a Line ended by CR, LF or CRLF.
voidreset()
voidsetByteLimit(int bytes)
Set the byte limit.
longskip(long n)

Constructor Detail

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)
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: UnsupportedEncodingException

Method Detail

available

public int available()

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)

markSupported

public boolean markSupported()

read

public int read()

read

public int read(byte[] b, int off, int len)

readLine

public String readLine()
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: IOException

readLine

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

readLine

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

readLineBuffer

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

readLineBuffer

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

reset

public void reset()

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)
Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.