OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <functional> |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "remoting/base/multiple_array_input_stream.h" |
| 9 |
| 10 namespace remoting { |
| 11 |
| 12 MultipleArrayInputStream::MultipleArrayInputStream(int count) |
| 13 : buffer_count_(count), |
| 14 current_buffer_(0), |
| 15 current_buffer_offset_(0), |
| 16 position_(0), |
| 17 last_returned_size_(0) { |
| 18 DCHECK_GT(buffer_count_, 0); |
| 19 buffers_.reset(new const uint8*[buffer_count_]); |
| 20 buffer_sizes_.reset(new int[buffer_count_]); |
| 21 } |
| 22 |
| 23 MultipleArrayInputStream::~MultipleArrayInputStream() { |
| 24 } |
| 25 |
| 26 bool MultipleArrayInputStream::Next(const void** data, int* size) { |
| 27 if (current_buffer_ < buffer_count_) { |
| 28 // Also reply with that is remaining in the current buffer. |
| 29 last_returned_size_ = |
| 30 buffer_sizes_[current_buffer_] - current_buffer_offset_; |
| 31 *data = buffers_[current_buffer_] + current_buffer_offset_; |
| 32 *size = last_returned_size_; |
| 33 |
| 34 // After reading the current buffer then advance to the next buffer. |
| 35 current_buffer_offset_ = 0; |
| 36 ++current_buffer_; |
| 37 position_ += last_returned_size_; |
| 38 return true; |
| 39 } |
| 40 |
| 41 // We've reached the end of the stream. So reset |last_returned_size_| |
| 42 // to zero to prevent any backup request. |
| 43 // This is the same as in ArrayInputStream. |
| 44 // See google/protobuf/io/zero_copy_stream_impl_lite.cc. |
| 45 last_returned_size_ = 0; |
| 46 return false; |
| 47 } |
| 48 |
| 49 void MultipleArrayInputStream::BackUp(int count) { |
| 50 DCHECK_LE(count, last_returned_size_); |
| 51 DCHECK_EQ(0, current_buffer_offset_); |
| 52 DCHECK_GT(current_buffer_, 0); |
| 53 |
| 54 // Rewind one buffer. |
| 55 --current_buffer_; |
| 56 current_buffer_offset_ = buffer_sizes_[current_buffer_] - count; |
| 57 position_ -= count; |
| 58 DCHECK_GE(current_buffer_offset_, 0); |
| 59 DCHECK_GE(position_, 0); |
| 60 } |
| 61 |
| 62 bool MultipleArrayInputStream::Skip(int count) { |
| 63 DCHECK_GE(count, 0); |
| 64 last_returned_size_ = 0; |
| 65 |
| 66 while (count && current_buffer_ < buffer_count_) { |
| 67 int read = std::min( |
| 68 count, |
| 69 buffer_sizes_[current_buffer_] - current_buffer_offset_); |
| 70 |
| 71 // Advance the current buffer offset and position. |
| 72 current_buffer_offset_ += read; |
| 73 position_ += read; |
| 74 count -= read; |
| 75 |
| 76 // If the current buffer is fully read, then advance to the next buffer. |
| 77 if (current_buffer_offset_ == buffer_sizes_[current_buffer_]) { |
| 78 ++current_buffer_; |
| 79 current_buffer_offset_ = 0; |
| 80 } |
| 81 } |
| 82 return count == 0; |
| 83 } |
| 84 |
| 85 int64 MultipleArrayInputStream::ByteCount() const { |
| 86 return position_; |
| 87 } |
| 88 |
| 89 void MultipleArrayInputStream::SetBuffer( |
| 90 int n, const uint8* buffer, int size) { |
| 91 CHECK(n < buffer_count_); |
| 92 buffers_[n] = buffer; |
| 93 buffer_sizes_[n] = size; |
| 94 } |
| 95 |
| 96 } // namespace remoting |
OLD | NEW |