Class FileUtils

java.lang.Object
ai.docling.serve.api.util.FileUtils

public final class FileUtils extends Object
Utility class that provides file-related operations such as reading files and encoding file contents to Base64.
  • Method Details

    • getBytes

      public static byte[] getBytes(Path file) throws IOException
      Reads the contents of the specified file and returns them as a byte array.
      Parameters:
      file - the path to the file to be read; must not be null, must exist, must be a regular file, and must be readable
      Returns:
      a byte array containing the contents of the file
      Throws:
      IOException - if an I/O error occurs while reading the file
      IllegalArgumentException - if the file does not exist, is not a regular file, or is not readable
    • convertToBase64

      public static String convertToBase64(Path file) throws IOException
      Converts the contents of the specified file to a Base64-encoded string.
      Parameters:
      file - the path to the file to be encoded; must not be null, must exist, must be a regular file, and must be readable
      Returns:
      a Base64-encoded string representing the contents of the file
      Throws:
      IOException - if an I/O error occurs while reading the file
      IllegalArgumentException - if the file does not exist, is not a regular file, or is not readable
    • convertToBase64

      public static String convertToBase64(byte[] bytes)
      Converts the given byte array to a Base64-encoded string.
      Parameters:
      bytes - the byte array to be encoded; must not be null
      Returns:
      a Base64-encoded string representing the input byte array; if the input is an empty array, an empty string is returned
      Throws:
      IllegalArgumentException - if the input byte array is null
    • createFileSources

      public static Stream<FileSource> createFileSources(Path... files)
      Creates a Stream of FileSource instances based on the provided file paths. Each FileSource will contain the filename and the Base64-encoded content of the corresponding file.
      Parameters:
      files - an array of Path objects representing the file paths to be processed; must not be null or empty, and each element must not be null
      Returns:
      a Stream of FileSource instances, one for each valid file path
      Throws:
      RuntimeException - if an I/O error occurs while reading any of the files
      IllegalArgumentException - if the input array is null or empty