Package com.storedobject.common
Class IO
java.lang.Object
com.storedobject.common.IO
Generic Input/Output routines.
All 'get' methods return 'buffered' streams/readers/writers. 'Double buffering' is avoided.
Conversion between streams and reader/writer are with UTF-8 encoding.
- Author:
- Syam
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBuffer
Create a read/write byte buffer from the content of the input stream.static void
close
(AutoCloseable... closeables) static BufferedInputStream
concat
(InputStream in1, InputStream in2) Create a concatenated stream from the 2 streams.static BufferedReader
Create a concatenated reader from the 2 readers.static InputStream
connect
(InputStream in, OutputStream out) Connect an input stream to an output stream so that the output stream is closed when the input stream is closed.static OutputStream
connect
(OutputStream out, InputStream in) Connect an output stream to an input stream so that the input stream is closed when the output stream is closed.static void
copy
(InputStream input, OutputStream output) Copy one stream to another.static void
copy
(InputStream input, OutputStream output, boolean close) Copy one stream to anotherstatic void
copy
(InputStream input, OutputStream output, int bufferSize) Copy one stream to another.static void
copy
(InputStream input, OutputStream output, int bufferSize, boolean close) Copy one stream to anotherstatic void
Copy from reader to writer.static void
Copy from reader to writer.static BufferedInputStream
get
(InputStream in) Convert a stream to a buffered streamstatic BufferedOutputStream
get
(OutputStream out) Convert a stream to a buffered streamstatic BufferedReader
Convert a reader to a buffered readerstatic BufferedWriter
Convert a writer to a buffered writerstatic DataInputStream
getData
(InputStream in) Convert a stream to a buffered data streamstatic DataOutputStream
getData
(OutputStream out) Convert a stream to a buffered data streamstatic DataInputStream
getDataInput
(File file) Create a buffered data stream from the filestatic DataInputStream
getDataInput
(String fileName) Create a buffered data stream from the filestatic DataInputStream
getDataInput
(Path path) Create a buffered data stream from the filestatic DataOutputStream
getDataOutput
(File file) Create a buffered data stream to write for the filestatic DataOutputStream
getDataOutput
(String fileName) Create a buffered data stream to write for the filestatic DataOutputStream
getDataOutput
(Path path) Create a buffered data stream to write for the pathstatic BufferedInputStream
Create a buffered stream from the filestatic BufferedInputStream
Create a buffered stream from the filestatic BufferedInputStream
Create a buffered stream from the pathstatic BufferedOutputStream
Create a buffered stream to write for the filestatic BufferedOutputStream
Create a buffered stream to write for the filestatic BufferedOutputStream
Create a buffered stream to write for the filestatic Path
Get the path class for the file name passed.static BufferedReader
Create a buffered reader from the filestatic BufferedReader
getReader
(InputStream in) Convert a stream to a buffered UTF-8 readerstatic BufferedReader
Create a buffered reader from the filestatic BufferedReader
Create a buffered reader from the pathstatic BufferedWriter
Create a buffered writer to write for the filestatic BufferedWriter
getWriter
(OutputStream out) Convert a stream to a buffered UTF-8 writerstatic BufferedWriter
Create a buffered writer to write for the filestatic BufferedWriter
Create a buffered writer to write for the pathstatic InputStream
Create a null input stream.static OutputStream
Create a null output stream.static ByteBuffer
Create a read only byte buffer from the content of the input stream.static OutputStream
tee
(OutputStream first, OutputStream second) Create a Tee output stream that simultaneously writes content to 2 different output streams.
-
Constructor Details
-
IO
public IO()
-
-
Method Details
-
copy
Copy one stream to another. Both the streams will be closed.- Parameters:
input
- Source streamoutput
- Destination stream- Throws:
IOException
- I/O exception
-
copy
Copy one stream to another. Both the streams will be closed.- Parameters:
input
- Source streamoutput
- Destination streambufferSize
- Buffer size to use- Throws:
IOException
- I/O exception
-
copy
Copy one stream to another- Parameters:
input
- Source streamoutput
- Destination streamclose
- Whether to close the streams or not- Throws:
IOException
- I/O exception
-
copy
public static void copy(InputStream input, OutputStream output, int bufferSize, boolean close) throws IOException Copy one stream to another- Parameters:
input
- Source streamoutput
- Destination streamclose
- Whether to close the streams or not- Throws:
IOException
- I/O exception
-
copy
Copy from reader to writer. Both reader and writer will be closed.- Parameters:
reader
- Readerwriter
- Writer- Throws:
IOException
- I/O exception
-
copy
Copy from reader to writer. Both reader and writer will be closed.- Parameters:
reader
- Readerwriter
- Writerclose
- Whether to close the reader/writer or not- Throws:
IOException
- I/O exception
-
get
Convert a stream to a buffered stream- Parameters:
in
- Stream to convert- Returns:
- Buffered stream
-
get
Convert a stream to a buffered stream- Parameters:
out
- Stream to convert- Returns:
- Buffered stream
-
getReader
Convert a stream to a buffered UTF-8 reader- Parameters:
in
- Stream to convert- Returns:
- Buffered reader
-
getWriter
Convert a stream to a buffered UTF-8 writer- Parameters:
out
- Stream to convert- Returns:
- Buffered writer
-
get
Convert a reader to a buffered reader- Parameters:
in
- Reader to convert- Returns:
- Buffered reader
-
get
Convert a writer to a buffered writer- Parameters:
out
- Writer to convert- Returns:
- Buffered writer
-
getData
Convert a stream to a buffered data stream- Parameters:
in
- Stream to convert- Returns:
- Buffered data stream
-
getData
Convert a stream to a buffered data stream- Parameters:
out
- Stream to convert- Returns:
- Buffered data stream
-
getInput
Create a buffered stream from the file- Parameters:
fileName
- Name of the file- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getPath
-
getInput
Create a buffered stream from the file- Parameters:
file
- File- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getInput
Create a buffered stream from the path- Parameters:
path
- Path- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getOutput
Create a buffered stream to write for the file- Parameters:
fileName
- Name of the file- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getOutput
Create a buffered stream to write for the file- Parameters:
file
- File- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getOutput
Create a buffered stream to write for the file- Parameters:
path
- Path- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getDataInput
Create a buffered data stream from the file- Parameters:
fileName
- Name of the file- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getDataInput
Create a buffered data stream from the file- Parameters:
file
- File- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getDataInput
Create a buffered data stream from the file- Parameters:
path
- Path- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getDataOutput
Create a buffered data stream to write for the file- Parameters:
fileName
- Name of the file- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getDataOutput
Create a buffered data stream to write for the file- Parameters:
file
- File- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getDataOutput
Create a buffered data stream to write for the path- Parameters:
path
- Path- Returns:
- Buffered stream
- Throws:
IOException
- if I/O error occurs.
-
getReader
Create a buffered reader from the file- Parameters:
fileName
- Name of the file- Returns:
- Buffered reader
- Throws:
IOException
- if I/O error occurs.
-
getReader
Create a buffered reader from the file- Parameters:
file
- File- Returns:
- Buffered reader
- Throws:
IOException
- if I/O error occurs.
-
getReader
Create a buffered reader from the path- Parameters:
path
- Path- Returns:
- Buffered reader
- Throws:
IOException
- if I/O error occurs.
-
getWriter
Create a buffered writer to write for the file- Parameters:
fileName
- Name of the file- Returns:
- Buffered writer
- Throws:
IOException
- if I/O error occurs.
-
getWriter
Create a buffered writer to write for the file- Parameters:
file
- File- Returns:
- Buffered writer
- Throws:
IOException
- if I/O error occurs.
-
getWriter
Create a buffered writer to write for the path- Parameters:
path
- Path- Returns:
- Buffered writer
- Throws:
IOException
- if I/O error occurs.
-
nullInput
Create a null input stream. A read from this stream always returns EOF (-1).- Returns:
- Null input stream
-
nullOutput
Create a null output stream. Anything written to this stream just disappears.- Returns:
- Null output stream
-
concat
Create a concatenated stream from the 2 streams. Data read from the resulting stream will be from the first stream and then, from the second stream when EOF is reached. One of the streams may benull
.- Parameters:
in1
- First stream.in2
- Second stream.- Returns:
- Concatenated stream.
-
concat
Create a concatenated reader from the 2 readers. Data read from the resulting reader will be from the first one and then, from the second one when EOF is reached. One of the readers may benull
.- Parameters:
reader1
- First reader.reader2
- Second reader.- Returns:
- Concatenated reader.
-
connect
Connect an output stream to an input stream so that the input stream is closed when the output stream is closed.- Parameters:
out
- Output streamin
- Input stream to connect to- Returns:
- Modified output stream
-
connect
Connect an input stream to an output stream so that the output stream is closed when the input stream is closed.- Parameters:
in
- Input streamout
- Output stream to connect to- Returns:
- Modified input stream
-
tee
Create a Tee output stream that simultaneously writes content to 2 different output streams.- Parameters:
first
- First output streamsecond
- Second output stream- Returns:
- An output stream that will write to 2 simultaneous streams.
-
readOnlyByteBuffer
Create a read only byte buffer from the content of the input stream.- Parameters:
in
- Input stream- Returns:
- A read only byte buffer containing the content of the input stream.
- Throws:
IOException
- if IO can not be happen.
-
byteBuffer
Create a read/write byte buffer from the content of the input stream.- Parameters:
in
- Input stream- Returns:
- A read/write byte buffer containing the content of the input stream.
- Throws:
IOException
- if buffer can not be created.
-
close
- Parameters:
closeables
- Closable instances to close.
-