7 #include <packager/media/base/buffer_writer.h>
9 #include <absl/base/internal/endian.h>
10 #include <absl/log/check.h>
11 #include <absl/log/log.h>
13 #include <packager/file.h>
18 BufferWriter::BufferWriter() {
19 const size_t kDefaultReservedCapacity = 0x40000;
20 buf_.reserve(kDefaultReservedCapacity);
22 BufferWriter::BufferWriter(
size_t reserved_size_in_bytes) {
23 buf_.reserve(reserved_size_in_bytes);
25 BufferWriter::~BufferWriter() {}
31 AppendInternal(absl::big_endian::FromHost16(v));
34 AppendInternal(absl::big_endian::FromHost32(v));
37 AppendInternal(absl::big_endian::FromHost64(v));
40 AppendInternal(absl::big_endian::FromHost16(v));
43 AppendInternal(absl::big_endian::FromHost32(v));
46 AppendInternal(absl::big_endian::FromHost64(v));
50 DCHECK_GE(
sizeof(v), num_bytes);
51 v = absl::big_endian::FromHost64(v);
52 const uint8_t* data =
reinterpret_cast<uint8_t*
>(&v);
53 AppendArray(&data[
sizeof(v) - num_bytes], num_bytes);
56 void BufferWriter::AppendVector(
const std::vector<uint8_t>& v) {
57 buf_.insert(buf_.end(), v.begin(), v.end());
60 void BufferWriter::AppendString(
const std::string& s) {
61 buf_.insert(buf_.end(), s.begin(), s.end());
64 void BufferWriter::AppendArray(
const uint8_t* buf,
size_t size) {
65 buf_.insert(buf_.end(), buf, buf + size);
68 void BufferWriter::AppendBuffer(
const BufferWriter& buffer) {
69 buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
74 DCHECK(!buf_.empty());
76 size_t remaining_size = buf_.size();
77 const uint8_t* buf = &buf_[0];
78 while (remaining_size > 0) {
79 int64_t size_written = file->Write(buf, remaining_size);
80 if (size_written <= 0) {
81 return Status(error::FILE_FAILURE,
82 "Fail to write to file in BufferWriter");
84 remaining_size -= size_written;
92 void BufferWriter::AppendInternal(T v) {
93 AppendArray(
reinterpret_cast<uint8_t*
>(&v),
sizeof(T));
All the methods that are virtual are virtual for mocking.