| 68 | static constexpr int64 kMaxSkipSize = 8 * 1024 * 1024; |
| 69 | |
| 70 | Status RandomAccessInputStream::SkipNBytes(int64 bytes_to_skip) { |
| 71 | if (bytes_to_skip < 0) { |
| 72 | return errors::InvalidArgument("Can't skip a negative number of bytes"); |
| 73 | } |
| 74 | std::unique_ptr<char[]> scratch(new char[kMaxSkipSize]); |
| 75 | // Try to read 1 bytes first, if we could complete the read then EOF is |
| 76 | // not reached yet and we could return. |
| 77 | if (bytes_to_skip > 0) { |
| 78 | StringPiece data; |
| 79 | Status s = file_->Read(pos_ + bytes_to_skip - 1, 1, &data, scratch.get()); |
| 80 | if ((s.ok() || errors::IsOutOfRange(s)) && data.size() == 1) { |
| 81 | pos_ += bytes_to_skip; |
| 82 | return Status::OK(); |
| 83 | } |
| 84 | } |
| 85 | // Read kDefaultSkipSize at a time till bytes_to_skip. |
| 86 | while (bytes_to_skip > 0) { |
| 87 | int64 bytes_to_read = std::min<int64>(kMaxSkipSize, bytes_to_skip); |
| 88 | StringPiece data; |
| 89 | Status s = file_->Read(pos_, bytes_to_read, &data, scratch.get()); |
| 90 | if (s.ok() || errors::IsOutOfRange(s)) { |
| 91 | pos_ += data.size(); |
| 92 | } else { |
| 93 | return s; |
| 94 | } |
| 95 | if (data.size() < bytes_to_read) { |
| 96 | return errors::OutOfRange("reached end of file"); |
| 97 | } |
| 98 | bytes_to_skip -= bytes_to_read; |
| 99 | } |
| 100 | return Status::OK(); |
| 101 | } |
| 102 | |
| 103 | int64 RandomAccessInputStream::Tell() const { return pos_; } |
| 104 |
nothing calls this directly
no test coverage detected