code: private int readFromStream(final int length) throws IOException {
          int total = 0;
          int actual = 0;
          while (total < length && actual != -1) {
  
              final int localIndex = getLocalWriterIndex();
              final int spaceLeft = getAvailableLocalWritingSpace();
              final int readAtMost = Math.min(length - total, spaceLeft);
  
              final java.nio.ByteBuffer bb = getWritingRow();
  
              actual = this.is.read(bb.array(), localIndex, readAtMost);
  
              if (actual > 0) {
                  this.upperBoundary += actual;
                  this.writerIndex = this.upperBoundary;
                  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