format/src/main/java/com/github/sadikovi/riff/io/InStream.java
/**
   * Depending on compression codec, read directly into uncompressed buffer or use compressed buffer
   * to either decompress bytes or reset to uncompressed depending on chunk header, since we do not
   * know if chunk is compressed or not; also ensure header consistency.
   *
   * This method assumes that uncompressed buffer is empty and needs buffering.
   */
Depending on compression codec, read directly into uncompressed buffer or use compressed buffer
to either decompress bytes or reset to uncompressed depending on chunk header, since we do not
know if chunk is compressed or not; also ensure header consistency.

This method assumes that uncompressed buffer is empty and needs buffering.
Depending on compression codec read directly into uncompressed buffer or use compressed buffer to either decompress bytes or reset to uncompressed depending on chunk header since we do not know if chunk is compressed or not ; also ensure header consistency . This method assumes that uncompressed buffer is empty and needs buffering .
private void readChunk() throws IOException {
    uncompressed.clear();
    if (codec == null) {
      source.copy(uncompressed);
    } else {
      assert OutStream.HEADER_SIZE == 4: "Inconsistent header";
      header.clear();
      source.copy(header);
      if (header.remaining() < OutStream.HEADER_SIZE) {
        throw new IOException("EOF, expected at least " + OutStream.HEADER_SIZE + " bytes");
      }
      int info = header.getInt();
      boolean isCompressed = (info & (1 << 31)) != 0;
      int chunkLength = info & ~(1 << 31);
      ByteBuffer compressed = ByteBuffer.allocate(chunkLength);
      source.copy(compressed);
      if (isCompressed) {
        if (uncompressed.limit() < bufferSize) {
          uncompressed = ByteBuffer.allocate(bufferSize);
        }
        codec.decompress(compressed, uncompressed);
      } else {
        uncompressed = compressed;
      }
    }
  }
private void readChunk ( ) throws IOException { uncompressed . clear ( ) ; if ( codec == null ) { source . copy ( uncompressed ) ; } else { assert OutStream . HEADER_SIZE == 4 : "Inconsistent header" ; header . clear ( ) ; source . copy ( header ) ; if ( header . remaining ( ) < OutStream . HEADER_SIZE ) { throw new IOException ( "EOF, expected at least " + OutStream . HEADER_SIZE + " bytes" ) ; } int info = header . getInt ( ) ; boolean isCompressed = ( info & ( 1 << 31 ) ) != 0 ; int chunkLength = info & ~ ( 1 << 31 ) ; ByteBuffer compressed = ByteBuffer . allocate ( chunkLength ) ; source . copy ( compressed ) ; if ( isCompressed ) { if ( uncompressed . limit ( ) < bufferSize ) { uncompressed = ByteBuffer . allocate ( bufferSize ) ; } codec . decompress ( compressed , uncompressed ) ; } else { uncompressed = compressed ; } } }
Depending on compression codec, read directly into uncompressed buffer or use compressed buffer
 to either decompress bytes or reset to uncompressed depending on chunk header, since we do not
 know if chunk is compressed or not; also ensure header consistency.
Depending on compression codec read directly into uncompressed buffer or use compressed buffer to either decompress bytes or reset to uncompressed depending on chunk header since we do not know if chunk is compressed or not ; also ensure header consistency .
// for header size of 4 bytes; use predefined header buffer and reset it for each chunk // read header // copy raw bytes (either compressed or uncompressed) into compressed buffer, we should be able // to just swap it in uncompressed case // fewer bytes can be read from stream // reset uncompressed buffer and decompress bytes, if uncompressed buffer has smaller than // bufferSize - reset it to bufferSize, otherwise just clear position and limit // TODO: resize uncompressed buffer in case chunkLength results in more than bufferSize bytes // right now it will throw exception, if this situation happens // at this point uncompressed buffer is set for reading, no need to flip // just swap byte buffers