Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2008, 08:34 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default How to use DataOutputStream and DataInputStream with PipedWriter and PipedReader
This Java tip shows how to use DataOutputStream and DataInputStream classes with PipedWriter and PipedReader classes to allow communication between two threads.

Code:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedBytes extends Object {
  public static void writeStuff(OutputStream rawOut) {
    try {
      DataOutputStream out = new DataOutputStream(
          new BufferedOutputStream(rawOut));

      int[] data = { 82, 105, 99, 104, 97, 114, 100, 32, 72, 121, 100,
          101 };

      for (int i = 0; i < data.length; i++) {
        out.writeInt(data[i]);
      }

      out.flush();
      out.close();
    } catch (IOException x) {
      x.printStackTrace();
    }
  }

  public static void readStuff(InputStream rawIn) {
    try {
      DataInputStream in = new DataInputStream(new BufferedInputStream(
          rawIn));

      boolean eof = false;
      while (!eof) {
        try {
          int i = in.readInt();
          System.out.println("just read: " + i);
        } catch (EOFException eofx) {
          eof = true;
        }
      }

      System.out.println("Read all data from the pipe");
    } catch (IOException x) {
      x.printStackTrace();
    }
  }

  public static void main(String[] args) {
    try {
      final PipedOutputStream out = new PipedOutputStream();

      final PipedInputStream in = new PipedInputStream(out);

      Runnable runA = new Runnable() {
        public void run() {
          writeStuff(out);
        }
      };

      Thread threadA = new Thread(runA, "threadA");
      threadA.start();

      Runnable runB = new Runnable() {
        public void run() {
          readStuff(in);
        }
      };

      Thread threadB = new Thread(runB, "threadB");
      threadB.start();
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
}
__________________
"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use BufferedWriter and BufferedReader with PipedWriter and PipedReader Java Tip java.io 0 06-26-2008 08:33 PM
File I/O with DataOutputStream Tzaphiel New To Java 0 12-16-2007 10:39 PM
DataInputStream readLine() ravian New To Java 2 11-26-2007 11:44 PM
Help with BufferedInputstream and/or DataInputStream barney New To Java 1 08-07-2007 08:16 AM


All times are GMT +2. The time now is 08:18 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org