public class IOUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE |
static IProgressListener |
IGNORING_PROGRESS_LISTENER |
static java.lang.String |
LINE_SEPARATOR |
protected static java.lang.Long |
NO_LIMIT |
Modifier | Constructor and Description |
---|---|
protected |
IOUtil()
Protected constructor allows subclassing!
|
Modifier and Type | Method and Description |
---|---|
protected void |
assertArgumentNotNull(java.lang.String argName,
java.lang.Object arg) |
java.io.InputStream |
asStream(java.lang.String string,
java.nio.charset.Charset charset)
Returns an input stream containing the given string using the provided charset
for encoding.
|
protected java.lang.String[] |
basicReadTextLinesFrom(java.io.Reader reader,
IStringFilter filter)
Reads all text lines that match the filter from the specified reader into
a String array.
|
boolean |
close(java.io.Closeable closeable)
Close the given closeable ignoring any exception and checking for null before closing.
|
long |
copyStream(java.io.InputStream inStream,
java.io.OutputStream outStream)
Copies all data from the input stream to the output stream using
a buffer with the default size (4096 bytes).
|
long |
copyStream(java.io.InputStream inStream,
java.io.OutputStream outStream,
int bufSize)
Copies all data from the input stream to the output stream using
a buffer of the given size in bytes.
|
long |
copyStream(java.io.InputStream inStream,
java.io.OutputStream outStream,
int bufSize,
IProgressListener listener)
Copies all data from the input stream to the output stream using
a buffer of the given size in bytes.
|
long |
copyStream(java.io.InputStream inStream,
java.io.OutputStream outStream,
int bufSize,
java.lang.Long limit)
Copies all data from the input stream to the output stream using
a buffer of the given size in bytes.
|
long |
copyStream(java.io.InputStream inStream,
java.io.OutputStream outStream,
int bufSize,
java.lang.Long limit,
IProgressListener listener)
Copies all data from the input stream to the output stream using
a buffer of the given size in bytes.
|
long |
copyStream(java.io.InputStream inStream,
java.io.OutputStream outStream,
IProgressListener listener)
Copies all data from the input stream to the output stream using
a buffer with the default size (4096 bytes).
|
protected void |
copyText(java.io.InputStream inStream,
java.nio.charset.Charset charset,
java.io.StringWriter writer) |
void |
copyText(java.io.Reader reader,
java.io.Writer writer)
Copies all text lines from the specified reader to the given writer.
|
void |
copyText(java.io.Reader reader,
java.io.Writer writer,
IProgressListener progressListener)
Copies all text lines from the specified reader to the given writer.
|
static IOUtil |
current()
Returns the only instance this class supports (design pattern "Singleton")
|
void |
processTextLines(java.io.InputStream inStream,
java.nio.charset.Charset charset,
ILineProcessor processor)
Reads all text lines from the specified input stream and passes them
one by one to the given line processor.
|
void |
processTextLines(java.io.InputStream inStream,
ICheckedCharset charsetName,
ILineProcessor processor)
Reads all text lines from the specified input stream and passes them
one by one to the given line processor.
|
void |
processTextLines(java.io.InputStream inStream,
ILineProcessor processor)
Reads all text lines from the specified input stream and passes them
one by one to the given line processor.
|
void |
processTextLines(java.io.Reader reader,
ILineProcessor processor)
Reads all text lines from the specified reader and passes them one by one
to the given line processor.
|
java.lang.String |
readTextFrom(java.io.InputStream inStream)
Reads the whole content of the given input stream and returns
it as a string.
|
java.lang.String |
readTextFrom(java.io.InputStream inStream,
java.nio.charset.Charset charset)
Reads the whole content of the given input stream and returns
it as a string.
|
java.lang.String |
readTextFrom(java.io.InputStream inStream,
ICheckedCharset charsetName)
Reads the whole content of the given input stream and returns
it as a string.
|
java.lang.String |
readTextFrom(java.io.Reader reader)
Reads all text from the given reader and returns it as a single string.
|
java.lang.String[] |
readTextLinesFrom(java.io.InputStream inStream)
Reads all text lines from the specified stream into a String array.
|
java.lang.String[] |
readTextLinesFrom(java.io.InputStream inStream,
java.nio.charset.Charset charset)
Reads all text lines from the specified stream into a String array.
|
java.lang.String[] |
readTextLinesFrom(java.io.InputStream inStream,
java.nio.charset.Charset charset,
IStringFilter filter)
Reads all text lines matching the filter from the specified stream into a
String array using the specified character encoding.
|
java.lang.String[] |
readTextLinesFrom(java.io.InputStream inStream,
ICheckedCharset charsetName,
IStringFilter filter)
Reads all text lines matching the filter from the specified stream into a
String array using the specified character encoding.
|
java.lang.String[] |
readTextLinesFrom(java.io.InputStream inStream,
IStringFilter filter)
Reads all text lines matching the filter from the specified stream into a
String array.
|
java.lang.String[] |
readTextLinesFrom(java.io.Reader reader)
Reads all text lines from the specified reader into a String array.
|
java.lang.String[] |
readTextLinesFrom(java.io.Reader reader,
IStringFilter filter)
Reads all text lines that match the filter from the specified reader into
a String array.
|
java.lang.String |
urlDecode(java.lang.String str)
Returns the given string with all %xx placeholders decoded.
|
java.lang.String |
urlDecode(java.lang.String str,
java.nio.charset.Charset charset)
Returns the given string with all %xx placeholders decoded using the
specified charset encoding.
|
java.lang.String |
urlEncode(java.lang.String str)
Returns the given string with all characters that are not allowed directly
in URLs encoded as %xx placeholders.
|
java.lang.String |
urlEncode(java.lang.String str,
java.nio.charset.Charset charset)
Returns the given string with all characters that are not allowed directly
in URLs encoded as %xx placeholders.
|
public static final java.lang.String LINE_SEPARATOR
public static final int DEFAULT_BUFFER_SIZE
public static final IProgressListener IGNORING_PROGRESS_LISTENER
protected static final java.lang.Long NO_LIMIT
public static IOUtil current()
public boolean close(java.io.Closeable closeable)
public void processTextLines(java.io.InputStream inStream, ILineProcessor processor) throws java.io.IOException
inStream
- The input stream that contains the textprocessor
- The processor that receives the lines from the textjava.io.IOException
public void processTextLines(java.io.InputStream inStream, ICheckedCharset charsetName, ILineProcessor processor) throws java.io.IOException
inStream
- The input stream that contains the text (must not be null).charsetName
- A charset name already checked to be supported (must not be null).processor
- The processor that receives the lines from the text (must not be null).java.io.IOException
public void processTextLines(java.io.InputStream inStream, java.nio.charset.Charset charset, ILineProcessor processor) throws java.io.IOException
inStream
- The input stream that contains the text (must not be null).charset
- A character set defining the encoding of the stream (must not be null).processor
- The processor that receives the lines from the text (must not be null).java.io.IOException
public void processTextLines(java.io.Reader reader, ILineProcessor processor) throws java.io.IOException
reader
- The reader that contains a text streamprocessor
- The processor that receives the lines from the textjava.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.Reader reader) throws java.io.IOException
reader
- The reader which provides the textjava.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.InputStream inStream) throws java.io.IOException
inStream
- The stream from which to read the text linesjava.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.InputStream inStream, IStringFilter filter) throws java.io.IOException
inStream
- The stream from which to read the text linesfilter
- A filter that defines which lines to return (may be null -> all lines)java.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.InputStream inStream, ICheckedCharset charsetName, IStringFilter filter) throws java.io.IOException
inStream
- The stream from which to read the text lines.charsetName
- A charset name already checked to be supported (must not be null).filter
- A filter that defines which lines to return (may be null -> all lines).java.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.InputStream inStream, java.nio.charset.Charset charset) throws java.io.IOException
inStream
- The stream from which to read the text lines (must not be null).charset
- A character set defining the encoding of the stream (must not be null).java.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.InputStream inStream, java.nio.charset.Charset charset, IStringFilter filter) throws java.io.IOException
inStream
- The stream from which to read the text lines (must not be null).charset
- A character set defining the encoding of the input stream (must not be null).filter
- A filter that defines which lines to return (may be null -> all lines).java.io.IOException
public java.lang.String[] readTextLinesFrom(java.io.Reader reader, IStringFilter filter) throws java.io.IOException
reader
- The reader which provides the textfilter
- A filter that defines which lines to return (may be null -> all lines)java.io.IOException
public java.lang.String readTextFrom(java.io.Reader reader) throws java.io.IOException
reader
- The reader from which to get the content (must not be null).java.io.IOException
- Any open/read problem.public void copyText(java.io.Reader reader, java.io.Writer writer) throws java.io.IOException
reader
- The reader which provides the text to copy (must not be null).writer
- The writer to which the text will be copied (must not be null).java.io.IOException
- If reading or writing fails.public void copyText(java.io.Reader reader, java.io.Writer writer, IProgressListener progressListener) throws java.io.IOException
reader
- The reader which provides the text to copy (must not be null).writer
- The writer to which the text will be copied (must not be null).progressListener
- A listener that will be called during the copy process before each line
gets actually written and once at the very end. The listener gets the current line number as argument and
the IProgressListener.UNDEFINED_SIZE
for the total lines.
The listener can be used to monitor the progress as well as stopping copying by returning false.
A null listener will be ignored.java.io.IOException
- If reading or writing fails.public java.lang.String readTextFrom(java.io.InputStream inStream) throws java.io.IOException
inStream
- The input stream to read.java.io.IOException
public java.lang.String readTextFrom(java.io.InputStream inStream, ICheckedCharset charsetName) throws java.io.IOException
inStream
- The input stream to read (must not be null).charsetName
- A charset name already checked to be supported (must not be null).java.io.IOException
public java.lang.String readTextFrom(java.io.InputStream inStream, java.nio.charset.Charset charset) throws java.io.IOException
inStream
- The input stream to read (must not be null).charset
- A character set defining the encoding of the stream (must not be null).java.io.IOException
public long copyStream(java.io.InputStream inStream, java.io.OutputStream outStream) throws java.io.IOException
inStream
- The source stream.outStream
- The target stream.java.io.IOException
public long copyStream(java.io.InputStream inStream, java.io.OutputStream outStream, IProgressListener listener) throws java.io.IOException
inStream
- The source stream.outStream
- The target stream.listener
- A listener that will be called during the copy process before each chunk of data (see bufferSize)
gets actually processed and once at the very end. The listener can be used to monitor the progress as well as
stopping copying by returning false. A null listener will be ignored.java.io.IOException
public long copyStream(java.io.InputStream inStream, java.io.OutputStream outStream, int bufSize) throws java.io.IOException
inStream
- The source stream.outStream
- The target stream.bufSize
- The size of the buffer that is used for the transfer of byte chunks (must be positive value).java.io.IOException
public long copyStream(java.io.InputStream inStream, java.io.OutputStream outStream, int bufSize, IProgressListener listener) throws java.io.IOException
inStream
- The source stream.outStream
- The target stream.bufSize
- The size of the buffer that is used for the transfer of byte chunks (must be positive value).listener
- A listener that will be called during the copy process before each chunk of data (see bufferSize)
gets actually processed and once at the very end. The listener can be used to monitor the progress as well as
stopping copying by returning false. A null listener will be ignored.java.io.IOException
public long copyStream(java.io.InputStream inStream, java.io.OutputStream outStream, int bufSize, java.lang.Long limit) throws java.io.IOException
inStream
- The source stream.outStream
- The target stream.bufSize
- The size of the buffer that is used for the transfer of byte chunks (must be positive value).limit
- The maximum number of bytes that will be copied. If exceeded an IOException will be thrown.
Setting this parameter to null, zero or a negative value will specify no limit.java.io.IOException
public long copyStream(java.io.InputStream inStream, java.io.OutputStream outStream, int bufSize, java.lang.Long limit, IProgressListener listener) throws java.io.IOException
inStream
- The source stream.outStream
- The target stream.bufSize
- The size of the buffer that is used for the transfer of byte chunks (must be positive value).limit
- The maximum number of bytes that will be copied. If exceeded an IOException will be thrown.
Setting this parameter to null, zero or a negative value will specify no limit.listener
- A listener that will be called during the copy process before each chunk of data (see bufferSize)
gets actually processed and once at the very end. The listener can be used to monitor the progress as well as
stopping copying by returning false. A null listener will be ignored.java.io.IOException
public java.io.InputStream asStream(java.lang.String string, java.nio.charset.Charset charset)
string
- The string to convert to a stream (must not be null).charset
- The character encoding to convert the string to bytes. If null,
the default platform encoding will be used.public java.lang.String urlDecode(java.lang.String str, java.nio.charset.Charset charset)
str
- The string to decode.charset
- The charset to be used for decoding.public java.lang.String urlDecode(java.lang.String str)
str
- The string to decodepublic java.lang.String urlEncode(java.lang.String str, java.nio.charset.Charset charset)
str
- The string to encode.charset
- The charset to be used for encoding.public java.lang.String urlEncode(java.lang.String str)
str
- The string to encode.protected java.lang.String[] basicReadTextLinesFrom(java.io.Reader reader, IStringFilter filter) throws java.io.IOException
reader
- The reader which provides the textfilter
- A filter that defines which lines to return (may be null -> all lines)java.io.IOException
protected void copyText(java.io.InputStream inStream, java.nio.charset.Charset charset, java.io.StringWriter writer) throws java.io.IOException
java.io.IOException
protected void assertArgumentNotNull(java.lang.String argName, java.lang.Object arg)