| 133 | } |
| 134 | |
| 135 | Status DoWrite(const void* data, int64_t nbytes, |
| 136 | const std::shared_ptr<Buffer>& buffer = nullptr) { |
| 137 | std::lock_guard<std::mutex> guard(lock_); |
| 138 | if (nbytes < 0) { |
| 139 | return Status::Invalid("write count should be >= 0"); |
| 140 | } |
| 141 | if (nbytes == 0) { |
| 142 | return Status::OK(); |
| 143 | } |
| 144 | if (nbytes + buffer_pos_ >= buffer_size_) { |
| 145 | RETURN_NOT_OK(FlushUnlocked()); |
| 146 | DCHECK_EQ(buffer_pos_, 0); |
| 147 | if (nbytes >= buffer_size_) { |
| 148 | // Invalidate cached raw pos |
| 149 | raw_pos_ = -1; |
| 150 | // Direct write |
| 151 | if (buffer) { |
| 152 | return raw_->Write(buffer); |
| 153 | } else { |
| 154 | return raw_->Write(data, nbytes); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | AppendToBuffer(data, nbytes); |
| 159 | return Status::OK(); |
| 160 | } |
| 161 | |
| 162 | Status FlushUnlocked() { |
| 163 | if (buffer_pos_ > 0) { |