Ejemplo n.º 1
0
  /**
   * Does the same thing as <code>readFully</code> do but without copying data (thread safe)
   *
   * @param length length of the bytes to read
   * @return buffer with portion of file content
   * @throws IOException on any fail of I/O operation
   */
  public synchronized ByteBuffer readBytes(int length) throws IOException {
    int remaining = buffer.remaining() - position;
    if (length > remaining)
      throw new IOException(
          String.format(
              "mmap segment underflow; remaining is %d but %d requested", remaining, length));

    if (length == 0) return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    ByteBuffer bytes = buffer.duplicate();
    bytes.position(buffer.position() + position).limit(buffer.position() + position + length);
    position += length;

    // we have to copy the data in case we unreference the underlying sstable.  See CASSANDRA-3179
    ByteBuffer clone = ByteBuffer.allocate(bytes.remaining());
    clone.put(bytes);
    clone.flip();
    return clone;
  }