michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Original author: ekr@rtfm.com michael@0: michael@0: #ifndef databuffer_h__ michael@0: #define databuffer_h__ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: class DataBuffer { michael@0: public: michael@0: DataBuffer() : data_(nullptr), len_(0) {} michael@0: DataBuffer(const uint8_t *data, size_t len) { michael@0: Assign(data, len); michael@0: } michael@0: michael@0: void Assign(const uint8_t *data, size_t len) { michael@0: data_ = new unsigned char[ len ? len : 1]; // Don't depend on new [0]. michael@0: memcpy(static_cast(data_.get()), michael@0: static_cast(data), len); michael@0: len_ = len; michael@0: } michael@0: michael@0: const uint8_t *data() const { return data_; } michael@0: size_t len() const { return len_; } michael@0: const bool empty() const { return len_ != 0; } michael@0: michael@0: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataBuffer) michael@0: michael@0: private: michael@0: ScopedDeleteArray data_; michael@0: size_t len_; michael@0: michael@0: DISALLOW_COPY_ASSIGN(DataBuffer); michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif