Class FileMessage

java.lang.Object
com.trafficparrot.sdk.file.FileMessage

public class FileMessage extends Object
  • Field Details

    • fileName

      public final String fileName
    • text

      public final String text
    • timestamp

      public final long timestamp
  • Constructor Details

    • FileMessage

      public FileMessage(String fileName, String text, long timestamp)
  • Method Details

    • ofBytes

      public static FileMessage ofBytes(String fileName, byte[] bytes, long timestamp)
      A message whose content is written back exactly as given, binary included. A static factory rather than a constructor overload, so an existing new FileMessage(name, null, timestamp) still compiles.
    • ofFile

      public static FileMessage ofFile(String fileName, Path content, long timestamp)
      A message whose content stays in content, for a file larger than trafficparrot.file.body.inMemory.max.size.mb: reading it into memory could exhaust the heap. Its text is null, and contentFile() gives the file.
      Throws:
      UncheckedIOException - when the file's size cannot be read
    • contentFile

      public Optional<Path> contentFile()
      The file holding this message's content, for a message built with ofFile(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.
    • bytes

      public byte[] bytes()
      The content to write: the exact bytes this message was built from with ofBytes(java.lang.String, byte[], long), otherwise the UTF-8 encoding of text, or no bytes when text is null. Always a fresh array, so the caller may modify it. For a message built with ofFile(java.lang.String, java.nio.file.Path, long) this reads the whole file into memory, which is what that message exists to avoid: prefer reading contentFile() as a stream.
      Throws:
      UncheckedIOException - when the content file cannot be read
    • toString

      public String 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.
      Overrides:
      toString in class Object