[Bug] Fix bug that the buffered reader may read at wrong position. (#5847)

The buffered reader's _cur_offset should be initialized as same as the inner file reader's,
to make sure that the reader will start to read at rignt position.
This commit is contained in:
Mingyu Chen
2021-05-22 23:38:10 +08:00
committed by GitHub
parent 07ad038870
commit 591d391bbc
2 changed files with 10 additions and 0 deletions

View File

@ -32,6 +32,9 @@ BufferedReader::BufferedReader(FileReader* reader, int64_t buffer_size)
_buffer_limit(0),
_cur_offset(0) {
_buffer = new char[_buffer_size];
// set the _cur_offset of this reader as same as the inner reader's,
// to make sure the buffer reader will start to read at right position.
_reader->tell(&_cur_offset);
}
BufferedReader::~BufferedReader() {

View File

@ -68,6 +68,7 @@ Status S3Reader::open() {
return Status::InternalError(out.str());
}
}
Status S3Reader::read(uint8_t* buf, int64_t buf_len, int64_t* bytes_read, bool* eof) {
DCHECK_NE(buf_len, 0);
RETURN_IF_ERROR(readat(_cur_offset, buf_len, bytes_read, buf));
@ -78,6 +79,7 @@ Status S3Reader::read(uint8_t* buf, int64_t buf_len, int64_t* bytes_read, bool*
}
return Status::OK();
}
Status S3Reader::readat(int64_t position, int64_t nbytes, int64_t* bytes_read, void* out) {
CHECK_S3_CLIENT(_client);
if (position >= _file_size) {
@ -107,6 +109,7 @@ Status S3Reader::readat(int64_t position, int64_t nbytes, int64_t* bytes_read, v
response.GetResult().GetBody().read((char*)out, *bytes_read);
return Status::OK();
}
Status S3Reader::read_one_message(std::unique_ptr<uint8_t[]>* buf, int64_t* length) {
bool eof;
int64_t file_size = size() - _cur_offset;
@ -123,17 +126,21 @@ Status S3Reader::read_one_message(std::unique_ptr<uint8_t[]>* buf, int64_t* leng
int64_t S3Reader::size() {
return _file_size;
}
Status S3Reader::seek(int64_t position) {
_cur_offset = position;
return Status::OK();
}
Status S3Reader::tell(int64_t* position) {
*position = _cur_offset;
return Status::OK();
}
void S3Reader::close() {
_closed = true;
}
bool S3Reader::closed() {
return _closed;
}