summaryrefslogtreecommitdiff
path: root/AK/BufferStream.h
blob: 5316e3756189bad2c51d48f0617d7ea9b99e6073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once

#include "ByteBuffer.h"

namespace AK {

class BufferStream {
public:
    explicit BufferStream(ByteBuffer& buffer)
        : m_buffer(buffer)
    {
    }

    void operator<<(byte value)
    {
        m_buffer[m_offset++] = value & 0xffu;
    }

    void operator<<(word value)
    {
        m_buffer[m_offset++] = value & 0xffu;
        m_buffer[m_offset++] = (byte)(value >> 8) & 0xffu;
    }

    void operator<<(dword value)
    {
        m_buffer[m_offset++] = value & 0xffu;
        m_buffer[m_offset++] = (byte)(value >> 8) & 0xffu;
        m_buffer[m_offset++] = (byte)(value >> 16) & 0xffu;
        m_buffer[m_offset++] = (byte)(value >> 24) & 0xffu;
    }

    void operator<<(const char* str)
    {
        size_t len = strlen(str);
        for (unsigned i = 0; i < len; ++i)
            m_buffer[m_offset++] = str[i];
    }

    void operator<<(const String& value)
    {
        for (unsigned i = 0; i < value.length(); ++i)
            m_buffer[m_offset++] = value[i];
    }

    void fillToEnd(byte ch)
    {
        while (m_offset < m_buffer.size())
            m_buffer[m_offset++] = ch;
    }

    size_t offset() const { return m_offset; }

private:
    ByteBuffer& m_buffer;
    size_t m_offset { 0 };
};

}

using AK::BufferStream;