diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/binsaver/buffered_io.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/binsaver/buffered_io.cpp')
-rw-r--r-- | library/cpp/binsaver/buffered_io.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/library/cpp/binsaver/buffered_io.cpp b/library/cpp/binsaver/buffered_io.cpp new file mode 100644 index 00000000000..dd88b04bc5d --- /dev/null +++ b/library/cpp/binsaver/buffered_io.cpp @@ -0,0 +1,39 @@ +#include "buffered_io.h" + +i64 IBinaryStream::LongWrite(const void* userBuffer, i64 size) { + Y_VERIFY(size >= 0, "IBinaryStream::Write() called with a negative buffer size."); + + i64 leftToWrite = size; + while (leftToWrite != 0) { + int writeSz = static_cast<int>(Min<i64>(leftToWrite, std::numeric_limits<int>::max())); + int written = WriteImpl(userBuffer, writeSz); + Y_ASSERT(written <= writeSz); + leftToWrite -= written; + // Assumption: if WriteImpl(buf, writeSz) returns < writeSz, the stream is + // full and there's no sense in continuing. + if (written < writeSz) + break; + } + Y_ASSERT(size >= leftToWrite); + return size - leftToWrite; +} + +i64 IBinaryStream::LongRead(void* userBuffer, i64 size) { + Y_VERIFY(size >= 0, "IBinaryStream::Read() called with a negative buffer size."); + + i64 leftToRead = size; + while (leftToRead != 0) { + int readSz = static_cast<int>(Min<i64>(leftToRead, std::numeric_limits<int>::max())); + int read = ReadImpl(userBuffer, readSz); + Y_ASSERT(read <= readSz); + leftToRead -= read; + // Assumption: if ReadImpl(buf, readSz) returns < readSz, the stream is + // full and there's no sense in continuing. + if (read < readSz) { + memset(static_cast<char*>(userBuffer) + (size - leftToRead), 0, leftToRead); + break; + } + } + Y_ASSERT(size >= leftToRead); + return size - leftToRead; +} |