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

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

Syntax:

int java.io.PushbackInputStream.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.

This method first reads any pushed-back bytes; after that, if fewer than len bytes have been read then it reads from the underlying input stream. If len is not zero, the method blocks until at least 1 byte of 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 this input stream has been closed by invoking its close() method, or an I/O error occurs.

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

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

        InputStream in = new InputStream() {

            @Override
            public int read() throws IOException {
                return 10;
            }
        };
        int size = 10;
        PushbackInputStream pushbackInputStream =
new PushbackInputStream(in, size);

        byte b[] = { 'a', 'b', 'c', 'd', 'e' };

        int off = 0, len = 3;

        System.out.println(pushbackInputStream.read(b, off, len));
        pushbackInputStream.close();
    }
}

Output:

3


Approach 2: NullPointerException 

Java

package com.PushbackInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

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

        InputStream in = new InputStream() {

            @Override
            public int read() throws IOException {
                return 10;
            }
        };
        int size = 10;
        PushbackInputStream pushbackInputStream =
new PushbackInputStream(in, size);

        byte b[] = null;

        int off = 0, len = 3;

        System.out.println(pushbackInputStream.read(b, off, len));
        pushbackInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.PushbackInputStream.read(PushbackInputStream.java:164) at com.PushbackInputStream.PushbackInputStreamread2.main(PushbackInputStreamread2.java:24)


Approach 3: IndexOutOfBoundsException 

Java

package com.PushbackInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

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

        InputStream in = new InputStream() {

            @Override
            public int read() throws IOException {
                return 10;
            }
        };
        int size = 10;
        PushbackInputStream pushbackInputStream =
new PushbackInputStream(in, size);

        byte b[] = { 'a', 'b', 'c', 'd', 'e' };

        int off = 0, len = 13;

        System.out.println(pushbackInputStream.read(b, off, len));
        pushbackInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.PushbackInputStream.read(PushbackInputStream.java:166) at com.PushbackInputStream.PushbackInputStreamread2.main(PushbackInputStreamread2.java:24)


Approach 4: IOException

Java

package com.PushbackInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

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

        InputStream in = new InputStream() {

            @Override
            public int read() throws IOException {
                return 10;
            }
        };
        int size = 10;
        PushbackInputStream pushbackInputStream =
new PushbackInputStream(in, size);

        byte b[] = { 'a', 'b', 'c', 'd', 'e' };

        int off = 0, len = 3;
        pushbackInputStream.close();
        System.out.println(pushbackInputStream.read(b, off, len));

    }
}

Output:

Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.PushbackInputStream.ensureOpen(PushbackInputStream.java:74) at java.base/java.io.PushbackInputStream.read(PushbackInputStream.java:162) at com.PushbackInputStream.PushbackInputStreamread2.main(PushbackInputStreamread2.java:24)


Some other methods of PushbackInputStream

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 invocation of a method for this input 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, which it does not.

PushbackInputStream(InputStream)This method creates a PushbackInputStream with a 1-byte pushback buffer and saves its argument, the input stream, for later use.

PushbackInputStream(InputStream, int)This method creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream, for later use.

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

read(byte[], int, int)This method reads up to len 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 this input stream.

unread(byte[])This method pushes back an array of bytes by copying it to the front of the pushback buffer.

unread(int)This method pushes back a byte by copying it to the front of the push-back buffer.

unread(byte[], int, int)This method pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.

No comments:

Post a Comment