Class FileWithBytes
- All Implemented Interfaces:
FileContent
FileWithBytes is used when file content needs to be transmitted inline with the message or artifact, rather than requiring a separate download. This is appropriate for:
- Small files that fit comfortably in a JSON payload
- Generated content that doesn't exist as a standalone file
- Content that must be preserved exactly as created
- Scenarios where URI accessibility is uncertain
The bytes field contains the base64-encoded file content. Decoders should handle the base64 encoding/decoding transparently.
This class uses lazy loading with soft-reference caching to reduce memory pressure: the
base64-encoded content is computed on-demand and held via a SoftReference, allowing
the JVM to reclaim it under memory pressure. If reclaimed, it is recomputed on next access.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionFileWithBytes(String mimeType, File file) Creates aFileWithBytesby reading the content of the givenFile.FileWithBytes(String mimeType, String name, byte[] content) Creates aFileWithBytesby base64-encoding the given raw byte array.FileWithBytes(String mimeType, String name, String bytes) Creates aFileWithByteswith pre-encoded base64 content.FileWithBytes(String mimeType, Path file) Creates aFileWithBytesby reading the content of the givenPath. -
Method Summary
Modifier and TypeMethodDescriptionbytes()Returns the base64-encoded file content.booleanCompares this FileWithBytes to another object for equality.inthashCode()Returns the identity hash code for this FileWithBytes.mimeType()Returns the MIME type of the file content.name()Returns the file name.toString()
-
Constructor Details
-
FileWithBytes
Creates aFileWithByteswith pre-encoded base64 content. This is the canonical constructor used by serialization frameworks.- Parameters:
mimeType- the MIME type of the file (e.g., "image/png", "application/pdf")name- the file name (e.g., "report.pdf", "diagram.png")bytes- the base64-encoded file content
-
FileWithBytes
Creates aFileWithBytesby reading the content of the givenFile. The file name is derived fromFile.getName().The file is validated at construction time to ensure it exists, is readable, is a regular file, and does not exceed the maximum size limit (10485760L bytes).
The file content is read and base64-encoded on the first call to
bytes(), then cached via a soft reference. The cache may be cleared by GC under memory pressure, in which case the file is re-read on the next access.- Parameters:
mimeType- the MIME type of the file (e.g.,"image/png")file- the file whose content will be read and encoded- Throws:
IllegalArgumentException- if the file does not exist, is not readable, is not a regular file, or exceeds the maximum size limitRuntimeException- if an I/O error occurs while checking the file
-
FileWithBytes
Creates aFileWithBytesby reading the content of the givenPath. The file name is derived fromPath.getFileName().The file is validated at construction time to ensure it exists, is readable, is a regular file, and does not exceed the maximum size limit (10485760L bytes).
The file content is read and base64-encoded on the first call to
bytes(), then cached via a soft reference. The cache may be cleared by GC under memory pressure, in which case the file is re-read on the next access.- Parameters:
mimeType- the MIME type of the file (e.g.,"image/png")file- the path whose content will be read and encoded- Throws:
IllegalArgumentException- if the file does not exist, is not readable, is not a regular file, or exceeds the maximum size limitRuntimeException- if an I/O error occurs while checking the file
-
FileWithBytes
Creates aFileWithBytesby base64-encoding the given raw byte array.A defensive copy of
contentis made at construction time, so subsequent mutations to the caller's array have no effect. The copy is base64-encoded on the first call tobytes(), then cached via a soft reference. The cache may be cleared by GC under memory pressure, in which case the encoding is recomputed from the retained copy.- Parameters:
mimeType- the MIME type of the file (e.g.,"application/pdf")name- the file name (e.g.,"report.pdf")content- the raw file content to be base64-encoded- Throws:
NullPointerException- ifcontentis null
-
-
Method Details
-
mimeType
Description copied from interface:FileContentReturns the MIME type of the file content.- Specified by:
mimeTypein interfaceFileContent- Returns:
- the MIME type (e.g., "image/png", "text/plain", "application/json")
-
name
Description copied from interface:FileContentReturns the file name.- Specified by:
namein interfaceFileContent- Returns:
- the file name (e.g., "document.pdf", "image.jpg")
-
bytes
Returns the base64-encoded file content.The content is computed on the first call and cached via a soft reference. Subsequent calls return the cached value. If the JVM reclaims the cache under memory pressure, the content is recomputed transparently on the next access.
For instances created from a
FileorPath, recomputation involves reading the file from disk. Callers in performance-sensitive paths should retain the returned value rather than calling this method repeatedly.- Returns:
- the base64-encoded file content
- Throws:
RuntimeException- if an I/O error occurs while reading a file-backed source
-
equals
Compares this FileWithBytes to another object for equality.Important: This method uses identity-based comparison to avoid triggering potentially expensive I/O operations. Two FileWithBytes instances are considered equal only if they are the same object (reference equality).
This design choice prevents:
- Unexpected file I/O during collection operations (HashMap, HashSet, etc.)
- Performance issues when comparing file-backed instances
- RuntimeExceptions from I/O errors during equality checks
If you need to compare the actual content of two FileWithBytes instances, use a separate method or compare the results of
bytes()explicitly. -
hashCode
public int hashCode()Returns the identity hash code for this FileWithBytes.This method uses
System.identityHashCode(Object)to avoid triggering I/O operations that would be required to compute a content-based hash code. This ensures that using FileWithBytes instances as keys in HashMap or elements in HashSet remains safe and efficient. -
toString
-