format/src/main/java/com/github/sadikovi/riff/io/OutStream.java
/**
   * Spill content of uncompressed buffer into underlying output receiver (output stream).
   * Note that spill method can be called on empty buffer that does not require spill.
   * Otherwise force spill buffer even if it is not full.
   * @throws IOException
   */
Spill content of uncompressed buffer into underlying output receiver (output stream).
Note that spill method can be called on empty buffer that does not require spill.
Otherwise force spill buffer even if it is not full.
Spill content of uncompressed buffer into underlying output receiver ( output stream ) . Note that spill method can be called on empty buffer that does not require spill . Otherwise force spill buffer even if it is not full .
private void spill() throws IOException {
    if (uncompressed.position() == 0) {
      return;
    }
    if (codec == null) {
      uncompressed.flip();
      receiver.write(uncompressed.array(), uncompressed.arrayOffset() + uncompressed.position(),
        uncompressed.remaining());
      uncompressed.clear();
    } else {
      uncompressed.flip(); 
      uncompressed.position(HEADER_SIZE);
      int length = uncompressed.remaining(); 
      int prevPos = compressed.position();
      compressed.position(prevPos + HEADER_SIZE);
      if (codec.compress(uncompressed, compressed, overflow)) {
        uncompressed.clear();
        uncompressed.position(HEADER_SIZE);
        int totalBytes = compressed.position() - prevPos - HEADER_SIZE;
        totalBytes += overflow.position();
        writeHeader(compressed, prevPos, totalBytes, true);
         either way it looks safer to swap buffers and clear overflow
        if (compressed.remaining() < HEADER_SIZE) {
          compressed.flip();
          receiver.write(compressed.array(), compressed.arrayOffset() + compressed.position(),
            compressed.remaining());
           swap buffers (copy content) and clear/reset overflow
          overflow.flip();
          compressed.put(overflow);
          overflow.clear();
        }
      } else {
         compression is okay, but compressed data is larger than uncompressed raw data
         in this case we just store uncompressed data, marking header accordingly
         before doing that, check if there are some bytes left in compressed buffer, which belong
         to previous spill; clear compressed and overflow buffers
        if (prevPos != 0) {
          compressed.position(prevPos);
          compressed.flip();
          receiver.write(compressed.array(), compressed.arrayOffset() + compressed.position(),
            compressed.remaining());
        }
        compressed.clear();
        overflow.clear();
        uncompressed.position(0);  beginning of the buffer and header
         write uncompressed data into receiver stream, and update header
        writeHeader(uncompressed, 0, uncompressed.limit() - HEADER_SIZE, false);
        receiver.write(uncompressed.array(), uncompressed.arrayOffset() + uncompressed.position(),
          uncompressed.remaining());
        uncompressed.clear();
        uncompressed.position(HEADER_SIZE);
      }
    }
  }
private void spill ( ) throws IOException { if ( uncompressed . position ( ) == 0 ) { return ; } if ( codec == null ) { uncompressed . flip ( ) ; receiver . write ( uncompressed . array ( ) , uncompressed . arrayOffset ( ) + uncompressed . position ( ) , uncompressed . remaining ( ) ) ; uncompressed . clear ( ) ; } else { uncompressed . flip ( ) ; uncompressed . position ( HEADER_SIZE ) ; int length = uncompressed . remaining ( ) ; int prevPos = compressed . position ( ) ; compressed . position ( prevPos + HEADER_SIZE ) ; if ( codec . compress ( uncompressed , compressed , overflow ) ) { uncompressed . clear ( ) ; uncompressed . position ( HEADER_SIZE ) ; int totalBytes = compressed . position ( ) - prevPos - HEADER_SIZE ; totalBytes += overflow . position ( ) ; writeHeader ( compressed , prevPos , totalBytes , true ) ; if ( compressed . remaining ( ) < HEADER_SIZE ) { compressed . flip ( ) ; receiver . write ( compressed . array ( ) , compressed . arrayOffset ( ) + compressed . position ( ) , compressed . remaining ( ) ) ; overflow . flip ( ) ; compressed . put ( overflow ) ; overflow . clear ( ) ; } } else { if ( prevPos != 0 ) { compressed . position ( prevPos ) ; compressed . flip ( ) ; receiver . write ( compressed . array ( ) , compressed . arrayOffset ( ) + compressed . position ( ) , compressed . remaining ( ) ) ; } compressed . clear ( ) ; overflow . clear ( ) ; uncompressed . position ( 0 ) ; writeHeader ( uncompressed , 0 , uncompressed . limit ( ) - HEADER_SIZE , false ) ; receiver . write ( uncompressed . array ( ) , uncompressed . arrayOffset ( ) + uncompressed . position ( ) , uncompressed . remaining ( ) ) ; uncompressed . clear ( ) ; uncompressed . position ( HEADER_SIZE ) ; } } }
Spill content of uncompressed buffer into underlying output receiver (output stream).
Spill content of uncompressed buffer into underlying output receiver ( output stream ) .
// buffer is empty, just ignore // select mode to spill // mark position as 0 and limit as current position, so we can read from buffer // codec is not provided - we deal with uncompressed stream, just write raw bytes from // buffer into receiver stream, and reset buffer // we deal with compressed stream // prepare for reads - flip uncompressed buffer and set position to HEADER_SIZE // position will be 0 // length of bytes // prepare compressed buffer; in general compressed buffer will have data left from previous // spill - this already has header written, which is not going to be altered. This new batch // will have new header, so make room for it // compression is okay and smaller than uncompressed bytes // reset uncompressed // at this point we work with compressed stream only // compute total bytes written with this `compress` call, prevPos does not include header // append any leftover bytes in overflow buffer // write header for compressed buffer // compressed stream should always have enough bytes to write header, if not - spill to // receiver, otherwise just do not do anything. // There are several situations why event mentioned above can happen: // 1. compressed stream is full up to the last byte, overflow is empty // 2. compressed stream is almost full, but remaining is still less than HEADER_SIZE, // overflow is empty // 3. compressed stream is full, and there are some bytes in overflow // // either way it looks safer to swap buffers and clear overflow // swap buffers (copy content) and clear/reset overflow // compression is okay, but compressed data is larger than uncompressed raw data // in this case we just store uncompressed data, marking header accordingly // before doing that, check if there are some bytes left in compressed buffer, which belong // to previous spill; clear compressed and overflow buffers // beginning of the buffer and header // write uncompressed data into receiver stream, and update header // reset uncompressed