• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
IOBuffer()11 IOBuffer::IOBuffer()
12     : data_(NULL) {
13 }
14 
IOBuffer(int buffer_size)15 IOBuffer::IOBuffer(int buffer_size) {
16   CHECK_GE(buffer_size, 0);
17   data_ = new char[buffer_size];
18 }
19 
IOBuffer(char * data)20 IOBuffer::IOBuffer(char* data)
21     : data_(data) {
22 }
23 
~IOBuffer()24 IOBuffer::~IOBuffer() {
25   delete[] data_;
26   data_ = NULL;
27 }
28 
IOBufferWithSize(int size)29 IOBufferWithSize::IOBufferWithSize(int size)
30     : IOBuffer(size),
31       size_(size) {
32 }
33 
IOBufferWithSize(char * data,int size)34 IOBufferWithSize::IOBufferWithSize(char* data, int size)
35     : IOBuffer(data),
36       size_(size) {
37 }
38 
~IOBufferWithSize()39 IOBufferWithSize::~IOBufferWithSize() {
40 }
41 
StringIOBuffer(const std::string & s)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 
~StringIOBuffer()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 
DrainableIOBuffer(IOBuffer * base,int size)55 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size)
56     : IOBuffer(base->data()),
57       base_(base),
58       size_(size),
59       used_(0) {
60 }
61 
DidConsume(int bytes)62 void DrainableIOBuffer::DidConsume(int bytes) {
63   SetOffset(used_ + bytes);
64 }
65 
BytesRemaining() const66 int DrainableIOBuffer::BytesRemaining() const {
67   return size_ - used_;
68 }
69 
70 // Returns the number of consumed bytes.
BytesConsumed() const71 int DrainableIOBuffer::BytesConsumed() const {
72   return used_;
73 }
74 
SetOffset(int bytes)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 
~DrainableIOBuffer()82 DrainableIOBuffer::~DrainableIOBuffer() {
83   // The buffer is owned by the |base_| instance.
84   data_ = NULL;
85 }
86 
GrowableIOBuffer()87 GrowableIOBuffer::GrowableIOBuffer()
88     : IOBuffer(),
89       capacity_(0),
90       offset_(0) {
91 }
92 
SetCapacity(int capacity)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 
set_offset(int offset)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 
RemainingCapacity()111 int GrowableIOBuffer::RemainingCapacity() {
112   return capacity_ - offset_;
113 }
114 
StartOfBuffer()115 char* GrowableIOBuffer::StartOfBuffer() {
116   return real_data_.get();
117 }
118 
~GrowableIOBuffer()119 GrowableIOBuffer::~GrowableIOBuffer() {
120   data_ = NULL;
121 }
122 
PickledIOBuffer()123 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {}
124 
Done()125 void PickledIOBuffer::Done() {
126   data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
127 }
128 
~PickledIOBuffer()129 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; }
130 
WrappedIOBuffer(const char * data)131 WrappedIOBuffer::WrappedIOBuffer(const char* data)
132     : IOBuffer(const_cast<char*>(data)) {
133 }
134 
~WrappedIOBuffer()135 WrappedIOBuffer::~WrappedIOBuffer() {
136   data_ = NULL;
137 }
138 
139 }  // namespace net
140