DataInputStream read(byte[], int, int) in Java

read(byte[], int, int): This method is available in the java.io.DataInputStream class of Java.

Syntax:

int java.io.DataInputStream.read(byte[] b, int off, int len) throws IOException

This method takes three arguments. This method reads up to len bytes of data from the contained input stream into an array of bytes. An attempt is made to readas many as len bytes, but a smaller number may be read,possibly zero.

Note: The number of bytes actually read is returned as an integer.

This method blocks until input data are available, the end of the file is detected, or an exception is thrown.

Parameters: Three parameters are required for this method.

b: the buffer into which the data is read.

off: the start offset in the destination array b.

len: the maximum number of bytes read.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Throws:

1. NullPointerException - If b is null.

2. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off.

3. IOException - if the first byte cannot be read for any reason other than end of file, the stream has been closed and the underlying input stream does not support reading after close, or another I/O error occurs.

Approach 1: When no exception

Java

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataInputStreamread2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("hello.txt");
        DataInputStream dataInputStream =
new DataInputStream(in);

        byte b[] = { 'a', 'c', 'd', 'b' };
        int off = 0, len = 2;

        System.out.println(dataInputStream.read(b, off, len));

        dataInputStream.close();
    }
}

Output:

2


Approach 2: NullPointerException 

Java

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataInputStreamread2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("hello.txt");
        DataInputStream dataInputStream =
new DataInputStream(in);

        byte b[] = null;
        int off = 0, len = 2;

        System.out.println(dataInputStream.read(b, off, len));

        dataInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.FileInputStream.readBytes(Native Method) at java.base/java.io.FileInputStream.read(FileInputStream.java:271) at java.base/java.io.DataInputStream.read(DataInputStream.java:148)



Approach 3: IndexOutOfBoundsException 

Java

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataInputStreamread2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("hello.txt");
        DataInputStream dataInputStream =
new DataInputStream(in);

        byte b[] = { 'a', 'c', 'd', 'b' };
        int off = -1, len = 2;

        System.out.println(dataInputStream.read(b, off, len));

        dataInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.FileInputStream.readBytes(Native Method) at java.base/java.io.FileInputStream.read(FileInputStream.java:271) at java.base/java.io.DataInputStream.read(DataInputStream.java:148)


No comments:

Post a Comment