buffered_io.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "buffered_io.h"
  2. i64 IBinaryStream::LongWrite(const void* userBuffer, i64 size) {
  3. Y_ABORT_UNLESS(size >= 0, "IBinaryStream::Write() called with a negative buffer size.");
  4. i64 leftToWrite = size;
  5. while (leftToWrite != 0) {
  6. int writeSz = static_cast<int>(Min<i64>(leftToWrite, std::numeric_limits<int>::max()));
  7. int written = WriteImpl(userBuffer, writeSz);
  8. Y_ASSERT(written <= writeSz);
  9. leftToWrite -= written;
  10. // Assumption: if WriteImpl(buf, writeSz) returns < writeSz, the stream is
  11. // full and there's no sense in continuing.
  12. if (written < writeSz)
  13. break;
  14. }
  15. Y_ASSERT(size >= leftToWrite);
  16. return size - leftToWrite;
  17. }
  18. i64 IBinaryStream::LongRead(void* userBuffer, i64 size) {
  19. Y_ABORT_UNLESS(size >= 0, "IBinaryStream::Read() called with a negative buffer size.");
  20. i64 leftToRead = size;
  21. while (leftToRead != 0) {
  22. int readSz = static_cast<int>(Min<i64>(leftToRead, std::numeric_limits<int>::max()));
  23. int read = ReadImpl(userBuffer, readSz);
  24. Y_ASSERT(read <= readSz);
  25. leftToRead -= read;
  26. // Assumption: if ReadImpl(buf, readSz) returns < readSz, the stream is
  27. // full and there's no sense in continuing.
  28. if (read < readSz) {
  29. memset(static_cast<char*>(userBuffer) + (size - leftToRead), 0, leftToRead);
  30. break;
  31. }
  32. }
  33. Y_ASSERT(size >= leftToRead);
  34. return size - leftToRead;
  35. }