docs: public static void runReadLoop(InputStream stream, StreamConnection connection) throws Exception {
          ByteBuffer dbuf = ByteBuffer.allocateDirect(Math.min(Math.max(connection.getMaxMessageSize(), BUFFER_SIZE_LOWER_BOUND), BUFFER_SIZE_UPPER_BOUND));
          byte[] readBuff = new byte[dbuf.capacity()];
          while (true) {
              // TODO Kill the message duplication here
              checkState(dbuf.remaining() > 0 && dbuf.remaining() <= readBuff.length);
              int read = stream.read(readBuff, 0, Math.max(1, Math.min(dbuf.remaining(), stream.available())));
              if (read == -1)
                  return;
              dbuf.put(readBuff, 0, read);
              // "flip" the buffer - setting the limit to the current position and setting position to 0
              dbuf.flip();
              // Use connection.receiveBytes's return value as a double-check that it stopped reading at the right
              // location
              int bytesConsumed = connection.receiveBytes(dbuf);
              checkState(dbuf.position() == bytesConsumed);
              // Now drop the bytes which were read by compacting dbuf (resetting limit and keeping relative
              // position)
              dbuf.compact();
          }
      }