Class Decoder

java.lang.Object
com.aayushatharva.brotli4j.decoder.Decoder
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
BrotliDecoderChannel

public class Decoder extends Object implements AutoCloseable
Base class for InputStream / Channel implementations.
  • Field Details

  • Constructor Details

    • Decoder

      public Decoder(ReadableByteChannel source, int inputBufferSize) throws IOException
      Creates a Decoder wrapper.
      Parameters:
      source - underlying source
      inputBufferSize - read buffer size
      Throws:
      IOException - If any failure during initialization
    • Decoder

      public Decoder(ReadableByteChannel source, int inputBufferSize, int maxOutputChunkSize) throws IOException
      Creates a Decoder wrapper with a per-pull output cap.
      Parameters:
      source - underlying source
      inputBufferSize - read buffer size
      maxOutputChunkSize - per-pull output cap in bytes; 0 for no cap
      Throws:
      IOException - If any failure during initialization
  • Method Details

    • decompress

      public static DirectDecompress decompress(byte[] data) throws IOException
      Decodes the given data buffer.
      Parameters:
      data - byte array of data to be decoded
      Returns:
      DirectDecompress instance
      Throws:
      IOException - If an error occurs during decoding
    • decompress

      public static DirectDecompress decompress(byte[] data, int maxOutputSize) throws IOException
      Decodes the given data buffer, failing if decompressed output would exceed maxOutputSize bytes. Use to mitigate decompression bombs.
      Parameters:
      data - byte array of data to be decoded
      maxOutputSize - cap on total decompressed bytes; 0 for no cap
      Returns:
      DirectDecompress instance
      Throws:
      IOException - If an error occurs during decoding, or output exceeds maxOutputSize
    • fail

      private void fail(String message) throws IOException
      Throws:
      IOException
    • attachDictionary

      void attachDictionary(ByteBuffer dictionary) throws IOException
      Throws:
      IOException
    • enableEagerOutput

      public void enableEagerOutput()
    • decode

      int decode() throws IOException
      Continue decoding.
      Returns:
      -1 if stream is finished, or number of bytes available in read buffer (> 0)
      Throws:
      IOException
    • discard

      void discard(int length)
    • consume

      int consume(ByteBuffer dst)
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • decompress

      public static byte[] decompress(byte[] data, int offset, int length) throws IOException
      Decodes the given data buffer starting at offset till length.
      Throws:
      IOException
    • decompress

      public static byte[] decompress(byte[] data, int offset, int length, int maxOutputSize) throws IOException
      Decodes the given data buffer starting at offset till length, failing if decompressed output would exceed maxOutputSize bytes. Use to mitigate decompression bombs.
      Parameters:
      data - source byte array
      offset - offset within data
      length - compressed length
      maxOutputSize - cap on total decompressed bytes; 0 for no cap
      Returns:
      decompressed bytes
      Throws:
      IOException - if input is corrupted, or output exceeds maxOutputSize