CatapultServer  v0.5.0.1 (Elephant)
BufferedFileStream.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "RawFile.h"
23 #include "Stream.h"
24 #include <vector>
25 
26 namespace catapult { namespace io {
27 
29  constexpr size_t Default_Stream_Buffer_Size = 4096;
30 
32  class BufferedOutputFileStream final : public OutputStream {
33  public:
35  BufferedOutputFileStream(RawFile&& rawFile, size_t bufferSize = Default_Stream_Buffer_Size);
36 
37  public:
38  void write(const RawBuffer& buffer) override;
39 
40  void flush() override;
41 
42  private:
44  std::vector<uint8_t> m_buffer;
45  size_t m_bufferPosition; // position of next write
46  };
47 
49  class BufferedInputFileStream final : public InputStream {
50  public:
52  BufferedInputFileStream(RawFile&& rawFile, size_t bufferSize = Default_Stream_Buffer_Size);
53 
54  public:
55  bool eof() const override;
56 
57  void read(const MutableRawBuffer& buffer) override;
58 
59  private:
61  std::vector<uint8_t> m_buffer;
62  size_t m_bufferPosition; // position of next read
63  // during reading m_numBytesInBuffer mostly will be m_buffer.size() and might be smaller near the end of file
65  };
66 }}
m_buffer
std::vector< uint8_t > & m_buffer
Definition: MemoryBlockStorage.cpp:161
catapult::io::BufferedInputFileStream
Provides a buffered input stream around raw file.
Definition: BufferedFileStream.h:49
exceptions.h
catapult::io::BufferedOutputFileStream
Provides a buffered output stream around raw file.
Definition: BufferedFileStream.h:32
catapult::io::RawFile
Wrapper for low-level i/o operations on files.
Definition: RawFile.h:49
CATAPULT_THROW_AND_LOG_2
#define CATAPULT_THROW_AND_LOG_2(TYPE, MESSAGE, PARAM1, PARAM2)
Macro used to throw a catapult exception with two parameters.
Definition: exceptions.h:159
catapult::io::BufferedInputFileStream::read
void read(const MutableRawBuffer &buffer) override
Definition: BufferedFileStream.cpp:88
catapult::io::BufferedOutputFileStream::write
void write(const RawBuffer &buffer) override
Definition: BufferedFileStream.cpp:38
catapult::io::Default_Stream_Buffer_Size
constexpr size_t Default_Stream_Buffer_Size
Default stream buffer size.
Definition: BufferedFileStream.h:29
catapult::io::BufferedInputFileStream::m_buffer
std::vector< uint8_t > m_buffer
Definition: BufferedFileStream.h:61
m_rawFile
RawFile & m_rawFile
Definition: FileBlockStorage.cpp:193
RawFile.h
catapult::io::BufferedOutputFileStream::m_rawFile
RawFile m_rawFile
Definition: BufferedFileStream.h:43
catapult::io::BufferedInputFileStream::eof
bool eof() const override
Returns true if no data is left in the stream.
Definition: BufferedFileStream.cpp:73
catapult::io::BufferedOutputFileStream::BufferedOutputFileStream
BufferedOutputFileStream(RawFile &&rawFile, size_t bufferSize=Default_Stream_Buffer_Size)
Creates a buffered output stream around rawFile with an optional internal buffer size (bufferSize).
Definition: BufferedFileStream.cpp:27
catapult::io::RawFile::position
uint64_t position() const
Returns position in the file.
Definition: RawFile.cpp:338
catapult::utils::MutableRawBuffer
BasicRawBuffer< uint8_t > MutableRawBuffer
A mutable binary buffer.
Definition: RawBuffer.h:65
catapult::io::BufferedOutputFileStream::m_buffer
std::vector< uint8_t > m_buffer
Definition: BufferedFileStream.h:44
catapult::io::InputStream
Reader interface.
Definition: Stream.h:27
BufferedFileStream.h
catapult::io::BufferedOutputFileStream::m_bufferPosition
size_t m_bufferPosition
Definition: BufferedFileStream.h:45
catapult::io::RawFile::size
uint64_t size() const
Returns size of the file.
Definition: RawFile.cpp:334
catapult::io::BufferedInputFileStream::m_numBytesInBuffer
size_t m_numBytesInBuffer
Definition: BufferedFileStream.h:64
catapult::io::BufferedOutputFileStream::flush
void flush() override
Definition: BufferedFileStream.cpp:33
catapult::io::BufferedInputFileStream::m_rawFile
RawFile m_rawFile
Definition: BufferedFileStream.h:60
catapult::io::BufferedInputFileStream::BufferedInputFileStream
BufferedInputFileStream(RawFile &&rawFile, size_t bufferSize=Default_Stream_Buffer_Size)
Creates a buffered input stream around rawFile with an optional internal buffer size (bufferSize).
Definition: BufferedFileStream.cpp:66
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::catapult_error
Base class for all catapult exceptions that derives from both std::exception and boost::exception.
Definition: exceptions.h:42
Logging.h
catapult::io::RawFile::write
void write(const RawBuffer &dataBuffer)
Definition: RawFile.cpp:305
Stream.h
catapult::io::RawFile::read
void read(const MutableRawBuffer &dataBuffer)
Definition: RawFile.cpp:313
catapult::io::BufferedInputFileStream::m_bufferPosition
size_t m_bufferPosition
Definition: BufferedFileStream.h:62
catapult::io::OutputStream
Writer interface.
Definition: Stream.h:41
catapult::utils::RawBuffer
BasicRawBuffer< const uint8_t > RawBuffer
A const binary buffer.
Definition: RawBuffer.h:62