Package gov.usgs.util
Class StreamUtils
java.lang.Object
gov.usgs.util.StreamUtils
Stream input, output, and transfer utilities.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
An InputStream that ignores calls to close.static class
An OutputStream that ignores calls to close. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Default buffer size used for stream reads and writes.static final int
Default connect timeout for url connections.static final int
Default read timeout for url connections. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
closeStream
(Object stream) Close an InputStream or OutputStream.static InputStream
getInputStream
(Object obj) Get an input stream for an Object if possible.static OutputStream
getOutputStream
(Object obj) Same as calling getOutputStream(obj, false).static OutputStream
getOutputStream
(Object obj, boolean append) Turn an object into an OutputStream if possible.static InputStream
getURLInputStream
(URL url) Get an InputStream from a URL.static InputStream
getURLInputStream
(URL url, int connectTimeout, int readTimeout) Get an InputStream from a URL.static byte[]
readStream
(Object from) Read stream contents into a byte array.static void
transferStream
(Object from, Object to) Transfer contents of Object from to Object to.static void
transferStream
(Object from, Object to, int bufferSize) Transfer contents of Object from to Object to.
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEDefault buffer size used for stream reads and writes.- See Also:
-
DEFAULT_URL_CONNECT_TIMEOUT
public static final int DEFAULT_URL_CONNECT_TIMEOUTDefault connect timeout for url connections.- See Also:
-
DEFAULT_URL_READ_TIMEOUT
public static final int DEFAULT_URL_READ_TIMEOUTDefault read timeout for url connections.- See Also:
-
-
Constructor Details
-
StreamUtils
public StreamUtils()
-
-
Method Details
-
getInputStream
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
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
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
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
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
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
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
Close an InputStream or OutputStream.- Parameters:
stream
- stream to close.
-