com.cburch.editor.util
Class DocumentReader

java.lang.Object
  extended by java.io.Reader
      extended by com.cburch.editor.util.DocumentReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class DocumentReader
extends java.io.Reader

A reader interface for a document. This is intended to be used where a existing code expects a Reader, but we have a Document.

The particular application it was design for is in tokenizing a Document: JFlex-generated code depends on fetching tokens from a Reader, but in our case we want to fetch tokens from a Document.

Version:
0.1 2005-05-31
Author:
Stephen Ostermiller, Carl Burch

Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
DocumentReader(javax.swing.text.Document document)
          Constructs a reader for the given document.
 
Method Summary
 void close()
          Does nothing.
 javax.swing.text.Document getDocument()
          Returns the document underlying this DocumentReader.
 void mark(int readAheadLimit)
          Saves the current position for reset.
 boolean markSupported()
          Returns true, since this reader supports mark and reset.
 int read()
          Reads a single character and advances the internal cursor one step forward.
 int read(char[] cbuf)
          Reads and fills the given buffer.
 int read(char[] cbuf, int off, int len)
          Reads and fills the buffer.
 boolean ready()
          Returns true since the Document is always available for reading.
 void reset()
          Resets this reader to the last mark, or to the document's beginning if a mark has not been set.
 void seek(long n)
          Seeks to the given position in the document.
 long skip(long n)
          Skips past characters in the document.
 
Methods inherited from class java.io.Reader
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DocumentReader

public DocumentReader(javax.swing.text.Document document)
Constructs a reader for the given document.

Parameters:
document - the document to be read.
Method Detail

getDocument

public javax.swing.text.Document getDocument()
Returns the document underlying this DocumentReader.

Returns:
the underlying document.

close

public void close()
Does nothing. With a subsequent seek, we can still use this Reader after the close. (It does reset the mark, however, so that a subsequent reset will go to the document's beginning.)

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader

mark

public void mark(int readAheadLimit)
Saves the current position for reset.

Overrides:
mark in class java.io.Reader
Parameters:
readAheadLimit - ignored.

markSupported

public boolean markSupported()
Returns true, since this reader supports mark and reset.

Overrides:
markSupported in class java.io.Reader
Returns:
true.

read

public int read()
Reads a single character and advances the internal cursor one step forward.

Overrides:
read in class java.io.Reader
Returns:
the next character code, or -1 if the end of the document has been reached.

read

public int read(char[] cbuf)
Reads and fills the given buffer. This method always fills the buffer unless the end of the document is reached.

Overrides:
read in class java.io.Reader
Parameters:
cbuf - the buffer to fill.
Returns:
the number of characters read or -1 if no more characters are available in the document.

read

public int read(char[] cbuf,
                int off,
                int len)
Reads and fills the buffer. This method always fills the buffer unless the end of the document is reached.

Specified by:
read in class java.io.Reader
Parameters:
cbuf - the buffer to fill.
off - offset into the buffer to begin the fill.
len - maximum number of characters to put in the buffer.
Returns:
the number of characters read or -1 if no more characters are available in the document.

ready

public boolean ready()
Returns true since the Document is always available for reading.

Overrides:
ready in class java.io.Reader
Returns:
true.

reset

public void reset()
Resets this reader to the last mark, or to the document's beginning if a mark has not been set.

Overrides:
reset in class java.io.Reader

skip

public long skip(long n)
Skips past characters in the document. This method always skips the maximum number of characters unless the end of the file is reached.

Overrides:
skip in class java.io.Reader
Parameters:
n - maximum number of characters to skip.
Returns:
the actual number of characters skipped.

seek

public void seek(long n)
Seeks to the given position in the document.

Parameters:
n - the offset to which to seek.