Package gov.usgs.util

Class StreamUtils


  • public class StreamUtils
    extends java.lang.Object
    Stream input, output, and transfer utilities.
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeStream​(java.lang.Object stream)
      Close an InputStream or OutputStream.
      static java.io.InputStream getInputStream​(java.lang.Object obj)
      Get an input stream for an Object if possible.
      static java.io.OutputStream getOutputStream​(java.lang.Object obj)
      Same as calling getOutputStream(obj, false).
      static java.io.OutputStream getOutputStream​(java.lang.Object obj, boolean append)
      Turn an object into an OutputStream if possible.
      static java.io.InputStream getURLInputStream​(java.net.URL url)
      Get an InputStream from a URL.
      static java.io.InputStream getURLInputStream​(java.net.URL url, int connectTimeout, int readTimeout)
      Get an InputStream from a URL.
      static byte[] readStream​(java.lang.Object from)
      Read stream contents into a byte array.
      static void transferStream​(java.lang.Object from, java.lang.Object to)
      Transfer contents of Object from to Object to.
      static void transferStream​(java.lang.Object from, java.lang.Object to, int bufferSize)
      Transfer contents of Object from to Object to.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        Default buffer size used for stream reads and writes.
        See Also:
        Constant Field Values
      • DEFAULT_URL_CONNECT_TIMEOUT

        public static final int DEFAULT_URL_CONNECT_TIMEOUT
        Default connect timeout for url connections.
        See Also:
        Constant Field Values
      • DEFAULT_URL_READ_TIMEOUT

        public static final int DEFAULT_URL_READ_TIMEOUT
        Default read timeout for url connections.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StreamUtils

        public StreamUtils()
    • Method Detail

      • getInputStream

        public static java.io.InputStream getInputStream​(java.lang.Object obj)
                                                  throws java.io.IOException,
                                                         java.lang.IllegalArgumentException
        Get an input stream for an Object if possible.
        Parameters:
        obj - an InputStream, File, byte[], or String.
        Returns:
        an InputStream or null. If obj is a File, the stream is Buffered.
        Throws:
        java.io.IOException - if an error occurs.
        java.lang.IllegalArgumentException - if obj is not an InputStream, URL, File, byte[], or String.
      • getURLInputStream

        public static java.io.InputStream getURLInputStream​(java.net.URL url)
                                                     throws java.io.IOException
        Get an InputStream from a URL. If URL is a HTTP url, attempts gzip compression.
        Parameters:
        url - the url being accessed.
        Returns:
        an InputStream to content at URL.
        Throws:
        java.io.IOException - if an error occurs.
      • getURLInputStream

        public static java.io.InputStream getURLInputStream​(java.net.URL url,
                                                            int connectTimeout,
                                                            int readTimeout)
                                                     throws java.io.IOException
        Get an InputStream from a URL. If URL is a HTTP url, attempts gzip compression.
        Parameters:
        url - the url being accessed.
        connectTimeout - allowed time in milliseconds before connection.
        readTimeout - allowed time in milliseconds before read.
        Returns:
        an InputStream to content at URL.
        Throws:
        java.io.IOException - if an error occurs.
      • getOutputStream

        public static java.io.OutputStream getOutputStream​(java.lang.Object obj,
                                                           boolean append)
                                                    throws java.io.IOException
        Turn an object into an OutputStream if possible.
        Parameters:
        obj - an OutputStream or File.
        append - if obj is a file and this parameter is true, the output stream will be opened in append mode.
        Returns:
        an OutputStream. If obj is a File, the stream is Buffered.
        Throws:
        java.io.IOException - if an error occurs
      • getOutputStream

        public static java.io.OutputStream getOutputStream​(java.lang.Object obj)
                                                    throws java.io.IOException
        Same as calling getOutputStream(obj, false). If obj is a file, it will open a new output stream and will not append.
        Parameters:
        obj - an OutputStream or File.
        Returns:
        an OutputStream. If obj is a File, the stream is Buffered.
        Throws:
        java.io.IOException - if an error occurs.
      • readStream

        public static byte[] readStream​(java.lang.Object from)
                                 throws java.io.IOException
        Read stream contents into a byte array.
        Parameters:
        from - stream to read.
        Returns:
        byte array of file content.
        Throws:
        java.io.IOException - if an error occurs while reading.
      • transferStream

        public static void transferStream​(java.lang.Object from,
                                          java.lang.Object to)
                                   throws java.io.IOException
        Transfer contents of Object from to Object to. Calls transferStream(from, to, DEFAULT_BUFFER_SIZE).
        Parameters:
        from - streamable source.
        to - streamable target.
        Throws:
        java.io.IOException - if IO error occurs
      • transferStream

        public static void transferStream​(java.lang.Object from,
                                          java.lang.Object to,
                                          int bufferSize)
                                   throws java.io.IOException
        Transfer contents of Object from to Object to. Uses getInputStream and getOutputStream to generate streams. Streams are closed after reading.
        Parameters:
        from - streamable source.
        to - streamable target.
        bufferSize - size of buffer
        Throws:
        java.io.IOException - if thrown by calls to read/write on streams.
      • closeStream

        public static void closeStream​(java.lang.Object stream)
        Close an InputStream or OutputStream.
        Parameters:
        stream - stream to close.