/*
 * Java Network Programming, Second Edition
 * Merlin Hughes, Michael Shoffner, Derek Hamner
 * Manning Publications Company; ISBN 188477749X
 *
 * http://nitric.com/jnp/
 *
 * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
 * all rights reserved; see license.txt for details.
 */

import java.io.*;

public class PipeTest extends Thread {
  protected DataInputStream dataIn;

  public PipeTest (InputStream in) {
    dataIn = new DataInputStream (in);
  }

  public void run () {
    try {
      String line;
      while ((line = dataIn.readLine ()) != null)
        System.out.println (line.toUpperCase ());
      dataIn.close ();
    } catch (IOException ex) {
      ex.printStackTrace ();
    }
  }

  public static void main (String[] args) throws IOException, InterruptedException {
    PipedOutputStream pipedOut = new PipedOutputStream ();
    PipedInputStream pipedIn = new PipedInputStream (pipedOut);
    PipeTest pipeTest = new PipeTest (pipedIn);
    pipeTest.start ();
    byte[] buffer = new byte[16];
    int numberRead;
    while ((numberRead = System.in.read (buffer)) > -1) {
      pipedOut.write (buffer, 0, numberRead);
      Thread.sleep (1000);
    }
    pipedOut.close ();
  }
}
