7 #include "packager/media/base/buffer_reader.h"
9 #include "packager/base/logging.h"
22 bool BufferReader::Read2(uint16_t* v) {
25 bool BufferReader::Read2s(int16_t* v) {
28 bool BufferReader::Read4(uint32_t* v) {
31 bool BufferReader::Read4s(int32_t* v) {
34 bool BufferReader::Read8(uint64_t* v) {
37 bool BufferReader::Read8s(int64_t* v) {
41 return ReadNBytes(v, num_bytes);
43 bool BufferReader::ReadNBytesInto8s(int64_t* v,
size_t num_bytes) {
44 return ReadNBytes(v, num_bytes);
47 bool BufferReader::ReadToVector(std::vector<uint8_t>* vec,
size_t count) {
51 vec->assign(buf_ + pos_, buf_ + pos_ + count);
56 bool BufferReader::ReadToString(std::string* str,
size_t size) {
60 str->assign(buf_ + pos_, buf_ + pos_ + size);
67 for (
size_t count = 0; pos_ + count < size_; count++) {
68 if (buf_[pos_ + count] == 0) {
69 str->assign(buf_ + pos_, buf_ + pos_ + count);
85 bool BufferReader::Read(T* v) {
86 return ReadNBytes(v,
sizeof(*v));
90 bool BufferReader::ReadNBytes(T* v,
size_t num_bytes) {
92 DCHECK_LE(num_bytes,
sizeof(*v));
99 const bool sign_extension_required =
100 num_bytes < sizeof(*v) && static_cast<T>(-1) < 0;
103 T tmp = sign_extension_required ?
static_cast<int8_t
>(buf_[pos_++])
105 for (
size_t i = 1; i < num_bytes; ++i) {