7 #include "packager/media/base/buffer_writer.h"
9 #include "packager/base/sys_byteorder.h"
10 #include "packager/media/file/file.h"
12 namespace edash_packager {
15 BufferWriter::BufferWriter() {
16 const size_t kDefaultReservedCapacity = 0x40000;
17 buf_.reserve(kDefaultReservedCapacity);
19 BufferWriter::BufferWriter(
size_t reserved_size_in_bytes) {
20 buf_.reserve(reserved_size_in_bytes);
22 BufferWriter::~BufferWriter() {}
28 AppendInternal(base::HostToNet16(v));
31 AppendInternal(base::HostToNet32(v));
34 AppendInternal(base::HostToNet64(v));
37 AppendInternal(base::HostToNet16(v));
40 AppendInternal(base::HostToNet32(v));
43 AppendInternal(base::HostToNet64(v));
47 DCHECK_GE(
sizeof(v), num_bytes);
48 v = base::HostToNet64(v);
49 const uint8_t* data =
reinterpret_cast<uint8_t*
>(&v);
50 AppendArray(&data[
sizeof(v) - num_bytes], num_bytes);
53 void BufferWriter::AppendVector(
const std::vector<uint8_t>& v) {
54 buf_.insert(buf_.end(), v.begin(), v.end());
57 void BufferWriter::AppendArray(
const uint8_t* buf,
size_t size) {
58 buf_.insert(buf_.end(), buf, buf + size);
61 void BufferWriter::AppendBuffer(
const BufferWriter& buffer) {
62 buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
67 DCHECK(!buf_.empty());
69 size_t remaining_size = buf_.size();
70 const uint8_t* buf = &buf_[0];
71 while (remaining_size > 0) {
72 int64_t size_written = file->
Write(buf, remaining_size);
73 if (size_written <= 0) {
74 return Status(error::FILE_FAILURE,
75 "Fail to write to file in BufferWriter");
77 remaining_size -= size_written;
85 void BufferWriter::AppendInternal(T v) {
86 AppendArray(reinterpret_cast<uint8_t*>(&v),
sizeof(T));