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