public class JPhyloIOReaderWriterFactory extends java.lang.Object implements JPhyloIOFormatIDs
JPhyloIOFormatInfo
instances.
It provides methods to create instances for a specific format, which is identified by its format ID, such as
getFormatInfo(String)
, getWriter(String)
and different versions of getReader()
.
Format IDs are defined in JPhyloIOFormatIDs
.
Additionally this class allows to guess a format from the data using the available guessFormat()
or
guessFormat()
methods. This is useful if an application user shall be able to load a file in any supported
format without explicitly specifying the format.
To be able to guess the format, a certain amount of the data to be read needs to be buffered. The buffer size may
be specified using setReadAheadLimit(int)
. The default value
(DEFAULT_READ_AHEAD_LIMIT
=
8192 bytes) should be sufficient in most
cases. An increase maybe necessary if unusual files (e.g. XML files with very long header information - such as
ENTITY
declarations - in front of the root tag) are expected.
The following examples shows how to obtain a reader for a file with an unknown format:
JPhyloIOEventReader reader = factory.guessReader(new File("path/to/file"), new ReadWriteParameterMap()); if (reader != null) { // read file } else { System.out.println("The format of the specified file is not supported."); }Internally this instance uses a map from the format IDs to instances of
SingleReaderWriterFactory
to
create reader and writer instances or guess formats. Third party readers and writers can be handled by this
factory, if according single format factory instances are specified using
addFactory(SingleReaderWriterFactory)
.JPhyloIOEventReader
,
JPhyloIOEventWriter
,
JPhyloIOFormatIDs
,
JPhyloIOFormatInfo
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_READ_AHEAD_LIMIT
The number of bytes that are buffered by methods of this factory to determine the format from an input stream.
|
FASTA_FORMAT_ID, FORMAT_ID_PREFIX, MEGA_FORMAT_ID, NEWICK_FORMAT_ID, NEXML_FORMAT_ID, NEXUS_FORMAT_ID, PDE_FORMAT_ID, PHYLIP_FORMAT_ID, PHYLOXML_FORMAT_ID, SEQUENTIAL_PHYLIP_FORMAT_ID, XTG_FORMAT_ID
Constructor and Description |
---|
JPhyloIOReaderWriterFactory()
Returns a new instance of this class, which supports all build-in readers and writers of JPhyloIO with a
buffer size as defined by
DEFAULT_READ_AHEAD_LIMIT . |
Modifier and Type | Method and Description |
---|---|
SingleReaderWriterFactory |
addFactory(SingleReaderWriterFactory factory)
Allows to add additional or replace existing single format factories to be used by this instance.
|
java.util.Set<java.lang.String> |
getFormatIDsSet()
Returns the set of IDs of formats that are currently supported by this factory.
|
JPhyloIOFormatInfo |
getFormatInfo(java.lang.String formatID)
Returns an information object for the specified format.
|
int |
getReadAheadLimit()
Returns the maximal number of bytes this factory will read to determine the format of an input in the
#guess*() methods. |
JPhyloIOEventReader |
getReader(java.lang.String formatID,
java.io.File file,
ReadWriteParameterMap parameters) |
JPhyloIOEventReader |
getReader(java.lang.String formatID,
java.io.InputStream stream,
ReadWriteParameterMap parameters) |
JPhyloIOEventReader |
getReader(java.lang.String formatID,
java.io.Reader reader,
ReadWriteParameterMap parameters) |
JPhyloIOEventWriter |
getWriter(java.lang.String formatID) |
java.lang.String |
guessFormat(java.io.File file)
Tries to determine the format of the contents of the specified file by examining at its beginning (e.g.
|
java.lang.String |
guessFormat(java.io.File file,
ReadWriteParameterMap parameters)
Tries to determine the format of the contents of the specified file by examining at its beginning (e.g.
|
java.lang.String |
guessFormat(java.io.InputStream stream)
Tries to determine the format of the contents of the specified reader by examining at its beginning (e.g.
|
java.lang.String |
guessFormat(java.io.InputStream stream,
ReadWriteParameterMap parameters)
Tries to determine the format of the contents of the specified input stream by examining at its beginning (e.g.
|
java.lang.String |
guessFormat(java.io.Reader reader)
Tries to determine the format of the contents of the specified reader by examining at its beginning (e.g.
|
java.lang.String |
guessFormat(java.io.Reader reader,
ReadWriteParameterMap parameters)
Tries to determine the format of the contents of the specified reader by examining at its beginning (e.g.
|
JPhyloIOEventReader |
guessReader(java.io.File file,
ReadWriteParameterMap parameters)
Tries to determine the format of the contents of the specified file as described in the documentation of
guessFormat(File) and than creates a reader for the according format from that stream. |
JPhyloIOEventReader |
guessReader(java.io.InputStream stream,
ReadWriteParameterMap parameters)
Tries to determine the format of the contents of the specified input stream as described in the documentation of
guessFormat(InputStream) , resets the input stream and than creates a reader for the according format
from that stream. |
void |
removeAllFactories()
Removes all single format factories from the internal map of this instance.
|
void |
setReadAheadLimit(int readAheadLimit)
Allows to specify the maximal number of bytes this factory will read to determine the format of an input
in the
#guess*() methods. |
public static final int DEFAULT_READ_AHEAD_LIMIT
setReadAheadLimit(int)
,
Constant Field Valuespublic JPhyloIOReaderWriterFactory()
DEFAULT_READ_AHEAD_LIMIT
.public SingleReaderWriterFactory addFactory(SingleReaderWriterFactory factory)
factory
- the single format factory object to be add to the internal map of this factory instancenull
if there was no
mapping for that format IDpublic void removeAllFactories()
addFactory(SingleReaderWriterFactory)
is called. If
all build-in readers and writers of JPhyloIO shall be supported, using this method will be unnecessary.public int getReadAheadLimit()
#guess*()
methods.
This method is thread save a secured with an ReadWriteLock
together with setReadAheadLimit(int)
.
public void setReadAheadLimit(int readAheadLimit)
#guess*()
methods.
This method is thread save a secured with an ReadWriteLock
together with getReadAheadLimit()
.
readAheadLimit
- the new read ahead limitpublic java.util.Set<java.lang.String> getFormatIDsSet()
public java.lang.String guessFormat(java.io.File file, ReadWriteParameterMap parameters) throws java.lang.Exception
SingleReaderWriterFactory.checkFormat(Reader, ReadWriteParameterMap)
until a matching factory is found.
The parameter map is only necessary for formats that are so variable that parameter values are needed to determine
how a valid input would look like. That is currently not the case for any format supported in JPhyloIO, but
may be necessary for third parts format-specific factories used in this instance. (Refer to the documentation of
third party format-specific factories for details.) In most cases the convenience method guessFormat(File)
will be sufficient.
Note that in contrast to guessReader(File, ReadWriteParameterMap)
, this method does not support
GZIPed inputs.
reader
- the reader providing the contentsparameters
- the parameter map containing parameters for the JPhyloIO event reader that would
be used with reader
null
if no supported format seems to be matching the contentsjava.lang.Exception
- if the underlying format specific factory throws an exception when testing the stream (e.g.
because of an IO error)JPhyloIOFormatIDs
,
guessReader(File, ReadWriteParameterMap)
public java.lang.String guessFormat(java.io.File file) throws java.lang.Exception
SingleReaderWriterFactory.checkFormat(Reader, ReadWriteParameterMap)
until a matching factory is found.
It uses an empty parameter map that is passed to the internal calls of
SingleReaderWriterFactory.checkFormat(Reader, ReadWriteParameterMap)
. Use
guessFormat(File, ReadWriteParameterMap)
if parameters are necessary to determine the format correctly.
Note that in contrast to guessReader(File, ReadWriteParameterMap)
, this method does not support
GZIPed inputs.
reader
- the reader providing the contentsnull
if no supported format seems to be matching the contentsjava.lang.Exception
- if the underlying format specific factory throws an exception when testing the stream (e.g.
because of an IO error)JPhyloIOFormatIDs
,
guessReader(File, ReadWriteParameterMap)
public java.lang.String guessFormat(java.io.Reader reader, ReadWriteParameterMap parameters) throws java.lang.Exception
SingleReaderWriterFactory.checkFormat(Reader, ReadWriteParameterMap)
until a matching factory is found.
The parameter map is only necessary for formats that are so variable that parameter values are needed to determine
how a valid input would look like. That is currently not the case for any format supported in JPhyloIO, but
may be necessary for third parts format-specific factories used in this instance. (Refer to the documentation of
third party format-specific factories for details.) In most cases the convenience method guessFormat(Reader)
will be sufficient.
Note that in contrast to guessReader(InputStream, ReadWriteParameterMap)
, this method does not support
GZIPed inputs.
reader
- the reader providing the contentsparameters
- the parameter map containing parameters for the JPhyloIO event reader that would
be used with reader
null
if no supported format seems to be matching the contentsjava.lang.Exception
- if the underlying format specific factory throws an exception when testing the stream (e.g.
because of an IO error)JPhyloIOFormatIDs
,
guessReader(InputStream, ReadWriteParameterMap)
public java.lang.String guessFormat(java.io.Reader reader) throws java.lang.Exception
SingleReaderWriterFactory.checkFormat(Reader, ReadWriteParameterMap)
until a matching factory is found.
It uses an empty parameter map that is passed to the internal calls of
SingleReaderWriterFactory.checkFormat(Reader, ReadWriteParameterMap)
. Use
guessFormat(Reader, ReadWriteParameterMap)
if parameters are necessary to determine the format correctly.
Note that in contrast to guessReader(InputStream, ReadWriteParameterMap)
, this method does not support
GZIPed inputs.
reader
- the reader providing the contentsnull
if no supported format seems to be matching the contentsjava.lang.Exception
- if the underlying format specific factory throws an exception when testing the stream (e.g.
because of an IO error)JPhyloIOFormatIDs
,
guessReader(InputStream, ReadWriteParameterMap)
public java.lang.String guessFormat(java.io.InputStream stream, ReadWriteParameterMap parameters) throws java.lang.Exception
SingleReaderWriterFactory.checkFormat(InputStream, ReadWriteParameterMap)
until a matching factory is found.
The parameter map is only necessary for formats that are so variable that parameter values are needed to determine
how a valid input would look like. That is currently not the case for any format supported in JPhyloIO, but
may be necessary for third parts format-specific factories used in this instance. (Refer to the documentation of
third party format-specific factories for details.) In most cases the convenience method
guessFormat(InputStream)
will be sufficient.
Note that in contrast to guessReader(InputStream, ReadWriteParameterMap)
, this method does not support
GZIPed inputs.
reader
- the reader providing the contentsparameters
- the parameter map containing parameters for the JPhyloIO event reader that would
be used with reader
null
if no supported format seems to be matching the contentsjava.lang.Exception
- if the underlying format specific factory throws an exception when testing the stream (e.g.
because of an IO error)JPhyloIOFormatIDs
,
guessReader(InputStream, ReadWriteParameterMap)
public java.lang.String guessFormat(java.io.InputStream stream) throws java.lang.Exception
SingleReaderWriterFactory.checkFormat(InputStream, ReadWriteParameterMap)
until a matching factory is found.
It uses an empty parameter map that is passed to the internal calls of
SingleReaderWriterFactory.checkFormat(InputStream, ReadWriteParameterMap)
. Use
guessFormat(InputStream, ReadWriteParameterMap)
if parameters are necessary to determine the format
correctly.
Note that in contrast to guessReader(InputStream, ReadWriteParameterMap)
, this method does not support
GZIPed inputs.
reader
- the reader providing the contentsnull
if no supported format seems to be matching the contentsjava.lang.Exception
- if the underlying format specific factory throws an exception when testing the stream (e.g.
because of an IO error)JPhyloIOFormatIDs
,
guessReader(InputStream, ReadWriteParameterMap)
public JPhyloIOFormatInfo getFormatInfo(java.lang.String formatID)
formatID
- the unique format ID specifying the formatpublic JPhyloIOEventReader guessReader(java.io.InputStream stream, ReadWriteParameterMap parameters) throws java.lang.Exception
guessFormat(InputStream)
, resets the input stream and than creates a reader for the according format
from that stream. Additionally this method checks, if the input data is GZIPed and then returns a reader instance
that unpacks the stream data while reading.
In other words, this method is able to return a functional reader for input streams in all formats supported by this factory, as well as GZIPed streams providing data in any of these formats.
Note that bytes of stream
will be consumed by this method to determine the format, even if no according
reader is found. If an event reader is returned, no events of this reader will have been consumed.
Note that there is no version of this method accepting a Reader
instead of an InputStream
,
because uncompressing data from a reader is not directly possible.
stream
- the stream to read the data fromparameters
- the parameter map optionally containing parameters for the returned readernull
if no reader fitting the format of the stream could be foundjava.lang.Exception
- if an exception occurs while determining the format from the stream or creating the returned
reader instance (Depending on the type of reader that is returned, this will mostly be
IOException
s.)guessFormat(InputStream)
,
getReader(String, InputStream, ReadWriteParameterMap)
public JPhyloIOEventReader guessReader(java.io.File file, ReadWriteParameterMap parameters) throws java.lang.Exception
guessFormat(File)
and than creates a reader for the according format from that stream. Additionally this
method checks, if the input data is GZIPed and then returns a reader instance that unpacks the stream data while
reading.
In other words, this method is able to return a functional reader for files in all formats supported by this factory, as well as GZIPed files in any of these formats.
Note that there is no version of this method accepting a Reader
instead of an InputStream
,
because uncompressing data from a reader is not directly possible.
stream
- the stream to read the data fromparameters
- the parameter map optionally containing parameters for the returned readernull
if no reader fitting the format of the stream could be foundjava.lang.Exception
- if an exception occurs while determining the format from the stream or creating the returned
reader instance (Depending on the type of reader that is returned, this will mostly be
IOException
s.)guessFormat(File)
,
getReader(String, File, ReadWriteParameterMap)
public JPhyloIOEventReader getReader(java.lang.String formatID, java.io.InputStream stream, ReadWriteParameterMap parameters) throws java.lang.Exception
java.lang.Exception
public JPhyloIOEventReader getReader(java.lang.String formatID, java.io.File file, ReadWriteParameterMap parameters) throws java.lang.Exception
java.lang.Exception
public JPhyloIOEventReader getReader(java.lang.String formatID, java.io.Reader reader, ReadWriteParameterMap parameters) throws java.lang.Exception
java.lang.Exception
public JPhyloIOEventWriter getWriter(java.lang.String formatID)