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

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

Syntax:

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

This method takes three arguments. This method reads up to len bytes of data from this input stream into an array of bytes.

If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

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 an I/O error occurs.

Approach 1: When no exception

Java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

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

        File data = new File("D:\\hello.txt");
        FileInputStream file = new FileInputStream(data);

        FilterInputStream filter =
new BufferedInputStream(file);

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

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

        filter.close();
    }
}

Output:

-1


Approach 2: NullPointerException

Java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

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

        File data = new File("D:\\hello.txt");
        FileInputStream file = new FileInputStream(data);

        FilterInputStream filter =
new BufferedInputStream(file);

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

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

        filter.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:335)



Approach 3: IndexOutOfBoundsException

Java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

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

        File data = new File("D:\\hello.txt");
        FileInputStream file = new FileInputStream(data);

        FilterInputStream filter =
new BufferedInputStream(file);

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

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

        filter.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:336)


Other methods of FilterInputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking the next caller of a method for this input stream.

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.

read()This method reads the next byte of data from this input stream.

read(byte[])This method reads up to b.length bytes of data from this input stream into an array of bytes.

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 the input stream.


No comments:

Post a Comment