Shaka Packager SDK
buffer_writer.cc
1 // Copyright 2014 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/media/base/buffer_writer.h>
8 
9 #include <absl/base/internal/endian.h>
10 #include <absl/log/check.h>
11 #include <absl/log/log.h>
12 
13 #include <packager/file.h>
14 
15 namespace shaka {
16 namespace media {
17 
18 BufferWriter::BufferWriter() {
19  const size_t kDefaultReservedCapacity = 0x40000; // 256KB.
20  buf_.reserve(kDefaultReservedCapacity);
21 }
22 BufferWriter::BufferWriter(size_t reserved_size_in_bytes) {
23  buf_.reserve(reserved_size_in_bytes);
24 }
25 BufferWriter::~BufferWriter() {}
26 
27 void BufferWriter::AppendInt(uint8_t v) {
28  buf_.push_back(v);
29 }
30 void BufferWriter::AppendInt(uint16_t v) {
31  AppendInternal(absl::big_endian::FromHost16(v));
32 }
33 void BufferWriter::AppendInt(uint32_t v) {
34  AppendInternal(absl::big_endian::FromHost32(v));
35 }
36 void BufferWriter::AppendInt(uint64_t v) {
37  AppendInternal(absl::big_endian::FromHost64(v));
38 }
39 void BufferWriter::AppendInt(int16_t v) {
40  AppendInternal(absl::big_endian::FromHost16(v));
41 }
42 void BufferWriter::AppendInt(int32_t v) {
43  AppendInternal(absl::big_endian::FromHost32(v));
44 }
45 void BufferWriter::AppendInt(int64_t v) {
46  AppendInternal(absl::big_endian::FromHost64(v));
47 }
48 
49 void BufferWriter::AppendNBytes(uint64_t v, size_t num_bytes) {
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);
54 }
55 
56 void BufferWriter::AppendVector(const std::vector<uint8_t>& v) {
57  buf_.insert(buf_.end(), v.begin(), v.end());
58 }
59 
60 void BufferWriter::AppendString(const std::string& s) {
61  buf_.insert(buf_.end(), s.begin(), s.end());
62 }
63 
64 void BufferWriter::AppendArray(const uint8_t* buf, size_t size) {
65  buf_.insert(buf_.end(), buf, buf + size);
66 }
67 
68 void BufferWriter::AppendBuffer(const BufferWriter& buffer) {
69  buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
70 }
71 
72 Status BufferWriter::WriteToFile(File* file) {
73  DCHECK(file);
74  DCHECK(!buf_.empty());
75 
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");
83  }
84  remaining_size -= size_written;
85  buf += size_written;
86  }
87  buf_.clear();
88  return Status::OK;
89 }
90 
91 template <typename T>
92 void BufferWriter::AppendInternal(T v) {
93  AppendArray(reinterpret_cast<uint8_t*>(&v), sizeof(T));
94 }
95 
96 } // namespace media
97 } // namespace shaka
Status WriteToFile(File *file)
void AppendNBytes(uint64_t v, size_t num_bytes)
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66