Pipe Test : Input Output Stream « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » File Input Output » Input Output StreamScreenshots 
Pipe Test

/**
 @version 1.20 1999-04-23
 @author Cay Horstmann
 */

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;
import java.util.Random;

public class PipeTest {
  public static void main(String args[]) {
    try /* set up pipes */
      PipedOutputStream pout1 = new PipedOutputStream();
      PipedInputStream pin1 = new PipedInputStream(pout1);

      PipedOutputStream pout2 = new PipedOutputStream();
      PipedInputStream pin2 = new PipedInputStream(pout2);

      /* construct threads */

      Producer prod = new Producer(pout1);
      Filter filt = new Filter(pin1, pout2);
      Consumer cons = new Consumer(pin2);

      /* start threads */

      prod.start();
      filt.start();
      cons.start();
    catch (IOException e) {
    }
  }
}

class Producer extends Thread {
  public Producer(OutputStream os) {
    out = new DataOutputStream(os);
  }

  public void run() {
    while (true) {
      try {
        double num = rand.nextDouble();
        out.writeDouble(num);
        out.flush();
        sleep(Math.abs(rand.nextInt() 1000));
      catch (Exception e) {
        System.out.println("Error: " + e);
      }
    }
  }

  private DataOutputStream out;

  private Random rand = new Random();
}

class Filter extends Thread {
  public Filter(InputStream is, OutputStream os) {
    in = new DataInputStream(is);
    out = new DataOutputStream(os);
  }

  public void run() {
    for (;;) {
      try {
        double x = in.readDouble();
        total += x;
        count++;
        if (count != 0)
          out.writeDouble(total / count);
      catch (IOException e) {
        System.out.println("Error: " + e);
      }
    }
  }

  private DataInputStream in;

  private DataOutputStream out;

  private double total = 0;

  private int count = 0;
}

class Consumer extends Thread {
  public Consumer(InputStream is) {
    in = new DataInputStream(is);
  }

  public void run() {
    for (;;) {
      try {
        double avg = in.readDouble();
        if (Math.abs(avg - old_avg0.01) {
          System.out.println("Current average is " + avg);
          old_avg = avg;
        }
      catch (IOException e) {
        System.out.println("Error: " + e);
      }
    }
  }

  private double old_avg = 0;

  private DataInputStream in;
}

           
       
Related examples in the same category
1. Buffer EqualityBuffer Equality
2. Input and output with human-readable text filesInput and output with human-readable text files
3. Input and output of primitive values with binary filesInput and output of primitive values with binary files
4. Input and output of arrays and objects with binary filesInput and output of arrays and objects with binary files
5. Input and output of primitive values with random access binary filesInput and output of primitive values with random access binary files
6. Input and output using strings and string buffersInput and output using strings and string buffers
7. Timing Unbuffered Reads
8. Reading Bytes from a DataInputStreamReading Bytes from a DataInputStream
9. Comparing Buffered and Unbuffered Writing Performance
10. Read a file and print, using LineReader and System.out
11. Demonstrate ProgressMeterInputStream
12. Converting text to and from ByteBuffersConverting text to and from ByteBuffers
13. Demonstrates standard I/O redirection
14. Creating a very large file using mappingCreating a very large file using mapping
15. Demonstrates the use of the File class to create directories and manipulate files
16. Mapping an entire file into memory for reading
17. Mapped IOMapped IO
18. What happens when the entire file isn't in your mapping region
19. Object serializationObject serialization
20. Locking portions of a mapped fileLocking portions of a mapped file
21. File read and writeFile read and write
www.j_a___v_a___2s._c_o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.