JMU
Raw Input and Output in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Background
Raw I/O Fundamentals
Important Examples
Some Streams for Input
images/streams_input.gif
Some Streams for Output
images/streams_output.gif
The OutputStream Class
The PrintStream Class
The InputStream Class
The DataInputStream Class
Reading Until End-of-Stream
javaexamples/streams/Googler.java
        package streams;


import java.awt.Desktop;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * A class that performs Google searches.
 *
 * Specifically, this class creates a new thread of execution that
 * continuously reads from a DataInputStream.  A Google search is
 * conducted for each line that is read.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Googler implements Runnable
{
    private DataInputStream    in;
    private Desktop            desktop;    
    private Thread             controlThread;

    /**
     * Explicit Value Constructor
     *
     * @param in   The DataInputStream to read from
     */
    public Googler(DataInputStream in)
    {
       this.in = in;
       desktop = Desktop.getDesktop();
    }

    /**
     * The code to execute (required by Runnable).
     */
    public void run()
    {
       String            line;
       URI               uri;

       try
       {
          while ((line = in.readLine()) != null) 
          {
             uri = new URI("http",null,"www.google.com",-1,
                           "/search","q="+line,null);

             desktop.browse(uri); // Returns immediately
          }
       }
       catch (IOException ioe) 
       {
          // Print the complete trace of the exception
          ioe.printStackTrace();
       }
       catch (URISyntaxException use)
       {
          // Print the complete trace of the exception
          use.printStackTrace();           
       }
    }    

    /**
     * Start this Googler (in its own thread of execution).
     */
    public void start()
    {
        if (controlThread == null)
        {
            controlThread = new Thread(this);
            controlThread.start();
        }
    }
}
        
Motivating Piped Streams

A Driver for the Googler

javaexamples/streams/GooglerDriver1.java
        import java.io.*;

import streams.Googler;


/**
 * A driver for the Googler class.
 *
 * This version makes it difficult/impossible to prompt the user
 * for input.
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class GooglerDriver1
{
    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
        DataInputStream   dis;
        Googler           googler;


        dis = new DataInputStream(System.in);
        googler = new Googler(dis);
        googler.start();
    }


}
        
Motivating Piped Streams (cont.)
A Factory for Piped Streams
javaexamples/streams/PipedStreamFactory.java
        package streams;

import java.io.*;
import java.util.*;

/**
 * A factory for constructing pairs of "piped streams"
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class PipedStreamFactory
{
    private PipedInputStream  in;
    private PipedOutputStream out;
    
    /**
     * Default Constructor
     */
    public PipedStreamFactory()
    {
       try 
       {
          in  = new PipedInputStream();
          out = new PipedOutputStream(in);
       }
       catch (IOException ioe) 
       {
          in  = null;
          out = null;          
       }
    }
    


    /**
     * Get the PipedInputStream
     *
     * @return       The PipedInputStream
     */
    public PipedInputStream getInputStream() throws IOException
    {
       if (in == null) throw new IOException();
       
       return in;
    }




    /**
     * Get the PipedOutputStream
     *
     * @return       The PipedOutputStream
     */
    public PipedOutputStream getOutputStream() throws IOException
    {
       if (out == null) throw new IOException();
       
       return out;
    }


}
        
Using Piped Streams

A Better Driver for the Googler

javaexamples/streams/GooglerDriver2.java
        import java.io.*;

import streams.Googler;
import streams.PipedStreamFactory;


/**
 * A driver for the Googler class.
 *
 * This version allows for line-by-line prompting
 * by using PipedStream objects
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class GooglerDriver2
{
    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws IOException
    {
        DataInputStream    console, dis;
        Googler            googler;
        PipedInputStream   pin;
        PipedOutputStream  pout;
        PipedStreamFactory factory;        
        PrintStream        out;
        String             line;


        factory = new PipedStreamFactory();        
        pin     = factory.getInputStream();
        pout    = factory.getOutputStream();

        console = new DataInputStream(System.in);
        dis     = new DataInputStream(pin);

        out     = new PrintStream(pout);

        googler = new Googler(dis);
        googler.start();
        
        System.out.print("Search for: ");
        while ((line = console.readLine()) != null)
        {
           out.println(line);
           System.out.print("Search for: ");
        }
    }
    

}
        
Using a Byte Array Stream
Putting a String in a Stream
javaexamples/streams/StreamFactory.java (Fragment: String)
            /**
     * Create a "line-based" InputStream from a String that is
     * delimited using other characters
     *
     * @param s          The source String to use
     * @param delimiter  The String used to delimit the "lines" in the source
     */
    public static InputStream createInputStream(String s, String delimiter)
    {
       ByteArrayInputStream       bis;
       
       s   = s.replaceAll(delimiter, "\r\n");
       s   = s.concat("\r\n");
       bis = new ByteArrayInputStream(s.getBytes());
       
       return bis;
    }