content: package io.github.headlesshq.headlessmc.lwjgl.util;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.nio.ByteBuffer;
  
  /**
   * {@link InputStream} wrapping a {@link ByteBuffer}. Note that every read will
   * advance the buffer.
   */
  // TODO: we should/could also read without advancing the buffer
  public class ByteBufferInputStream extends InputStream {
      private final ByteBuffer buffer;
  
      public ByteBufferInputStream(ByteBuffer buffer) {
          this.buffer = buffer;
      }
  
      @Override
      public int read() throws IOException {
          if (!buffer.hasRemaining()) {
              return -1;
          }
  
          return buffer.get() & 0xFF;
      }
  
      @Override
      @SuppressWarnings("NullableProblems")
      public int read(byte[] bytes, int off, int len) throws IOException {
          if (!buffer.hasRemaining()) {
              return -1;
          }
  
          len = Math.min(len, buffer.remaining());
          buffer.get(bytes, off, len);
          return len;
      }
  
      @Override
      public int available() {
          return buffer.remaining();
      }
  
  }
  