| 403 | } |
| 404 | |
| 405 | Result<int64_t> Read(int64_t nbytes, void* out) { |
| 406 | if (ARROW_PREDICT_FALSE(nbytes < 0)) { |
| 407 | return Status::Invalid("Bytes to read must be positive. Received:", nbytes); |
| 408 | } |
| 409 | |
| 410 | // 1. First consume pre-buffered data. |
| 411 | int64_t pre_buffer_copy_bytes = std::min(nbytes, bytes_buffered_); |
| 412 | if (pre_buffer_copy_bytes > 0) { |
| 413 | memcpy(out, buffer_data_ + buffer_pos_, pre_buffer_copy_bytes); |
| 414 | ConsumeBuffer(pre_buffer_copy_bytes); |
| 415 | } |
| 416 | int64_t remaining_bytes = nbytes - pre_buffer_copy_bytes; |
| 417 | if (raw_read_bound_ >= 0) { |
| 418 | remaining_bytes = std::min(remaining_bytes, raw_read_bound_ - raw_read_total_); |
| 419 | } |
| 420 | if (remaining_bytes == 0) { |
| 421 | return pre_buffer_copy_bytes; |
| 422 | } |
| 423 | DCHECK_EQ(0, bytes_buffered_); |
| 424 | |
| 425 | // 2. Read from storage. |
| 426 | if (remaining_bytes >= buffer_size_) { |
| 427 | // 2.1. If read is larger than buffer size, read directly from storage. |
| 428 | ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, |
| 429 | raw_->Read(remaining_bytes, reinterpret_cast<uint8_t*>(out) + |
| 430 | pre_buffer_copy_bytes)); |
| 431 | raw_read_total_ += bytes_read; |
| 432 | RewindBuffer(); |
| 433 | return pre_buffer_copy_bytes + bytes_read; |
| 434 | } else { |
| 435 | // 2.2. If read is smaller than buffer size, fill buffer and copy from buffer. |
| 436 | RETURN_NOT_OK(DoBuffer()); |
| 437 | int64_t bytes_copy_after_buffer = std::min(bytes_buffered_, remaining_bytes); |
| 438 | memcpy(reinterpret_cast<uint8_t*>(out) + pre_buffer_copy_bytes, |
| 439 | buffer_data_ + buffer_pos_, bytes_copy_after_buffer); |
| 440 | ConsumeBuffer(bytes_copy_after_buffer); |
| 441 | return pre_buffer_copy_bytes + bytes_copy_after_buffer; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) { |
| 446 | ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(nbytes, pool_)); |