Package ai.docling.serve.api.util
Class FileUtils
java.lang.Object
ai.docling.serve.api.util.FileUtils
Utility class that provides file-related operations such as reading files
and encoding file contents to Base64.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringconvertToBase64(byte[] bytes) Converts the given byte array to a Base64-encoded string.static StringconvertToBase64(Path file) Converts the contents of the specified file to a Base64-encoded string.static Stream<FileSource>createFileSources(Path... files) Creates aStreamofFileSourceinstances based on the provided file paths.static byte[]Reads the contents of the specified file and returns them as a byte array.
-
Method Details
-
getBytes
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 fileIllegalArgumentException- if the file does not exist, is not a regular file, or is not readable
-
convertToBase64
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 fileIllegalArgumentException- if the file does not exist, is not a regular file, or is not readable
-
convertToBase64
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
Creates aStreamofFileSourceinstances based on the provided file paths. EachFileSourcewill contain the filename and the Base64-encoded content of the corresponding file.- Parameters:
files- an array ofPathobjects representing the file paths to be processed; must not be null or empty, and each element must not be null- Returns:
- a
StreamofFileSourceinstances, one for each valid file path - Throws:
RuntimeException- if an I/O error occurs while reading any of the filesIllegalArgumentException- if the input array is null or empty
-