PipedInputStream and PipedOutputStream with example



PipedInputStream

A piped input stream should be connected  to a piped output stream; the piped  input  stream then provides whatever data bytes  are written to the piped output  stream.  Typically, data is read from a PipedInputStream object by one thread  and data is written  to the corresponding  PipedOutputStream by some  other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.

public PipedInputStream(PipedOutputStream src) throws IOException

PipedInputStream()

PipedOutputStream

A piped output stream can be connected to a piped input stream to create a communications pipe. The piped output stream is the sending end of the pipe. Typically, data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread. Attempting to use both objects from a single thread is not recommended as it may deadlock the thread. The pipe is said to be broken if a thread that was reading data bytes from the connected piped input stream is no longer alive.

public PipedOutputStream(PipedInputStream snk) throws IOException

public PipedOutputStream()

Example:

public class ReaderThread extends Thread {

    PipedOutputStream out;

    public ReaderThread(PipedOutputStream out) {

        this.out = out;

    }

    @Override

    public void run() {

        try {

            System.out.println("Enter text for reading");

            BufferedReader b = new BufferedReader(new InputStreamReader(System.in));

            while (true) {

                String s = b.readLine();

                if (s.equalsIgnoreCase("end")) {

                    break;

                }

                out.write(s.getBytes());

            }

            out.flush();

            out.close();

            System.out.println("All data is provided to writer");

        } catch (Exception e) {

        }

    }

}

public class WriterThread extends Thread {

    PipedInputStream in;

    public WriterThread(PipedInputStream in){

        this.in=in;

    }

    @Override

    public void run(){

        try{

            System.out.println("Writer is started..");

            PrintWriter out=new PrintWriter("d:/a.txt");

            BufferedReader b=new BufferedReader(new InputStreamReader(in));

            while(true){

                String s=b.readLine();

                if(s==null)

                    break;

                out.println(s.toUpperCase());

            }

            out.flush();

            out.close();

            in.close();

            System.out.println("Writer is successfully saved data");

        }catch(Exception e){}

    }

}

public class ReaderWriter {

    public static void main(String as[]) {

        PipedOutputStream out=null;

        try {

            PipedInputStream in = new PipedInputStream();

            out = new PipedOutputStream(in);

            ReaderThread rt=new ReaderThread(out);

            WriterThread wt=new WriterThread(in);

            rt.start();

            wt.start();

           rt.join();

           wt.join();

        } catch (Exception ex) {

            Logger.getLogger(ReaderWriter.class.getName()).log(Level.SEVERE, null, ex);

        } finally {

            try {

                out.close();

            } catch (IOException ex) {

                Logger.getLogger(ReaderWriter.class.getName()).log(Level.SEVERE, null, ex);

            }

        }

    }

}

No comments:

Post a Comment