|
|
|
@ -31,12 +31,13 @@ public class IOUtil { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Represents the end-of-file (or stream). |
|
|
|
|
* |
|
|
|
|
* @since 2.5 (made public) |
|
|
|
|
*/ |
|
|
|
|
public static final int EOF = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final int DEFAULT_BUFFER_SIZE = 1024*8; |
|
|
|
|
public static final int DEFAULT_BUFFER_SIZE = 1024 * 8; |
|
|
|
|
private static final byte[] SKIP_BYTE_BUFFER = new byte[DEFAULT_BUFFER_SIZE]; |
|
|
|
|
|
|
|
|
|
// Allocated in the relevant skip method if necessary.
|
|
|
|
@ -77,11 +78,12 @@ public class IOUtil { |
|
|
|
|
*/ |
|
|
|
|
public static byte[] toByteArray(InputStream in) throws IOException { |
|
|
|
|
ByteArrayOutputStream result = new ByteArrayOutputStream(); |
|
|
|
|
copy(in, result,DEFAULT_BUFFER_SIZE); |
|
|
|
|
copy(in, result); |
|
|
|
|
result.flush(); |
|
|
|
|
return result.toByteArray(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Reads data from the InputStream, using the specified buffer size. |
|
|
|
|
* <p> |
|
|
|
@ -105,7 +107,7 @@ public class IOUtil { |
|
|
|
|
result = new ByteArrayOutputStream(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
copy(in, result,DEFAULT_BUFFER_SIZE); |
|
|
|
|
copy(in, result); |
|
|
|
|
result.flush(); |
|
|
|
|
return result.toByteArray(); |
|
|
|
|
} catch (OutOfMemoryError error) { |
|
|
|
@ -138,7 +140,15 @@ public class IOUtil { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
public static void copy(InputStream in, OutputStream result) throws IOException { |
|
|
|
|
int buffer=in.available(); |
|
|
|
|
if(buffer>IOUtil.DEFAULT_BUFFER_SIZE||buffer==0){ |
|
|
|
|
buffer=IOUtil.DEFAULT_BUFFER_SIZE; |
|
|
|
|
} |
|
|
|
|
copy(in, result,buffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Copies bytes from an <code>InputStream</code> to an <code>OutputStream</code> using an internal buffer of the |
|
|
|
|
* given size. |
|
|
|
@ -227,7 +237,7 @@ public class IOUtil { |
|
|
|
|
*/ |
|
|
|
|
public static void copy(final InputStream input, final Writer output, final String inputCharsetName) |
|
|
|
|
throws IOException { |
|
|
|
|
copy(input, output,Charset.forName(inputCharsetName)); |
|
|
|
|
copy(input, output, Charset.forName(inputCharsetName)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -828,6 +838,7 @@ public class IOUtil { |
|
|
|
|
throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the length of the given array in a null-safe manner. |
|
|
|
|
* |
|
|
|
@ -871,6 +882,7 @@ public class IOUtil { |
|
|
|
|
public static int length(final Object[] array) { |
|
|
|
|
return array == null ? 0 : array.length; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Closes the given {@link Closeable} as a null-safe operation. |
|
|
|
|
* |
|
|
|
|