Class MessageBufferOutputStream

java.lang.Object
java.io.OutputStream
com.mimecast.robin.storage.MessageBufferOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class MessageBufferOutputStream extends OutputStream
Buffers message receipt in memory and spills to disk after a configured threshold.
  • Field Details

    • thresholdBytes

      private final long thresholdBytes
    • spillFile

      private final Path spillFile
    • memoryStream

      private ByteArrayOutputStream memoryStream
    • activeStream

      private OutputStream activeStream
    • size

      private long size
    • closed

      private boolean closed
  • Constructor Details

    • MessageBufferOutputStream

      public MessageBufferOutputStream(long thresholdBytes, Path spillFile)
      Constructs a new buffered output stream.
      Parameters:
      thresholdBytes - Spill threshold.
      spillFile - Spill target file.
  • Method Details

    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • size

      public long size()
      Gets the buffered size.
      Returns:
      Size in bytes.
    • toMessageSource

      public MessageSource toMessageSource()
      Converts the current buffer into a canonical message source.

      For file-backed messages, returns a RefCountedFileMessageSource to support safe concurrent access with automatic cleanup.

      Returns:
      MessageSource instance.
    • isSpilledToFile

      public boolean isSpilledToFile()
      Checks if the stream spilled to a file.
      Returns:
      Boolean.
    • forceSpillToFile

      public void forceSpillToFile() throws IOException
      Forces the buffer to spill to file regardless of threshold.

      Useful for bot processing where file-based access is required for thread-safe concurrent access to the message content.

      Throws:
      IOException - If an I/O error occurs.
    • spillIfNeeded

      private void spillIfNeeded(int nextWriteBytes) throws IOException
      Throws:
      IOException
    • ensureOpen

      private void ensureOpen() throws IOException
      Throws:
      IOException