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
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_BUFFER_WRITER_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_BUFFER_WRITER_H_
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2023-11-30 06:51:37 +00:00
|
|
|
#include <cstdint>
|
2013-11-14 00:08:54 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-01-29 21:23:12 +00:00
|
|
|
#include "packager/base/macros.h"
|
2017-06-29 22:23:53 +00:00
|
|
|
#include "packager/status.h"
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
class File;
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// A simple buffer writer implementation which appends various data types to
|
|
|
|
/// buffer.
|
2013-11-14 00:08:54 +00:00
|
|
|
class BufferWriter {
|
|
|
|
public:
|
|
|
|
BufferWriter();
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Construct the object with a reserved capacity.
|
|
|
|
/// @param reserved_size_in_bytes is intended for optimization and is
|
|
|
|
/// not a hard limit. It does not affect the actual size of the buffer,
|
|
|
|
/// which still starts from zero.
|
2013-11-14 00:08:54 +00:00
|
|
|
explicit BufferWriter(size_t reserved_size_in_bytes);
|
|
|
|
~BufferWriter();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// These convenience functions append the integers (in network byte order,
|
|
|
|
/// i.e. big endian) of various size and signedness to the end of the buffer.
|
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
void AppendInt(uint8_t v);
|
|
|
|
void AppendInt(uint16_t v);
|
|
|
|
void AppendInt(uint32_t v);
|
|
|
|
void AppendInt(uint64_t v);
|
|
|
|
void AppendInt(int16_t v);
|
|
|
|
void AppendInt(int32_t v);
|
|
|
|
void AppendInt(int64_t v);
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Append the least significant @a num_bytes of @a v to buffer.
|
|
|
|
/// @param num_bytes should not be larger than sizeof(@a v), i.e. 8 on a
|
|
|
|
/// 64-bit system.
|
2014-09-30 21:52:21 +00:00
|
|
|
void AppendNBytes(uint64_t v, size_t num_bytes);
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void AppendVector(const std::vector<uint8_t>& v);
|
2018-05-07 22:54:59 +00:00
|
|
|
void AppendString(const std::string& s);
|
2014-09-30 21:52:21 +00:00
|
|
|
void AppendArray(const uint8_t* buf, size_t size);
|
2013-11-14 00:08:54 +00:00
|
|
|
void AppendBuffer(const BufferWriter& buffer);
|
|
|
|
|
|
|
|
void Swap(BufferWriter* buffer) { buf_.swap(buffer->buf_); }
|
2014-09-30 21:52:21 +00:00
|
|
|
void SwapBuffer(std::vector<uint8_t>* buffer) { buf_.swap(*buffer); }
|
2014-04-18 01:57:31 +00:00
|
|
|
|
2013-11-14 00:08:54 +00:00
|
|
|
void Clear() { buf_.clear(); }
|
|
|
|
size_t Size() const { return buf_.size(); }
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return Underlying buffer. Behavior is undefined if the buffer size is 0.
|
2016-01-29 21:23:12 +00:00
|
|
|
const uint8_t* Buffer() const { return buf_.data(); }
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Write the buffer to file. The internal buffer will be cleared after
|
|
|
|
/// writing.
|
|
|
|
/// @param file should not be NULL.
|
|
|
|
/// @return OK on success.
|
2013-11-14 00:08:54 +00:00
|
|
|
Status WriteToFile(File* file);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Internal implementation of multi-byte write.
|
|
|
|
template <typename T>
|
|
|
|
void AppendInternal(T v);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> buf_;
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BufferWriter);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_BUFFER_WRITER_H_
|