PushbackInputStream unread(byte[]) in Java

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

Syntax:

void java.io.PushbackInputStream.unread(byte[] b) throws IOException

This method takes one argument. This method pushes back an array of bytes by copying it to the front of the pushback buffer.

Note: After this method returns, the next byte to be read will have the value b[0], the byte after that will have the value b[1], and so forth.

Parameters: One parameter is required for this method.

b: the byte array to push back.

Throws:

1. NullPointerException - If b is null.

2. IOException - If there is not enough room in the pushback buffer for the specified number of bytes or this input stream has been closed by invoking its close() method.

Approach 1: When no exception

Java

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

public class PushbackInputStreamunread {
    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' };
        pushbackInputStream.unread(b);

        System.out.println("Successfully unread");
        pushbackInputStream.close();
    }
}

Output:

Successfully unread


Approach 2: NullPointerException 

Java

package com.PushbackInputStream;

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

public class PushbackInputStreamunread {
    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;
        pushbackInputStream.unread(b);

        System.out.println("Successfully unread");
        pushbackInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.PushbackInputStream.unread(PushbackInputStream.java:250) at com.PushbackInputStream.PushbackInputStreamunread.main(PushbackInputStreamunread.java:21)


Approach 3: IOException 

Java

package com.PushbackInputStream;

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

public class PushbackInputStreamunread {
    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' };
        pushbackInputStream.close();
        pushbackInputStream.unread(b);

        System.out.println("Successfully unread");

    }
}

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.unread(PushbackInputStream.java:227) at java.base/java.io.PushbackInputStream.unread(PushbackInputStream.java:250) at com.PushbackInputStream.PushbackInputStreamunread.main(PushbackInputStreamunread.java:22)


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