|
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/. */ |
|
6 |
|
7 // Original author: ekr@rtfm.com |
|
8 |
|
9 #ifndef databuffer_h__ |
|
10 #define databuffer_h__ |
|
11 #include <algorithm> |
|
12 #include <mozilla/Scoped.h> |
|
13 #include <m_cpp_utils.h> |
|
14 #include <nsISupportsImpl.h> |
|
15 |
|
16 namespace mozilla { |
|
17 |
|
18 class DataBuffer { |
|
19 public: |
|
20 DataBuffer() : data_(nullptr), len_(0) {} |
|
21 DataBuffer(const uint8_t *data, size_t len) { |
|
22 Assign(data, len); |
|
23 } |
|
24 |
|
25 void Assign(const uint8_t *data, size_t len) { |
|
26 data_ = new unsigned char[ len ? len : 1]; // Don't depend on new [0]. |
|
27 memcpy(static_cast<void *>(data_.get()), |
|
28 static_cast<const void *>(data), len); |
|
29 len_ = len; |
|
30 } |
|
31 |
|
32 const uint8_t *data() const { return data_; } |
|
33 size_t len() const { return len_; } |
|
34 const bool empty() const { return len_ != 0; } |
|
35 |
|
36 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataBuffer) |
|
37 |
|
38 private: |
|
39 ScopedDeleteArray<uint8_t> data_; |
|
40 size_t len_; |
|
41 |
|
42 DISALLOW_COPY_ASSIGN(DataBuffer); |
|
43 }; |
|
44 |
|
45 } |
|
46 |
|
47 #endif |