Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #ifndef BYTE_WRITER_H_
6 : #define BYTE_WRITER_H_
7 :
8 : #include "mozilla/EndianUtils.h"
9 : #include "nsTArray.h"
10 :
11 : namespace mp4_demuxer {
12 :
13 : class ByteWriter
14 : {
15 : public:
16 0 : explicit ByteWriter(nsTArray<uint8_t>& aData)
17 0 : : mPtr(aData)
18 : {
19 0 : }
20 0 : ~ByteWriter()
21 0 : {
22 0 : }
23 :
24 : MOZ_MUST_USE bool WriteU8(uint8_t aByte)
25 : {
26 : return Write(&aByte, 1);
27 : }
28 :
29 0 : MOZ_MUST_USE bool WriteU16(uint16_t aShort)
30 : {
31 : uint8_t c[2];
32 0 : mozilla::BigEndian::writeUint16(&c[0], aShort);
33 0 : return Write(&c[0], 2);
34 : }
35 :
36 0 : MOZ_MUST_USE bool WriteU32(uint32_t aLong)
37 : {
38 : uint8_t c[4];
39 0 : mozilla::BigEndian::writeUint32(&c[0], aLong);
40 0 : return Write(&c[0], 4);
41 : }
42 :
43 : MOZ_MUST_USE bool Write32(int32_t aLong)
44 : {
45 : uint8_t c[4];
46 : mozilla::BigEndian::writeInt32(&c[0], aLong);
47 : return Write(&c[0], 4);
48 : }
49 :
50 : MOZ_MUST_USE bool WriteU64(uint64_t aLongLong)
51 : {
52 : uint8_t c[8];
53 : mozilla::BigEndian::writeUint64(&c[0], aLongLong);
54 : return Write(&c[0], 8);
55 : }
56 :
57 : MOZ_MUST_USE bool Write64(int64_t aLongLong)
58 : {
59 : uint8_t c[8];
60 : mozilla::BigEndian::writeInt64(&c[0], aLongLong);
61 : return Write(&c[0], 8);
62 : }
63 :
64 0 : MOZ_MUST_USE bool Write(const uint8_t* aSrc, size_t aCount)
65 : {
66 0 : return mPtr.AppendElements(aSrc, aCount, mozilla::fallible);
67 : }
68 :
69 : private:
70 : nsTArray<uint8_t>& mPtr;
71 : };
72 :
73 : } // namespace mp4_demuxer
74 :
75 : #endif
|