FileDocCategorySizeDatePackage
LineNumberInputStream.javaAPI DocAndroid 1.5 API9777Wed May 06 22:41:04 BST 2009java.io

LineNumberInputStream

public class LineNumberInputStream extends FilterInputStream
Wraps an existing {@link InputStream} and counts the line terminators encountered while reading the data. Line numbering starts at 0. Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and {@code "\r\n"}. When using {@code read}, line terminator sequences are always translated into {@code '\n'}.
deprecated
Use {@link LineNumberReader}
since
Android 1.0

Fields Summary
private int
lineNumber
private int
markedLineNumber
private int
lastChar
private int
markedLastChar
Constructors Summary
public LineNumberInputStream(InputStream in)
Constructs a new {@code LineNumberInputStream} on the {@link InputStream} {@code in}. Line numbers are counted for all data read from this stream.

param
in The non-null input stream to count line numbers.
since
Android 1.0


                                                        
       
        super(in);
    
Methods Summary
public intavailable()
Returns the number of bytes that are available before this stream will block.

Note: The source stream may just be a sequence of {@code "\r\n"} bytes which are converted into {@code '\n'} by this stream. Therefore, {@code available} returns only {@code in.available() / 2} bytes as result.

return
the guaranteed number of bytes available before blocking.
throws
IOException if an error occurs in this stream.
since
Android 1.0

        return in.available() / 2 + (lastChar == -1 ? 0 : 1);
    
public intgetLineNumber()
Returns the current line number for this stream. Numbering starts at 0.

return
the current line number.
since
Android 1.0

        return lineNumber;
    
public voidmark(int readlimit)
Sets a mark position in this stream. The parameter {@code readlimit} indicates how many bytes can be read before the mark is invalidated. Sending {@code reset()} will reposition this stream back to the marked position, provided that {@code readlimit} has not been surpassed. The line number count will also be reset to the last marked line number count.

This implementation sets a mark in the filtered stream.

param
readlimit the number of bytes that can be read from this stream before the mark is invalidated.
see
#markSupported()
see
#reset()
since
Android 1.0

        in.mark(readlimit);
        markedLineNumber = lineNumber;
        markedLastChar = lastChar;
    
public intread()
Reads a single byte from the filtered stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of this stream has been reached.

The line number count is incremented if a line terminator is encountered. Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and {@code "\r\n"}. Line terminator sequences are always translated into {@code '\n'}.

return
the byte read or -1 if the end of the filtered stream has been reached.
throws
IOException if the stream is closed or another IOException occurs.
since
Android 1.0

        int currentChar = lastChar;
        if (currentChar == -1) {
            currentChar = in.read();
        } else {
            lastChar = -1;
        }
        switch (currentChar) {
            case '\r":
                currentChar = '\n";
                lastChar = in.read();
                if (lastChar == '\n") {
                    lastChar = -1;
                }
                // fall through
            case '\n":
                lineNumber++;
        }
        return currentChar;
    
public intread(byte[] buffer, int offset, int length)
Reads at most {@code length} bytes from the filtered stream and stores them in the byte array {@code buffer} starting at {@code offset}. Returns the number of bytes actually read or -1 if no bytes have been read and the end of this stream has been reached.

The line number count is incremented if a line terminator is encountered. Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and {@code "\r\n"}. Line terminator sequences are always translated into {@code '\n'}.

param
buffer the array in which to store the bytes read.
param
offset the initial position in {@code buffer} to store the bytes read from this stream.
param
length the maximum number of bytes to store in {@code buffer}.
return
the number of bytes actually read or -1 if the end of the filtered stream has been reached while reading.
throws
IndexOutOfBoundsException if {@code offset < 0} or {@code length < 0}, or if {@code offset + length} is greater than the length of {@code buffer}.
throws
IOException if this stream is closed or another IOException occurs.
throws
NullPointerException if {@code buffer} is {@code null}.
since
Android 1.0

        // BEGIN android-changed
        if (buffer == null) {
            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
        }
        // avoid int overflow
        // Exception priorities (in case of multiple errors) differ from
        // RI, but are spec-compliant.
        // removed redundant check, used (offset | length) < 0
        // instead of (offset < 0) || (length < 0) to safe one operation
        if ((offset | length) < 0 || length > buffer.length - offset) {
            throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        // END android-changed

        for (int i = 0; i < length; i++) {
            int currentChar;
            try {
                currentChar = read();
            } catch (IOException e) {
                if (i != 0) {
                    return i;
                }
                throw e;
            }
            if (currentChar == -1) {
                return i == 0 ? -1 : i;
            }
            buffer[offset + i] = (byte) currentChar;
        }
        return length;
    
public voidreset()
Resets this stream to the last marked location. It also resets the line count to what is was when this stream was marked.

throws
IOException if this stream is already closed, no mark has been set or the mark is no longer valid because more than {@code readlimit} bytes have been read since setting the mark.
see
#mark(int)
see
#markSupported()
since
Android 1.0

        in.reset();
        lineNumber = markedLineNumber;
        lastChar = markedLastChar;
    
public voidsetLineNumber(int lineNumber)
Sets the line number of this stream to the specified {@code lineNumber}. Note that this may have side effects on the line number associated with the last marked position.

param
lineNumber the new lineNumber value.
see
#mark(int)
see
#reset()
since
Android 1.0

        this.lineNumber = lineNumber;
    
public longskip(long count)
Skips {@code count} number of bytes in this stream. Subsequent {@code read()}'s will not return these bytes unless {@code reset()} is used. This implementation skips {@code count} number of bytes in the filtered stream and increments the line number count whenever line terminator sequences are skipped.

param
count the number of bytes to skip.
return
the number of bytes actually skipped.
throws
IOException if this stream is closed or another IOException occurs.
see
#mark(int)
see
#read()
see
#reset()
since
Android 1.0

        if (count <= 0) {
            return 0;
        }
        for (int i = 0; i < count; i++) {
            int currentChar = read();
            if (currentChar == -1) {
                return i;
            }
        }
        return count;