1// Copyright (c) 2011 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 "net/base/io_buffer.h"
6
7#include "base/logging.h"
8
9namespace net {
10
11IOBuffer::IOBuffer()
12    : data_(NULL) {
13}
14
15IOBuffer::IOBuffer(int buffer_size) {
16  DCHECK(buffer_size > 0);
17  data_ = new char[buffer_size];
18}
19
20IOBuffer::IOBuffer(char* data)
21    : data_(data) {
22}
23
24IOBuffer::~IOBuffer() {
25  delete[] data_;
26  data_ = NULL;
27}
28
29IOBufferWithSize::IOBufferWithSize(int size)
30    : IOBuffer(size),
31      size_(size) {
32}
33
34IOBufferWithSize::~IOBufferWithSize() {
35}
36
37StringIOBuffer::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
56void 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
69void DrainableIOBuffer::SetOffset(int bytes) {
70  DCHECK(bytes >= 0 && bytes <= size_);
71  used_ = bytes;
72  data_ = base_->data() + used_;
73}
74
75DrainableIOBuffer::~DrainableIOBuffer() {
76  // The buffer is owned by the |base_| instance.
77  data_ = NULL;
78}
79
80GrowableIOBuffer::GrowableIOBuffer()
81    : IOBuffer(),
82      capacity_(0),
83      offset_(0) {
84}
85
86void GrowableIOBuffer::SetCapacity(int capacity) {
87  DCHECK(capacity >= 0);
88  // realloc will crash if it fails.
89  real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
90  capacity_ = capacity;
91  if (offset_ > capacity)
92    set_offset(capacity);
93  else
94    set_offset(offset_);  // The pointer may have changed.
95}
96
97void GrowableIOBuffer::set_offset(int offset) {
98  DCHECK(offset >= 0 && offset <= capacity_);
99  offset_ = offset;
100  data_ = real_data_.get() + offset;
101}
102
103int GrowableIOBuffer::RemainingCapacity() {
104  return capacity_ - offset_;
105}
106
107char* GrowableIOBuffer::StartOfBuffer() {
108  return real_data_.get();
109}
110
111GrowableIOBuffer::~GrowableIOBuffer() {
112  data_ = NULL;
113}
114
115PickledIOBuffer::PickledIOBuffer() : IOBuffer() {}
116
117void PickledIOBuffer::Done() {
118  data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
119}
120
121PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; }
122
123WrappedIOBuffer::WrappedIOBuffer(const char* data)
124    : IOBuffer(const_cast<char*>(data)) {
125}
126
127WrappedIOBuffer::~WrappedIOBuffer() {
128  data_ = NULL;
129}
130
131}  // namespace net
132