blob: aa048706d9e3bb5c744428bcb99d48aee71d9dd2 [file] [log] [blame]
[email protected]df18fe1c2011-04-12 23:46:211// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]319d9e6f2009-02-18 19:47:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/io_buffer.h"
6
7#include "base/logging.h"
8
9namespace net {
10
[email protected]6d22a972010-07-21 15:47:1911IOBuffer::IOBuffer()
12 : data_(NULL) {
13}
14
[email protected]319d9e6f2009-02-18 19:47:2115IOBuffer::IOBuffer(int buffer_size) {
[email protected]bbbb03742009-03-11 20:55:5416 DCHECK(buffer_size > 0);
[email protected]319d9e6f2009-02-18 19:47:2117 data_ = new char[buffer_size];
18}
[email protected]c19c7152009-10-14 17:35:3719
[email protected]6d22a972010-07-21 15:47:1920IOBuffer::IOBuffer(char* data)
21 : data_(data) {
22}
23
24IOBuffer::~IOBuffer() {
25 delete[] data_;
[email protected]df18fe1c2011-04-12 23:46:2126 data_ = NULL;
[email protected]6d22a972010-07-21 15:47:1927}
28
29IOBufferWithSize::IOBufferWithSize(int size)
30 : IOBuffer(size),
31 size_(size) {
32}
33
[email protected]7e4468d52010-09-22 19:42:0034IOBufferWithSize::~IOBufferWithSize() {
35}
36
[email protected]6d22a972010-07-21 15:47:1937StringIOBuffer::StringIOBuffer(const std::string& s)
38 : IOBuffer(static_cast<char*>(NULL)),
39 string_data_(s) {
40 data_ = const_cast<char*>(string_data_.data());
41}
42
43StringIOBuffer::~StringIOBuffer() {
44 // We haven't allocated the buffer, so remove it before the base class
45 // destructor tries to delete[] it.
46 data_ = NULL;
47}
48
49DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size)
50 : IOBuffer(base->data()),
51 base_(base),
52 size_(size),
53 used_(0) {
54}
55
[email protected]6d22a972010-07-21 15:47:1956void DrainableIOBuffer::DidConsume(int bytes) {
57 SetOffset(used_ + bytes);
58}
59
60int DrainableIOBuffer::BytesRemaining() const {
61 return size_ - used_;
62}
63
64// Returns the number of consumed bytes.
65int DrainableIOBuffer::BytesConsumed() const {
66 return used_;
67}
68
[email protected]c19c7152009-10-14 17:35:3769void DrainableIOBuffer::SetOffset(int bytes) {
[email protected]52af5d52011-05-05 09:59:2870 DCHECK_GE(bytes, 0);
71 DCHECK_LE(bytes, size_);
[email protected]c19c7152009-10-14 17:35:3772 used_ = bytes;
73 data_ = base_->data() + used_;
74}
75
[email protected]be1a48b2011-01-20 00:12:1376DrainableIOBuffer::~DrainableIOBuffer() {
77 // The buffer is owned by the |base_| instance.
78 data_ = NULL;
79}
80
[email protected]6d22a972010-07-21 15:47:1981GrowableIOBuffer::GrowableIOBuffer()
82 : IOBuffer(),
83 capacity_(0),
84 offset_(0) {
85}
86
[email protected]776e1752009-11-10 18:51:3487void GrowableIOBuffer::SetCapacity(int capacity) {
[email protected]52af5d52011-05-05 09:59:2888 DCHECK_GE(capacity, 0);
[email protected]776e1752009-11-10 18:51:3489 // realloc will crash if it fails.
90 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
[email protected]c19c7152009-10-14 17:35:3791 capacity_ = capacity;
[email protected]c19c7152009-10-14 17:35:3792 if (offset_ > capacity)
93 set_offset(capacity);
94 else
95 set_offset(offset_); // The pointer may have changed.
96}
97
98void GrowableIOBuffer::set_offset(int offset) {
[email protected]52af5d52011-05-05 09:59:2899 DCHECK_GE(offset, 0);
100 DCHECK_LE(offset, capacity_);
[email protected]c19c7152009-10-14 17:35:37101 offset_ = offset;
102 data_ = real_data_.get() + offset;
[email protected]84d4cee2009-06-18 23:46:58103}
[email protected]319d9e6f2009-02-18 19:47:21104
[email protected]6d22a972010-07-21 15:47:19105int GrowableIOBuffer::RemainingCapacity() {
106 return capacity_ - offset_;
107}
108
109char* GrowableIOBuffer::StartOfBuffer() {
110 return real_data_.get();
111}
112
[email protected]be1a48b2011-01-20 00:12:13113GrowableIOBuffer::~GrowableIOBuffer() {
114 data_ = NULL;
115}
[email protected]6d22a972010-07-21 15:47:19116
[email protected]be1a48b2011-01-20 00:12:13117PickledIOBuffer::PickledIOBuffer() : IOBuffer() {}
[email protected]6d22a972010-07-21 15:47:19118
119void PickledIOBuffer::Done() {
120 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
121}
122
[email protected]be1a48b2011-01-20 00:12:13123PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; }
[email protected]6d22a972010-07-21 15:47:19124
125WrappedIOBuffer::WrappedIOBuffer(const char* data)
126 : IOBuffer(const_cast<char*>(data)) {
127}
128
129WrappedIOBuffer::~WrappedIOBuffer() {
130 data_ = NULL;
131}
132
[email protected]319d9e6f2009-02-18 19:47:21133} // namespace net