1 // Copyright (c) 2013 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 "net/spdy/spdy_read_queue.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "net/spdy/spdy_buffer.h"
13 SpdyReadQueue::SpdyReadQueue() : total_size_(0) {}
15 SpdyReadQueue::~SpdyReadQueue() {
19 bool SpdyReadQueue::IsEmpty() const {
20 DCHECK_EQ(queue_
.empty(), total_size_
== 0);
21 return queue_
.empty();
24 size_t SpdyReadQueue::GetTotalSize() const {
28 void SpdyReadQueue::Enqueue(scoped_ptr
<SpdyBuffer
> buffer
) {
29 DCHECK_GT(buffer
->GetRemainingSize(), 0u);
30 total_size_
+= buffer
->GetRemainingSize();
31 queue_
.push_back(buffer
.release());
34 size_t SpdyReadQueue::Dequeue(char* out
, size_t len
) {
36 size_t bytes_copied
= 0;
37 while (!queue_
.empty() && bytes_copied
< len
) {
38 SpdyBuffer
* buffer
= queue_
.front();
39 size_t bytes_to_copy
=
40 std::min(len
- bytes_copied
, buffer
->GetRemainingSize());
41 memcpy(out
+ bytes_copied
, buffer
->GetRemainingData(), bytes_to_copy
);
42 bytes_copied
+= bytes_to_copy
;
43 if (bytes_to_copy
== buffer
->GetRemainingSize()) {
44 delete queue_
.front();
47 buffer
->Consume(bytes_to_copy
);
50 total_size_
-= bytes_copied
;
54 void SpdyReadQueue::Clear() {
55 STLDeleteElements(&queue_
);