When to use the readNBytes() method of InputStream in Java 9?



In this article, we will learn to use the readNBytes() method of InputStream in Java 9. We will get to know the InputStream Class with its methods as readNBytes() is a method of this class. After that, we will learn about the readNBytes() method with its syntax and when to use this method, along with an example.

InputStream Class

The Java InputStream class is the superclass of all classes representing an input stream of bytes. Every subclass of InputStream always needs to introduce a method to return the next byte on request.

The following are some of the common methods of the InputStream class:

  • mark(): This method marks the current position of the input stream.
  • read(): This method reads the next byte of data from the Input Stream.
  • reset(): This method repositions the input stream to the marked position.

The readNBytes() Method

In Java 9, the readNBytes() method can be added to the InputStream class. This method reads the requested number of bytes from an input stream into the given byte array. This method blocks until len bytes of input data have been read, the end of a stream is detected, or an exception is thrown.

The readNBytes() method doesn't close an input stream. This method returns the actual number of bytes read into the buffer. Throws an IOException error if an I/O error occurs and a NullPointerException error if b is null.

Syntax

The following is the syntax for the readNBytes() Method declaration:

public int readNBytes(byte[] b, int off, int len) throws IOException

Parameters:

  • b: Represents the byte array into which the data is read.
  • off: Gives the start offset in "b" at which the data is written.
  • len: Defines the maximum number of bytes to be read in the buffer.

When to Use the readNBytes() Method?

The following are some of the use cases for the readNBytes() Method in Java:

  • The primary use of the readNBytes() method is when you need to read an exact number of bytes from the stream to the buffer.
  • Many network protocols and file formats use this method for fixed-length fields or messages.
  • This method can be useful to avoid memory problems with large files.
  • This method can be used in cryptography and digital signatures.

Java Code to Read Content of a File Using readNBytes() Method

Below is an example to read content from "Technology.txt" using the readNBytes() method in Java:

import java.io.*;
import java.util.stream.*;
import java.nio.*;
import java.nio.file.*;

public class InputStreamReadNByteMethodTest {
   InputStream inputStream = inputStreamReadNByteMethodTest.class.getResourceAsStream("Technology.txt");

   public void testReadNBytes() throws Exception {
      final byte[] data = new byte[10];
      inputStream.readNBytes(data, 0, 7);
      System.out.println(new String(data));
   }
   public static void main(String args[]) throws Exception {
      InputStreamReadNByteMethodTest t = new InputStreamReadNByteMethodTest();
      t.testReadNBytes();  
   }
}

Output

We have created a "Technology.txt" file in a "C:\Temp" folder with simple data: { "JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"}.

"JAVA",
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-12T17:36:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements