Bump docserver to 3-48-0.
[chromium-blink-merge.git] / components / pairing / message_buffer.cc
blob3cfe95ddac2a74b0f52d4d0615e5f55d45513428
1 // Copyright 2014 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.
5 #include "components/pairing/message_buffer.h"
7 #include "net/base/io_buffer.h"
9 namespace pairing_chromeos {
11 MessageBuffer::MessageBuffer()
12 : buffer_offset_(0),
13 total_buffer_size_(0) {
16 MessageBuffer::~MessageBuffer() {}
18 int MessageBuffer::AvailableBytes() {
19 return total_buffer_size_ - buffer_offset_;
22 void MessageBuffer::ReadBytes(char* buffer, int size) {
23 CHECK_LE(size, AvailableBytes());
25 int offset = 0;
26 while (offset < size) {
27 scoped_refptr<net::IOBuffer> io_buffer = pending_data_.front().first;
28 int io_buffer_size = pending_data_.front().second;
30 CHECK_GT(io_buffer_size, buffer_offset_);
31 int copy_size = std::min(size - offset, io_buffer_size - buffer_offset_);
32 memcpy(&buffer[offset], &io_buffer->data()[buffer_offset_], copy_size);
34 offset += copy_size;
35 buffer_offset_ += copy_size;
37 CHECK_LE(buffer_offset_, io_buffer_size);
38 if (buffer_offset_ == io_buffer_size) {
39 CHECK_GE(total_buffer_size_, io_buffer_size);
40 total_buffer_size_ -= io_buffer_size;
41 pending_data_.pop_front();
42 buffer_offset_ = 0;
45 CHECK_EQ(offset, size);
48 void MessageBuffer::AddIOBuffer(scoped_refptr<net::IOBuffer> io_buffer,
49 int size) {
50 if (size == 0)
51 return;
52 pending_data_.push_back(std::make_pair(io_buffer, size));
53 total_buffer_size_ += size;
56 } // namespace pairing_chromeos