7 #include "packager/media/base/buffer_writer.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/sys_byteorder.h"
11 #include "packager/file/file.h"
16 BufferWriter::BufferWriter() {
17 const size_t kDefaultReservedCapacity = 0x40000;
18 buf_.reserve(kDefaultReservedCapacity);
20 BufferWriter::BufferWriter(
size_t reserved_size_in_bytes) {
21 buf_.reserve(reserved_size_in_bytes);
23 BufferWriter::~BufferWriter() {}
29 AppendInternal(base::HostToNet16(v));
32 AppendInternal(base::HostToNet32(v));
35 AppendInternal(base::HostToNet64(v));
38 AppendInternal(base::HostToNet16(v));
41 AppendInternal(base::HostToNet32(v));
44 AppendInternal(base::HostToNet64(v));
48 DCHECK_GE(
sizeof(v), num_bytes);
49 v = base::HostToNet64(v);
50 const uint8_t* data =
reinterpret_cast<uint8_t*
>(&v);
51 AppendArray(&data[
sizeof(v) - num_bytes], num_bytes);
54 void BufferWriter::AppendVector(
const std::vector<uint8_t>& v) {
55 buf_.insert(buf_.end(), v.begin(), v.end());
58 void BufferWriter::AppendArray(
const uint8_t* buf,
size_t size) {
59 buf_.insert(buf_.end(), buf, buf + size);
62 void BufferWriter::AppendBuffer(
const BufferWriter& buffer) {
63 buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
68 DCHECK(!buf_.empty());
70 size_t remaining_size = buf_.size();
71 const uint8_t* buf = &buf_[0];
72 while (remaining_size > 0) {
73 int64_t size_written = file->Write(buf, remaining_size);
74 if (size_written <= 0) {
75 return Status(error::FILE_FAILURE,
76 "Fail to write to file in BufferWriter");
78 remaining_size -= size_written;
86 void BufferWriter::AppendInternal(T v) {
87 AppendArray(reinterpret_cast<uint8_t*>(&v),
sizeof(T));