Package io.microsphere.io
Class IOUtils
- java.lang.Object
-
- io.microsphere.util.BaseUtils
-
- io.microsphere.io.IOUtils
-
-
Field Summary
Fields Modifier and Type Field Description static int
BUFFER_SIZE
The buffer size for I/O
-
Constructor Summary
Constructors Constructor Description IOUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
close(java.io.Closeable closeable)
Unconditionally close aCloseable
.static void
close(java.net.URLConnection conn)
Closes a URLConnection.static int
copy(java.io.InputStream in, java.io.OutputStream out)
Copy the contents of the given InputStream to the given OutputStream.static byte[]
toByteArray(java.io.InputStream in)
Copy the contents of the given InputStream into a new byte array.static java.lang.String
toString(java.io.InputStream in, java.lang.String encoding)
Copy the contents of the given InputStream into a newString
.static java.lang.String
toString(java.io.InputStream in, java.nio.charset.Charset charset)
Copy the contents of the given InputStream into a newString
.
-
-
-
Method Detail
-
toByteArray
public static byte[] toByteArray(java.io.InputStream in) throws java.io.IOException
Copy the contents of the given InputStream into a new byte array.Leaves the stream open when done.
- Parameters:
in
- the stream to copy from (may benull
or empty)- Returns:
- the new byte array that has been copied to (possibly empty)
- Throws:
java.io.IOException
- in case of I/O errors
-
toString
public static java.lang.String toString(java.io.InputStream in, java.lang.String encoding) throws java.io.IOException
Copy the contents of the given InputStream into a newString
.Leaves the stream open when done.
- Parameters:
in
- the stream to copy from (may benull
or empty)encoding
- the encoding to use, if it'snull
, take theSystemUtils.FILE_ENCODING
as default- Returns:
- the new byte array that has been copied to (possibly empty)
- Throws:
java.io.IOException
- in case of I/O errors
-
toString
public static java.lang.String toString(java.io.InputStream in, java.nio.charset.Charset charset) throws java.io.IOException
Copy the contents of the given InputStream into a newString
.Leaves the stream open when done.
- Parameters:
in
- the stream to copy from (may benull
or empty)charset
- the charset to use, if it'snull
, take theSystemUtils.FILE_ENCODING
as default- Returns:
- the new byte array that has been copied to (possibly empty)
- Throws:
java.io.IOException
- in case of I/O errors
-
copy
public static int copy(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
Copy the contents of the given InputStream to the given OutputStream.Leaves both streams open when done.
- Parameters:
in
- the InputStream to copy fromout
- the OutputStream to copy to- Returns:
- the number of bytes copied
- Throws:
java.io.IOException
- in case of I/O errors
-
close
public static void close(java.net.URLConnection conn)
Closes a URLConnection.- Parameters:
conn
- the connection to close.
-
close
public static void close(java.io.Closeable closeable)
Unconditionally close aCloseable
.Equivalent to
Closeable.close()
, except any exceptions will be ignored. This is typically used in finally blocks.Example code:
Closeable closeable = null; try { closeable = new FileReader("foo.txt"); // process closeable closeable.close(); } catch (Exception e) { // error handling } finally { IOUtils.closeQuietly(closeable); }
- Parameters:
closeable
- the object to close, may be null or already closed
-
-