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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » File Input Output » PipedInputStreamScreenshots 
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.