Package org.apache.camel.util
Class IOHelper
- java.lang.Object
-
- org.apache.camel.util.IOHelper
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIOHelper.EncodingFileReaderEncoding-aware file reader.static classIOHelper.EncodingFileWriterEncoding-aware file writer.static classIOHelper.EncodingInputStreamEncoding-aware input stream.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFFER_SIZEstatic Supplier<Charset>defaultCharsetstatic longINITIAL_OFFSET
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 BufferedReaderbuffered(Reader reader)Wraps the passedreaderinto aBufferedReaderobject and returns that.static BufferedWriterbuffered(Writer writer)Wraps the passedwriterinto aBufferedWriterobject and returns that.static voidclose(Closeable closeable)Closes the given resource if it is available.static voidclose(Closeable... closeables)Closes the given resources if they are available.static voidclose(Closeable closeable, String name)Closes the given resource if it is available.static voidclose(Closeable closeable, String name, org.slf4j.Logger log)Closes 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 intcopy(Reader input, Writer output, int bufferSize)static 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 StringlookupEnvironmentVariable(String key)Lookup 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 BufferedReadertoReader(File file, String charset)static BufferedReadertoReader(File file, Charset charset)static StringtoString(BufferedReader reader)static StringtoString(BufferedReader reader, long offset)static StringtoString(Reader reader)static StringtoString(Reader reader, long offset)static BufferedWritertoWriter(FileOutputStream os, String charset)static BufferedWritertoWriter(FileOutputStream os, Charset charset)static voidtransfer(ReadableByteChannel input, WritableByteChannel output)static voidvalidateCharset(String charset)static voidwriteText(String text, File file)Writes the text to the file.static voidwriteText(String text, OutputStream os)Writes the text to the stream.
-
-
-
Field Detail
-
defaultCharset
public static Supplier<Charset> defaultCharset
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
INITIAL_OFFSET
public static final long INITIAL_OFFSET
- See Also:
- Constant Field Values
-
-
Method Detail
-
buffered
public static BufferedInputStream buffered(InputStream in)
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
public static BufferedOutputStream buffered(OutputStream out)
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
public static BufferedReader buffered(Reader reader)
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
public static BufferedWriter buffered(Writer writer)
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
public static String toString(Reader reader) throws IOException
- Throws:
IOException
-
toString
public static String toString(Reader reader, long offset) throws IOException
- Throws:
IOException
-
toString
public static String toString(BufferedReader reader) throws IOException
- Throws:
IOException
-
toString
public static String toString(BufferedReader reader, long offset) throws IOException
- Throws:
IOException
-
copy
public static int copy(InputStream input, OutputStream output) throws IOException
- Throws:
IOException
-
copy
public static int copy(InputStream input, OutputStream output, int bufferSize) throws IOException
- 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
public static void copyAndCloseInput(InputStream input, OutputStream output) throws IOException
- Throws:
IOException
-
copyAndCloseInput
public static void copyAndCloseInput(InputStream input, OutputStream output, int bufferSize) throws IOException
- Throws:
IOException
-
copy
public static int copy(Reader input, Writer output, int bufferSize) throws IOException
- Throws:
IOException
-
transfer
public static void transfer(ReadableByteChannel input, WritableByteChannel output) throws IOException
- Throws:
IOException
-
force
public 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.- 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
public 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.- 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
public static void close(Closeable closeable, String name, org.slf4j.Logger log)
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
public static void closeWithException(Closeable closeable) throws IOException
Closes the given resource if it is available and don't catch the exception- Parameters:
closeable- the object to close- Throws:
IOException
-
close
public 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. 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
public static void close(Closeable closeable, String name)
Closes the given resource if it is available.- Parameters:
closeable- the object to closename- the name of the resource
-
close
public static void close(Closeable closeable)
Closes the given resource if it is available.- Parameters:
closeable- the object to close
-
close
public static void close(Closeable... closeables)
Closes the given resources if they are available.- Parameters:
closeables- the objects to close
-
closeIterator
public static void closeIterator(Object it) throws IOException
- Throws:
IOException
-
validateCharset
public static void validateCharset(String charset) throws UnsupportedCharsetException
- Throws:
UnsupportedCharsetException
-
loadText
public static String loadText(InputStream in) throws IOException
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
public static void appendText(String text, File file) throws IOException
Appends the text to the file.- Throws:
IOException
-
writeText
public static void writeText(String text, File file) throws IOException
Writes the text to the file.- Throws:
IOException
-
writeText
public static void writeText(String text, OutputStream os) throws IOException
Writes the text to the stream.- Throws:
IOException
-
getCharsetNameFromContentType
public static String getCharsetNameFromContentType(String contentType)
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
public static String normalizeCharset(String charset)
This method will take off the quotes and double quotes of the charset
-
lookupEnvironmentVariable
public static String lookupEnvironmentVariable(String key)
Lookup the OS environment variable in a safe manner by using upper case keys and underscore instead of dash.
-
toInputStream
public static InputStream toInputStream(File file, String charset) throws IOException
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
public static BufferedReader toReader(File file, String charset) throws IOException
- Throws:
IOException
-
toReader
public static BufferedReader toReader(File file, Charset charset) throws IOException
- Throws:
IOException
-
toWriter
public static BufferedWriter toWriter(FileOutputStream os, String charset) throws IOException
- Throws:
IOException
-
toWriter
public static BufferedWriter toWriter(FileOutputStream os, Charset charset)
-
-