edu.cmu.sphinx.util
Class StreamFactory

java.lang.Object
  extended byedu.cmu.sphinx.util.StreamFactory

public class StreamFactory
extends java.lang.Object

Supports the loading and saving of files from different sources, e.g., a ZIP file or a plain directory. Provides methods that returns an InputStream or OutputStream to the named file in the given source.


Field Summary
static java.lang.String DIRECTORY
          Identifies a plain directory.
static java.lang.String ZIP_FILE
          Identifies a ZIP file.
 
Constructor Summary
StreamFactory()
           
 
Method Summary
static java.io.InputStream getInputStream(java.lang.String location, java.lang.String file)
          Returns an appropriate InputStream of the given file in the given URL location.
static java.io.InputStream getInputStream(java.lang.String format, java.lang.String location, java.lang.String file)
          According to the given data format, returns an appropriate InputStream of the given file in the given URL location.
static java.io.OutputStream getOutputStream(java.lang.String location, java.lang.String file)
          Returns an appropriate OutputStream of the given file in the given URL location.
static java.io.OutputStream getOutputStream(java.lang.String location, java.lang.String file, boolean append)
          Returns an appropriate OutputStream of the given file in the given URL location.
static java.io.OutputStream getOutputStream(java.lang.String format, java.lang.String location, java.lang.String file)
          According to the given data format, returns an appropriate OutputStream of the given file in the given URL location.
static java.io.OutputStream getOutputStream(java.lang.String format, java.lang.String location, java.lang.String file, boolean append)
          According to the given data format, returns an appropriate OutputStream of the given file in the given URL location.
static java.lang.String resolve(java.lang.String sourceName)
          Returns the type of the given data source.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZIP_FILE

public static final java.lang.String ZIP_FILE
Identifies a ZIP file.

See Also:
Constant Field Values

DIRECTORY

public static final java.lang.String DIRECTORY
Identifies a plain directory.

See Also:
Constant Field Values
Constructor Detail

StreamFactory

public StreamFactory()
Method Detail

getInputStream

public static java.io.InputStream getInputStream(java.lang.String location,
                                                 java.lang.String file)
                                          throws java.io.FileNotFoundException,
                                                 java.io.IOException,
                                                 java.util.zip.ZipException
Returns an appropriate InputStream of the given file in the given URL location. The location can be a plain directory or a ZIP file (these are the only two supported at this point). The resolve method is called to resolve whether "location" refers to a ZIP file or a directory. Suppose you want the InputStream to the file "dict/dictionary.txt" in the ZIP file "file:/lab/speech/sphinx4/data/wsj.zip". You will do: StreamFactory.getInputStream( "file:/lab/speech/sphinx4/data/wsj.zip", "dict/dictionary.txt"); Suppose you want the InputStream to the file "dict/dictionary.txt" in the directory "file:/lab/speech/sphinx4/data/wsj", you will do: StreamFactory.getInputStream( "file:/lab/speech/sphinx4/data/wsj", "dict/dictionary.txt"); The StreamFactory.resolve() method is called to resolve whether "location" refers to a ZIP file or a directory.

Parameters:
location - the URL location of the input data, it can now be a directory or a ZIP file
file - the file in the given location to obtain the InputStream
Returns:
an InputStream of the given file in the given location
Throws:
java.io.FileNotFoundException
java.io.IOException
java.util.zip.ZipException

getInputStream

public static java.io.InputStream getInputStream(java.lang.String format,
                                                 java.lang.String location,
                                                 java.lang.String file)
                                          throws java.io.FileNotFoundException,
                                                 java.io.IOException,
                                                 java.util.zip.ZipException
According to the given data format, returns an appropriate InputStream of the given file in the given URL location. The location can be a plain directory or a JAR or ZIP file (these are the only ones supported at this point). Suppose you want the InputStream to the file "dict/dictionary.txt" in the ZIP file "file:/lab/speech/sphinx4/data/wsj.zip". You will do: StreamFactory.getInputStream(StreamFactory.ZIP_FILE, "file:/lab/speech/sphinx4/data/wsj.zip", "dict/dictionary.txt"); Suppose you want the InputStream to the file "dict/dictionary.txt" in the directory "file:/lab/speech/sphinx4/data/wsj", you will do: StreamFactory.getInputStream(StreamFactory.DIRECTORY, "file:/lab/speech/sphinx4/data/wsj", "dict/dictionary.txt");

Parameters:
format - the format of the input data, the currently supported formats are:
StreamFactory.ZIP_FILE
StreamFactory.DIRECTORY
location - the URL location of the input data, it can now be a directory or a JAR or ZIP file, or null if no location is given, which means that the argument also specifies the exact location
file - the file in the given location to obtain the InputStream
Returns:
an InputStream of the given file in the given location
Throws:
java.io.FileNotFoundException
java.io.IOException
java.util.zip.ZipException

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String location,
                                                   java.lang.String file,
                                                   boolean append)
                                            throws java.io.FileNotFoundException,
                                                   java.io.IOException,
                                                   java.util.zip.ZipException
Returns an appropriate OutputStream of the given file in the given URL location. The location can be a plain directory or a ZIP file (these are the only two supported at this point). The resolve method is called to resolve whether "location" refers to a ZIP file or a directory. If saving to a zip or jar, the file can be appended or overwritten. If saving to a directory, files are always overwritten. Suppose you want the OutputStream to the file "dict/dictionary.txt" in the ZIP file "file:/lab/speech/sphinx4/data/wsj.zip". You will do: StreamFactory.getOutputStream( "file:/lab/speech/sphinx4/data/wsj.zip", "dict/dictionary.txt", true); Suppose you want the OutputStream to the file "dict/dictionary.txt" in the directory "file:/lab/speech/sphinx4/data/wsj", you will do: StreamFactory.getOutputStream( "file:/lab/speech/sphinx4/data/wsj", "dict/dictionary.txt", false); The StreamFactory.resolve() method is called to resolve whether "location" refers to a ZIP file or a directory.

