input.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include "buffer.h"
  3. #include <util/generic/ptr.h>
  4. #include <util/generic/strbuf.h>
  5. #include <util/system/types.h>
  6. #include <util/system/yassert.h>
  7. #include <cstddef>
  8. #include <memory>
  9. class IInputStream;
  10. class IZeroCopyInput;
  11. namespace NYsonPull {
  12. namespace NInput {
  13. //! \brief Input stream adaptor interface.
  14. //!
  15. //! Represents a model of a chunked input data stream.
  16. class IStream {
  17. input_buffer buffer_;
  18. bool at_end_ = false;
  19. public:
  20. virtual ~IStream() = default;
  21. bool at_end() const {
  22. return at_end_;
  23. }
  24. input_buffer& buffer() noexcept {
  25. return buffer_;
  26. }
  27. const input_buffer& buffer() const noexcept {
  28. return buffer_;
  29. }
  30. void fill_buffer() {
  31. while (buffer_.is_empty() && !at_end()) {
  32. at_end_ = do_fill_buffer() == result::at_end;
  33. }
  34. }
  35. protected:
  36. enum class result {
  37. have_more_data, //! May continue reading
  38. at_end, //! Reached end of stream
  39. };
  40. //! \brief Read next chunk of data.
  41. //!
  42. //! The implementation is to discard the buffer contents
  43. //! and reset the buffer to a next chunk of data.
  44. //! End-of-stream condition is to be reported via return value.
  45. //!
  46. //! Read is assumed to always succeed unless it throws an exception.
  47. virtual result do_fill_buffer() = 0;
  48. };
  49. //! \brief Read data from a contiguous memory block (i.e. a string)
  50. //!
  51. //! Does not take ownership on memory.
  52. THolder<IStream> FromMemory(TStringBuf data);
  53. //! \brief Read data from C FILE* object.
  54. //!
  55. //! Does not take ownership on file object.
  56. //! Data is buffered internally regardless of file buffering.
  57. THolder<IStream> FromStdioFile(FILE* file, size_t buffer_size = 65536);
  58. //! \brief Read data from POSIX file descriptor.
  59. //!
  60. //! Does not take ownership on streambuf.
  61. THolder<IStream> FromPosixFd(int fd, size_t buffer_size = 65536);
  62. THolder<IStream> FromZeroCopyInput(IZeroCopyInput* input);
  63. THolder<IStream> FromInputStream(IInputStream* input, size_t buffer_size = 65536);
  64. }
  65. }