Class IOUtil

java.lang.Object
org.eclipse.rdf4j.common.io.IOUtil

public class IOUtil extends Object
Utility methods for I/O working with Readers, Writers, InputStreams and OutputStreams.
  • Constructor Details

    • IOUtil

      public IOUtil()
  • Method Details

    • readString

      public static String readString(File file) throws IOException
      Read the contents as a string from the given (unbuffered) file.
      Parameters:
      file - file to read
      Returns:
      content as one single string
      Throws:
      IOException
    • readString

      public static String readString(URL url) throws IOException
      Read the contents of a (unbuffered) resource into one single string.
      Parameters:
      url - url to get the data from
      Returns:
      string
      Throws:
      IOException
    • readString

      public static String readString(InputStream in) throws IOException
      Read the contents of an (unbuffered) input stream into a single string.
      Parameters:
      in - input stream
      Returns:
      string
      Throws:
      IOException
    • readString

      public static String readString(Reader r) throws IOException
      Reads all characters from the supplied reader and returns them as a string.
      Parameters:
      r - The Reader supplying the characters
      Returns:
      A String containing all characters from the supplied reader.
      Throws:
      IOException
    • readString

      public static String readString(Reader r, int maxChars) throws IOException
      Reads a string of at most length maxChars from the supplied Reader.
      Parameters:
      r - The Reader to read the string from.
      maxChars - The maximum number of characters to read.
      Returns:
      A String of length maxChars, or less if the supplied Reader did not contain that much characters.
      Throws:
      IOException
    • readChars

      public static char[] readChars(URL url) throws IOException
      Read the contents of a (unbuffered) resource into a character array.
      Parameters:
      url - url to get the data from
      Returns:
      character array
      Throws:
      IOException
    • readChars

      public static char[] readChars(Reader r) throws IOException
      Reads all characters from the supplied reader and returns them.
      Parameters:
      r - The Reader supplying the characters
      Returns:
      A character array containing all characters from the supplied reader.
      Throws:
      IOException
    • readChars

      public static int readChars(Reader r, char[] charArray) throws IOException
      Fills the supplied character array with characters read from the specified Reader. This method will only stop reading when the character array has been filled completely, or the end of the stream has been reached.
      Parameters:
      r - The Reader to read the characters from.
      charArray - The character array to fill with characters.
      Returns:
      The number of characters written to the character array.
      Throws:
      IOException
    • readBytes

      public static byte[] readBytes(File file) throws IOException
      Reads all bytes from the specified file and returns them as a byte array.
      Parameters:
      file - The file to read.
      Returns:
      A byte array containing all bytes from the specified file.
      Throws:
      IOException - If an I/O error occurred while reading from the file.
      IllegalArgumentException - If the file size exceeds the maximum array length (larger than Integer.MAX_VALUE.
    • readBytes

      public static byte[] readBytes(InputStream in) throws IOException
      Reads all bytes from the supplied input stream and returns them as a byte array.
      Parameters:
      in - The InputStream supplying the bytes.
      Returns:
      A byte array containing all bytes from the supplied input stream.
      Throws:
      IOException
    • readBytes

      public static byte[] readBytes(InputStream in, int maxBytes) throws IOException
      Reads at most maxBytes bytes from the supplied input stream and returns them as a byte array.
      Parameters:
      in - The InputStream supplying the bytes.
      maxBytes - The maximum number of bytes to read from the input stream.
      Returns:
      A byte array of size maxBytes if the input stream can produce that amount of bytes, or a smaller byte array containing all available bytes from the stream otherwise.
      Throws:
      IOException
    • readBytes

      public static int readBytes(InputStream in, byte[] byteArray) throws IOException
      Fills the supplied byte array with bytes read from the specified InputStream. This method will only stop reading when the byte array has been filled completely, or the end of the stream has been reached.
      Parameters:
      in - The InputStream to read the bytes from.
      byteArray - The byte array to fill with bytes.
      Returns:
      The number of bytes written to the byte array.
      Throws:
      IOException
    • readProperties

      public static Properties readProperties(File propsFile) throws IOException
      Read properties from the specified file.
      Parameters:
      propsFile - the file to read from
      Returns:
      Properties loaded from the specified file
      Throws:
      IOException - when the file could not be read properly
    • readProperties

      public static Properties readProperties(File propsFile, Properties defaults) throws IOException
      Read properties from the specified file.
      Parameters:
      propsFile - the file to read from
      defaults - the default properties to use
      Returns:
      Properties loaded from the specified file
      Throws:
      IOException - when the file could not be read properly
    • readProperties

      public static Properties readProperties(InputStream in) throws IOException
      Read properties from the specified InputStream.
      Parameters:
      in - the stream to read from. The stream will be closed by this method.
      Returns:
      Properties loaded from the specified stream. The stream will be closed by this method.
      Throws:
      IOException - when the stream could not be read properly
    • readProperties

      public static Properties readProperties(InputStream in, Properties defaults) throws IOException
      Read properties from the specified InputStream.
      Parameters:
      in - the stream to read from. The stream will be closed by this method.
      defaults - the default properties
      Returns:
      Properties loaded from the specified stream. The stream will be closed by this method.
      Throws:
      IOException - when the stream could not be read properly
    • writeProperties

      public static void writeProperties(Properties props, File file, boolean includeDefaults) throws IOException
      Write the specified properties to the specified file.
      Parameters:
      props - the properties to write
      file - the file to write to
      includeDefaults - true when default values need to be included
      Throws:
      IOException - when the properties could not be written to the file properly
    • writeProperties

      public static void writeProperties(Properties props, OutputStream out, boolean includeDefaults) throws IOException
      Write the specified properties to the specified output stream.
      Parameters:
      props - the properties to write
      out - the output stream to write to
      includeDefaults - true if default values need to be included
      Throws:
      IOException - when the properties could not be written to the output stream properly
    • writeStream

      public static void writeStream(InputStream in, File file) throws IOException
      Writes all data that can be read from the supplied InputStream to the specified file.
      Parameters:
      in - An InputStream.
      file - The file to write the data to.
      Throws:
      IOException - If an I/O error occurred.
    • writeString

      public static void writeString(String contents, File file) throws IOException
      Write the contents of a string (unbuffered) to a file
      Parameters:
      contents - string contents to write
      file - file to write to
      Throws:
      IOException
    • writeBytes

      public static void writeBytes(byte[] data, File file) throws IOException
      Write the contents of a byte array (unbuffered) to a file.
      Parameters:
      data - data to write
      file - file
      Throws:
      IOException
    • writeBytes

      public static void writeBytes(byte[] data, OutputStream out) throws IOException
      Write he contents of a byte array (unbuffered) to an output stream.
      Parameters:
      data - data to write
      out - file
      Throws:
      IOException
    • urlToReader

      public static Reader urlToReader(URL url) throws IOException
      Read the contents of a resource into a reader. Currently ignores HTTP Content-Encoding response header.
      Parameters:
      url - url
      Returns:
      reader
      Throws:
      IOException
    • transfer

      public static final long transfer(InputStream in, OutputStream out) throws IOException
      Transfers all bytes that can be read from in to out.
      Parameters:
      in - The InputStream to read data from.
      out - The OutputStream to write data to.
      Returns:
      The total number of bytes transfered.
      Throws:
      IOException
    • transfer

      public static final long transfer(InputStream in, File file) throws IOException
      Writes all bytes from an InputStream to a file.
      Parameters:
      in - The InputStream containing the data to write to the file.
      file - The file to write the data to.
      Returns:
      The total number of bytes written.
      Throws:
      IOException - If an I/O error occurred while trying to write the data to the file.
    • transfer

      public static final long transfer(Reader in, Writer out) throws IOException
      Transfers all characters that can be read from in to out .
      Parameters:
      in - The Reader to read characters from.
      out - The Writer to write characters to.
      Returns:
      The total number of characters transfered.
      Throws:
      IOException
    • transfer

      public static final long transfer(Reader reader, File file) throws IOException
      Writes all characters from a Reader to a file using the default character encoding.
      Parameters:
      reader - The Reader containing the data to write to the file.
      file - The file to write the data to.
      Returns:
      The total number of characters written.
      Throws:
      IOException - If an I/O error occurred while trying to write the data to the file.
      See Also:
    • writeVarInt

      public static void writeVarInt(OutputStream out, int value) throws IOException

      Write an variable length (non-negative) integer.

      The variable length integer encoding (also know as varint and vint) writes non-negative / unsigned integers in a minimal number of bytes (binary octets). The encoding uses the most significant bit is used to indicate whether another varint byte follows. The remaining 7 bits of every octet are used to store the binary representation of the actual integer value. Note that the least significant bytes of the integer value are stored before more significate bytes.

      The table below shows a few examples of decimals encoded both as a 32-bit integer and as variable length binary:

       
       decimal |           32-bit integer            |          variable length binary
             0 | 00000000 00000000 00000000 00000000 |                   00000000   (1 byte)
             1 | 00000000 00000000 00000000 00000001 |                   00000001   (1 byte)
            42 | 00000000 00000000 00000000 00101010 |                   00101010   (1 byte)
           421 | 00000000 00000000 00000001 10100101 |          10100101 00000011   (2 bytes)
        100000 | 00000000 00000001 10000110 10100000 | 10100000 10001101 00000110   (3 bytes)
       
       

      Parameters:
      out - The OutputStream to write to.
      value - The int value to write.
      Throws:
      IOException - If an error occurred while writing the integer.
    • main

      public static void main(String[] args) throws IOException
      Throws:
      IOException
    • readVarInt

      public static int readVarInt(InputStream in) throws IOException
      Read an variable length integer. See writeVarInt(OutputStream, int) for encoding details.
      Parameters:
      in - The InputStream to read from.
      Returns:
      The integer read.
      Throws:
      IOException - If an error occurred while reading the integer.