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::AppendString(
const std::string& s) {
59 buf_.insert(buf_.end(), s.begin(), s.end());
62 void BufferWriter::AppendArray(
const uint8_t* buf,
size_t size) {
63 buf_.insert(buf_.end(), buf, buf + size);
66 void BufferWriter::AppendBuffer(
const BufferWriter& buffer) {
67 buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
72 DCHECK(!buf_.empty());
74 size_t remaining_size = buf_.size();
75 const uint8_t* buf = &buf_[0];
76 while (remaining_size > 0) {
77 int64_t size_written = file->
Write(buf, remaining_size);
78 if (size_written <= 0) {
79 return Status(error::FILE_FAILURE,
80 "Fail to write to file in BufferWriter");
82 remaining_size -= size_written;
90 void BufferWriter::AppendInternal(T v) {
91 AppendArray(
reinterpret_cast<uint8_t*
>(&v),
sizeof(T));