CatapultServer  v0.5.0.1 (Elephant)
BufferInputStreamAdapter.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Stream.h"
23 #include "catapult/exceptions.h"
24 #include <sstream>
25 
26 namespace catapult { namespace io {
27 
29  template<typename TContainer>
31  public:
33  explicit BufferInputStreamAdapter(const TContainer& input)
34  : m_input(input)
35  , m_position(0)
36  {}
37 
38  public:
40  size_t position() const {
41  return m_position;
42  }
43 
44  public:
45  bool eof() const override {
46  RawBuffer input(m_input);
47  return m_position == input.Size;
48  }
49 
50  void read(const MutableRawBuffer& buffer) override {
51  RawBuffer input(m_input);
52  if (buffer.Size + m_position > input.Size) {
53  std::ostringstream out;
54  out
55  << "BufferInputStreamAdapter invalid read (buffer-size = " << buffer.Size
56  << ", position = " << m_position
57  << ", input-size = " << input.Size << ")";
58  CATAPULT_THROW_FILE_IO_ERROR(out.str().c_str());
59  }
60 
61  std::memcpy(buffer.pData, input.pData + m_position, buffer.Size);
62  m_position += buffer.Size;
63  }
64 
65  private:
66  const TContainer& m_input;
67  size_t m_position;
68  };
69 }}
exceptions.h
catapult::io::BufferInputStreamAdapter::position
size_t position() const
Returns read position.
Definition: BufferInputStreamAdapter.h:40
CATAPULT_THROW_FILE_IO_ERROR
#define CATAPULT_THROW_FILE_IO_ERROR(MESSAGE)
Macro used to throw a catapult file io error.
Definition: exceptions.h:195
catapult::io::BufferInputStreamAdapter::m_position
size_t m_position
Definition: BufferInputStreamAdapter.h:67
catapult::utils::MutableRawBuffer
BasicRawBuffer< uint8_t > MutableRawBuffer
A mutable binary buffer.
Definition: RawBuffer.h:65
catapult::io::InputStream
Reader interface.
Definition: Stream.h:27
catapult::io::BufferInputStreamAdapter::BufferInputStreamAdapter
BufferInputStreamAdapter(const TContainer &input)
Creates an input stream around input.
Definition: BufferInputStreamAdapter.h:33
catapult::io::BufferInputStreamAdapter
Adapt a typed buffer to be used as an input stream.
Definition: BufferInputStreamAdapter.h:30
catapult::io::BufferInputStreamAdapter::m_input
const TContainer & m_input
Definition: BufferInputStreamAdapter.h:66
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::io::BufferInputStreamAdapter::read
void read(const MutableRawBuffer &buffer) override
Definition: BufferInputStreamAdapter.h:50
Stream.h
catapult::utils::RawBuffer
BasicRawBuffer< const uint8_t > RawBuffer
A const binary buffer.
Definition: RawBuffer.h:62
catapult::io::BufferInputStreamAdapter::eof
bool eof() const override
Returns true if no data is left in the stream.
Definition: BufferInputStreamAdapter.h:45