simpleNetworking
Class ReaderWriter

java.lang.Object
  extended by simpleNetworking.ReaderWriter

public class ReaderWriter
extends Object

A class for simple reading (via readLine and readLineIfReady) and writing (via writeLine), using a socket (i.e. through the network).

Author:
David Mutchler, using examples.example1_one_client from the Java Tutorials. May, 2009.

Constructor Summary
ReaderWriter(Socket socket)
          Constructs and stores a reader and writer for the given socket.
 
Method Summary
(package private)  void close()
          Closes all resources that this ReaderWriter uses: writer, reader, socket.
 String readLine()
          Returns the next line available from the socket associated with this connection, stripping the terminating newline.
 String readLineIfReady()
          Returns the next line available from the socket associated with this connection, stripping the terminating newline.
 void writeLine(String stringToWrite)
          Sends the given String, appending a newline to it, to the socket associated with this connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReaderWriter

public ReaderWriter(Socket socket)
             throws IOException
Constructs and stores a reader and writer for the given socket.

Parameters:
socket - Socket from which to construct a reader and writer.
Throws:
IOException - if the reader and/or writer cannot be constructed.
Method Detail

readLine

public String readLine()
                throws IOException
Returns the next line available from the socket associated with this connection, stripping the terminating newline. Blocks (waits) if no line is available yet.

Returns:
the next line available from the socket associated with this connection, but with the terminating newline stripped.
Throws:
IOException - if an IO error occurs while reading.

readLineIfReady

public String readLineIfReady()
                       throws IOException
Returns the next line available from the socket associated with this connection, stripping the terminating newline. Returns immediately (returning null) if no line is available yet.

Returns:
the next line available from the socket associated with this connection, but with the terminating newline stripped, or null if no line is available yet.
Throws:
IOException - if an IO error occurs while reading.

writeLine

public void writeLine(String stringToWrite)
               throws IOException
Sends the given String, appending a newline to it, to the socket associated with this connection.

Parameters:
stringToWrite - String to send (with a newline appended) to the socket associated with this connection.
Throws:
IOException - if an error occurs while writing.

close

void close()
Closes all resources that this ReaderWriter uses: writer, reader, socket.