Class TruncatedOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.elasticsearch.common.io.stream.TruncatedOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class TruncatedOutputStream extends FilterOutputStream
Truncates writes once the max size is exceeded. However, when writing byte arrays, the stream does not check whether there is capacity for the full array prior to writing, so there is overspill of up to b.length - 1.
  • Constructor Details

    • TruncatedOutputStream

      public TruncatedOutputStream(OutputStream out, IntSupplier currentSizeSupplier, int maxSize)
  • Method Details

    • hasCapacity

      public boolean hasCapacity()
      Returns:
      True if there is at least one byte of space left to write
    • write

      public void write(int b) throws IOException
      If there is at least one byte of space left in the stream then write the byte
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - The byte to write to the underlying stream
      Throws:
      IOException - – if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • write

      public void write(byte[] b) throws IOException
      If there is at least one byte of space left in the stream then writes the byte stream. Therefore, up to b.length - 1 bytes will overflow.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - The bytes to write to the underlying stream
      Throws:
      IOException - – if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • write

      public void write(byte[] b, int off, int len) throws IOException
      If there is at least one byte of space left in the stream then writes the byte stream. Therefore, up to len - 1 bytes will overflow.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - The byte array to write from
      off - The index of the first byte to write.
      len - The number of bytes to write
      Throws:
      IOException - – if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.