Package org.openscience.cdk.io.random
Class RandomAccessReader
- java.lang.Object
-
- org.openscience.cdk.io.random.DefaultRandomAccessChemObjectReader
-
- org.openscience.cdk.io.random.RandomAccessReader
-
- All Implemented Interfaces:
Iterator<IChemObject>
,ListIterator<IChemObject>
,IRandomAccessChemObjectReader<IChemObject>
- Direct Known Subclasses:
RandomAccessSDFReader
public abstract class RandomAccessReader extends DefaultRandomAccessChemObjectReader implements IRandomAccessChemObjectReader<IChemObject>
Random access to text files of compounds. Reads the file as a text and builds an index file, if the index file doesn't already exist. The index stores offset, length and a third field reserved for future use. Subsequent access for a record N uses this index to seek the record and return the molecule. Useful for very big files.- Author:
- Nina Jeliazkova <nina@acad.bg>
- Source code:
- main
- Belongs to CDK module:
- io
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
b
protected IChemObjectBuilder
builder
protected ISimpleChemObjectReader
chemObjectReader
protected int
currentRecord
protected IOSetting[]
headerOptions
protected long[][]
index
protected boolean
indexCreated
protected int
indexVersion
protected static ILoggingTool
logger
protected RandomAccessFile
raFile
protected int
records
-
Fields inherited from class org.openscience.cdk.io.random.DefaultRandomAccessChemObjectReader
mode
-
-
Constructor Summary
Constructors Constructor Description RandomAccessReader(File file, IChemObjectBuilder builder)
Reads the file and builds an index file, if the index file doesn't already exist.RandomAccessReader(File file, IChemObjectBuilder builder, IReaderListener listener)
Reads the file and builds an index file, if the index file doesn't already exist.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
add(IChemObject arg0)
void
addChemObjectIOListener(IChemObjectIOListener listener)
void
close()
abstract ISimpleChemObjectReader
createChemObjectReader()
protected void
finalize()
IChemObject
first()
IChemObjectReader
getChemObjectReader()
int
getCurrentRecord()
static File
getIndexFile(String filename)
Opens the file index file_cdk.index
in a temporary folder, as specified by "java.io.tmpdir" property.boolean
hasNext()
boolean
hasPrevious()
boolean
isIndexCreated()
protected abstract boolean
isRecordEnd(String line)
IChemObject
last()
protected void
loadIndex(File file)
protected void
makeIndex()
The index filegetIndexFile(String)
is loaded, if already exists, or created a new.IChemObject
next()
int
nextIndex()
IChemObject
previous()
int
previousIndex()
protected IChemObject
processContent()
The reader is already set to read the record buffer.protected String
readContent(int record)
Reads the record text content into a String.IChemObject
readRecord(int record)
Returns the object at given record No.void
removeChemObjectIOListener(IChemObjectIOListener listener)
protected long[][]
resize(long[][] index, int newLength)
protected void
saveIndex(File file)
void
set(IChemObject arg0)
void
setChemObjectReader(ISimpleChemObjectReader chemObjectReader)
void
setIndexCreated(boolean indexCreated)
int
size()
String
toString()
-
Methods inherited from class org.openscience.cdk.io.random.DefaultRandomAccessChemObjectReader
fireIOSettingQuestion, getIOSettings, setReaderMode
-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Methods inherited from interface java.util.ListIterator
remove
-
-
-
-
Field Detail
-
logger
protected static final ILoggingTool logger
-
raFile
protected RandomAccessFile raFile
-
headerOptions
protected IOSetting[] headerOptions
-
chemObjectReader
protected ISimpleChemObjectReader chemObjectReader
-
indexVersion
protected final int indexVersion
- See Also:
- Constant Field Values
-
index
protected long[][] index
-
records
protected int records
-
currentRecord
protected int currentRecord
-
b
protected byte[] b
-
builder
protected final IChemObjectBuilder builder
-
indexCreated
protected boolean indexCreated
-
-
Constructor Detail
-
RandomAccessReader
public RandomAccessReader(File file, IChemObjectBuilder builder) throws IOException
Reads the file and builds an index file, if the index file doesn't already exist.- Parameters:
file
- the file object containing the molecules to be indexedbuilder
- a chem object builder- Throws:
IOException
- if there is an error during reading
-
RandomAccessReader
public RandomAccessReader(File file, IChemObjectBuilder builder, IReaderListener listener) throws IOException
Reads the file and builds an index file, if the index file doesn't already exist.- Parameters:
file
- file the file object containg the molecules to be indexedbuilder
- builder a chem object builderlistener
- listen for read event- Throws:
IOException
- if there is an error during reading
-
-
Method Detail
-
finalize
protected void finalize() throws Throwable
-
readRecord
public IChemObject readRecord(int record) throws Exception
Returns the object at given record No. Record numbers are zero-based!- Specified by:
readRecord
in interfaceIRandomAccessChemObjectReader<IChemObject>
- Throws:
Exception
-
readContent
protected String readContent(int record) throws IOException, CDKException
Reads the record text content into a String.- Parameters:
record
- The record number- Returns:
- A String representation of the record
- Throws:
IOException
- if error occurs during readingCDKException
- if the record number is invalid
-
processContent
protected IChemObject processContent() throws CDKException
The reader is already set to read the record buffer.- Returns:
- the read IChemObject
- Throws:
CDKException
- an error occurred whilst reading the file
-
resize
protected long[][] resize(long[][] index, int newLength)
-
isRecordEnd
protected abstract boolean isRecordEnd(String line)
-
makeIndex
protected void makeIndex() throws Exception
The index filegetIndexFile(String)
is loaded, if already exists, or created a new.- Throws:
Exception
-
getIndexFile
public static File getIndexFile(String filename)
Opens the file index file_cdk.index
in a temporary folder, as specified by "java.io.tmpdir" property.- Parameters:
filename
- the name of the file for which the index was generated- Returns:
- a file object representing the index file
-
close
public void close() throws IOException
- Throws:
IOException
-
getChemObjectReader
public IChemObjectReader getChemObjectReader()
-
createChemObjectReader
public abstract ISimpleChemObjectReader createChemObjectReader()
-
setChemObjectReader
public void setChemObjectReader(ISimpleChemObjectReader chemObjectReader)
-
hasNext
public boolean hasNext()
- Specified by:
hasNext
in interfaceIterator<IChemObject>
- Specified by:
hasNext
in interfaceListIterator<IChemObject>
-
hasPrevious
public boolean hasPrevious()
- Specified by:
hasPrevious
in interfaceListIterator<IChemObject>
-
first
public IChemObject first()
- Specified by:
first
in interfaceIRandomAccessChemObjectReader<IChemObject>
-
last
public IChemObject last()
- Specified by:
last
in interfaceIRandomAccessChemObjectReader<IChemObject>
-
next
public IChemObject next()
- Specified by:
next
in interfaceIterator<IChemObject>
- Specified by:
next
in interfaceListIterator<IChemObject>
-
previous
public IChemObject previous()
- Specified by:
previous
in interfaceListIterator<IChemObject>
-
set
public void set(IChemObject arg0)
- Specified by:
set
in interfaceListIterator<IChemObject>
-
add
public void add(IChemObject arg0)
- Specified by:
add
in interfaceListIterator<IChemObject>
-
previousIndex
public int previousIndex()
- Specified by:
previousIndex
in interfaceListIterator<IChemObject>
-
nextIndex
public int nextIndex()
- Specified by:
nextIndex
in interfaceListIterator<IChemObject>
-
size
public int size()
- Specified by:
size
in interfaceIRandomAccessChemObjectReader<IChemObject>
-
addChemObjectIOListener
public void addChemObjectIOListener(IChemObjectIOListener listener)
- Overrides:
addChemObjectIOListener
in classDefaultRandomAccessChemObjectReader
-
removeChemObjectIOListener
public void removeChemObjectIOListener(IChemObjectIOListener listener)
- Overrides:
removeChemObjectIOListener
in classDefaultRandomAccessChemObjectReader
-
getCurrentRecord
public int getCurrentRecord()
-
isIndexCreated
public boolean isIndexCreated()
-
setIndexCreated
public void setIndexCreated(boolean indexCreated)
-
-