Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Unified Diff: remoting/base/multiple_array_input_stream.cc

Issue 2690003: Copy the (early prototype of) remoting in Chrome into the public tree.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/base/multiple_array_input_stream.h ('k') | remoting/base/multiple_array_input_stream_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/base/multiple_array_input_stream.cc
===================================================================
--- remoting/base/multiple_array_input_stream.cc (revision 0)
+++ remoting/base/multiple_array_input_stream.cc (revision 0)
@@ -0,0 +1,96 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <functional>
+
+#include "base/logging.h"
+#include "remoting/base/multiple_array_input_stream.h"
+
+namespace remoting {
+
+MultipleArrayInputStream::MultipleArrayInputStream(int count)
+ : buffer_count_(count),
+ current_buffer_(0),
+ current_buffer_offset_(0),
+ position_(0),
+ last_returned_size_(0) {
+ DCHECK_GT(buffer_count_, 0);
+ buffers_.reset(new const uint8*[buffer_count_]);
+ buffer_sizes_.reset(new int[buffer_count_]);
+}
+
+MultipleArrayInputStream::~MultipleArrayInputStream() {
+}
+
+bool MultipleArrayInputStream::Next(const void** data, int* size) {
+ if (current_buffer_ < buffer_count_) {
+ // Also reply with that is remaining in the current buffer.
+ last_returned_size_ =
+ buffer_sizes_[current_buffer_] - current_buffer_offset_;
+ *data = buffers_[current_buffer_] + current_buffer_offset_;
+ *size = last_returned_size_;
+
+ // After reading the current buffer then advance to the next buffer.
+ current_buffer_offset_ = 0;
+ ++current_buffer_;
+ position_ += last_returned_size_;
+ return true;
+ }
+
+ // We've reached the end of the stream. So reset |last_returned_size_|
+ // to zero to prevent any backup request.
+ // This is the same as in ArrayInputStream.
+ // See google/protobuf/io/zero_copy_stream_impl_lite.cc.
+ last_returned_size_ = 0;
+ return false;
+}
+
+void MultipleArrayInputStream::BackUp(int count) {
+ DCHECK_LE(count, last_returned_size_);
+ DCHECK_EQ(0, current_buffer_offset_);
+ DCHECK_GT(current_buffer_, 0);
+
+ // Rewind one buffer.
+ --current_buffer_;
+ current_buffer_offset_ = buffer_sizes_[current_buffer_] - count;
+ position_ -= count;
+ DCHECK_GE(current_buffer_offset_, 0);
+ DCHECK_GE(position_, 0);
+}
+
+bool MultipleArrayInputStream::Skip(int count) {
+ DCHECK_GE(count, 0);
+ last_returned_size_ = 0;
+
+ while (count && current_buffer_ < buffer_count_) {
+ int read = std::min(
+ count,
+ buffer_sizes_[current_buffer_] - current_buffer_offset_);
+
+ // Advance the current buffer offset and position.
+ current_buffer_offset_ += read;
+ position_ += read;
+ count -= read;
+
+ // If the current buffer is fully read, then advance to the next buffer.
+ if (current_buffer_offset_ == buffer_sizes_[current_buffer_]) {
+ ++current_buffer_;
+ current_buffer_offset_ = 0;
+ }
+ }
+ return count == 0;
+}
+
+int64 MultipleArrayInputStream::ByteCount() const {
+ return position_;
+}
+
+void MultipleArrayInputStream::SetBuffer(
+ int n, const uint8* buffer, int size) {
+ CHECK(n < buffer_count_);
+ buffers_[n] = buffer;
+ buffer_sizes_[n] = size;
+}
+
+} // namespace remoting
Property changes on: remoting/base/multiple_array_input_stream.cc
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « remoting/base/multiple_array_input_stream.h ('k') | remoting/base/multiple_array_input_stream_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698