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 classEncoding-aware file reader.static classEncoding-aware file writer.static classEncoding-aware input stream. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final long -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendText(String text, File file) Appends the text to the file.static BufferedInputStreambuffered(InputStream in) Wraps the passedininto aBufferedInputStreamobject and returns that.static BufferedOutputStreambuffered(OutputStream out) Wraps the passedoutinto aBufferedOutputStreamobject and returns that.static BufferedReaderWraps the passedreaderinto aBufferedReaderobject and returns that.static BufferedWriterWraps the passedwriterinto aBufferedWriterobject and returns that.static voidCloses the given resource if it is available.static voidCloses the given resources if they are available.static voidCloses the given resource if it is available.static voidCloses the given resource if it is available, logging any closing exceptions to the given log.static voidclose(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 voidclose(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 voidcloseIterator(Object it) static voidcloseWithException(Closeable closeable) Closes the given resource if it is available and don't catch the exceptionstatic intcopy(InputStream input, OutputStream output) static intcopy(InputStream input, OutputStream output, int bufferSize) static intcopy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite) static intcopy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite, long maxSize) static intstatic voidcopyAndCloseInput(InputStream input, OutputStream output) static voidcopyAndCloseInput(InputStream input, OutputStream output, int bufferSize) static voidforce(FileOutputStream os, String name, org.slf4j.Logger log) Forces any updates to a FileOutputStream be written to the storage device that contains it.static voidforce(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 StringgetCharsetNameFromContentType(String contentType) Get the charset name from the content type stringstatic StringloadText(InputStream in) Loads the entire stream into memory as a String and returns it.static StringLookup the OS environment variable in a safe manner by using upper case keys and underscore instead of dash.static StringnormalizeCharset(String charset) This method will take off the quotes and double quotes of the charsetstatic InputStreamtoInputStream(File file, String charset) Converts the givenFilewith the given charset toInputStreamwith the JVM default charsetstatic BufferedReaderstatic BufferedReaderstatic StringtoString(BufferedReader reader) static StringtoString(BufferedReader reader, long offset) static Stringstatic Stringstatic BufferedWritertoWriter(FileOutputStream os, String charset) static BufferedWritertoWriter(FileOutputStream os, Charset charset) static voidtransfer(ReadableByteChannel input, WritableByteChannel output) static voidvalidateCharset(String charset) static voidWrites the text to the file.static voidwriteText(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 passedininto aBufferedInputStreamobject and returns that. If the passedinis already an instance ofBufferedInputStreamreturns the same passedinreference as is (avoiding double wrapping).- Parameters:
in- the wrapee to be used for the buffering support- Returns:
- the passed
indecorated through aBufferedInputStreamobject as wrapper
-
buffered
Wraps the passedoutinto aBufferedOutputStreamobject and returns that. If the passedoutis already an instance ofBufferedOutputStreamreturns the same passedoutreference as is (avoiding double wrapping).- Parameters:
out- the wrapee to be used for the buffering support- Returns:
- the passed
outdecorated through aBufferedOutputStreamobject as wrapper
-
buffered
Wraps the passedreaderinto aBufferedReaderobject and returns that. If the passedreaderis already an instance ofBufferedReaderreturns the same passedreaderreference as is (avoiding double wrapping).- Parameters:
reader- the wrapee to be used for the buffering support- Returns:
- the passed
readerdecorated through aBufferedReaderobject as wrapper
-
buffered
Wraps the passedwriterinto aBufferedWriterobject and returns that. If the passedwriteris already an instance ofBufferedWriterreturns the same passedwriterreference as is (avoiding double wrapping).- Parameters:
writer- the writer to be used for the buffering support- Returns:
- the passed
writerdecorated through aBufferedWriterobject 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 ownLoggerif 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 ownLoggerif 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 ownLoggerif 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 ownLoggerif 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 ownLoggerif 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
-
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 givenFilewith the given charset toInputStreamwith 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
-