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 classAn InputStream that ignores calls to close.static classAn OutputStream that ignores calls to close. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault buffer size used for stream reads and writes.static final intDefault connect timeout for url connections.static final intDefault read timeout for url connections. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseStream(Object stream) Close an InputStream or OutputStream.static InputStreamgetInputStream(Object obj) Get an input stream for an Object if possible.static OutputStreamgetOutputStream(Object obj) Same as calling getOutputStream(obj, false).static OutputStreamgetOutputStream(Object obj, boolean append) Turn an object into an OutputStream if possible.static InputStreamgetURLInputStream(URL url) Get an InputStream from a URL.static InputStreamgetURLInputStream(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 voidtransferStream(Object from, Object to) Transfer contents of Object from to Object to.static voidtransferStream(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.
-