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