de.caff.io

## Class LimitedByteArrayOutputStream

- java.lang.Object
- - java.io.OutputStream
  - - de.caff.io.LimitedByteArrayOutputStream

- All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

      public class LimitedByteArrayOutputStream
      extends java.io.OutputStream

  A byte array output stream with a limited size. It will discard earlier bytes of more than the given number of bytes is written to it.
  Since:
    August 10, 2022
  Author:
    Rammi

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `LimitedByteArrayOutputStream(int bufferSize)` Create a buffering output stream with the given size limit.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `void` | `flush()`
    `long` | `getTotalNumberOfBytes()` Get the total number of bytes written to this stream.
    `void` | `reset()` Clear all cached bytes.
    `byte[]` | `toByteArray()` Get the cached bytes.
    `java.lang.String` | `toString()` Get a string representation.
    `java.lang.String` | `toString(java.nio.charset.Charset charset)` Get the content of this buffer as a string.
    `void` | `write(byte[] b)`
    `void` | `write(byte[] b, int off, int len)`
    `void` | `write(int b)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### LimitedByteArrayOutputStream

          public LimitedByteArrayOutputStream(int bufferSize)

      Create a buffering output stream with the given size limit.
      Parameters:
        `bufferSize` - maximum number of buffered bytes

  - ### Method Detail

    - #### write

          public void write(int b)
                     throws java.io.IOException

      Specified by:
        `write` in class `java.io.OutputStream`
      Throws:
        `java.io.IOException`

    - #### write

          public void write(@NotNull
                            byte[] b)
                     throws java.io.IOException

      Overrides:
        `write` in class `java.io.OutputStream`
      Throws:
        `java.io.IOException`

    - #### write

          public void write(@NotNull
                            byte[] b,
                            int off,
                            int len)
                     throws java.io.IOException

      Overrides:
        `write` in class `java.io.OutputStream`
      Throws:
        `java.io.IOException`

    - #### flush

          public void flush()
                     throws java.io.IOException

      Specified by:
        `flush` in interface `java.io.Flushable`
      Overrides:
        `flush` in class `java.io.OutputStream`
      Throws:
        `java.io.IOException`

    - #### close

          public void close()
                     throws java.io.IOException

      Specified by:
        `close` in interface `java.io.Closeable`
      Specified by:
        `close` in interface `java.lang.AutoCloseable`
      Overrides:
        `close` in class `java.io.OutputStream`
      Throws:
        `java.io.IOException`

    - #### toByteArray

          @NotNull
          public byte[] toByteArray()

      Get the cached bytes.
      Returns:
        bytes cached in this limited byte array output stream

    - #### reset

          public void reset()

      Clear all cached bytes. This will not clear the total `total number of bytes`.

    - #### getTotalNumberOfBytes

          public long getTotalNumberOfBytes()

      Get the total number of bytes written to this stream.
      Returns:
        total number of bytes written so far

    - #### toString

          @NotNull
          public java.lang.String toString(@NotNull
                                                    java.nio.charset.Charset charset)

      Get the content of this buffer as a string. Please note that for multi-byte character encodings the internal buffer might start at
      Parameters:
        `charset` - charset used to decode the cached bytes
      Returns:
        string representation of the buffer content

    - #### toString

          @NotNull
          public java.lang.String toString()

      Get a string representation. This follows the example of `ByteArrayOutputStream` which uses `toString(Charset)` which is not always a good idea.
      Overrides:
        `toString` in class `java.lang.Object`
      Returns:
        string representation created from the bytes using the default charset