net.rim.device.api.io
Class LineReader

java.lang.Object
  |
  +--net.rim.device.api.io.LineReader
Direct Known Subclasses:
ScanLine

public class LineReader
extends Object

Utility class for reading out CRLF delimited lines from a SharedInputStream.

Sample code

Example 1:

// Get an input stream from the file.
InputStream stream = getClass().getResourceAsStream("/folder/file.txt");

if(stream != null)
{
    LineReader lineReader = new LineReader(stream);                              
           
    // We read data from input stream one line at a time until we
    // reach end of file. Each line is parsed to extract data.       
    for(;;)
    {
        try
        {
            String line = new String(lineReader.readLine());
                
            // Parse the current line...
                            
        }
        catch(EOFException eof)
        {
            // We've reached the end of the file.
            break;
        }
        catch(IOException ioe)
        {
            // Error reading data from file
        }               
    }
}

Example 2:

Note that the first call to lengthUnreadData() always returns zero, so you must call readLine() first :

LineReader lineReader = new LineReader(inputStream);

String line = null;
try
{
    line = new String(lineReader.readLine());
}
catch (EOFException eofe)
{
}

for (; line != null; line = new String(lineReader.readLine()))
{
   
    // Parse line...
   
    if (lineReader.lengthUnreadData() <= 0)
    {
        break; 
    }
}


Field Summary
protected  byte[] _buffer
           
protected  int _bufferLength
           
protected  int _bufferOffset
           
protected  InputStream _stream
           
protected static int BUFFER_LENGTH
           
 
Constructor Summary
LineReader(InputStream stream)
          Creates a new LineReader instance.
 
Method Summary
 byte[] getBuffer()
          Deprecated. Only subclasses should access the underlying buffer, and they may directly access the protected _buffer field instead of using this method.
 int getBufferLength()
          Deprecated. Only subclasses should access the buffer length, and they may directly access the protected _bufferLength field instead of using this method.
 int getBufferOffset()
          Deprecated. Only subclasses should access the buffer offset, and they may directly access the protected _bufferOffset field instead of using this method.
 InputStream getStream()
          Deprecated. Only subclasses should access the underlying stream, and they may directly access the protected _stream field instead of using this method.
 int lengthUnreadData()
          Returns the number of bytes currently remaining in the underlying line buffer.
 byte[] readLine()
          Read CRLF-delimited line from the stream.
 void setBufferLength(int bufferLength)
          Deprecated. Only subclasses should access the buffer length, and they may directly access the protected _bufferLength field instead of using this method.
 void setBufferOffset(int bufferOffset)
          Deprecated. Only subclasses should access the buffer offset, and they may directly access the protected _bufferOffset field instead of using this method.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_buffer

protected byte[] _buffer
Since:
JDE 3.6.0

_bufferLength

protected int _bufferLength
Since:
JDE 3.6.0

_bufferOffset

protected int _bufferOffset
Since:
JDE 3.6.0

_stream

protected InputStream _stream
Since:
JDE 3.6.0

BUFFER_LENGTH

protected static final int BUFFER_LENGTH
Constructor Detail

LineReader

public LineReader(InputStream stream)
Creates a new LineReader instance.

Parameters:
stream - input stream from which to read the lines.
Method Detail

getBuffer

public byte[] getBuffer()
Deprecated. Only subclasses should access the underlying buffer, and they may directly access the protected _buffer field instead of using this method.

Retrieves underlying line buffer.

Returns:
Underlying line buffer this reader uses to assemble lines.

getBufferLength

public int getBufferLength()
Deprecated. Only subclasses should access the buffer length, and they may directly access the protected _bufferLength field instead of using this method.

Retrieves current length of the underlying line buffer.

Returns:
Current length of underlying line buffer.

getBufferOffset

public int getBufferOffset()
Deprecated. Only subclasses should access the buffer offset, and they may directly access the protected _bufferOffset field instead of using this method.

Retrieves current position in underlying line buffer.

Returns:
Current read/write position in underlying line buffer.

getStream

public InputStream getStream()
Deprecated. Only subclasses should access the underlying stream, and they may directly access the protected _stream field instead of using this method.

Retrieves underlying stream.

Returns:
Underlying data stream for this line reader.

lengthUnreadData

public int lengthUnreadData()
Returns the number of bytes currently remaining in the underlying line buffer.
Note: this method always returns 0 before the first invocation of readLine().

Returns:
Number of bytes currently remaining in the underlying line buffer, or 0 if readLine() has not yet been invoked.
See Also:
readLine()

readLine

public byte[] readLine()
                throws IOException
Read CRLF-delimited line from the stream.

Invoke this method to read bytes from the input stream until encountering a carriage-return line-feed pair marking the end of a line. The CRLF pair is stripped out of the stream, and not returned.

Note: This method will also attempt to properly build the stream even if it only streams a LF or a CR. For example, if the inputStream only provides an LF instead of CRLF for line endings this will not crash the method. Again, it will not return the LF or CR characters.

Returns:
Byte array containing the next line from the stream, without the CRLF pair.
Throws:
IOException - If an I/O error occurs.
EOFException - If the end of the stream has been reached.

setBufferLength

public void setBufferLength(int bufferLength)
Deprecated. Only subclasses should access the buffer length, and they may directly access the protected _bufferLength field instead of using this method.

Sets the length of the underlying line buffer.

Parameters:
bufferLength - New length for the underlying line buffer.

setBufferOffset

public void setBufferOffset(int bufferOffset)
Deprecated. Only subclasses should access the buffer offset, and they may directly access the protected _bufferOffset field instead of using this method.

Sets the read/write position in the underlying line buffer.

Parameters:
bufferOffset - New read/write position.



Copyright 1999-2009 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.