Add StreamBuffer::open

This commit is contained in:
2024-09-04 23:14:39 -04:00
parent 43a02db7f3
commit 2d8015c2ae
2 changed files with 22 additions and 12 deletions

View File

@@ -349,6 +349,14 @@ public:
init(true, false, const_cast<char *>(data.data()), data.size());
write_cursor_ = buf_hi_;
}
// Modify an existing streambuffer to read from an external block of bytes.
//
void open(std::string_view data) {
if (owned_ && (buf_lo_ != 0)) CoreHandler::basebuffer_free(buf_lo_);
init(true, false, const_cast<char *>(data.data()), data.size());
write_cursor_ = buf_hi_;
}
// Destructor. Frees the buffer, if any.
//
@@ -420,19 +428,21 @@ public:
}
// Discard all data. Reset total read and write counts.
// Releases the allocated buffer, if any.
// May release the allocated buffer, if it is large.
//
void clear() {
assert(owned_);
if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) {
CoreHandler::basebuffer_free(buf_lo_);
buf_lo_ = nullptr;
buf_hi_ = nullptr;
if (!owned_) {
open("");
} else {
if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) {
CoreHandler::basebuffer_free(buf_lo_);
buf_lo_ = nullptr;
buf_hi_ = nullptr;
}
read_cursor_ = buf_lo_;
write_cursor_ = buf_lo_;
pre_read_count_ = 0;
}
owned_ = true;
read_cursor_ = buf_lo_;
write_cursor_ = buf_lo_;
pre_read_count_ = 0;
}
// Write block of bytes into the buffer.