Class CmdlineUtils


  • @PublicAPI
    @Beta
    public final class CmdlineUtils
    extends Object
    Command line related utilities. IO is accessed through a 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.

    • Method Detail

      • 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
      • 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 calls System.exit(int). Test time implementations might decide to throw ExitWithZeroStatus or ExitWithNonZeroStatus instead of delegating to System.exit(int). These RuntimeExceptions 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 to System.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 to System.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 by FileInputStream(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 through autoGzip(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 stdin
        autoGzip - Wrap returned stream through autoGzip(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 a 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

        Parameters:
        location - File name or "-" for stdin
        autoGzip - Wrap returned stream through autoGzip(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 a 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

        Parameters:
        location - File name or "-" for stdin
        Returns:
        LineIterator for the given location
        Throws:
        IOException - re-thrown
      • 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 proper finally block by invoking its ObjectInputStream.close() method. If stdin used then a CloseShieldInputStream 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 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.
        Returns:
        OutputStream for given location which is safe and should be closed explicitly
        Throws:
        FileNotFoundException - thrown by FileOutputStream(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 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.
        Returns:
        UTF-8 encoded PrintStream for given location which is safe and should be closed explicitly
        Throws:
        FileNotFoundException - thrown by FileOutputStream(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 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.
        Returns:
        ObjectOutputStream for given location which is safe and should be closed explicitly
        Throws:
        IOException - thrown by underlying streams
      • 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 by FileOutputStream(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 null
        autoGzip - Wrap returned stream through autoGzip(java.io.InputStream).
        Returns:
        Contents
      • stringFromNullableLocation

        public static com.google.common.base.Optional<String> stringFromNullableLocation​(String location)
        Read the contents of a location when it is not null.
        Parameters:
        location - A location or null
        Returns:
        Contents