1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #include "draco/core/data_buffer.h"
16
17 #include <algorithm>
18
19 namespace draco {
20
DataBuffer()21 DataBuffer::DataBuffer() {}
22
Update(const void * data,int64_t size)23 bool DataBuffer::Update(const void *data, int64_t size) {
24 const int64_t offset = 0;
25 return this->Update(data, size, offset);
26 }
27
Update(const void * data,int64_t size,int64_t offset)28 bool DataBuffer::Update(const void *data, int64_t size, int64_t offset) {
29 if (data == nullptr) {
30 if (size + offset < 0) {
31 return false;
32 }
33 // If no data is provided, just resize the buffer.
34 data_.resize(size + offset);
35 } else {
36 if (size < 0) {
37 return false;
38 }
39 if (size + offset > static_cast<int64_t>(data_.size())) {
40 data_.resize(size + offset);
41 }
42 const uint8_t *const byte_data = static_cast<const uint8_t *>(data);
43 std::copy(byte_data, byte_data + size, data_.data() + offset);
44 }
45 descriptor_.buffer_update_count++;
46 return true;
47 }
48
Resize(int64_t size)49 void DataBuffer::Resize(int64_t size) {
50 data_.resize(size);
51 descriptor_.buffer_update_count++;
52 }
53
WriteDataToStream(std::ostream & stream)54 void DataBuffer::WriteDataToStream(std::ostream &stream) {
55 if (data_.empty()) {
56 return;
57 }
58 stream.write(reinterpret_cast<char *>(data_.data()), data_.size());
59 }
60
61 } // namespace draco
62