- add sources.
[platform/framework/web/crosswalk.git] / src / net / base / io_buffer.cc
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
9 namespace net {
10
11 IOBuffer::IOBuffer()
12     : data_(NULL) {
13 }
14
15 IOBuffer::IOBuffer(int buffer_size) {
16   CHECK_GE(buffer_size, 0);
17   data_ = new char[buffer_size];
18 }
19
20 IOBuffer::IOBuffer(char* data)
21     : data_(data) {
22 }
23
24 IOBuffer::~IOBuffer() {
25   delete[] data_;
26   data_ = NULL;
27 }
28
29 IOBufferWithSize::IOBufferWithSize(int size)
30     : IOBuffer(size),
31       size_(size) {
32 }
33
34 IOBufferWithSize::IOBufferWithSize(char* data, int size)
35     : IOBuffer(data),
36       size_(size) {
37 }
38
39 IOBufferWithSize::~IOBufferWithSize() {
40 }
41
42 StringIOBuffer::StringIOBuffer(const std::string& s)
43     : IOBuffer(static_cast<char*>(NULL)),
44       string_data_(s) {
45   CHECK_LT(s.size(), static_cast<size_t>(INT_MAX));
46   data_ = const_cast<char*>(string_data_.data());
47 }
48
49 StringIOBuffer::~StringIOBuffer() {
50   // We haven't allocated the buffer, so remove it before the base class
51   // destructor tries to delete[] it.
52   data_ = NULL;
53 }
54
55 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size)
56     : IOBuffer(base->data()),
57       base_(base),
58       size_(size),
59       used_(0) {
60 }
61
62 void DrainableIOBuffer::DidConsume(int bytes) {
63   SetOffset(used_ + bytes);
64 }
65
66 int DrainableIOBuffer::BytesRemaining() const {
67   return size_ - used_;
68 }
69
70 // Returns the number of consumed bytes.
71 int DrainableIOBuffer::BytesConsumed() const {
72   return used_;
73 }
74
75 void DrainableIOBuffer::SetOffset(int bytes) {
76   DCHECK_GE(bytes, 0);
77   DCHECK_LE(bytes, size_);
78   used_ = bytes;
79   data_ = base_->data() + used_;
80 }
81
82 DrainableIOBuffer::~DrainableIOBuffer() {
83   // The buffer is owned by the |base_| instance.
84   data_ = NULL;
85 }
86
87 GrowableIOBuffer::GrowableIOBuffer()
88     : IOBuffer(),
89       capacity_(0),
90       offset_(0) {
91 }
92
93 void GrowableIOBuffer::SetCapacity(int capacity) {
94   DCHECK_GE(capacity, 0);
95   // realloc will crash if it fails.
96   real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
97   capacity_ = capacity;
98   if (offset_ > capacity)
99     set_offset(capacity);
100   else
101     set_offset(offset_);  // The pointer may have changed.
102 }
103
104 void GrowableIOBuffer::set_offset(int offset) {
105   DCHECK_GE(offset, 0);
106   DCHECK_LE(offset, capacity_);
107   offset_ = offset;
108   data_ = real_data_.get() + offset;
109 }
110
111 int GrowableIOBuffer::RemainingCapacity() {
112   return capacity_ - offset_;
113 }
114
115 char* GrowableIOBuffer::StartOfBuffer() {
116   return real_data_.get();
117 }
118
119 GrowableIOBuffer::~GrowableIOBuffer() {
120   data_ = NULL;
121 }
122
123 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {}
124
125 void PickledIOBuffer::Done() {
126   data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
127 }
128
129 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; }
130
131 WrappedIOBuffer::WrappedIOBuffer(const char* data)
132     : IOBuffer(const_cast<char*>(data)) {
133 }
134
135 WrappedIOBuffer::~WrappedIOBuffer() {
136   data_ = NULL;
137 }
138
139 }  // namespace net