PushbackInputStream

Java-PushbackInputStream

PushbackInputStream

The PushbackInputStream can be used to push a single or multiple bytes(that was previously read) back into the input stream so that it can be reread. This is useful in situations where it is convenient for a fragment of code to read an indefinite number of data bytes that are delimited by a particular byte value; after reading the terminating byte, the code fragment can “unread” it, so that the next read operation on the input stream will reread the byte that was pushed back. For example, bytes representing the characters constituting an identifier might be terminated by a byte representing an operator character; a method whose job is to read just an identifier can read until it sees the operator and then push the operator back to be re-read.

PushbackInputStream provides various unread( ) method, which pushes back a single byte or more bytes by copying it to the front of the pushback buffer. Closing a PushbackInputStream also closes the specified underlying stream. An IOException will be thrown if there is an attempt to return a byte when the pushback buffer is full.

PushbackInputStream Constructors

Constructor Description
PushbackInputStream(InputStream in) Creates a pushback input stream with a one-byte pushback buffer.
PushbackInputStream(InputStream in, int bsize) Creates a pushback input stream that allows multiple byte to be pushed back. The size of the pushback buffer is passed in bsize.

PushbackInputStream Methods

Method Description
int  read() Reads a single byte underlying input stream.
int  read(byte[ ] b) Reads up to b.length bytes from the underlying input stream into an array of bytes.
int  read(byte[ ] b, int off, int len) Reads up to len bytes from the underlying input stream into an array of bytes.
void  unread(int b) Pushes back a single byte by copying it to the front of the pushback buffer.
void  unread(byte[] b) Pushes back an array of bytes by copying it to the front of the pushback buffer.
void  unread(byte[] b, int off, int len) Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.
boolean  ready() Tells whether this stream is ready to be read.
int    available() Return the number of remaining bytes that can be read from the input stream.
long  skip(long n) Skips over n bytes from this stream.
void  close() Closes the stream and releases any system resources associated with it.

Program

2 PushbackInputStream

Program Source

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;
import java.io.IOException;

public class Javaapp {
    
    public static void main(String[] args) throws IOException {
        
        byte by[] = {10,20,20,30,40,40,50,50,50,60,60};
        
        try(ByteArrayInputStream bais = new ByteArrayInputStream(by);
            PushbackInputStream pbis = new PushbackInputStream(bais)){
            
            int a,b;
            int i;
            while((a=pbis.read())!=-1)
            {
                i = 1;
                while((b=pbis.read())!=-1)
                {
                    if(a==b)
                    {
                        i++;
                    }else
                    {
                        pbis.unread(b);
                        break;
                    }
                }
                System.out.println(a+"("+i+")");
            }
        }
    }
}

Leave a Comment