Class CmdlineUtils
- java.lang.Object
-
- com.chemaxon.calculations.util.CmdlineUtils
-
@PublicAPI @Beta public final class CmdlineUtils extends Object
Command line related utilities. IO is accessed through aCmdlineContext
acquired withServiceLoader
. A single service registration is expected.Please note that this class is marked with @Beta annotation, so it can be subject of incompatible changes or removal in later releases.
-
-
Field Summary
Fields Modifier and Type Field Description static String
UTF8ENCODING
UTF-8 encoding String literal.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InputStream
autoGzip(InputStream in)
Detect gzippedInputStream
.static void
exit(int status)
Terminate execution with given status code.static PrintStream
getErr()
Get stderr to use.static InputStream
getIn()
Get stdin to use.static PrintStream
getOut()
Get stdout to use.static InputStream
inputStreamFromLocation(String location)
Create InputStream from not nullable location.static InputStream
inputStreamFromLocation(String location, boolean autoGzip)
Create InputStream from not nullable location.static com.google.common.base.Optional<InputStream>
inputStreamFromNullableLocation(String location)
Create in input stream from nullable location.static com.google.common.base.Optional<InputStream>
inputStreamFromNullableLocation(String location, boolean autoGzip)
Create in input stream from nullable location.static boolean
isFileExists(String name)
Check if a file exists.static com.chemaxon.calculations.util.CloseableLineIterator
lineIteratorFromLocation(String location)
Create aLineIterator
from a not nullable location.static com.chemaxon.calculations.util.CloseableLineIterator
lineIteratorFromLocation(String location, boolean autoGzip)
Create aLineIterator
from a not nullable location.static ObjectInputStream
objectInputStreamFromLocation(String location)
Create ObjectInputStream from a not nullable location.static ObjectInputStream
objectInputStreamFromLocation(String location, boolean autoGzip)
Create ObjectInputStream from a not nullable location.static com.google.common.base.Optional<ObjectInputStream>
objectInputStreamFromNullableLocation(String location)
Create in object input stream from nullable location.static ObjectOutputStream
objectOutputStreamFromLocation(String location)
ObjectOutputStream from a location.static com.google.common.base.Optional<ObjectOutputStream>
objectOutputStreamFromNullableLocation(String location)
ObjectOutputStream from a nullable location.static InputStream
openUrlStream(String spec)
Open an URL connection.static OutputStream
outputStreamFromLocation(String location)
OutputStream from a location.static PrintStream
printStreamFromLocation(String location)
PrintStream from a location.static com.google.common.base.Optional<PrintStream>
printStreamFromNullableLocation(String location)
PrintStream from a nullable location.static String
stringFromLocation(String location)
Read the contents of a location into a String.static String
stringFromLocation(String location, boolean autoGzip)
Read the contents of a location into a String.static com.google.common.base.Optional<String>
stringFromNullableLocation(String location)
Read the contents of a location when it is not null.static com.google.common.base.Optional<String>
stringFromNullableLocation(String location, boolean autoGzip)
Read the contents of a location when it is not null.
-
-
-
Field Detail
-
UTF8ENCODING
public static final String UTF8ENCODING
UTF-8 encoding String literal.- See Also:
- Constant Field Values
-
-
Method Detail
-
autoGzip
public static InputStream autoGzip(InputStream in) throws IOException
Detect gzippedInputStream
. ChecksGZIPInputStream.GZIP_MAGIC
and wraps input when match found. Further validity check is not done. See http://stackoverflow.com/questions/1080381/gzipinputstream-reading-line-by-line.- Parameters:
in
- Input- Returns:
- Input wrapped in
GZIPInputStream
whenGZIPInputStream.GZIP_MAGIC
match, otherwise input, possibly wrapped in aBufferedInputStream
. - Throws:
IOException
- Propagated
-
getOut
public static PrintStream getOut()
Get stdout to use.- Returns:
- StdOut specified by the default command line context
-
getErr
public static PrintStream getErr()
Get stderr to use.- Returns:
- StdErr specified by the default command line context
-
getIn
public static InputStream getIn()
Get stdin to use.- Returns:
- StdIn specified by the default command line context
-
isFileExists
public static boolean isFileExists(String name)
Check if a file exists.- Parameters:
name
- Name to delegate
-
openUrlStream
public static InputStream openUrlStream(String spec) throws IOException
Open an URL connection.- Parameters:
spec
- URL spec- Returns:
- Stream
- Throws:
IOException
- Propagated
-
exit
public static void exit(int status) throws com.chemaxon.calculations.util.CmdlineContext.ExitWithNonZeroStatus, com.chemaxon.calculations.util.CmdlineContext.ExitWithZeroStatus
Terminate execution with given status code. By default implementation callsSystem.exit(int)
. Test time implementations might decide to throwExitWithZeroStatus
orExitWithNonZeroStatus
instead of delegating toSystem.exit(int)
. TheseRuntimeException
s are helps for test implementations, regular application code must not catch them.- Parameters:
status
- Status code; by convention nonzero status code indicates abnormal termination- Throws:
com.chemaxon.calculations.util.CmdlineContext.ExitWithZeroStatus
- Test time implementation might throw this instead of delegating toSystem.exit(int)
Note that normal application code must not catch this.com.chemaxon.calculations.util.CmdlineContext.ExitWithNonZeroStatus
- Test time implementation might throw this instead of delegating toSystem.exit(int)
Note that normal application code must not catch this.
-
inputStreamFromLocation
public static InputStream inputStreamFromLocation(String location) throws FileNotFoundException
Create InputStream from not nullable location.- Parameters:
location
- File name or "-" for stdin.- Returns:
- Input stream representing given location. Returned input stream should be closed after usage. If stdin
requested it is wrapped in a
CloseShieldInputStream
- Throws:
FileNotFoundException
- thrown byFileInputStream(java.lang.String)
-
inputStreamFromLocation
public static InputStream inputStreamFromLocation(String location, boolean autoGzip) throws IOException
Create InputStream from not nullable location.- Parameters:
location
- File name or "-" for stdin.autoGzip
- Wrap returned stream throughautoGzip(java.io.InputStream)
.- Returns:
- Input stream representing given location. Returned input stream should be closed after usage. If stdin
requested it is wrapped in a
CloseShieldInputStream
- Throws:
IOException
- propagated
-
objectInputStreamFromLocation
public static ObjectInputStream objectInputStreamFromLocation(String location, boolean autoGzip) throws IOException
Create ObjectInputStream from a not nullable location.- Parameters:
location
- File name or "-" for stdinautoGzip
- Wrap returned stream throughautoGzip(java.io.InputStream)
.- Returns:
ObjectInputStream
to be used- Throws:
IOException
- re-thrown
-
objectInputStreamFromLocation
public static ObjectInputStream objectInputStreamFromLocation(String location) throws IOException
Create ObjectInputStream from a not nullable location.- Parameters:
location
- File name or "-" for stdin- Returns:
ObjectInputStream
to be used- Throws:
IOException
- re-thrown
-
lineIteratorFromLocation
public static com.chemaxon.calculations.util.CloseableLineIterator lineIteratorFromLocation(String location, boolean autoGzip) throws IOException
Create aLineIterator
from a not nullable location.Note that the returned instance must be closed by calling
LineIterator.close()
orLineIterator.closeQuietly(org.apache.commons.io.LineIterator)
, preferably in afinally
block- Parameters:
location
- File name or "-" for stdinautoGzip
- Wrap returned stream throughautoGzip(java.io.InputStream)
.- Returns:
LineIterator
for the given location- Throws:
IOException
- re-thrown
-
lineIteratorFromLocation
public static com.chemaxon.calculations.util.CloseableLineIterator lineIteratorFromLocation(String location) throws IOException
Create aLineIterator
from a not nullable location.Note that the returned instance must be closed by calling
LineIterator.close()
orLineIterator.closeQuietly(org.apache.commons.io.LineIterator)
, preferably in afinally
block- Parameters:
location
- File name or "-" for stdin- Returns:
LineIterator
for the given location- Throws:
IOException
- re-thrown
-
inputStreamFromNullableLocation
public static com.google.common.base.Optional<InputStream> inputStreamFromNullableLocation(String location, boolean autoGzip) throws IOException
Create in input stream from nullable location.The returned
InputStream
should be closed in a properfinally
block by invoking itsInputStream.close()
method. If stdin used then aCloseShieldInputStream
is returned which is safe to close.- Parameters:
location
- File name or "-" for stdinautoGzip
- Wrap returned stream throughautoGzip(java.io.InputStream)
.- Returns:
- Input stream representing given location
- Throws:
IOException
- thrown byFileInputStream(java.lang.String)
or byautoGzip(java.io.InputStream)
.
-
inputStreamFromNullableLocation
public static com.google.common.base.Optional<InputStream> inputStreamFromNullableLocation(String location) throws IOException
Create in input stream from nullable location.The returned
InputStream
should be closed in a properfinally
block by invoking itsInputStream.close()
method. If stdin used then aCloseShieldInputStream
is returned which is safe to close.- Parameters:
location
- File name or "-" for stdin- Returns:
- Input stream representing given location
- Throws:
IOException
- thrown byFileInputStream(java.lang.String)
or byautoGzip(java.io.InputStream)
.
-
objectInputStreamFromNullableLocation
public static com.google.common.base.Optional<ObjectInputStream> objectInputStreamFromNullableLocation(String location) throws IOException
Create in object input stream from nullable location.The returned
Object
should be closed in a properfinally
block by invoking itsObjectInputStream.close()
method. If stdin used then aCloseShieldInputStream
is used which is safe to close.- Parameters:
location
- File name or "-" for stdin- Returns:
- ObjectInputStream representing given location
- Throws:
IOException
- re-thrown
-
outputStreamFromLocation
public static OutputStream outputStreamFromLocation(String location) throws FileNotFoundException
OutputStream from a location.- Parameters:
location
- File name or "-" for stdout, "-2" for stderr. Location "/dev/null" is considered as aNullOutputStream
. Note that NullOutputStream is available both in apache commons io and in Guava, however Guava version is marked withBeta
, so apache version is used.- Returns:
- OutputStream for given location which is safe and should be closed explicitly
- Throws:
FileNotFoundException
- thrown byFileOutputStream(java.lang.String)
-
printStreamFromLocation
public static PrintStream printStreamFromLocation(String location) throws FileNotFoundException
PrintStream from a location.- Parameters:
location
- File name or "-" for stdout, "-2" for stderr. Location "/dev/null" is considered as aNullOutputStream
. Note that NullOutputStream is available both in apache commons io and in Guava, however Guava version is marked withBeta
, so apache version is used.- Returns:
- UTF-8 encoded PrintStream for given location which is safe and should be closed explicitly
- Throws:
FileNotFoundException
- thrown byFileOutputStream(java.lang.String)
-
objectOutputStreamFromLocation
public static ObjectOutputStream objectOutputStreamFromLocation(String location) throws IOException
ObjectOutputStream from a location.- Parameters:
location
- File name or "-" for stdout, "-2" for stderr. Location "/dev/null" is considered as aNullOutputStream
. Note that NullOutputStream is available both in apache commons io and in Guava, however Guava version is marked withBeta
, so apache version is used.- Returns:
- ObjectOutputStream for given location which is safe and should be closed explicitly
- Throws:
IOException
- thrown by underlying streams
-
stringFromLocation
public static String stringFromLocation(String location, boolean autoGzip) throws IllegalArgumentException
Read the contents of a location into a String.- Parameters:
location
- Location passed toinputStreamFromLocation(java.lang.String)
autoGzip
- Wrap returned stream throughautoGzip(java.io.InputStream)
.- Returns:
- Contents read by
IOUtils.toString(java.io.InputStream, java.lang.String)
- Throws:
IllegalArgumentException
- When something goes wrong
-
stringFromLocation
public static String stringFromLocation(String location) throws IllegalArgumentException
Read the contents of a location into a String.- Parameters:
location
- Location passed toinputStreamFromLocation(java.lang.String)
- Returns:
- Contents read by
IOUtils.toString(java.io.InputStream, java.lang.String)
- Throws:
IllegalArgumentException
- When something goes wrong
-
printStreamFromNullableLocation
public static com.google.common.base.Optional<PrintStream> printStreamFromNullableLocation(String location) throws FileNotFoundException
PrintStream from a nullable location.- Parameters:
location
- File name or "-" for stdout, "-2" for stderr- Returns:
- UTF-8 encoded PrintStream for given location which is safe and should be closed explicitly when not null given
- Throws:
FileNotFoundException
- thrown byFileOutputStream(java.lang.String)
-
objectOutputStreamFromNullableLocation
public static com.google.common.base.Optional<ObjectOutputStream> objectOutputStreamFromNullableLocation(String location) throws IOException
ObjectOutputStream from a nullable location.- Parameters:
location
- File name or "-" for stdout, "-2" for stderr- Returns:
- ObjectOutputStream for given location which is safe and should be closed explicitly when not null given
- Throws:
IOException
- re-thrown
-
stringFromNullableLocation
public static com.google.common.base.Optional<String> stringFromNullableLocation(String location, boolean autoGzip)
Read the contents of a location when it is not null.- Parameters:
location
- A location or nullautoGzip
- Wrap returned stream throughautoGzip(java.io.InputStream)
.- Returns:
- Contents
-
-