zipios++  2.0.2
Zipios++ – a small C++ library that provides easy access to .zip files.
Public Member Functions | Private Attributes | List of all members
zipios::BackBuffer Class Reference

To read a file by chunk from the end. More...

#include <backbuffer.hpp>

Inheritance diagram for zipios::BackBuffer:
Inheritance graph
[legend]
Collaboration diagram for zipios::BackBuffer:
Collaboration graph
[legend]

Public Member Functions

 BackBuffer (std::istream &is, VirtualSeeker const &vs=VirtualSeeker(), ssize_t const chunk_size=1024)
 
ssize_t readChunk (ssize_t &read_pointer)
 Read a chunk of data. More...
 

Private Attributes

ssize_t m_chunk_size
 
std::streampos m_file_pos
 
std::istream & m_is
 
VirtualSeeker m_vs
 

Detailed Description

A BackBuffer instance is useful for reading the last part of a file in an efficient manner, when it is not known exactly how far back (towards the front!) to go, to find the start of the desired data block.

BackBuffer is an std::vector<unsigned char> that fills itself with data from a file by reading chunks from the end of the file progressing towards the start. Upon construction the BackBuffer instance is associated with a file and a chunksize can be specified. To read a chunk of the file into the BackBuffer call readChunk().

Note that this is not a good option for really large files as the buffer is enlarged using an insert() on each call to readChunk().

Definition at line 41 of file backbuffer.hpp.

Constructor & Destructor Documentation

zipios::BackBuffer::BackBuffer ( std::istream &  is,
VirtualSeeker const &  vs = VirtualSeeker(),
ssize_t const  chunk_size = 1024 
)

BackBuffer constructor.

Initialize a buffer that reads data backward.

The content of the buffer is defined as the content of the is stream. The stream must be open and seekable. The file pointer of the stream is modified by this constructor and on each call to readChunk().

Exceptions
IOExceptionThis exception is raised when the VirtualSeeker (vs) returns an invalid answer determining the size of the stream.
Parameters
[in,out]isThe istream to read the data from. The stream must be seekable, as BackBuffer will reposition the file pointer to read chunks from the back of the file.
[in]vsThe virtual seeker used to change the file pointer.
[in]chunk_sizeSpecifies the size of the chunks to read the file into the BackBuffer in.

Definition at line 79 of file backbuffer.cpp.

References m_chunk_size, m_file_pos, m_is, m_vs, zipios::VirtualSeeker::vseekg(), and zipios::VirtualSeeker::vtellg().

Member Function Documentation

ssize_t zipios::BackBuffer::readChunk ( ssize_t &  read_pointer)

Reads another chunk and returns the size of the chunk that has been read. Returns 0 on I/O failure.

When a new chunk is read in the already stored bytes change position in the BackBuffer. read_pointer is assumed by readChunk() to be a pointer into a position in the BackBuffer, and is updated to point to the same position in the file as it pointed to before the new chunk was prepended.

When first calling readChunk(), read_pointer is expected to be zero and represent the position of EOF.

Warning
The file is read backward.
The function may change m_chunk_size. In the end will be zero and no more data can be read.
The function changes the file pointer of the attached stream.
Bug:
We may want to throw an error if m_is.is_good() returns false.
Parameters
[in,out]read_pointerThe buffer pointer.
Returns
The number of bytes read if any, otherwise zero. Note that if an error occurs, the function also returns zero.

Definition at line 140 of file backbuffer.cpp.

References m_chunk_size, m_file_pos, m_is, m_vs, zipios::VirtualSeeker::vseekg(), and zipios::zipRead().

Referenced by zipios::ZipFile::ZipFile().

Member Data Documentation

ssize_t zipios::BackBuffer::m_chunk_size
private

Definition at line 50 of file backbuffer.hpp.

Referenced by BackBuffer(), and readChunk().

std::streampos zipios::BackBuffer::m_file_pos
private

Definition at line 52 of file backbuffer.hpp.

Referenced by BackBuffer(), and readChunk().

std::istream& zipios::BackBuffer::m_is
private

Definition at line 51 of file backbuffer.hpp.

Referenced by BackBuffer(), and readChunk().

VirtualSeeker zipios::BackBuffer::m_vs
private

Definition at line 49 of file backbuffer.hpp.

Referenced by BackBuffer(), and readChunk().


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