Parameters:
location - the URL location of the output data, it can now be a directory or a ZIP file
file - the file in the given location to obtain the OutputStream
append - if true and saving to a zip file, then file is appended rather than overwritten.
Returns:
an OutputStream of the given file in the given location
Throws:
java.io.FileNotFoundException
java.io.IOException
java.util.zip.ZipException

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String format,
                                                   java.lang.String location,
                                                   java.lang.String file,
                                                   boolean append)
                                            throws java.io.FileNotFoundException,
                                                   java.io.IOException,
                                                   java.util.zip.ZipException
According to the given data format, returns an appropriate OutputStream of the given file in the given URL location. The location can be a plain directory or a JAR or ZIP file (these are the only ones supported at this point). If saving to a zip or jar, the file can be appended or overwritten. If saving to a directory, files are always overwritten. Suppose you want the OutputStream to the file "dict/dictionary.txt" in the ZIP file "file:/lab/speech/sphinx4/data/wsj.zip". You will do: StreamFactory.getOutputStream(StreamFactory.ZIP_FILE, "file:/lab/speech/sphinx4/data/wsj.zip", "dict/dictionary.txt", true); Suppose you want the OutputStream to the file "dict/dictionary.txt" in the directory "file:/lab/speech/sphinx4/data/wsj", you will do: StreamFactory.getOutputStream(StreamFactory.DIRECTORY, "file:/lab/speech/sphinx4/data/wsj", "dict/dictionary.txt", false);

Parameters:
format - the format of the output data, the currently supported formats are:
StreamFactory.ZIP_FILE
StreamFactory.DIRECTORY
location - the URL location of the output data, it can now be a directory or a JAR or ZIP file, or null if no location is given, which means that the argument also specifies the exact location
file - the file in the given location to obtain the OutputStream
append - if true and saving to a zip file, then file is appended rather than overwritten.
Returns:
an OutputStream of the given file in the given location
Throws:
java.io.FileNotFoundException
java.io.IOException
java.util.zip.ZipException

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String location,
                                                   java.lang.String file)
                                            throws java.io.FileNotFoundException,
                                                   java.io.IOException,
                                                   java.util.zip.ZipException
Returns an appropriate OutputStream of the given file in the given URL location. The location can be a plain directory or a ZIP file (these are the only two supported at this point). The resolve method is called to resolve whether "location" refers to a ZIP file or a directory. Files are overwritten, which may be risky for ZIP of JAR files. Suppose you want the OutputStream to the file "dict/dictionary.txt" in the ZIP file "file:/lab/speech/sphinx4/data/wsj.zip". You will do: StreamFactory.getOutputStream( "file:/lab/speech/sphinx4/data/wsj.zip", "dict/dictionary.txt"); Suppose you want the OutputStream to the file "dict/dictionary.txt" in the directory "file:/lab/speech/sphinx4/data/wsj", you will do: StreamFactory.getOutputStream( "file:/lab/speech/sphinx4/data/wsj", "dict/dictionary.txt"); The StreamFactory.resolve() method is called to resolve whether "location" refers to a ZIP file or a directory.

Parameters:
location - the URL location of the output data, it can now be a directory or a ZIP file
file - the file in the given location to obtain the OutputStream
Returns:
an OutputStream of the given file in the given location
Throws:
java.io.FileNotFoundException
java.io.IOException
java.util.zip.ZipException

getOutputStream

public static java.io.OutputStream getOutputStream(java.lang.String format,
                                                   java.lang.String location,
                                                   java.lang.String file)
                                            throws java.io.FileNotFoundException,
                                                   java.io.IOException,
                                                   java.util.zip.ZipException
According to the given data format, returns an appropriate OutputStream of the given file in the given URL location. The location can be a plain directory or a JAR or ZIP file (these are the only ones supported at this point). Files are always overwritten, which can be risky for ZIP or JAR files. Suppose you want the OutputStream to the file "dict/dictionary.txt" in the ZIP file "file:/lab/speech/sphinx4/data/wsj.zip". You will do: StreamFactory.getOutputStream(StreamFactory.ZIP_FILE, "file:/lab/speech/sphinx4/data/wsj.zip", "dict/dictionary.txt"); Suppose you want the OutputStream to the file "dict/dictionary.txt" in the directory "file:/lab/speech/sphinx4/data/wsj", you will do: StreamFactory.getOutputStream(StreamFactory.DIRECTORY, "file:/lab/speech/sphinx4/data/wsj", "dict/dictionary.txt");

Parameters:
format - the format of the output data, the currently supported formats are:
StreamFactory.ZIP_FILE
StreamFactory.DIRECTORY
location - the URL location of the output data, it can now be a directory or a JAR or ZIP file, or null if no location is given, which means that the argument also specifies the exact location
file - the file in the given location to obtain the OutputStream
Returns:
an OutputStream of the given file in the given location
Throws:
java.io.FileNotFoundException
java.io.IOException
java.util.zip.ZipException

resolve

public static java.lang.String resolve(java.lang.String sourceName)
Returns the type of the given data source. The current supported types are: StreamFactory.ZIP_FILE StreamFactory.DIRECTORY