Bitcoin Core 28.99.0
P2P Digital Currency
Public Member Functions | Private Member Functions | Private Attributes | List of all members
BufferedFile Class Reference

Wrapper around an AutoFile& that implements a ring buffer to deserialize from. More...

#include <streams.h>

Collaboration diagram for BufferedFile:
[legend]

Public Member Functions

 BufferedFile (AutoFile &file, uint64_t nBufSize, uint64_t nRewindIn)
 
bool eof () const
 check whether we're at the end of the source file More...
 
void read (Span< std::byte > dst)
 read a number of bytes More...
 
void SkipTo (const uint64_t file_pos)
 Move the read position ahead in the stream to the given position. More...
 
uint64_t GetPos () const
 return the current reading position More...
 
bool SetPos (uint64_t nPos)
 rewind to a given reading position More...
 
bool SetLimit (uint64_t nPos=std::numeric_limits< uint64_t >::max())
 prevent reading beyond a certain position no argument removes the limit More...
 
template<typename T >
BufferedFileoperator>> (T &&obj)
 
void FindByte (std::byte byte)
 search for a given byte in the stream, and remain positioned on it More...
 

Private Member Functions

bool Fill ()
 read data from the source to fill the buffer More...
 
std::pair< std::byte *, size_t > AdvanceStream (size_t length)
 Advance the stream's read pointer (m_read_pos) by up to 'length' bytes, filling the buffer from the file so that at least one byte is available. More...
 

Private Attributes

AutoFilem_src
 
uint64_t nSrcPos {0}
 how many bytes have been read from source More...
 
uint64_t m_read_pos {0}
 how many bytes have been read from this More...
 
uint64_t nReadLimit
 up to which position we're allowed to read More...
 
uint64_t nRewind
 how many bytes we guarantee to rewind More...
 
std::vector< std::byte > vchBuf
 the buffer More...
 

Detailed Description

Wrapper around an AutoFile& that implements a ring buffer to deserialize from.

It guarantees the ability to rewind a given number of bytes.

Will automatically close the file when it goes out of scope if not null. If you need to close the file early, use file.fclose() instead of fclose(file).

Definition at line 476 of file streams.h.

Constructor & Destructor Documentation

◆ BufferedFile()

BufferedFile::BufferedFile ( AutoFile file,
uint64_t  nBufSize,
uint64_t  nRewindIn 
)
inline

Definition at line 526 of file streams.h.

Member Function Documentation

◆ AdvanceStream()

std::pair< std::byte *, size_t > BufferedFile::AdvanceStream ( size_t  length)
inlineprivate

Advance the stream's read pointer (m_read_pos) by up to 'length' bytes, filling the buffer from the file so that at least one byte is available.

Return a pointer to the available buffer data and the number of bytes (which may be less than the requested length) that may be accessed beginning at that pointer.

Definition at line 508 of file streams.h.

Here is the call graph for this function:

◆ eof()

bool BufferedFile::eof ( ) const
inline

check whether we're at the end of the source file

Definition at line 534 of file streams.h.

Here is the call graph for this function:

◆ Fill()

bool BufferedFile::Fill ( )
inlineprivate

read data from the source to fill the buffer

Definition at line 487 of file streams.h.

Here is the call graph for this function:

◆ FindByte()

void BufferedFile::FindByte ( std::byte  byte)
inline

search for a given byte in the stream, and remain positioned on it

Definition at line 594 of file streams.h.

◆ GetPos()

uint64_t BufferedFile::GetPos ( ) const
inline

return the current reading position

Definition at line 557 of file streams.h.

◆ operator>>()

template<typename T >
BufferedFile & BufferedFile::operator>> ( T &&  obj)
inline

Definition at line 588 of file streams.h.

Here is the call graph for this function:

◆ read()

void BufferedFile::read ( Span< std::byte >  dst)
inline

read a number of bytes

Definition at line 539 of file streams.h.

Here is the call graph for this function:

◆ SetLimit()

bool BufferedFile::SetLimit ( uint64_t  nPos = std::numeric_limits<uint64_t>::max())
inline

prevent reading beyond a certain position no argument removes the limit

Definition at line 580 of file streams.h.

◆ SetPos()

bool BufferedFile::SetPos ( uint64_t  nPos)
inline

rewind to a given reading position

Definition at line 562 of file streams.h.

◆ SkipTo()

void BufferedFile::SkipTo ( const uint64_t  file_pos)
inline

Move the read position ahead in the stream to the given position.

Use SetPos() to back up in the stream, not SkipTo().

Definition at line 550 of file streams.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_read_pos

uint64_t BufferedFile::m_read_pos {0}
private

how many bytes have been read from this

Definition at line 481 of file streams.h.

◆ m_src

AutoFile& BufferedFile::m_src
private

Definition at line 479 of file streams.h.

◆ nReadLimit

uint64_t BufferedFile::nReadLimit
private

up to which position we're allowed to read

Definition at line 482 of file streams.h.

◆ nRewind

uint64_t BufferedFile::nRewind
private

how many bytes we guarantee to rewind

Definition at line 483 of file streams.h.

◆ nSrcPos

uint64_t BufferedFile::nSrcPos {0}
private

how many bytes have been read from source

Definition at line 480 of file streams.h.

◆ vchBuf

std::vector<std::byte> BufferedFile::vchBuf
private

the buffer

Definition at line 484 of file streams.h.


The documentation for this class was generated from the following file: