@PublicAPI @Beta public final class CmdlineUtils extends 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 String |
UTF8ENCODING
UTF-8 encoding String literal.
|
Modifier and Type | Method and Description |
---|---|
static InputStream |
autoGzip(InputStream in)
Detect gzipped
InputStream . |
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 Optional<InputStream> |
inputStreamFromNullableLocation(String location)
Create in input stream from nullable location.
|
static 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 CloseableLineIterator |
lineIteratorFromLocation(String location)
Create a
LineIterator from a not nullable location. |
static CloseableLineIterator |
lineIteratorFromLocation(String location,
boolean autoGzip)
Create a
LineIterator 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 Optional<ObjectInputStream> |
objectInputStreamFromNullableLocation(String location)
Create in object input stream from nullable location.
|
static ObjectOutputStream |
objectOutputStreamFromLocation(String location)
ObjectOutputStream from a location.
|
static 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 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 Optional<String> |
stringFromNullableLocation(String location)
Read the contents of a location when it is not null.
|
static Optional<String> |
stringFromNullableLocation(String location,
boolean autoGzip)
Read the contents of a location when it is not null.
|
public static final String UTF8ENCODING
public static InputStream autoGzip(InputStream in) throws 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
.IOException
- Propagatedpublic static PrintStream getOut()
public static PrintStream getErr()
public static InputStream getIn()
public static boolean isFileExists(String name)
name
- Name to delegatepublic static InputStream openUrlStream(String spec) throws IOException
spec
- URL specIOException
- Propagatedpublic static void exit(int status) throws CmdlineContext.ExitWithNonZeroStatus, 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 InputStream inputStreamFromLocation(String location) throws FileNotFoundException
location
- File name or "-" for stdin.CloseShieldInputStream
FileNotFoundException
- thrown by FileInputStream.FileInputStream(java.lang.String)
public static InputStream inputStreamFromLocation(String location, boolean autoGzip) throws IOException
location
- File name or "-" for stdin.autoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.CloseShieldInputStream
IOException
- propagatedpublic static ObjectInputStream objectInputStreamFromLocation(String location, boolean autoGzip) throws IOException
location
- File name or "-" for stdinautoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.ObjectInputStream
to be usedIOException
- re-thrownpublic static ObjectInputStream objectInputStreamFromLocation(String location) throws IOException
location
- File name or "-" for stdinObjectInputStream
to be usedIOException
- re-thrownpublic static CloseableLineIterator lineIteratorFromLocation(String location, boolean autoGzip) throws 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 locationIOException
- re-thrownpublic static CloseableLineIterator lineIteratorFromLocation(String location) throws 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 locationIOException
- re-thrownpublic static Optional<InputStream> inputStreamFromNullableLocation(String location, boolean autoGzip) throws 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)
.IOException
- thrown by FileInputStream.FileInputStream(java.lang.String)
or by autoGzip(java.io.InputStream)
.public static Optional<InputStream> inputStreamFromNullableLocation(String location) throws 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 stdinIOException
- thrown by FileInputStream.FileInputStream(java.lang.String)
or by autoGzip(java.io.InputStream)
.public static Optional<ObjectInputStream> objectInputStreamFromNullableLocation(String location) throws 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 stdinIOException
- re-thrownpublic static OutputStream outputStreamFromLocation(String location) throws 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.FileNotFoundException
- thrown by FileOutputStream.FileOutputStream(java.lang.String)
public static PrintStream printStreamFromLocation(String location) throws 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.FileNotFoundException
- thrown by FileOutputStream.FileOutputStream(java.lang.String)
public static ObjectOutputStream objectOutputStreamFromLocation(String location) throws 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.IOException
- thrown by underlying streamspublic static String stringFromLocation(String location, boolean autoGzip) throws 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)
IllegalArgumentException
- When something goes wrongpublic static String stringFromLocation(String location) throws IllegalArgumentException
location
- Location passed to inputStreamFromLocation(java.lang.String)
IOUtils.toString(java.io.InputStream, java.lang.String)
IllegalArgumentException
- When something goes wrongpublic static Optional<PrintStream> printStreamFromNullableLocation(String location) throws FileNotFoundException
location
- File name or "-" for stdout, "-2" for stderrFileNotFoundException
- thrown by FileOutputStream.FileOutputStream(java.lang.String)
public static Optional<ObjectOutputStream> objectOutputStreamFromNullableLocation(String location) throws IOException
location
- File name or "-" for stdout, "-2" for stderrIOException
- re-thrownpublic static Optional<String> stringFromNullableLocation(String location, boolean autoGzip)
location
- A location or nullautoGzip
- Wrap returned stream through autoGzip(java.io.InputStream)
.