stream.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include <library/cpp/yson_pull/detail/macros.h>
  3. #include <library/cpp/yson_pull/input.h>
  4. #include <util/stream/buffered.h>
  5. #include <util/stream/file.h>
  6. #include <util/stream/zerocopy.h>
  7. #include <util/system/file.h>
  8. namespace NYsonPull {
  9. namespace NDetail {
  10. namespace NInput {
  11. class TStreamBase: public NYsonPull::NInput::IStream {
  12. protected:
  13. result DoFillBufferFrom(IZeroCopyInput& input) {
  14. void* ptr = nullptr;
  15. size_t size = input.Next(&ptr);
  16. if (Y_UNLIKELY(size == 0)) {
  17. return result::at_end;
  18. }
  19. buffer().reset(static_cast<ui8*>(ptr), static_cast<ui8*>(ptr) + size);
  20. return result::have_more_data;
  21. }
  22. };
  23. class TZeroCopy: public TStreamBase {
  24. IZeroCopyInput* Input;
  25. public:
  26. explicit TZeroCopy(IZeroCopyInput* input)
  27. : Input(input)
  28. {
  29. }
  30. protected:
  31. result do_fill_buffer() override {
  32. return DoFillBufferFrom(*Input);
  33. }
  34. };
  35. template <typename TBuffered>
  36. class TOwned: public TStreamBase {
  37. TBuffered Input;
  38. public:
  39. template <typename... Args>
  40. explicit TOwned(Args&&... args)
  41. : Input(std::forward<Args>(args)...)
  42. {
  43. }
  44. protected:
  45. result do_fill_buffer() override {
  46. return DoFillBufferFrom(Input);
  47. }
  48. };
  49. class TFHandle: public TOwned<TFileInput> {
  50. public:
  51. TFHandle(int fd, size_t buffer_size)
  52. : TOwned<TFileInput>(Duplicate(fd), buffer_size)
  53. {
  54. }
  55. };
  56. }
  57. } // namespace NDetail
  58. }