Java.io.PipedReader Class in Java

io.PipedReader Class in Java

This class is basically a piped character-input streams.In I/O Piped, simply means a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination.
A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.

Declaration:



public class PipedReader
  extends Reader

Constructor :

  • PipedReader() : creates a PipedReader(), that it is not connected.
  • PipedReader(int pSize) : creates a PipedReader, that it is not connected with specified pipe size.
  • PipedReader(PipedWriterStream src) : creates a PipedReader, that it is connected to PipedWriterStream – ‘src’.
  • PipedReader(PipedWriterStream src, int pSize) : creates a Piped Reader that is connected to Piped Writer with the specified pipe size.

Methods:

  • read() : java.io.PipedReader.read() reads the next character from PipedReader. This method blocks until characters are available. Returns -1 if end of the stream is detected, or an exception is thrown and the method blocks
    Syntax() :

    public int read()
    Parameters: 
    -----------
    Return :
    reads the next character from PipedReader.
    else, return-1 if end of the stream is detected.
    Exception : 
    -> IOException : if in case an IO error occurs.
    

    Implementation:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program illustrating the working of read() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws IOException
        {
            PipedReader geek_reader = new PipedReader();
            PipedWriter geek_writer = new PipedWriter();
              
                geek_reader.connect(geek_writer);
      
                // Use of read() method 
                geek_writer.write(71);
                System.out.println("using read() : " + (char)geek_reader.read());
                geek_writer.write(69);
                System.out.println("using read() : " + (char)geek_reader.read());
                geek_writer.write(75);
                System.out.println("using read() : " + (char)geek_reader.read());        
        }
    }

    chevron_right

    
    

    Output :

    using read() : G
    using read() : E
    using read() : K
  • read(char[] carray, int offset, int maxlen) : java.io.PipedReader.read(char[] carray, int offset, int maxlen) reads upto maxlen character from PipedReader Stream to the character array. The method blocks if end of Stream is reached or exception is thrown.
    Syntax :

    public int read(char[] carray, int offset, int maxlen)
    Parameters : 
    carray : buffer into which the data is to be read
    offset : starting in the destination array - 'carray'.
    maxlen : maximum length of array to be read
    Return :                                               
    next 'maxlen' bytes of the data as an integer value 
    return -1 is end of stream is reached
    Exception :
    -> IOException : if in case IO error occurs.
    
  • close() : java.io.PipedPipedReader.close() closes the PipedReader Stream and releases the allocated resources.
    Syntax :

    public void close()
    Parameters : 
    --------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • connect(PipedWriter source) : java.io.PipedReader.connect(PipedWriter source) connects the PipedReader to the ‘source’ Piped Writer and in case ‘source’ is pipes with some other stream, IO exception is thrown
    Syntax :

    public void connect(PipedWriter source)
    Parameters : 
    source : the PipedWriter to be connected to
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • ready() : java.io.PipedPipedReader.ready() tells whether the stream is ready to be read or not
    Syntax :

    public boolean ready()
    Parameters : 
    --------------
    Return :                                               
    true : if the stream is ready to be read else, false
    Exception :
    -> IOException : if in case IO error occurs.

    Java program illustrating the working of PipedReader class methods :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program illustrating the working of PipedReader
    // connect(), read(char[] carray, int offset, int maxlen),
    // close(), ready()
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws IOException
        {
            PipedReader geek_reader = new PipedReader();
            PipedWriter geek_writer = new PipedWriter();
      
            // Use of connect() : connecting geek_reader with geek_writer
            geek_reader.connect(geek_writer);
      
            geek_writer.write(71);
            geek_writer.write(69);
            geek_writer.write(69);
            geek_writer.write(75);
            geek_writer.write(83);
      
            // Use of ready() method
            System.out.print("Stream is ready to be read : "+geek_reader.ready());
      
            // Use of read(char[] carray, int offset, int maxlen)
            System.out.print("\nUse of read(carray, offset, maxlen) : ");
            char[] carray = new char[5];
            geek_reader.read(carray, 0, 5);
      
            for (int i = 0; i < 5; i++)
            {
                System.out.print(carray[i]);
            }
      
            // USe of close() method :
            System.out.println("\nClosing the stream");
            geek_reader.close();
        }
    }

    chevron_right

    
    

    Output :

    Stream is ready to be read : true
    Use of read(carray, offset, maxlen) : GEEKS
    Closing the stream
  • Next Article: Java.io.PipedWriter Class in Java

    This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
    If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.



    My Personal Notes arrow_drop_up


    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.