Line data Source code
1 : #include "source/common/buffer/zero_copy_input_stream_impl.h" 2 : 3 : #include "source/common/buffer/buffer_impl.h" 4 : #include "source/common/common/assert.h" 5 : 6 : namespace Envoy { 7 : namespace Buffer { 8 : 9 18 : ZeroCopyInputStreamImpl::ZeroCopyInputStreamImpl() : buffer_(new Buffer::OwnedImpl) {} 10 : 11 : ZeroCopyInputStreamImpl::ZeroCopyInputStreamImpl(Buffer::InstancePtr&& buffer) 12 600 : : buffer_(std::move(buffer)) { 13 600 : finish(); 14 600 : } 15 : 16 6 : void ZeroCopyInputStreamImpl::move(Buffer::Instance& instance) { 17 6 : ASSERT(!finished_); 18 : 19 6 : buffer_->move(instance); 20 6 : } 21 : 22 1211 : void ZeroCopyInputStreamImpl::drainLastSlice() { 23 1211 : if (position_ != 0) { 24 611 : buffer_->drain(position_); 25 611 : position_ = 0; 26 611 : } 27 1211 : } 28 : 29 1211 : bool ZeroCopyInputStreamImpl::Next(const void** data, int* size) { 30 1211 : drainLastSlice(); 31 : 32 1211 : Buffer::RawSlice slice = buffer_->frontSlice(); 33 : 34 1211 : if (slice.len_ > 0) { 35 611 : *data = slice.mem_; 36 611 : *size = slice.len_; 37 611 : position_ = slice.len_; 38 611 : byte_count_ += slice.len_; 39 611 : return true; 40 611 : } 41 : 42 600 : if (!finished_) { 43 0 : *data = nullptr; 44 0 : *size = 0; 45 0 : return true; 46 0 : } 47 600 : return false; 48 600 : } 49 : 50 0 : bool ZeroCopyInputStreamImpl::Skip(int count) { 51 0 : ASSERT(count >= 0); 52 0 : drainLastSlice(); 53 : 54 : // Could not skip more than buffer length. 55 0 : if (static_cast<uint64_t>(count) > buffer_->length()) { 56 0 : return false; 57 0 : } 58 : 59 0 : buffer_->drain(count); 60 0 : byte_count_ += count; 61 0 : return true; 62 0 : } 63 : 64 0 : void ZeroCopyInputStreamImpl::BackUp(int count) { 65 0 : ASSERT(count >= 0); 66 0 : ASSERT(uint64_t(count) <= position_); 67 : 68 : // Preconditions for BackUp: 69 : // - The last method called must have been Next(). 70 : // - count must be less than or equal to the size of the last buffer returned by Next(). 71 : // Due to preconditions above, it is safe to just adjust position_ and byte_count_ here, and 72 : // drain in Next(). 73 0 : position_ -= count; 74 0 : byte_count_ -= count; 75 0 : } 76 : } // namespace Buffer 77 : } // namespace Envoy