Package org.apache.camel.util
Class IOHelper
java.lang.Object
org.apache.camel.util.IOHelper
IO helper class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Encoding-aware file reader.static class
Encoding-aware file writer.static class
Encoding-aware input stream. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final long
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendText
(String text, File file) Appends the text to the file.static BufferedInputStream
buffered
(InputStream in) Wraps the passedin
into aBufferedInputStream
object and returns that.static BufferedOutputStream
buffered
(OutputStream out) Wraps the passedout
into aBufferedOutputStream
object and returns that.static BufferedReader
Wraps the passedreader
into aBufferedReader
object and returns that.static BufferedWriter
Wraps the passedwriter
into aBufferedWriter
object and returns that.static void
Closes the given resource if it is available.static void
Closes the given resources if they are available.static void
Closes the given resource if it is available.static void
Closes the given resource if it is available, logging any closing exceptions to the given log.static void
close
(Writer writer, FileOutputStream os, String name, org.slf4j.Logger log, boolean force) Closes the given writer, logging any closing exceptions to the given log.static void
close
(FileChannel channel, String name, org.slf4j.Logger log, boolean force) Closes the given channel if it is available, logging any closing exceptions to the given log.static void
closeIterator
(Object it) static void
closeWithException
(Closeable closeable) Closes the given resource if it is available and don't catch the exceptionstatic int
copy
(InputStream input, OutputStream output) static int
copy
(InputStream input, OutputStream output, int bufferSize) static int
copy
(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite) static int
copy
(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite, long maxSize) static int
static void
copyAndCloseInput
(InputStream input, OutputStream output) static void
copyAndCloseInput
(InputStream input, OutputStream output, int bufferSize) static void
force
(FileOutputStream os, String name, org.slf4j.Logger log) Forces any updates to a FileOutputStream be written to the storage device that contains it.static void
force
(FileChannel channel, String name, org.slf4j.Logger log) Forces any updates to this channel's file to be written to the storage device that contains it.static String
getCharsetNameFromContentType
(String contentType) Get the charset name from the content type stringstatic String
loadText
(InputStream in) Loads the entire stream into memory as a String and returns it.static String
loadTextLine
(InputStream in, int lineNumber) Loads the entire stream into memory as a String and returns the given line number.static String
Lookup the OS environment variable in a safe manner by using upper case keys and underscore instead of dash.static String
normalizeCharset
(String charset) This method will take off the quotes and double quotes of the charsetstatic String
stripLineComments
(Path path, String commentPrefix, boolean stripBlankLines) Reads the file under the givenpath
, strips lines starting withcommentPrefix
and optionally also strips blank lines (the ones for whichString.isBlank()
returnstrue
.static InputStream
toInputStream
(File file, String charset) Converts the givenFile
with the given charset toInputStream
with the JVM default charsetstatic BufferedReader
static BufferedReader
static String
toString
(BufferedReader reader) static String
toString
(BufferedReader reader, long offset) static String
static String
static BufferedWriter
toWriter
(FileOutputStream os, String charset) static BufferedWriter
toWriter
(FileOutputStream os, Charset charset) static void
transfer
(ReadableByteChannel input, WritableByteChannel output) static void
validateCharset
(String charset) static void
Writes the text to the file.static void
writeText
(String text, OutputStream os) Writes the text to the stream.
-
Field Details
-
defaultCharset
-
DEFAULT_BUFFER_SIZE
- See Also:
-
INITIAL_OFFSET
- See Also:
-
-
Method Details
-
buffered
Wraps the passedin
into aBufferedInputStream
object and returns that. If the passedin
is already an instance ofBufferedInputStream
returns the same passedin
reference as is (avoiding double wrapping).- Parameters:
in
- the wrapee to be used for the buffering support- Returns:
- the passed
in
decorated through aBufferedInputStream
object as wrapper
-
buffered
Wraps the passedout
into aBufferedOutputStream
object and returns that. If the passedout
is already an instance ofBufferedOutputStream
returns the same passedout
reference as is (avoiding double wrapping).- Parameters:
out
- the wrapee to be used for the buffering support- Returns:
- the passed
out
decorated through aBufferedOutputStream
object as wrapper
-
buffered
Wraps the passedreader
into aBufferedReader
object and returns that. If the passedreader
is already an instance ofBufferedReader
returns the same passedreader
reference as is (avoiding double wrapping).- Parameters:
reader
- the wrapee to be used for the buffering support- Returns:
- the passed
reader
decorated through aBufferedReader
object as wrapper
-
buffered
Wraps the passedwriter
into aBufferedWriter
object and returns that. If the passedwriter
is already an instance ofBufferedWriter
returns the same passedwriter
reference as is (avoiding double wrapping).- Parameters:
writer
- the writer to be used for the buffering support- Returns:
- the passed
writer
decorated through aBufferedWriter
object as wrapper
-
toString
- Throws:
IOException
-
toString
- Throws:
IOException
-
toString
- Throws:
IOException
-
toString
- Throws:
IOException
-
copy
- Throws:
IOException
-
copy
- Throws:
IOException
-
copy
public static int copy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite) throws IOException - Throws:
IOException
-
copy
public static int copy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite, long maxSize) throws IOException - Throws:
IOException
-
copyAndCloseInput
- Throws:
IOException
-
copyAndCloseInput
public static void copyAndCloseInput(InputStream input, OutputStream output, int bufferSize) throws IOException - Throws:
IOException
-
copy
- Throws:
IOException
-
transfer
public static void transfer(ReadableByteChannel input, WritableByteChannel output) throws IOException - Throws:
IOException
-
force
Forces any updates to this channel's file to be written to the storage device that contains it.- Parameters:
channel
- the file channelname
- the name of the resourcelog
- the log to use when reporting warnings, will use this class's ownLogger
if log == null
-
force
Forces any updates to a FileOutputStream be written to the storage device that contains it.- Parameters:
os
- the file output streamname
- the name of the resourcelog
- the log to use when reporting warnings, will use this class's ownLogger
if log == null
-
close
public static void close(Writer writer, FileOutputStream os, String name, org.slf4j.Logger log, boolean force) Closes the given writer, logging any closing exceptions to the given log. An associated FileOutputStream can optionally be forced to disk.- Parameters:
writer
- the writer to closeos
- an underlying FileOutputStream that will to be forced to disk according to the force parametername
- the name of the resourcelog
- the log to use when reporting warnings, will use this class's ownLogger
if log == nullforce
- forces the FileOutputStream to disk
-
close
Closes the given resource if it is available, logging any closing exceptions to the given log.- Parameters:
closeable
- the object to closename
- the name of the resourcelog
- the log to use when reporting closure warnings, will use this class's ownLogger
if log == null
-
closeWithException
Closes the given resource if it is available and don't catch the exception- Parameters:
closeable
- the object to close- Throws:
IOException
-
close
Closes the given channel if it is available, logging any closing exceptions to the given log. The file's channel can optionally be forced to disk.- Parameters:
channel
- the file channelname
- the name of the resourcelog
- the log to use when reporting warnings, will use this class's ownLogger
if log == nullforce
- forces the file channel to disk
-
close
Closes the given resource if it is available.- Parameters:
closeable
- the object to closename
- the name of the resource
-
close
Closes the given resource if it is available.- Parameters:
closeable
- the object to close
-
close
Closes the given resources if they are available.- Parameters:
closeables
- the objects to close
-
closeIterator
- Throws:
IOException
-
validateCharset
- Throws:
UnsupportedCharsetException
-
loadText
Loads the entire stream into memory as a String and returns it. Notice: This implementation appends a \n as line terminator at the of the text. Warning, don't use for crazy big streams :)- Throws:
IOException
-
loadTextLine
Loads the entire stream into memory as a String and returns the given line number. Warning, don't use for crazy big streams :)- Throws:
IOException
-
appendText
Appends the text to the file.- Throws:
IOException
-
writeText
Writes the text to the file.- Throws:
IOException
-
writeText
Writes the text to the stream.- Throws:
IOException
-
getCharsetNameFromContentType
Get the charset name from the content type string- Parameters:
contentType
- the content type- Returns:
- the charset name, or UTF-8 if no found
-
normalizeCharset
This method will take off the quotes and double quotes of the charset -
lookupEnvironmentVariable
Lookup the OS environment variable in a safe manner by using upper case keys and underscore instead of dash. -
toInputStream
Converts the givenFile
with the given charset toInputStream
with the JVM default charset- Parameters:
file
- the file to be convertedcharset
- the charset the file is read with- Returns:
- the input stream with the JVM default charset
- Throws:
IOException
-
toReader
- Throws:
IOException
-
toReader
- Throws:
IOException
-
toWriter
- Throws:
IOException
-
toWriter
-
stripLineComments
Reads the file under the givenpath
, strips lines starting withcommentPrefix
and optionally also strips blank lines (the ones for whichString.isBlank()
returnstrue
. Normalizes EOL characters to'\n'
.- Parameters:
path
- the path of the file to readcommentPrefix
- the leading character sequence of comment lines.stripBlankLines
- if truetrue
the lines matchingString.isBlank()
will not appear in the result- Returns:
- the filtered content of the file
-