Package com.trafficparrot.sdk.file
Class FileMessage
java.lang.Object
com.trafficparrot.sdk.file.FileMessage
A file as a message: one that a file message virtual service picked up from a request directory, which is what a
FileMessageResponseTransformer receives, or one it is about to write to a response directory.
Read a request's content from text when it is text, from bytes() when it may be binary, and from
contentFile() when it may be too large for memory:
Optional<Path> large = requestMessage.contentFile();
if (large.isPresent()) {
try (InputStream content = Files.newInputStream(large.get())) {
...
}
} else {
byte[] content = requestMessage.bytes();
...
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal StringThe file's name, without its directory.final StringThe file's content as text, decoded as UTF-8.final longA time in milliseconds since the epoch: for a file picked up from a local directory, when it was last modified; for one fetched from a remote server, when Traffic Parrot received it; for a response, when it was created. -
Constructor Summary
ConstructorsConstructorDescriptionFileMessage(String fileName, String text, long timestamp) Creates a message with text content, written as UTF-8. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]bytes()The content to write: the exact bytes this message was built from withofBytes(java.lang.String, byte[], long), otherwise the UTF-8 encoding oftext, or no bytes whentextis null.The file holding this message's content, for a message built withofFile(java.lang.String, java.nio.file.Path, long); empty for every other message.static FileMessageA message whose content is written back exactly as given, binary included.static FileMessageA message whose content stays incontent, for a file larger thantrafficparrot.file.body.inMemory.max.size.mb: reading it into memory could exhaust the heap.toString()A binary message is described by its size: its text is a lossy decoding that can run to megabytes.
-
Field Details
-
fileName
The file's name, without its directory. -
text
The file's content as text, decoded as UTF-8. For a binary file it is a lossy decoding, so readbytes(); for a file too large for memory it is null, so readcontentFile(). -
timestamp
public final long timestampA time in milliseconds since the epoch: for a file picked up from a local directory, when it was last modified; for one fetched from a remote server, when Traffic Parrot received it; for a response, when it was created.
-
-
Constructor Details
-
FileMessage
Creates a message with text content, written as UTF-8.- Parameters:
fileName- the file's name, without its directorytext- the content; null for an empty filetimestamp- a time in milliseconds since the epoch, astimestampdescribes
-
-
Method Details
-
ofBytes
A message whose content is written back exactly as given, binary included. A static factory rather than a constructor overload, so an existingnew FileMessage(name, null, timestamp)still compiles. -
ofFile
A message whose content stays incontent, for a file larger thantrafficparrot.file.body.inMemory.max.size.mb: reading it into memory could exhaust the heap. Itstextis null, andcontentFile()gives the file.- Parameters:
fileName- the file's name, without its directorycontent- the file that holds the contenttimestamp- a time in milliseconds since the epoch, astimestampdescribes- Returns:
- the message
- Throws:
UncheckedIOException- when the file's size cannot be read
-
contentFile
The file holding this message's content, for a message built withofFile(java.lang.String, java.nio.file.Path, long); empty for every other message. It is valid only while the message is being handled: Traffic Parrot deletes or replaces it afterwards. Read it, do not modify it, and close any stream opened on it before returning, since an open file cannot be deleted on Windows.- Returns:
- the file that holds the content, or empty when the content is in memory
-
bytes
public byte[] bytes()The content to write: the exact bytes this message was built from withofBytes(java.lang.String, byte[], long), otherwise the UTF-8 encoding oftext, or no bytes whentextis null. Always a fresh array, so the caller may modify it. For a message built withofFile(java.lang.String, java.nio.file.Path, long)this reads the whole file into memory, which is what that message exists to avoid: prefer readingcontentFile()as a stream.- Returns:
- the content as bytes, never null
- Throws:
UncheckedIOException- when the content file cannot be read
-
toString
A binary message is described by its size: its text is a lossy decoding that can run to megabytes. A long text is given by its start and length, since every file received and every reply sent is logged at INFO.
-