code: private int readFromStream(final long length) throws IOException {
          if (getReadableBytes() + length > localCapacity ) {
              throw new IllegalArgumentException("Trying to read too far ahead, will cause wrap-around issues: " + length);
          }
          int total = 0;
          int actual = 0;
          while (total < length && actual != -1) {
  
              final int localIndex = getLocalWriterIndex();
              final int spaceLeft = getAvailableLocalWritingSpace();
              final int readAtMost = (int)Math.min(length - total, spaceLeft);
  
              actual = this.is.read(this.buffer, localIndex, readAtMost);
  
              if (actual > 0) {
                  this.writerIndex += actual;
                  total += actual;
              }
          }
          return total;
      }
docs: Method for reading bytes off the stream and store it in the local
  "storage"
  
  @param length
  the length we wish to read
  @return the actual amount of bytes we read
  @throws IOException
  in case anything goes wrong while reading