Class IOHelper

java.lang.Object
org.apache.camel.util.IOHelper

public final class IOHelper extends Object
IO helper class.
  • Field Details

  • Method Details

    • buffered

      Wraps the passed in into a BufferedInputStream object and returns that. If the passed in is already an instance of BufferedInputStream returns the same passed in reference as is (avoiding double wrapping).
      Parameters:
      in - the wrapee to be used for the buffering support
      Returns:
      the passed in decorated through a BufferedInputStream object as wrapper
    • buffered

      Wraps the passed out into a BufferedOutputStream object and returns that. If the passed out is already an instance of BufferedOutputStream returns the same passed out reference as is (avoiding double wrapping).
      Parameters:
      out - the wrapee to be used for the buffering support
      Returns:
      the passed out decorated through a BufferedOutputStream object as wrapper
    • buffered

      public static BufferedReader buffered(Reader reader)
      Wraps the passed reader into a BufferedReader object and returns that. If the passed reader is already an instance of BufferedReader returns the same passed reader reference as is (avoiding double wrapping).
      Parameters:
      reader - the wrapee to be used for the buffering support
      Returns:
      the passed reader decorated through a BufferedReader object as wrapper
    • buffered

      public static BufferedWriter buffered(Writer writer)
      Wraps the passed writer into a BufferedWriter object and returns that. If the passed writer is already an instance of BufferedWriter returns the same passed writer reference as is (avoiding double wrapping).
      Parameters:
      writer - the writer to be used for the buffering support
      Returns:
      the passed writer decorated through a BufferedWriter object 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
      Copies the data from the input stream to the output stream. Uses InputStream.transferTo(OutputStream).
      Parameters:
      input - the input stream buffer
      output - the output stream buffer
      Returns:
      the number of bytes copied
      Throws:
      IOException - for I/O errors
    • copy

      public static int copy(InputStream input, OutputStream output, int bufferSize) throws IOException
      Copies the data from the input stream to the output stream. Uses the legacy copy logic. Prefer using copy(InputStream, OutputStream) unless you have to control how data is flushed the buffer
      Parameters:
      input - the input stream buffer
      output - the output stream buffer
      bufferSize - 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 using copy(InputStream, OutputStream) unless you have to control how data is flushed the buffer
      Parameters:
      input - the input stream buffer
      output - the output stream buffer
      bufferSize - the size of the buffer used for the copies
      flushOnEachWrite - 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
      Copies the data from the input stream to the output stream. Uses the legacy copy logic. Prefer using copy(InputStream, OutputStream) unless you have to control how data is flushed the buffer
      Parameters:
      input - the input stream buffer
      output - the output stream buffer
      bufferSize - the size of the buffer used for the copies
      flushOnEachWrite - 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

      public static void copyAndCloseInput(InputStream input, OutputStream output) throws IOException
      Copies the data from the input stream to the output stream and closes the input stream afterward. Uses InputStream.transferTo(OutputStream).
      Parameters:
      input - the input stream buffer
      output - 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 using copyAndCloseInput(InputStream, OutputStream) unless you need a specific buffer size.
      Parameters:
      input - the input stream buffer
      output - the output stream buffer
      bufferSize - the size of the buffer used for the copies
      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 channel
      name - the name of the resource
      log - the log to use when reporting warnings, will use this class's own Logger if 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 stream
      name - the name of the resource
      log - the log to use when reporting warnings, will use this class's own Logger 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 close
      os - an underlying FileOutputStream that will to be forced to disk according to the force parameter
      name - the name of the resource
      log - the log to use when reporting warnings, will use this class's own Logger if log == null
      force - 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 close
      name - the name of the resource
      log - the log to use when reporting closure warnings, will use this class's own Logger if 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 channel
      name - the name of the resource
      log - the log to use when reporting warnings, will use this class's own Logger if log == null
      force - 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 close
      name - 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
    • loadTextLine

      public static String loadTextLine(InputStream in, int lineNumber) throws IOException
      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

      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

      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

      public static InputStream toInputStream(File file, String charset) throws IOException
      Converts the given File with the given charset to InputStream with the JVM default charset
      Parameters:
      file - the file to be converted
      charset - the charset the file is read with
      Returns:
      the input stream with the JVM default charset
      Throws:
      IOException
    • toInputStream

      public static InputStream toInputStream(Path file, String charset) throws IOException
      Converts the given File with the given charset to InputStream with the JVM default charset
      Parameters:
      file - the file to be converted
      charset - the charset the file is read with
      Returns:
      the input stream with the JVM default charset
      Throws:
      IOException
    • toReader

      public static BufferedReader toReader(Path file, String charset) throws IOException
      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
    • toReader

      public static BufferedReader toReader(Path 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)
    • stripLineComments

      public static String stripLineComments(Path path, String commentPrefix, boolean stripBlankLines)
      Reads the file under the given path, strips lines starting with commentPrefix and optionally also strips blank lines (the ones for which String.isBlank() returns true. Normalizes EOL characters to '\n'.
      Parameters:
      path - the path of the file to read
      commentPrefix - the leading character sequence of comment lines.
      stripBlankLines - if true true the lines matching String.isBlank() will not appear in the result
      Returns:
      the filtered content of the file