Package gov.usgs.util

Class StreamUtils

java.lang.Object
gov.usgs.util.StreamUtils

public class StreamUtils extends Object
Stream input, output, and transfer utilities.
  • Field Details

    • DEFAULT_BUFFER_SIZE

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

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

      public static final int DEFAULT_URL_READ_TIMEOUT
      Default read timeout for url connections.
      See Also:
  • Constructor Details

    • StreamUtils

      public StreamUtils()
  • Method Details

    • getInputStream

      public static InputStream getInputStream(Object obj) throws IOException, 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:
      IOException - if an error occurs.
      IllegalArgumentException - if obj is not an InputStream, URL, File, byte[], or String.
    • getURLInputStream

      public static InputStream getURLInputStream(URL url) throws 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:
      IOException - if an error occurs.
    • getURLInputStream

      public static InputStream getURLInputStream(URL url, int connectTimeout, int readTimeout) throws 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:
      IOException - if an error occurs.
    • getOutputStream

      public static OutputStream getOutputStream(Object obj, boolean append) throws 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:
      IOException - if an error occurs
    • getOutputStream

      public static OutputStream getOutputStream(Object obj) throws 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:
      IOException - if an error occurs.
    • readStream

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

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

      public static void transferStream(Object from, Object to, int bufferSize) throws 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:
      IOException - if thrown by calls to read/write on streams.
    • closeStream

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