IOUtils

General IO stream manipulation utilities.

This class provides static utility methods for input/output operations.

  • closeQuietly - these methods close a stream ignoring nulls and exceptions
  • toXxx/read - these methods read data from a stream
  • write - these methods write data to a stream
  • copy - these methods copy all the data from one stream to another
  • contentEquals - these methods compare the content of two streams

The byte-to-char methods and char-to-byte methods involve a conversion step. Two methods are provided in each case, one that uses the platform default encoding and the other which allows you to specify an encoding. You are encouraged to always specify an encoding because relying on the platform default can lead to unexpected results, for example when moving from development to production.

All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream or BufferedReader. The default buffer size of 4K has been shown to be efficient in tests.

Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.

Origin of code: Excalibur.

Author

Sandy McArthur

open class IOUtils

Constructors

IOUtils
Link copied to clipboard

Instances should NOT be constructed in standard programming.

open fun IOUtils()

Functions

closeQuietly
Link copied to clipboard

Unconditionally close an InputStream.

Equivalent to , except any exceptions will be ignored. This is typically used in finally blocks.

open fun closeQuietly(input: InputStream)

Unconditionally close an OutputStream.

Equivalent to , except any exceptions will be ignored. This is typically used in finally blocks.

open fun closeQuietly(output: OutputStream)
copy
Link copied to clipboard

Copy bytes from an InputStream to an OutputStream.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

Large streams (over 2GB) will return a bytes copied value of -1 after the copy has completed since the correct number of bytes cannot be returned as an int. For large streams use the copyLarge(InputStream, OutputStream) method.

open fun copy(input: InputStream, output: OutputStream): Int

Copy bytes from an InputStream to chars on a Writer using the default character encoding of the platform.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

This method uses InputStreamReader .

open fun copy(input: InputStream, output: Writer)

Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

This method buffers the input internally, so there is no need to use a BufferedReader.

Due to the implementation of OutputStreamWriter, this method performs a flush.

This method uses OutputStreamWriter .

open fun copy(input: Reader, output: OutputStream)

Copy chars from a Reader to a Writer.

This method buffers the input internally, so there is no need to use a BufferedReader.

Large streams (over 2GB) will return a chars copied value of -1 after the copy has completed since the correct number of chars cannot be returned as an int. For large streams use the copyLarge(Reader, Writer) method.

open fun copy(input: Reader, output: Writer): Int

Copy bytes from an InputStream to chars on a Writer using the specified character encoding.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

Character encoding names can be found at IANA.

This method uses InputStreamReader .

open fun copy(input: InputStream, output: Writer, encoding: String)

Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

This method buffers the input internally, so there is no need to use a BufferedReader.

Character encoding names can be found at IANA.

Due to the implementation of OutputStreamWriter, this method performs a flush.

This method uses OutputStreamWriter .

open fun copy(input: Reader, output: OutputStream, encoding: String)
copyLarge
Link copied to clipboard

Copy bytes from a large (over 2GB) InputStream to an OutputStream.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

open fun copyLarge(input: InputStream, output: OutputStream): Long

Copy chars from a large (over 2GB) Reader to a Writer.

This method buffers the input internally, so there is no need to use a BufferedReader.

open fun copyLarge(input: Reader, output: Writer): Long
toString
Link copied to clipboard

Get the contents of a byte[] as a String using the default character encoding of the platform.

open fun toString(input: Array<Byte>): String

Get the contents of an InputStream as a String using the default character encoding of the platform.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

open fun toString(input: InputStream): String

Get the contents of a Reader as a String.

This method buffers the input internally, so there is no need to use a BufferedReader.

open fun toString(input: Reader): String

Get the contents of a byte[] as a String using the specified character encoding.

Character encoding names can be found at IANA.

open fun toString(input: Array<Byte>, encoding: String): String

Get the contents of an InputStream as a String using the specified character encoding.

Character encoding names can be found at IANA.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

open fun toString(input: InputStream, encoding: String): String
write
Link copied to clipboard

Writes bytes from a byte[] to an OutputStream.

open fun write(data: Array<Byte>, output: OutputStream)

Writes bytes from a byte[] to chars on a Writer using the default character encoding of the platform.

This method uses String .

open fun write(data: Array<Byte>, output: Writer)

Writes chars from a char[] to bytes on an OutputStream.

This method uses String and getBytes .

open fun write(data: Array<Char>, output: OutputStream)

Writes chars from a char[] to a Writer using the default character encoding of the platform.

open fun write(data: Array<Char>, output: Writer)

Writes chars from a String to bytes on an OutputStream using the default character encoding of the platform.

This method uses getBytes .

open fun write(data: String, output: OutputStream)

Writes chars from a String to a Writer.

open fun write(data: String, output: Writer)

Writes chars from a StringBuffer to bytes on an OutputStream using the default character encoding of the platform.

This method uses getBytes .

open fun write(data: StringBuffer, output: OutputStream)

Writes chars from a StringBuffer to a Writer.

open fun write(data: StringBuffer, output: Writer)

Writes bytes from a byte[] to chars on a Writer using the specified character encoding.

Character encoding names can be found at IANA.

This method uses String .

open fun write(data: Array<Byte>, output: Writer, encoding: String)

Writes chars from a char[] to bytes on an OutputStream using the specified character encoding.

Character encoding names can be found at IANA.

This method uses String and getBytes .

open fun write(data: Array<Char>, output: OutputStream, encoding: String)

Writes chars from a String to bytes on an OutputStream using the specified character encoding.

Character encoding names can be found at IANA.

This method uses getBytes .

open fun write(data: String, output: OutputStream, encoding: String)

Writes chars from a StringBuffer to bytes on an OutputStream using the specified character encoding.

Character encoding names can be found at IANA.

This method uses getBytes .

open fun write(data: StringBuffer, output: OutputStream, encoding: String)

Properties

DIR_SEPARATOR
Link copied to clipboard

The system directory separator character.

val DIR_SEPARATOR: Char
DIR_SEPARATOR_UNIX
Link copied to clipboard

The Unix directory separator character.

val DIR_SEPARATOR_UNIX: Char
DIR_SEPARATOR_WINDOWS
Link copied to clipboard

The Windows directory separator character.

val DIR_SEPARATOR_WINDOWS: Char
LINE_SEPARATOR
Link copied to clipboard

The system line separator string.

val LINE_SEPARATOR: String
LINE_SEPARATOR_UNIX
Link copied to clipboard

The Unix line separator string.

val LINE_SEPARATOR_UNIX: String
LINE_SEPARATOR_WINDOWS
Link copied to clipboard

The Windows line separator string.

val LINE_SEPARATOR_WINDOWS: String