Java - LineNumberInputStream read(byte[] b, int off, int len) method



Description

The Java LineNumberInputStream read(byte[] b, int off, int len) method reads up to len bytes from this input stream into an array of bytes. This method blocks until input is available.

Declaration

Following is the declaration for java.io.LineNumberInputStream.read(byte[] b, int off, int len) method −

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

Parameters

  • b − The buffer into which the data is read.

  • off − The start offset of the data.

  • len − The maximum number of bytes read.

Return Value

The method returns the total number of bytes read into the buffer, else -1 if there is no more data.

Exception

IOException − If an I/O error occurs.

Example - Usage of LineNumberInputStream read(byte[] b, int off, int len) method

The following example shows the usage of Java LineNumberInputStream read(byte[] b, int off, int len) method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      byte[] buf = new byte[5];
      int i;
      char c;
      
      try {
         // create new input stream
         fis = new FileInputStream("test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // read bytes to the buffer
         i = lnis.read(buf, 2, 3);
         System.out.println("The number of char read: "+i);
               
         // for each byte in buffer
         for(byte b:buf) {
         
            // if byte is zero
            if(b == 0)
               c = '-';
            else
               c = (char)b;
      
            // print char
            System.out.print(c);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();      
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

The number of char read: 3
--ABC

Example - Reading from a File

The following example shows the usage of Java LineNumberInputStream read(byte[] b, int off, int len) method. This example reads a file and prints the contents while tracking the line numbers.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try {
         FileInputStream fis = new FileInputStream("example.txt");
         LineNumberInputStream lnis = new LineNumberInputStream(fis);

         byte[] buffer = new byte[50];
         int bytesRead;

         while ((bytesRead = lnis.read(buffer, 0, buffer.length)) != -1) {
            System.out.print(new String(buffer, 0, bytesRead));
            System.out.println(" (Line Number: " + lnis.getLineNumber() + ")");
         }

         lnis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "JavaProgramming")

Let us compile and run the above program, this will produce the following result−

JavaProgramming
 (Line Number: 1)

Explanation

  • A FileInputStream reads data from example.txt.

  • LineNumberInputStream wraps FileInputStream to track line numbers.

  • The read(byte[] b, int off, int len) method reads data into a buffer.

  • The contents are printed along with the current line number.

Example - Reading from a String (Using ByteArrayInputStream)

The following example shows the usage of Java LineNumberInputStream read(byte[] b, int off, int len) method. This example uses ByteArrayInputStream to simulate reading from a file.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      String text = "Hello World\nThis is Java\nLineNumberInputStream Example";
      byte[] data = text.getBytes();

      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      LineNumberInputStream lnis = new LineNumberInputStream(bais);

      byte[] buffer = new byte[20];
      int bytesRead;

      while ((bytesRead = lnis.read(buffer, 0, buffer.length)) != -1) {
         System.out.print(new String(buffer, 0, bytesRead));
         System.out.println(" (Line Number: " + lnis.getLineNumber() + ")");
      }

      lnis.close();
   }
}

Output(if example.txt contains "Microservices")

Let us compile and run the above program, this will produce the following result−

Hello World
This is  (Line Number: 1)
Java
LineNumberInput (Line Number: 2)
Stream Example (Line Number: 2)

Explanation

  • A string is converted into a byte array.

  • ByteArrayInputStream is used as an input source.

  • LineNumberInputStream wraps it to track line numbers.

  • The read(byte[] b, int off, int len) method reads chunks of data.

java_io_linenumberinputstream.htm