Class LineNumberInputStream in Java

digitization, transformation, binary

Class LineNumberInputStream in Java

A line is a sequence of bytes ending either a carriage return character(“r”), a new line character(“n”) or a carriage return character followed immediately by a line feed character. In all these cases, the line terminating character(s) are returned as a single newline character.
LineNumberInputStream class is an input stream filter that provides the added functionality of keeping track of the current line number. The line number begins at zero and it is incremented by one when a read returns a newline character.

LineNumberInputStream keeps track of the line numbers in the InputStream(This class is a FilterInputStream that keeps track of the number of lines of data that have been read).

Ads code goes here

The line number starts at 0 and is incremented each time an end-of-line character is encountered.

LineNumberInputStream recognizes “\n”, “\r”, or “\r\n” as the end of a line. Regardless of
the end-of-line character it reads, LineNumberInputStream returns only “\n”.

We can call getLineNumber()[getLine Number () returns the current line number,] and setLineNumber()[setLineNumber () sets the line number of the current line.] to get a specific line and to work on a line.

The constructor takes InputStream as an argument to add line numbering. We need to attach this to an interface object. Subsequent lines are numbered starting from that number.

This class is deprecated in Java 1.1 because it does not properly convert bytes to characters. We need to use LineNumber instead.

READ  Abstract Interface FilenameFilter in Java

The structure of the class LineNumberInputStream is given as:

public class java.io.LineNumberInputStream extends java.io.FilterInputStream{
//constructor
public LineNumberInputStream(InputStream in);
//constructs a new LineNumberInputStream  which reads its input stream.
//methods
public int available()throws IOException; //Overrides FilterInputStream
public int read()throws IOException; //Overrides FilterInputStream
public int read(byte b[],int offSet,int length)throws IOException;//Overrides FilterInputStream
public long skip(long l)throws IOException;//Overrides FilterInputStream
public void mark(int readLimit);// Overrides FilterInputStream
public void reset()throws IOException;//Overrides FilterInputStream;
public int getLineNumber();
public void setLineNumber(int lineNumber);
}
 

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
The details about the methods are given as:

public LineNumberInputStream(InputStream in);

public LineNumberInputStream(InputStream in) constructor creates a LineNumberInputStream that gets its data from in.

Parameter
in-The input stream to use.

public int available()throws IOException; //Overrides FilterInputStream

public int available()throws IOException method returns the number of bytes of input that can be read without having to wait for more input to become available.

This method returns the number of bytes that can be read without blocking.

public int read()throws IOException; //Overrides FilterInputStream

public int read()throws IOException method reads a byte of input from the underlying stream. If “\n”, “\r”, or “\r\n” is read from the stream, “\n” is returned. Otherwise, the byte read from the underlying stream is returned verbatim.

The method blocks until the byte is read, the end of the stream is encountered, or an exception is thrown.

This method returns the next byte of data or -1 if the end of the stream is encountered.

public int read(byte b[],int offSet,int length)throws IOException;//Overrides FilterInputStream

public int read(byte b[], int offset, int length)throws IOException method reads up to len bytes of input into the given array starting at index off. If “\n”, “\r”, or “\r\n” is read from the stream, “\n” is returned. The method does this by repeatedly calling read(), which is not efficient, especially if the underlying stream is not buffered.

READ  Class MuiticastSocket in Java

The method blocks until some data are available.

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.
Parameter
b– An array of bytes to be filled from the stream.
off– An offset into the byte array.
len– The number of bytes to read.

public long skip(long l)throws IOException;//Overrides FilterInputStream

public long skip(long l)throws IOException method skips n bytes of input. Note that since LineNumberInputStream returns “\r\n” as a single character, “\n”, this method may skip over more bytes than we expect.

This method returns the actual number of bytes skipped.
Parameter
l-The number of bytes to skip.

public void mark(int readLimit);// Overrides FilterInputStream

public void mark(int readLimit) method instructs the LineNumberInputStream to remember its current position.

A subsequent call to reset() causes the object to return to that saved position and thus reread a portion of the input.

The method calls the mark() method of the underlying stream, so it only works if the underlying stream supports mark() and reset().

Parameter
readlimit-The maximum number of bytes that can be read before the saved position becomes invalid.

public void reset()throws IOException;//Overrides FilterInputStream;

public void reset()throws IOException method calls the reset() method of the underlying stream.

If the underlying stream supports mark() and reset(), this method sets the position of the stream to a position that was saved by a previous call to mark().

Subsequent bytes read from this stream will begin from the saved position and continue normally. The method also restores the line number to its correct value for the mark location.

READ  Operators in Java

The method only works if the underlying stream supports mark() and reset().

public int getLineNumber();

public int getLineNumber() method returns the current line number.
This method returns the current line number.

public void setLineNumber(int lineNumber);

public void setLineNumber(int lineNumber) method sets the current line number of the LineNumberInputStream. The method does not change the position of the stream.
Parameter
lineNumber-The new line number.
Apart from these LineNumberInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

LineNumberInputStream class also has inherited methods from FilterInputStream. They are as follows:

  • markSupported()
  • close()
  • read(byte[])

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *