1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Original author: ekr@rtfm.com
10 #define databuffer_h__
12 #include <mozilla/UniquePtr.h>
13 #include <m_cpp_utils.h>
14 #include <nsISupportsImpl.h>
20 DataBuffer() : data_(nullptr), len_(0) {}
21 DataBuffer(const uint8_t *data
, size_t len
) {
25 void Assign(const uint8_t *data
, size_t len
) {
27 memcpy(static_cast<void *>(data_
.get()),
28 static_cast<const void *>(data
), len
);
31 void Allocate(size_t len
) {
32 data_
.reset(new uint8_t[len
? len
: 1]); // Don't depend on new [0].
36 const uint8_t *data() const { return data_
.get(); }
37 uint8_t *data() { return data_
.get(); }
38 size_t len() const { return len_
; }
39 const bool empty() const { return len_
!= 0; }
42 UniquePtr
<uint8_t[]> data_
;
45 DISALLOW_COPY_ASSIGN(DataBuffer
);