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) Copies the data from the input stream to the output stream.static intcopy(InputStream input, OutputStream output, int bufferSize) Deprecated.static intcopy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite) Copies the data from the input stream to the output stream.static intcopy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite, long maxSize) Deprecated.Prefer usingcopy(InputStream, OutputStream)static intstatic voidcopyAndCloseInput(InputStream input, OutputStream output) Copies the data from the input stream to the output stream and closes the input stream afterward.static voidcopyAndCloseInput(InputStream input, OutputStream output, int bufferSize) Copies the data from the input stream to the output stream and closes the input stream afterward.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 StringloadTextLine(InputStream in, int lineNumber) Loads the entire stream into memory as a String and returns the given line number.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 StringConvert given key into an OS environment variable.static StringstripLineComments(Path path, String commentPrefix, boolean stripBlankLines) Reads the file under the givenpath, strips lines starting withcommentPrefixand optionally also strips blank lines (the ones for whichString.isBlank()returnstrue.static InputStreamtoInputStream(File file, String charset) Converts the givenFilewith the given charset toInputStreamwith the JVM default charsetstatic InputStreamtoInputStream(Path file, String charset) Converts the givenFilewith the given charset toInputStreamwith the JVM default charsetstatic BufferedReaderstatic BufferedReaderstatic 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
Copies the data from the input stream to the output stream. UsesInputStream.transferTo(OutputStream).- Parameters:
input- the input stream bufferoutput- the output stream buffer- Returns:
- the number of bytes copied
- Throws:
IOException- for I/O errors
-
copy
@Deprecated(since="4.8.0") public static int copy(InputStream input, OutputStream output, int bufferSize) throws IOException Deprecated.Prefer usingcopy(InputStream, OutputStream)Copies the data from the input stream to the output stream. Uses the legacy copy logic. Prefer usingcopy(InputStream, OutputStream)unless you have to control how data is flushed the buffer- Parameters:
input- the input stream bufferoutput- the output stream bufferbufferSize- the size of the buffer used for the copies- Returns:
- the number of bytes copied
- Throws:
IOException- for I/O errors
-
copy
public static int copy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite) throws IOException Copies the data from the input stream to the output stream. Uses the legacy copy logic. Prefer usingcopy(InputStream, OutputStream)unless you have to control how data is flushed the buffer- Parameters:
input- the input stream bufferoutput- the output stream bufferbufferSize- the size of the buffer used for the copiesflushOnEachWrite- whether to flush the data everytime that data is written to the buffer- Returns:
- the number of bytes copied
- Throws:
IOException- for I/O errors
-
copy
public static int copy(InputStream input, OutputStream output, int bufferSize, boolean flushOnEachWrite, long maxSize) throws IOException Deprecated.Prefer usingcopy(InputStream, OutputStream)Copies the data from the input stream to the output stream. Uses the legacy copy logic. Prefer usingcopy(InputStream, OutputStream)unless you have to control how data is flushed the buffer- Parameters:
input- the input stream bufferoutput- the output stream bufferbufferSize- the size of the buffer used for the copiesflushOnEachWrite- whether to flush the data everytime that data is written to the buffer- Returns:
- the number of bytes copied
- Throws:
IOException- for I/O errors
-
copyAndCloseInput
Copies the data from the input stream to the output stream and closes the input stream afterward. UsesInputStream.transferTo(OutputStream).- Parameters:
input- the input stream bufferoutput- the output stream buffer- Throws:
IOException
-
copyAndCloseInput
public static void copyAndCloseInput(InputStream input, OutputStream output, int bufferSize) throws IOException Copies the data from the input stream to the output stream and closes the input stream afterward. Uses Camel's own copying logic. Prefer usingcopyAndCloseInput(InputStream, OutputStream)unless you need a specific buffer size.- Parameters:
input- the input stream bufferoutput- the output stream bufferbufferSize- the size of the buffer used for the copies- 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
-
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. At first lookup attempt is made without considering camelCase keys. The second lookup is converting camelCase to underscores. For example given an ENV variable in either format: - CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN=myArn - CAMEL_KAMELET_AWS_S3_SOURCE_BUCKET_NAME_OR_ARN=myArn Then the following keys can look up both ENV formats above: - camel.kamelet.awsS3Source.bucketNameOrArn - camel.kamelet.aws-s3-source.bucketNameOrArn - camel.kamelet.aws-s3-source.bucket-name-or-arn -
normalizeEnvironmentVariable
Convert given key into an OS environment variable. Uses uppercase keys and converts dashes and dots to underscores. -
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
-
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
-
toReader
- Throws:
IOException
-
toReader
- Throws:
IOException
-
toWriter
- Throws:
IOException
-
toWriter
-
stripLineComments
Reads the file under the givenpath, strips lines starting withcommentPrefixand 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 truetruethe lines matchingString.isBlank()will not appear in the result- Returns:
- the filtered content of the file
-
copy(InputStream, OutputStream)