Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static ReaderSupplier createSupplier(FileHandle fileHandle)
@Override
public void readFully(float[] dest) throws IOException
{
BufferHolder bh = bufferHolder;
BufferHolder bh = getBufferHolder();
long position = getPosition();

FloatBuffer floatBuffer;
Expand Down Expand Up @@ -94,7 +94,7 @@ public void read(int[] dest, int offset, int count) throws IOException
if (count == 0)
return;

BufferHolder bh = bufferHolder;
BufferHolder bh = getBufferHolder();
long position = getPosition();

IntBuffer intBuffer;
Expand Down
18 changes: 14 additions & 4 deletions src/java/org/apache/cassandra/io/util/RandomAccessReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public class RandomAccessReader extends RebufferingInputStream implements FileDa
private long markedPointer;

final Rebufferer rebufferer;
protected BufferHolder bufferHolder = Rebufferer.EMPTY;
private BufferHolder bufferHolder = Rebufferer.EMPTY;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made it private to avoid modifying the value in subclasses without updating the paired bufferHolderOffset value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document this in the code explicitly, not just by commenting here


private long bufferHolderOffset = 0;

/**
* Only created through Builder
Expand Down Expand Up @@ -67,7 +69,9 @@ private void reBufferAt(long position)
bufferHolder.release();
bufferHolder = rebufferer.rebuffer(position);
buffer = bufferHolder.buffer();
buffer.position(Ints.checkedCast(position - bufferHolder.offset()));
long newOffset = bufferHolder.offset();
bufferHolderOffset = newOffset;
buffer.position(Ints.checkedCast(position - newOffset));

assert buffer.order() == ByteOrder.BIG_ENDIAN : "Buffer must have BIG ENDIAN byte ordering";
}
Expand All @@ -80,9 +84,14 @@ public long getFilePointer()
return current();
}

protected BufferHolder getBufferHolder()
{
return bufferHolder;
}

protected long current()
{
return bufferHolder.offset() + buffer.position();
return bufferHolderOffset + buffer.position();
}

public String getPath()
Expand Down Expand Up @@ -164,6 +173,7 @@ public void close()
rebufferer.closeReader();
buffer = null;
bufferHolder = null;
bufferHolderOffset = 0;

//For performance reasons we don't keep a reference to the file
//channel so we don't close it
Expand Down Expand Up @@ -197,7 +207,7 @@ public void seek(long newPosition)
if (buffer == null)
throw new IllegalStateException("Attempted to seek in a closed RAR");

long bufferOffset = bufferHolder.offset();
long bufferOffset = bufferHolderOffset;
if (newPosition >= bufferOffset && newPosition < bufferOffset + buffer.limit())
{
buffer.position((int) (newPosition - bufferOffset));
Expand Down