Package org.terrier.utility
Class LookAheadReader
- java.lang.Object
-
- java.io.Reader
-
- org.terrier.utility.LookAheadReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
public class LookAheadReader extends java.io.ReaderImplements a Reader, that encapsulates another stream, but only upto the point that a pre-defined end marker in the stream is identified. The Reader will then become endOfFile, and refuse to return any more characters from the stream. Suppose that we create an instance of a LookAheadReader with the end marker END. For the following input: a b c d END e f g... the LookAheadReader will stop after reading the string END. Note that the end marker will be missing from the parent stream.- Author:
- Craig Macdonald, Vassilis Plachouras
- See Also:
LookAheadStream
-
-
Constructor Summary
Constructors Constructor Description LookAheadReader(java.io.Reader parent, java.lang.String endMarker)Creates an instance of a LookAheadReader that will read from the given stream until the end marker is found.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the current stream, by setting the end of file flag equal to true.static voidmain(java.lang.String[] args)A testing method forvoidmark(int x)This method is not implemented.booleanmarkSupported()Support for marking is not implemented.intread()Read a character from the parent stream, first checking that it doesnt form part of the end marker.intread(char[] cbuf)Read characters into an array.intread(char[] cbuf, int offset, int len)Read characters into a portion of an array.booleanready()Indicates whether there are more characters available to read from the stream.voidreset()Reset the stream.longskip(long n)Skips n characters from the stream.
-
-
-
Constructor Detail
-
LookAheadReader
public LookAheadReader(java.io.Reader parent, java.lang.String endMarker)Creates an instance of a LookAheadReader that will read from the given stream until the end marker is found.- Parameters:
parent- Reader the stream used for reading the input/endMarker- String the marker which signifies the end of the stream.
-
-
Method Detail
-
read
public int read() throws java.io.IOExceptionRead a character from the parent stream, first checking that it doesnt form part of the end marker.- Overrides:
readin classjava.io.Reader- Returns:
- int the code of the read character, or -1 if the end of the stream has been reached.
- Throws:
java.io.IOException- if there is any error while reading from the stream.
-
read
public int read(char[] cbuf) throws java.io.IOExceptionRead characters into an array. This method will read 100 characters or the array length, and until the end of the stream is reached. NB: Uses read() internally.- Overrides:
readin classjava.io.Reader- Parameters:
cbuf- cbuf - Destination buffer- Returns:
- The number of characters read, or -1 if the end of the stream has been reached.
- Throws:
java.io.IOException- If an I/O error occurs
-
read
public int read(char[] cbuf, int offset, int len) throws java.io.IOExceptionRead characters into a portion of an array. It will try to read the specified number of characters into the buffer. NB:Implemented in terms of read().- Specified by:
readin classjava.io.Reader- Parameters:
cbuf- Destination bufferoffset- Offset at which to start storing characterslen- Maximum number of characters to read- Returns:
- The number of characters read, or -1 if the end of the stream has been reached
- Throws:
java.io.IOException- If an I/O error occurs
-
reset
public void reset() throws java.io.IOExceptionReset the stream. Attempts to reset it in some way appropriate to the particular stream, for example by positioning it to its starting point. Not all character-input streams support the reset() operation. Use at your own risk.- Overrides:
resetin classjava.io.Reader- Throws:
java.io.IOException- thrown if ParentStream.reset();
-
skip
public long skip(long n) throws java.io.IOExceptionSkips n characters from the stream. If the end of the stream has been reached before reading n characters, then it returns. NB: This method uses read() internally.- Overrides:
skipin classjava.io.Reader- Parameters:
n- long the number of characters to skip.- Returns:
- long the number of characters skipped.
- Throws:
java.io.IOException- if there is any error while reading from the stream.
-
ready
public boolean ready() throws java.io.IOExceptionIndicates whether there are more characters available to read from the stream.- Overrides:
readyin classjava.io.Reader- Returns:
- boolean true if there are more characters available for reading, otherwise it returns false.
- Throws:
java.io.IOException- if there is any error while reading from the stream.
-
close
public void close() throws java.io.IOExceptionCloses the current stream, by setting the end of file flag equal to true.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException
-
markSupported
public boolean markSupported()
Support for marking is not implemented.- Overrides:
markSupportedin classjava.io.Reader- Returns:
- boolean false.
-
mark
public void mark(int x) throws java.io.IOExceptionThis method is not implemented.- Overrides:
markin classjava.io.Reader- Throws:
java.io.IOException
-
main
public static void main(java.lang.String[] args)
A testing method for- Parameters:
args- the command line arguments, which should contain the name of a file with documents delimited by the second command line argument.
-
-