Package jodd.io
Class IOUtil
java.lang.Object
jodd.io.IOUtil
Optimized byte and character stream utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intstatic intBuffer size for various I/O operations.private static final intprivate static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static intReturns default IO buffer size.private static intbufferSize(int count) Returns either count or default IO buffer size (whichever is smaller).static voidCloses silently the closable object.static booleancompare(InputStream input1, InputStream input2) Compares the content of two byte streams (InputStreams).static booleanCompares the content of two character streams (Readers).static CharArrayWritercopy(InputStream input) static CharArrayWritercopy(InputStream input, int count) static intcopy(InputStream input, OutputStream output) Copies bytes fromInputStreamtoOutputStreamusing buffer.static intcopy(InputStream input, OutputStream output, int count) Copies specified number of bytes fromInputStreamtoOutputStreamusing buffer.static CharArrayWritercopy(InputStream input, Charset encoding) static CharArrayWritercopy(InputStream input, Charset encoding, int count) CopiesInputStreamto a newCharArrayWriterusing buffer and specified encoding.static <T extends Writer>
Tcopy(InputStream input, T output) static <T extends Writer>
Tcopy(InputStream input, T output, int count) static <T extends Writer>
Tcopy(InputStream input, T output, Charset encoding) static <T extends Writer>
Tcopy(InputStream input, T output, Charset encoding, int count) CopiesInputStreamtoWriterusing buffer and specified encoding.static CharArrayWriterstatic CharArrayWriterCopiesReaderto a newCharArrayWriterusing buffer and specified encoding.static intstatic intstatic <T extends OutputStream>
Tstatic <T extends OutputStream>
Tstatic <T extends OutputStream>
Tstatic <T extends OutputStream>
TCopiesReadertoOutputStreamusing buffer and specified encoding.static ByteArrayOutputStreamcopyToOutputStream(InputStream input) static ByteArrayOutputStreamcopyToOutputStream(InputStream input, int count) CopiesInputStreamto a newByteArrayOutputStreamusing buffer and specified encoding.static ByteArrayOutputStreamcopyToOutputStream(Reader input) static ByteArrayOutputStreamcopyToOutputStream(Reader input, int count) static ByteArrayOutputStreamcopyToOutputStream(Reader input, Charset encoding) static ByteArrayOutputStreamcopyToOutputStream(Reader input, Charset encoding, int count) CopiesReaderto a newByteArrayOutputStreamusing buffer and specified encoding.private static ByteArrayOutputStreamprivate static CharArrayWriterReturns newCharArrayWriterusing default IO buffer size.private static Charsetencoding()Returns default encoding.static InputStreamReaderinputStreamReadeOf(InputStream input) static InputStreamReaderinputStreamReadeOf(InputStream input, Charset encoding) Returns newInputStreamReaderusing specifiedInputStreamand encoding.static OutputStreamWriteroutputStreamWriterOf(OutputStream output) static OutputStreamWriteroutputStreamWriterOf(OutputStream output, Charset encoding) Returns newOutputStreamWriterusing specifiedOutputStreamand encoding.static byte[]readAvailableBytes(InputStream input) Reads all available bytes fromInputStreamas a byte array.static byte[]readBytes(InputStream input) static byte[]readBytes(InputStream input, int count) static byte[]static byte[]static byte[]static byte[]static char[]readChars(InputStream input) static char[]readChars(InputStream input, int count) static char[]readChars(InputStream input, Charset encoding) static char[]readChars(InputStream input, Charset encoding, int count) static char[]static char[]
-
Field Details
-
ioBufferSize
public static int ioBufferSizeBuffer size for various I/O operations. -
ZERO
private static final int ZERO- See Also:
-
NEGATIVE_ONE
private static final int NEGATIVE_ONE- See Also:
-
ALL
private static final int ALL- See Also:
-
-
Constructor Details
-
IOUtil
public IOUtil()
-
-
Method Details
-
close
Closes silently the closable object. If it isFlushable, it will be flushed first. No exception will be thrown if an I/O error occurs. -
copy
Copies bytes fromReadertoWriterusing buffer.ReaderandWriterdon't have to be wrapped to buffered, since copying is already optimized.- Parameters:
input-Readerto read.output-Writerto write to.- Returns:
- The total number of characters read.
- Throws:
IOException- if there is an error reading or writing.
-
copy
Copies bytes fromInputStreamtoOutputStreamusing buffer.InputStreamandOutputStreamdon't have to be wrapped to buffered, since copying is already optimized.- Parameters:
input-InputStreamto read.output-OutputStreamto write to.- Returns:
- The total number of bytes read.
- Throws:
IOException- if there is an error reading or writing.
-
copy
Copies specified number of characters fromReadertoWriterusing buffer.ReaderandWriterdon't have to be wrapped to buffered, since copying is already optimized.- Parameters:
input-Readerto read.output-Writerto write to.count- The number of characters to read.- Returns:
- The total number of characters read.
- Throws:
IOException- if there is an error reading or writing.
-
copy
Copies specified number of bytes fromInputStreamtoOutputStreamusing buffer.InputStreamandOutputStreamdon't have to be wrapped to buffered, since copying is already optimized.- Parameters:
input-InputStreamto read.output-OutputStreamto write to.count- The number of bytes to read.- Returns:
- The total number of bytes read.
- Throws:
IOException- if there is an error reading or writing.
-
readAvailableBytes
Reads all available bytes fromInputStreamas a byte array. UsesInputStream.available()to determine the size of input stream. This is the fastest method for readingInputStreamto byte array, but depends onInputStreamimplementation ofInputStream.available().- Parameters:
input-InputStreamto read.- Returns:
- byte[]
- Throws:
IOException- if total read is less thanInputStream.available();
-
copy
- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
public static <T extends OutputStream> T copy(Reader input, T output, Charset encoding) throws IOException - Throws:
IOException- See Also:
-
copy
public static <T extends OutputStream> T copy(Reader input, T output, Charset encoding, int count) throws IOException CopiesReadertoOutputStreamusing buffer and specified encoding.- Throws:
IOException- See Also:
-
copyToOutputStream
- Throws:
IOException- See Also:
-
copyToOutputStream
public static ByteArrayOutputStream copyToOutputStream(InputStream input, int count) throws IOException CopiesInputStreamto a newByteArrayOutputStreamusing buffer and specified encoding.- Throws:
IOException- See Also:
-
copyToOutputStream
- Throws:
IOException- See Also:
-
copyToOutputStream
public static ByteArrayOutputStream copyToOutputStream(Reader input, Charset encoding) throws IOException - Throws:
IOException- See Also:
-
copyToOutputStream
- Throws:
IOException- See Also:
-
copyToOutputStream
public static ByteArrayOutputStream copyToOutputStream(Reader input, Charset encoding, int count) throws IOException CopiesReaderto a newByteArrayOutputStreamusing buffer and specified encoding.- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
public static <T extends Writer> T copy(InputStream input, T output, Charset encoding) throws IOException - Throws:
IOException- See Also:
-
copy
public static <T extends Writer> T copy(InputStream input, T output, Charset encoding, int count) throws IOException CopiesInputStreamtoWriterusing buffer and specified encoding.- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
public static CharArrayWriter copy(InputStream input, Charset encoding, int count) throws IOException CopiesInputStreamto a newCharArrayWriterusing buffer and specified encoding.- Throws:
IOException- See Also:
-
copy
- Throws:
IOException- See Also:
-
copy
CopiesReaderto a newCharArrayWriterusing buffer and specified encoding.- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readBytes
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
readChars
- Throws:
IOException- See Also:
-
compare
Compares the content of two byte streams (InputStreams).- Returns:
trueif the content of the firstInputStreamis equal to the content of the secondInputStream.- Throws:
IOException
-
compare
Compares the content of two character streams (Readers).- Returns:
trueif the content of the firstReaderis equal to the content of the secondReader.- Throws:
IOException
-
bufferSize
private static int bufferSize()Returns default IO buffer size.- Returns:
- default IO buffer size.
-
bufferSize
private static int bufferSize(int count) Returns either count or default IO buffer size (whichever is smaller).- Parameters:
count- Number of characters or bytes to retrieve.- Returns:
- buffer size (either count or default IO buffer size, whichever is smaller).
-
encoding
Returns default encoding. -
createFastCharArrayWriter
Returns newCharArrayWriterusing default IO buffer size.- Returns:
- new
CharArrayWriterusing default IO buffer size.
-
createFastByteArrayOutputStream
-
inputStreamReadeOf
- See Also:
-
inputStreamReadeOf
Returns newInputStreamReaderusing specifiedInputStreamand encoding.- Parameters:
input-InputStreamencoding- Encoding asStringto use forInputStreamReader.- Returns:
- new
InputStreamReader
-
outputStreamWriterOf
- See Also:
-
outputStreamWriterOf
Returns newOutputStreamWriterusing specifiedOutputStreamand encoding.- Parameters:
output-OutputStreamencoding- Encoding asStringto use forOutputStreamWriter.- Returns:
- new
OutputStreamWriter
-