2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
#include "media/base/buffer_writer.h"
|
|
|
|
|
|
|
|
#include "base/sys_byteorder.h"
|
|
|
|
#include "media/file/file.h"
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-11-14 00:08:54 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
BufferWriter::BufferWriter() {
|
|
|
|
const size_t kDefaultReservedCapacity = 0x40000; // 256KB.
|
|
|
|
buf_.reserve(kDefaultReservedCapacity);
|
|
|
|
}
|
|
|
|
BufferWriter::BufferWriter(size_t reserved_size_in_bytes) {
|
|
|
|
buf_.reserve(reserved_size_in_bytes);
|
|
|
|
}
|
|
|
|
BufferWriter::~BufferWriter() {}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendInt(uint8_t v) {
|
|
|
|
buf_.push_back(v);
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(uint16_t v) {
|
|
|
|
AppendInternal(base::HostToNet16(v));
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(uint32_t v) {
|
|
|
|
AppendInternal(base::HostToNet32(v));
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(uint64_t v) {
|
|
|
|
AppendInternal(base::HostToNet64(v));
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(int16_t v) {
|
|
|
|
AppendInternal(base::HostToNet16(v));
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(int32_t v) {
|
|
|
|
AppendInternal(base::HostToNet32(v));
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(int64_t v) {
|
|
|
|
AppendInternal(base::HostToNet64(v));
|
|
|
|
}
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendNBytes(uint64_t v, size_t num_bytes) {
|
2013-11-14 00:08:54 +00:00
|
|
|
DCHECK_GE(sizeof(v), num_bytes);
|
|
|
|
v = base::HostToNet64(v);
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data = reinterpret_cast<uint8_t*>(&v);
|
2013-11-14 00:08:54 +00:00
|
|
|
AppendArray(&data[sizeof(v) - num_bytes], num_bytes);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendVector(const std::vector<uint8_t>& v) {
|
2013-11-14 00:08:54 +00:00
|
|
|
buf_.insert(buf_.end(), v.begin(), v.end());
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendArray(const uint8_t* buf, size_t size) {
|
2013-11-14 00:08:54 +00:00
|
|
|
buf_.insert(buf_.end(), buf, buf + size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferWriter::AppendBuffer(const BufferWriter& buffer) {
|
|
|
|
buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BufferWriter::WriteToFile(File* file) {
|
|
|
|
DCHECK(file);
|
|
|
|
|
|
|
|
size_t remaining_size = buf_.size();
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* buf = &buf_[0];
|
2013-11-14 00:08:54 +00:00
|
|
|
while (remaining_size > 0) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t size_written = file->Write(buf, remaining_size);
|
2013-11-14 00:08:54 +00:00
|
|
|
if (size_written <= 0) {
|
|
|
|
return Status(error::FILE_FAILURE,
|
|
|
|
"Fail to write to file in BufferWriter");
|
|
|
|
}
|
|
|
|
remaining_size -= size_written;
|
|
|
|
buf += size_written;
|
|
|
|
}
|
|
|
|
buf_.clear();
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void BufferWriter::AppendInternal(T v) {
|
2014-09-30 21:52:21 +00:00
|
|
|
AppendArray(reinterpret_cast<uint8_t*>(&v), sizeof(T));
|
2013-11-14 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|