FilterReader read(char[], int, int) in Java

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

Syntax:

int java.io.FilterReader.read(char[] cbuf, int off, int len) throws IOException

This method takes three arguments. This method reads characters into a portion of an array.

Parameters: Three parameters are required for this method.

cbuf: Destination buffer.

off: Offset at which to start storing characters.

len: Maximum number of characters to read.

Returns: The number of characters read, or -1 if the end of the stream has been reached

Throws:

1. IOException - If an I/O error occurs

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

3. NullPointerException: if cbuf is null.

Approach 1: When no exception

Java

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

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

        Reader reader = new StringReader("hello");
        FilterReader filterReader = new FilterReader(reader) {
        };

        char cbuf[] = { 'a', 'c', 'd', 'e', 'r' };
        int off = 0, len = 3;

        System.out.println(filterReader.read(cbuf, off, len));
        filterReader.close();
    }
}

Output:

3


Approach 2: IOException

Java

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

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

        Reader reader = new StringReader("hello");
        FilterReader filterReader = new FilterReader(reader) {
        };

        char cbuf[] = { 'a', 'c', 'd', 'e', 'r' };
        int off = 0, len = 3;
        filterReader.close();
        System.out.println(filterReader.read(cbuf, off, len));
        filterReader.close();
    }
}

Output:

Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.StringReader.ensureOpen(StringReader.java:56) at java.base/java.io.StringReader.read(StringReader.java:91) at java.base/java.io.FilterReader.read(FilterReader.java:75)


Approach 3: IndexOutOfBoundsException 

Java

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

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

        Reader reader = new StringReader("hello");
        FilterReader filterReader = new FilterReader(reader) {
        };

        char cbuf[] = { 'a', 'c', 'd', 'e', 'r' };
        int off = 0, len = 100;
        System.out.println(filterReader.read(cbuf, off, len));
        filterReader.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.StringReader.read(StringReader.java:94) at java.base/java.io.FilterReader.read(FilterReader.java:75)


Approach 4: NullPointerException

Java

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

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

        Reader reader = new StringReader("hello");
        FilterReader filterReader = new FilterReader(reader) {
        };

        char cbuf[] = null;
        int off = 0, len = 3;

        System.out.println(filterReader.read(cbuf, off, len));
        filterReader.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "cbuf" is null at java.base/java.io.StringReader.read(StringReader.java:92) at java.base/java.io.FilterReader.read(FilterReader.java:75)


Some more methods of FilterReader class.

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

mark(int)This method marks the present position in the stream.

markSupported()This method tells whether this stream supports the mark() operation.

read()This method reads a single character.

ready()This method tells whether this stream is ready to be read.

reset()This method resets the stream.

skip(long)This method skips characters.

No comments:

Post a Comment