1
#pragma once
2

            
3
#include <cstdint>
4
#include <string>
5

            
6
#include "envoy/buffer/buffer.h"
7

            
8
#include "source/common/protobuf/protobuf.h"
9

            
10
namespace Envoy {
11

            
12
namespace Buffer {
13

            
14
class ZeroCopyInputStreamImpl : public virtual Protobuf::io::ZeroCopyInputStream {
15
public:
16
  // Create input stream with one buffer, and finish immediately
17
  ZeroCopyInputStreamImpl(Buffer::InstancePtr&& buffer);
18

            
19
  // Create input stream with empty buffer
20
  ZeroCopyInputStreamImpl();
21

            
22
  // Add a buffer to input stream, will consume all buffer from parameter
23
  // if the stream is not finished
24
  void move(Buffer::Instance& instance);
25

            
26
  // Mark the stream is finished
27
140814
  void finish() { finished_ = true; }
28

            
29
  // Protobuf::io::ZeroCopyInputStream
30
  // See
31
  // https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyInputStream
32
  // for each method details.
33

            
34
  // Note Next() will return true with no data until more data is available if the stream is not
35
  // finished. It is the caller's responsibility to finish the stream or wrap with
36
  // LimitingInputStream before passing to protobuf code to avoid a spin loop.
37
  bool Next(const void** data, int* size) override;
38
  void BackUp(int count) override;
39
  bool Skip(int count) override;
40
190
  ProtobufTypes::Int64 ByteCount() const override { return byte_count_; }
41

            
42
protected:
43
  // The last slice is kept to support limited BackUp() calls.
44
  // This function will drain it.
45
  void drainLastSlice();
46

            
47
  Buffer::InstancePtr buffer_;
48
  uint64_t position_{0};
49
  bool finished_{false};
50

            
51
private:
52
  uint64_t byte_count_{0};
53
};
54

            
55
} // namespace Buffer
56
} // namespace Envoy