public abstract static class FileBasedSource.FileBasedReader<T> extends ByteOffsetBasedSource.ByteOffsetBasedReader<T>
reader that implements code common to readers of
FileBasedSources.
This reader uses a ReadableByteChannel created for the file represented by the
corresponding source to efficiently move to the correct starting position defined in the
source. Subclasses of this reader should implement startReading(java.nio.channels.ReadableByteChannel) to get access to this
channel. If the source corresponding to the reader is for a subrange of a file the
ReadableByteChannel provided is guaranteed to be an instance of the type
SeekableByteChannel, which may be used by subclass to traverse back in the channel to
determine the correct starting position.
RangeTracker, using
isAtSplitPoint().
Sequential reading is implemented using readNextRecord().
Then FileBasedReader implements "reading a range [A, B)" in the following way.
start() opens the file
start() seeks the SeekableByteChannel to A (reading offset ranges for
non-seekable files is not supported) and calls startReading()
start() calls advance() once, which, via readNextRecord(),
locates the first record which is at a split point AND its offset is at or after A.
If this record is at or after B, advance() returns false and reading is finished.
true sequential reading starts and
advance() will be called repeatedly
advance() calls readNextRecord() on the subclass, and stops (returns false) if
the new record is at a split point AND the offset of the new record is at or after B.
Since this class implements Source.Reader it guarantees thread safety. Abstract
methods defined here will not be accessed by more than one thread concurrently.
rangeTracker| Constructor and Description |
|---|
FileBasedReader(FileBasedSource<T> source)
Subclasses should not perform IO operations at the constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
advance()
Advances the reader to the next valid record.
|
void |
close()
Closes any
ReadableByteChannel created for the current reader. |
FileBasedSource<T> |
getCurrentSource()
Returns a
Source describing the same input that this Reader reads
(including items already read). |
protected abstract boolean |
isAtSplitPoint()
Specifies if the current record of the reader is at a split point.
|
protected abstract boolean |
readNextRecord()
Reads the next record from the channel provided by
startReading(java.nio.channels.ReadableByteChannel). |
boolean |
start()
Initializes the reader and advances the reader to the first record.
|
protected abstract void |
startReading(ReadableByteChannel channel)
Performs any initialization of the subclass of
FileBasedReader that involves IO
operations. |
getCurrentOffset, getFractionConsumed, splitAtFractiongetCurrentTimestampclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetCurrent, getCurrentTimestamppublic FileBasedReader(FileBasedSource<T> source)
startReading(java.nio.channels.ReadableByteChannel) method is invoked.public FileBasedSource<T> getCurrentSource()
Source.ReaderSource describing the same input that this Reader reads
(including items already read).
A reader created from the result of getCurrentSource, if consumed, MUST
return the same data items as the current reader.
getCurrentSource in interface BoundedSource.BoundedReader<T>getCurrentSource in interface Source.Reader<T>getCurrentSource in class ByteOffsetBasedSource.ByteOffsetBasedReader<T>public final boolean start()
throws IOException
Source.Reader This method should be called exactly once. The invocation should occur prior to calling
Source.Reader.advance() or Source.Reader.getCurrent(). This method may perform expensive operations that
are needed to initialize the reader.
true if a record was read, false if there is no more input available.IOExceptionpublic final boolean advance()
throws IOException
Source.Readertrue if a record was read, false if there is no more input available.IOExceptionpublic void close()
throws IOException
ReadableByteChannel created for the current reader. This implementation is
idempotent. Any close() method introduced by a subclass must be idempotent and must
call the close() method in the FileBasedReader.IOExceptionprotected abstract boolean isAtSplitPoint()
This returns true if the last record returned by readNextRecord() is at a
split point, false otherwise. Please refer to FileBasedReader for the definition of split points.
protected abstract void startReading(ReadableByteChannel channel) throws IOException
FileBasedReader that involves IO
operations. Will only be invoked once and before that invocation the base class will seek the
channel to the source's starting offset.
Provided ReadableByteChannel is for the file represented by the source of this
reader. Subclass may use the channel to build a higher level IO abstraction, e.g., a
BufferedReader or an XML parser.
If the corresponding source is for a subrange of a file, channel is guaranteed to
be an instance of the type SeekableByteChannel.
After this method is invoked the base class will not be reading data from the channel or adjusting the position of the channel. But the base class is responsible for properly closing the channel.
channel - a byte channel representing the file backing the reader.IOExceptionprotected abstract boolean readNextRecord()
throws IOException
startReading(java.nio.channels.ReadableByteChannel). Methods
Source.Reader.getCurrent(), ByteOffsetBasedSource.ByteOffsetBasedReader.getCurrentOffset(), and isAtSplitPoint() should return
the corresponding information about the record read by the last invocation of this method.
Note that this method will be called the same way for reading the first record in the source (file or offset range in the file) and for reading subsequent records. It is up to the subclass to do anything special for locating and reading the first record, if necessary.
true if a record was successfully read, false if the end of the
channel was reached before successfully reading a new record.IOException