net.rim.device.api.io
Class LineReader

java.lang.Object
  extended by 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 static int BUFFER_LENGTH
           
protected  byte[] _buffer
           
protected  int _bufferLength
           
protected  int _bufferOffset
           
protected  InputStream _stream
           
 
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_LENGTH

protected static final int BUFFER_LENGTH
See Also:
Constant Field Values

_stream

protected InputStream _stream
Since:
BlackBerry API 3.6.0

_buffer

protected byte[] _buffer
Since:
BlackBerry API 3.6.0

_bufferOffset

protected int _bufferOffset
Since:
BlackBerry API 3.6.0

_bufferLength

protected int _bufferLength
Since:
BlackBerry API 3.6.0


Constructor Detail

LineReader

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

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


Method Detail

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.

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.

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.

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.

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.

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.

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:
LineReader.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.





Copyright 1999-2011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Java is a trademark of Oracle America Inc. in the US and other countries.
Legal