HADOOP-18530. ChecksumFileSystem::readVectored might return byte buffers not positioned at 0 (#5168)
Contributed by Harshit Gupta
This commit is contained in:
parent
3f256fa209
commit
77dfd7ed24
@ -307,9 +307,16 @@ public static ByteBuffer sliceTo(ByteBuffer readData, long readOffset,
|
||||
FileRange request) {
|
||||
int offsetChange = (int) (request.getOffset() - readOffset);
|
||||
int requestLength = request.getLength();
|
||||
// Create a new buffer that is backed by the original contents
|
||||
// The buffer will have position 0 and the same limit as the original one
|
||||
readData = readData.slice();
|
||||
// Change the offset and the limit of the buffer as the reader wants to see
|
||||
// only relevant data
|
||||
readData.position(offsetChange);
|
||||
readData.limit(offsetChange + requestLength);
|
||||
// Create a new buffer after the limit change so that only that portion of the data is
|
||||
// returned to the reader.
|
||||
readData = readData.slice();
|
||||
return readData;
|
||||
}
|
||||
|
||||
|
@ -61,6 +61,9 @@ public void testSliceTo() {
|
||||
.describedAs("Slicing on the same offset shouldn't " +
|
||||
"create a new buffer")
|
||||
.isEqualTo(slice);
|
||||
Assertions.assertThat(slice.position())
|
||||
.describedAs("Slicing should return buffers starting from position 0")
|
||||
.isEqualTo(0);
|
||||
|
||||
// try slicing a range
|
||||
final int offset = 100;
|
||||
@ -77,6 +80,9 @@ public void testSliceTo() {
|
||||
.describedAs("Slicing should use the same underlying " +
|
||||
"data")
|
||||
.isEqualTo(slice.array());
|
||||
Assertions.assertThat(slice.position())
|
||||
.describedAs("Slicing should return buffers starting from position 0")
|
||||
.isEqualTo(0);
|
||||
// test the contents of the slice
|
||||
intBuffer = slice.asIntBuffer();
|
||||
for(int i=0; i < sliceLength / Integer.BYTES; ++i) {
|
||||
|
Loading…
Reference in New Issue
Block a user