| 86 | } |
| 87 | |
| 88 | Status BufferedInputStream::ReadNBytes(int64 bytes_to_read, string* result) { |
| 89 | if (bytes_to_read < 0) { |
| 90 | return errors::InvalidArgument("Can't read a negative number of bytes: ", |
| 91 | bytes_to_read); |
| 92 | } |
| 93 | result->clear(); |
| 94 | if (!file_status_.ok() && bytes_to_read > 0) { |
| 95 | return file_status_; |
| 96 | } |
| 97 | result->reserve(bytes_to_read); |
| 98 | |
| 99 | Status s; |
| 100 | while (result->size() < static_cast<size_t>(bytes_to_read)) { |
| 101 | // Check whether the buffer is fully read or not. |
| 102 | if (pos_ == limit_) { |
| 103 | s = FillBuffer(); |
| 104 | // If we didn't read any bytes, we're at the end of the file; break out. |
| 105 | if (limit_ == 0) { |
| 106 | DCHECK(!s.ok()); |
| 107 | file_status_ = s; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | const int64 bytes_to_copy = |
| 112 | std::min<int64>(limit_ - pos_, bytes_to_read - result->size()); |
| 113 | result->insert(result->size(), buf_, pos_, bytes_to_copy); |
| 114 | pos_ += bytes_to_copy; |
| 115 | } |
| 116 | // Filling the buffer might lead to a situation when we go past the end of |
| 117 | // the file leading to an OutOfRange() status return. But we might have |
| 118 | // obtained enough data to satisfy the function call. Returning OK then. |
| 119 | if (errors::IsOutOfRange(s) && |
| 120 | (result->size() == static_cast<size_t>(bytes_to_read))) { |
| 121 | return Status::OK(); |
| 122 | } |
| 123 | return s; |
| 124 | } |
| 125 | |
| 126 | Status BufferedInputStream::SkipNBytes(int64 bytes_to_skip) { |
| 127 | if (bytes_to_skip < 0) { |
no test coverage detected