#include "dl_stream_input.h" #include #include #include "io/dl_log.h" #include "memory/dl_buffer.h" #include "io/dl_file.h" #include "memory/dl_memory.h" namespace dl { StreamInput::StreamInput() : _head{0} { } StreamInput::~StreamInput() { } const std::byte* StreamInput::GetBuffer() { return _buf.data(); } bool StreamInput::Read(void* data, size_t byte_count) { size_t result_head = _head + byte_count; if (result_head > _buf.size()) { log_warn0("Read超出了buffer大小!"); _head = _buf.size(); return false; } memcpy(data, _buf.data() + _head, byte_count); _head = result_head; return true; } bool StreamInput::LoadFromFile(std::string_view path_name, bool decompress, size_t length) { std::ifstream ifs = File::Get(path_name); if (!ifs.is_open()) { return false; } //有可能已经加载过了 _buf.clear(); _head = 0; if (decompress || length == 0) { ifs.seekg(0, std::ios::end); _buf.resize((size_t)ifs.tellg()); } else { ifs.seekg(length, std::ios::beg); _buf.resize(length); } ifs.seekg(0); ifs.read((char*)_buf.data(), _buf.size()); ifs.close(); if (decompress) { BufferView zip { _buf }; _buf = Memory::DecompressLz4(zip); } return true; } void StreamInput::LoadFromMemory(BufferView buf, bool decompress) { //有可能已经加载过了 _buf.clear(); _head = 0; if (decompress) { _buf = Memory::DecompressLz4(buf); } else { _buf = CreateBuffer(buf); } } void StreamInput::SetPos(size_t pos) { if (pos > _buf.size()) { log_err("SetPos超出了buffer大小!", 10); return; } _head = pos; } bool StreamInput::IsEnd() { assert(_head <= _buf.size()); return _head == _buf.size(); } }