uk.ac.gla.dcs.renaissance.ir
Class StringBufferReader

java.lang.Object
  extended by java.io.Reader
      extended by uk.ac.gla.dcs.renaissance.ir.StringBufferReader
All Implemented Interfaces:
Closeable, Readable

public class StringBufferReader
extends Reader

A non-blocking Reader for reading the text that appears in a shared StringBuffer.

This class in intended to be thread-safe, and to be used both from inside and outside a vat. When used in conjunction with StringWriter or our own StringBufferWriter, the pair is like a non-blocking variant of PipedReader/PipedWriter. The sharing must be set up at creation time, as a StringBufferReader encapsulates its StringBuffer.

In order to be non-blocking, we make the following change to the Reader contract: If a read(..) is attempted on a non-ready StringBufferReader, it will throw an IOException, rather than blocking or returning zero.

In order to be thread-safe, it uses the StringBuffer as its Reader.lock.

This class does no newline conversion. This should normally be done by the object that writes into the shared StringBuffer.

Author:
Mark S. Miller, Terry Stanley

Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
StringBufferReader(StringBuffer buf)
          Makes a Reader for reading from 'buf'
 
Method Summary
 int available()
          How many characters are immediately available? If the buffer is empty, then give the filler one chance and try again.
 void close()
           
 boolean isDone()
          Are we at end-of-stream?
 void mark(int readAheadLimit)
          readAheadLimit is ignored
 int read(char[] cbuf, int off, int len)
          Reads up to len chars, as many as it can without blocking.
 Character readChar()
          Returns the next available character, or null at end of file.
 String readReady()
          Returns as much as is available, or null at end-of-stream.
 String readString(int size)
          Reads no more than 'size' characters from the file, and return them as a String.
 String readText()
          Reads everything else and returns it.
 boolean ready()
          True if either at least one character is available, or we're at the end-of-stream.
 void reset()
           
 
Methods inherited from class java.io.Reader
markSupported, read, read, read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringBufferReader

public StringBufferReader(StringBuffer buf)
Makes a Reader for reading from 'buf'

Method Detail

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException

available

public int available()
              throws IOException
How many characters are immediately available? If the buffer is empty, then give the filler one chance and try again. If instead you want to know the largest number of characters that are available, and buffer then as a result, then call fill() first.

Currently, this entire operation is performed with 'lock' held. XXX Should we release 'lock' around the call to myOptFiller?

Throws:
IOException

ready

public boolean ready()
              throws IOException
True if either at least one character is available, or we're at the end-of-stream.

Overrides:
ready in class Reader
Throws:
IOException

isDone

public boolean isDone()
               throws IOException
Are we at end-of-stream?

Throws:
IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException
Reads up to len chars, as many as it can without blocking.

If it can't read even a single char without blocking, throw an IOException rather than blocking or returning 0.

Specified by:
read in class Reader
Throws:
IOException

mark

public void mark(int readAheadLimit)
          throws IOException
readAheadLimit is ignored

Overrides:
mark in class Reader
Throws:
IOException

reset

public void reset()
           throws IOException
Overrides:
reset in class Reader
Throws:
IOException

readChar

public Character readChar()
                   throws IOException
Returns the next available character, or null at end of file.

If nothing is currently available, throws rather than blocking or returning -1.

Throws:
IOException

readString

public String readString(int size)
                  throws IOException
Reads no more than 'size' characters from the file, and return them as a String. If at end-of-file, return null.

Throws:
IOException

readReady

public String readReady()
                 throws IOException
Returns as much as is available, or null at end-of-stream.

If nothing is currently available, return "" rather than throwing.

Throws:
IOException

readText

public String readText()
                throws IOException
Reads everything else and returns it.

If everything else isn't yet available, then throws rather than blocking or returning something funny, but characters have still been consumed anyway. If this doesn't suit you, use mark() and reset() to protect yourself.

Throws:
IOException


Copyright © 2011. All Rights Reserved.