InputStream readNBytes(int) in Java

readNBytes(int): This method is available in the java.io.InputStream class of Java.

Syntax:

byte[] java.io.InputStream.readNBytes(int len) throws IOException

This method takes one argument. This method reads up to a specified number of bytes from the input stream. This method blocks until the requested number of bytes have been read, the end of the stream is detected, or an exception is thrown. This method does not close the input stream.

The length of the returned array equals the number of bytes read from the stream. If len is zero, then no bytes are read and an empty byte array is returned. Otherwise, up to len bytes are read from the stream. Fewer than len bytes may be read if the end of the stream is encountered.

Note:

When this stream reaches the end of the stream, further invocations of this method will return an empty byte array.

Parameters: One parameter is required for this method.

len: the maximum number of bytes to read.

Returns: a byte array containing the bytes read from this input stream.

Throws:

1. IllegalArgumentException - if the length is negative.

2. IOException - if an I/O error occurs.

3. OutOfMemoryError - if an array of the required size cannot be allocated.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class InputStreamreadNBytes {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        int len = 2;

        try {
            System.out.println(Arrays.toString(inputStream.readNBytes(len)));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException occurs");
        }

        inputStream.close();
    }
}

Output:

[]


Approach 2: IllegalArgumentException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class InputStreamreadNBytes {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        int len = -1;

        try {
            System.out.println(Arrays.toString(inputStream.readNBytes(len)));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException occurs");
        }

        inputStream.close();
    }
}

Output:

IllegalArgumentException occurs

Approach 3: IOException

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class InputStreamreadNBytes {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        int len = 2;

        inputStream.close();
        try {
            System.out.println(Arrays.toString(inputStream.readNBytes(len)));
        } catch (IOException e) {
            System.out.println("IOException occurs");
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException occurs");
        }

    }
}

Output:

IOException occurs



Some other methods of InputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when the end of the stream is detected.

close()This method closes this input stream and releases any system resources associated with the stream.

mark(int) This method marks the current position in this input stream.

markSupported()This method tests if this input stream supports the mark and reset methods.

InputStream.nullInputStream()This method returns a new InputStream that reads no bytes. The returned stream is initially open.

read()This method reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.

read(byte[])This method reads some number of bytes from the input stream and stores them into the buffer array b.

read(byte[], int, int)his method reads up to len bytes of data from the input stream into an array of bytes.

readAllBytes()This method reads all remaining bytes from the input stream.

readNBytes(byte[], int, int)This method reads the requested number of bytes from the input stream into the given byte array.

reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.

skip(long) This method skips over and discards n bytes of data from this input stream.

skipNBytes(long)This method skips over and discards exactly n bytes of data from this input stream.

transferTo(OutputStream)This method reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.


No comments:

Post a Comment