@PublicAPI @Beta public final class CmdlineUtils extends java.lang.Object
CmdlineContext
acquired with ServiceLoader
. 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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
UTF8ENCODING
UTF-8 encoding String literal.
|
Modifier and Type | Method and Description |
---|---|
static java.io.InputStream |
autoGzip(java.io.InputStream in)
Detect gzipped
InputStream . |
static void |
exit(int status)
Terminate execution with given status code.
|
static java.io.PrintStream |
getErr()
Get stderr to use.
|
static java.io.InputStream |
getIn()
Get stdin to use.
|
static java.io.PrintStream |
getOut()
Get stdout to use.
|
static java.io.InputStream |
inputStreamFromLocation(java.lang.String location)
Create InputStream from not nullable location.
|
static java.io.InputStream |
inputStreamFromLocation(java.lang.String location,
boolean autoGzip)
Create InputStream from not nullable location.
|
static com.google.common.base.Optional<java.io.InputStream> |
inputStreamFromNullableLocation(java.lang.String location)
Create in input stream from nullable location.
|
static com.google.common.base.Optional<java.io.InputStream> |
inputStreamFromNullableLocation(java.lang.String location,
boolean autoGzip)
Create in input stream from nullable location.
|
static boolean |
isFileExists(java.lang.String name)
Check if a file exists.
|
static com.chemaxon.calculations.util.CloseableLineIterator |
lineIteratorFromLocation(java.lang.String location)
Create a
LineIterator from a not nullable location. |
static com.chemaxon.calculations.util.CloseableLineIterator |
lineIteratorFromLocation(java.lang.String location,
boolean autoGzip)
Create a
LineIterator from a not nullable location. |
static java.io.ObjectInputStream |
objectInputStreamFromLocation(java.lang.String location)
Create ObjectInputStream from a not nullable location.
|
static java.io.ObjectInputStream |
objectInputStreamFromLocation(java.lang.String location,
boolean autoGzip)
Create ObjectInputStream from a not nullable location.
|
static com.google.common.base.Optional<java.io.ObjectInputStream> |
objectInputStreamFromNullableLocation(java.lang.String location)
Create in object input stream from nullable location.
|
static java.io.ObjectOutputStream |
objectOutputStreamFromLocation(java.lang.String location)
ObjectOutputStream from a location.
|
static com.google.common.base.Optional<java.io.ObjectOutputStream> |
objectOutputStreamFromNullableLocation(java.lang.String location)
ObjectOutputStream from a nullable location.
|
static java.io.InputStream |
openUrlStream(java.lang.String spec)
Open an URL connection.
|
static java.io.OutputStream |
outputStreamFromLocation(java.lang.String location)
OutputStream from a location.
|
static java.io.PrintStream |
printStreamFromLocation(java.lang.String location)
PrintStream from a location.
|
static com.google.common.base.Optional<java.io.PrintStream> |
printStreamFromNullableLocation(java.lang.String location)
PrintStream from a nullable location.
|
static java.lang.String |
stringFromLocation(java.lang.String location)
Read the contents of a location into a String.
|
static java.lang.String |
stringFromLocation(java.lang.String location,
boolean autoGzip)
Read the contents of a location into a String.
|
static com.google.common.base.Optional<java.lang.String> |
stringFromNullableLocation(java.lang.String location)
Read the contents of a location when it is not null.
|
static com.google.common.base.Optional<java.lang.String> |
stringFromNullableLocation(java.lang.String location,
boolean autoGzip)
Read the contents of a location when it is not null.
|
public static final java.lang.String UTF8ENCODING
public static java.io.InputStream autoGzip(java.io.InputStream in) throws java.io.IOException
InputStream
.
Checks GZIPInputStream.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.in
- InputGZIPInputStream
when GZIPInputStream.GZIP_MAGIC
match, otherwise input,
possibly wrapped in a BufferedInputStream
.java.io.IOException
- Propagatedpublic static java.io.PrintStream getOut()
public static java.io.PrintStream getErr()
public static java.io.InputStream getIn()
public static boolean isFileExists(java.lang.String name)
name
- Name to delegatepublic static java.io.InputStream openUrlStream(java.lang.String spec) throws java.io.IOException
spec
- URL specjava.io.IOException
- Propagatedpublic static void exit(int status) throws com.chemaxon.calculations.util.CmdlineContext.ExitWithNonZeroStatus, com.chemaxon.calculations.util.CmdlineContext.ExitWithZeroStatus
System.exit(int)
. Test time implementations might decide to throw
ExitWithZeroStatus
or ExitWithNonZeroStatus
instead of delegating to System.exit(int)
.
These RuntimeException
s are helps for test implementations, regular application code must not catch them.status
- Status code; by convention nonzero status code indicates abnormal terminationCmdlineContext.ExitWithZeroStatus
- Test time implementation might throw this instead of delegating to
System.exit(int)
Note that normal application code must not catch this.CmdlineContext.ExitWithNonZeroStatus
- Test time implementation might throw this instead of delegating to
System.exit(int)
Note that normal application code must not catch this.public static java.io.InputStream inputStreamFromLocation(java.lang.String location) throws java.io.FileNotFoundException
location
- File name or "-" for stdin.CloseShieldInputStream
java.io.FileNotFoundException
- thrown by FileInputStream.FileInputStream(java.lang.String)
public static java.io.InputStream inputStreamFromLocation(java.lang.String location, boolean autoGzip) throws java.io.IOException
location
- File name or "-" for stdin.autoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.CloseShieldInputStream
java.io.IOException
- propagatedpublic static java.io.ObjectInputStream objectInputStreamFromLocation(java.lang.String location, boolean autoGzip) throws java.io.IOException
location
- File name or "-" for stdinautoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.ObjectInputStream
to be usedjava.io.IOException
- re-thrownpublic static java.io.ObjectInputStream objectInputStreamFromLocation(java.lang.String location) throws java.io.IOException
location
- File name or "-" for stdinObjectInputStream
to be usedjava.io.IOException
- re-thrownpublic static com.chemaxon.calculations.util.CloseableLineIterator lineIteratorFromLocation(java.lang.String location, boolean autoGzip) throws java.io.IOException
LineIterator
from a not nullable location.
Note that the returned instance must be closed by calling LineIterator.close()
or
LineIterator.closeQuietly(org.apache.commons.io.LineIterator)
, preferably in a finally
block
location
- File name or "-" for stdinautoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.LineIterator
for the given locationjava.io.IOException
- re-thrownpublic static com.chemaxon.calculations.util.CloseableLineIterator lineIteratorFromLocation(java.lang.String location) throws java.io.IOException
LineIterator
from a not nullable location.
Note that the returned instance must be closed by calling LineIterator.close()
or
LineIterator.closeQuietly(org.apache.commons.io.LineIterator)
, preferably in a finally
block
location
- File name or "-" for stdinLineIterator
for the given locationjava.io.IOException
- re-thrownpublic static com.google.common.base.Optional<java.io.InputStream> inputStreamFromNullableLocation(java.lang.String location, boolean autoGzip) throws java.io.IOException
The returned InputStream
should be closed in a proper finally
block by invoking its
InputStream.close()
method. If stdin used then a CloseShieldInputStream
is returned which is safe
to close.
location
- File name or "-" for stdinautoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.java.io.IOException
- thrown by FileInputStream.FileInputStream(java.lang.String)
or by autoGzip(java.io.InputStream)
.public static com.google.common.base.Optional<java.io.InputStream> inputStreamFromNullableLocation(java.lang.String location) throws java.io.IOException
The returned InputStream
should be closed in a proper finally
block by invoking its
InputStream.close()
method. If stdin used then a CloseShieldInputStream
is returned which is safe
to close.
location
- File name or "-" for stdinjava.io.IOException
- thrown by FileInputStream.FileInputStream(java.lang.String)
or by autoGzip(java.io.InputStream)
.public static com.google.common.base.Optional<java.io.ObjectInputStream> objectInputStreamFromNullableLocation(java.lang.String location) throws java.io.IOException
The returned Object
should be closed in a proper finally
block by invoking its
ObjectInputStream.close()
method. If stdin used then a CloseShieldInputStream
is used which is
safe to close.
location
- File name or "-" for stdinjava.io.IOException
- re-thrownpublic static java.io.OutputStream outputStreamFromLocation(java.lang.String location) throws java.io.FileNotFoundException
location
- File name or "-" for stdout, "-2" for stderr. Location "/dev/null" is considered as a
NullOutputStream
. Note that NullOutputStream is available both in apache commons io and in Guava, however
Guava version is marked with Beta
, so apache version is used.java.io.FileNotFoundException
- thrown by FileOutputStream.FileOutputStream(java.lang.String)
public static java.io.PrintStream printStreamFromLocation(java.lang.String location) throws java.io.FileNotFoundException
location
- File name or "-" for stdout, "-2" for stderr. Location "/dev/null" is considered as a
NullOutputStream
. Note that NullOutputStream is available both in apache commons io and
in Guava, however Guava version is marked with Beta
, so apache version is used.java.io.FileNotFoundException
- thrown by FileOutputStream.FileOutputStream(java.lang.String)
public static java.io.ObjectOutputStream objectOutputStreamFromLocation(java.lang.String location) throws java.io.IOException
location
- File name or "-" for stdout, "-2" for stderr. Location "/dev/null" is considered as a
NullOutputStream
. Note that NullOutputStream is available both in apache commons io and
in Guava, however Guava version is marked with Beta
, so apache version is used.java.io.IOException
- thrown by underlying streamspublic static java.lang.String stringFromLocation(java.lang.String location, boolean autoGzip) throws java.lang.IllegalArgumentException
location
- Location passed to inputStreamFromLocation(java.lang.String)
autoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.IOUtils.toString(java.io.InputStream, java.lang.String)
java.lang.IllegalArgumentException
- When something goes wrongpublic static java.lang.String stringFromLocation(java.lang.String location) throws java.lang.IllegalArgumentException
location
- Location passed to inputStreamFromLocation(java.lang.String)
IOUtils.toString(java.io.InputStream, java.lang.String)
java.lang.IllegalArgumentException
- When something goes wrongpublic static com.google.common.base.Optional<java.io.PrintStream> printStreamFromNullableLocation(java.lang.String location) throws java.io.FileNotFoundException
location
- File name or "-" for stdout, "-2" for stderrjava.io.FileNotFoundException
- thrown by FileOutputStream.FileOutputStream(java.lang.String)
public static com.google.common.base.Optional<java.io.ObjectOutputStream> objectOutputStreamFromNullableLocation(java.lang.String location) throws java.io.IOException
location
- File name or "-" for stdout, "-2" for stderrjava.io.IOException
- re-thrownpublic static com.google.common.base.Optional<java.lang.String> stringFromNullableLocation(java.lang.String location, boolean autoGzip)
location
- A location or nullautoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.public static com.google.common.base.Optional<java.lang.String> stringFromNullableLocation(java.lang.String location)
location
- A location or null