All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
ByteLimitOutputStream, DechunkedOutputStream

public abstract class LimitOutputStreamextends FilterOutputStream
Abstract output stream that limits writes and provides completion callback.

This class extends FilterOutputStream to provide a framework for implementing output streams with write limitations or size restrictions. It tracks completion state and calls a callback when the stream is finished, preventing multiple completion attempts.

Subclasses implement specific limiting behavior while inheriting callback functionality and completion state management. Useful for implementing streams that need to enforce size limits or provide notification when writing is complete.

  • Field Details

    • _buf1

      private final byte[] _buf1
      Single-byte buffer for the write(int) method.
    • _callback

      protected final LimitOutputStream.DoneCallback _callback
      Invoked once by setDone() when the subclass has finished writing.
    • _isDone

      protected boolean _isDone
      True once setDone() has run, so the callback cannot fire twice.
  • Constructor Details

    • LimitOutputStream

      public LimitOutputStream(OutputStream out, LimitOutputStream.DoneCallback done)
      Creates a limiting stream wrapping the given target stream.
      Parameters:
      out - the stream writes are forwarded to
      done - non-null
  • Method Details

    • write

      public void write(int c) throws IOException
      Buffers the single byte and writes it through the bulk method.
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • write

      public void write(byte[] buf, int off, int len) throws IOException
      Subclasses MUST override the following method such that it calls done() when finished and throws EOFException if called again.
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • isDone

      protected boolean isDone()
      Whether setDone() has already run, so the done callback cannot fire twice.
      Returns:
      true if setDone() has already run
    • setDone

      protected void setDone() throws IOException
      flush(), call the callback, and set _isDone.
      Throws:
      IOException - if the wrapped stream cannot be flushed