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 DCHECK(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() { 35 } 36 37 StringIOBuffer::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 43 StringIOBuffer::~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 49 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) 50 : IOBuffer(base->data()), 51 base_(base), 52 size_(size), 53 used_(0) { 54 } 55 56 void DrainableIOBuffer::DidConsume(int bytes) { 57 SetOffset(used_ + bytes); 58 } 59 60 int DrainableIOBuffer::BytesRemaining() const { 61 return size_ - used_; 62 } 63 64 // Returns the number of consumed bytes. 65 int DrainableIOBuffer::BytesConsumed() const { 66 return used_; 67 } 68 69 void DrainableIOBuffer::SetOffset(int bytes) { 70 DCHECK(bytes >= 0 && bytes <= size_); 71 used_ = bytes; 72 data_ = base_->data() + used_; 73 } 74 75 DrainableIOBuffer::~DrainableIOBuffer() { 76 // The buffer is owned by the |base_| instance. 77 data_ = NULL; 78 } 79 80 GrowableIOBuffer::GrowableIOBuffer() 81 : IOBuffer(), 82 capacity_(0), 83 offset_(0) { 84 } 85 86 void 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 97 void GrowableIOBuffer::set_offset(int offset) { 98 DCHECK(offset >= 0 && offset <= capacity_); 99 offset_ = offset; 100 data_ = real_data_.get() + offset; 101 } 102 103 int GrowableIOBuffer::RemainingCapacity() { 104 return capacity_ - offset_; 105 } 106 107 char* GrowableIOBuffer::StartOfBuffer() { 108 return real_data_.get(); 109 } 110 111 GrowableIOBuffer::~GrowableIOBuffer() { 112 data_ = NULL; 113 } 114 115 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {} 116 117 void PickledIOBuffer::Done() { 118 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); 119 } 120 121 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; } 122 123 WrappedIOBuffer::WrappedIOBuffer(const char* data) 124 : IOBuffer(const_cast<char*>(data)) { 125 } 126 127 WrappedIOBuffer::~WrappedIOBuffer() { 128 data_ = NULL; 129 } 130 131 } // namespace net 132