Test PipedInputStream and PipedOutputStream with Thread : PipedInputStream « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » File Input Output » PipedInputStream 




Test PipedInputStream and PipedOutputStream with Thread


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

public class TestPipes {
  public static void writeData(OutputStream os) {
    try {
      DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os));

      int[] numArray = 1234};

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

      out.flush();

      out.close();
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void readData(InputStream is) {
    DataInputStream in = new DataInputStream(new BufferedInputStream(is));
    boolean eof = false;
    try {
      while (!eof) {
          int iValue = in.readInt();
          System.out.println("read value = " + iValue);
      }
    catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("End of Data");
  }

  public static void main(String[] argsthrows Exception {
    final PipedOutputStream pos = new PipedOutputStream();

    final PipedInputStream pis = new PipedInputStream(pos);

    Runnable runOutput = new Runnable() {
      public void run() {
        writeData(pos);
      }
    };

    Thread outThread = new Thread(runOutput, "outThread");
    outThread.start();

    Runnable runInput = new Runnable() {
      public void run() {
        readData(pis);
      }
    };

    Thread inThread = new Thread(runInput, "inThread");
    inThread.start();
  }
}

 














Related examples in the same category
1.Defines utility routines that use Java serialization
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.