2018-10-24 13:43:52 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <AK/ByteBuffer.h>
|
2018-10-24 13:43:52 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class BufferStream {
|
|
|
|
public:
|
|
|
|
explicit BufferStream(ByteBuffer& buffer)
|
|
|
|
: m_buffer(buffer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void operator<<(u8 value)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value & 0xffu;
|
|
|
|
}
|
|
|
|
|
2019-02-03 01:13:12 +03:00
|
|
|
void operator<<(char value)
|
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
m_buffer[m_offset++] = (u8)value;
|
2019-02-03 01:13:12 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void operator<<(u16 value)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value & 0xffu;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_buffer[m_offset++] = (u8)(value >> 8) & 0xffu;
|
2018-10-24 13:43:52 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void operator<<(u32 value)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value & 0xffu;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_buffer[m_offset++] = (u8)(value >> 8) & 0xffu;
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 16) & 0xffu;
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 24) & 0xffu;
|
2018-10-24 13:43:52 +03:00
|
|
|
}
|
|
|
|
|
2019-06-02 13:26:28 +03:00
|
|
|
void operator<<(const StringView& value)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
2019-02-26 00:06:55 +03:00
|
|
|
for (ssize_t i = 0; i < value.length(); ++i)
|
2018-10-24 13:43:52 +03:00
|
|
|
m_buffer[m_offset++] = value[i];
|
|
|
|
}
|
|
|
|
|
2019-02-03 01:13:12 +03:00
|
|
|
void operator<<(const ByteBuffer& value)
|
|
|
|
{
|
2019-02-26 00:06:55 +03:00
|
|
|
for (ssize_t i = 0; i < value.size(); ++i)
|
2019-02-03 01:13:12 +03:00
|
|
|
m_buffer[m_offset++] = value[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool at_end() const
|
|
|
|
{
|
|
|
|
return m_offset == m_buffer.size();
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void fill_to_end(u8 ch)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
2019-02-03 01:13:12 +03:00
|
|
|
while (!at_end())
|
2018-10-24 13:43:52 +03:00
|
|
|
m_buffer[m_offset++] = ch;
|
|
|
|
}
|
|
|
|
|
2019-02-26 00:06:55 +03:00
|
|
|
ssize_t offset() const { return m_offset; }
|
2018-10-24 13:43:52 +03:00
|
|
|
|
2019-03-19 18:29:06 +03:00
|
|
|
void snip()
|
|
|
|
{
|
|
|
|
m_buffer.trim(m_offset);
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:43:52 +03:00
|
|
|
private:
|
|
|
|
ByteBuffer& m_buffer;
|
2019-02-26 00:06:55 +03:00
|
|
|
ssize_t m_offset { 0 };
|
2018-10-24 13:43:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::BufferStream;
|