nidas v1.2.3
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
nidas::core::IOStream Class Reference

A base class for buffering data. More...

#include <IOStream.h>

Public Member Functions

 IOStream (IOChannel &input, size_t buflen=8192)
 Create IOStream.
 
virtual ~IOStream ()
 
size_t available () const
 Number of bytes available to be copied from the buffer of IOStream.
 
bool isNewInput () const
 Did last read(), or read(buf,len) call result in a new file being opened?
 
size_t read ()
 Do an IOChannel::read into the internal buffer of IOStream.
 
size_t readBuf (void *buf, size_t len) throw ()
 Copy available bytes from the internal buffer to buf, returning the number of bytes copied, which may be less then len.
 
size_t read (void *buf, size_t len)
 Read len bytes of data into buf.
 
size_t skip (size_t len)
 If the internal buffer is empty, do an IOChannel::read into the buffer.
 
size_t backup (size_t len) throw ()
 Move the read buffer pointer backwards by len number of bytes, so that the next readBuf will return data that was previously read.
 
size_t backup () throw ()
 Move the read buffer pointer backwards to the beginning of the buffer.
 
size_t readUntil (void *buf, size_t len, char term)
 Read into the user buffer until a terminating character is found or len-1 bytes have been read.
 
size_t write (const struct iovec *iov, int nbufs, bool flush)
 Write data.
 
size_t write (const void *buf, size_t len, bool flush)
 
void flush ()
 Flush buffer to physical device.
 
dsm_time_t createFile (dsm_time_t t, bool exact)
 Request that IOChannel object open a new file, with a name based on a time.
 
const std::string & getName () const
 
long long getNumInputBytes () const
 Number of bytes read with this IOStream.
 
void addNumInputBytes (int val)
 
long long getNumOutputBytes () const
 Total number of bytes written with this IOStream.
 
void addNumOutputBytes (int val)
 

Protected Member Functions

void reallocateBuffer (size_t len)
 

Protected Attributes

IOChannel_iochannel
 

Private Member Functions

 IOStream (const IOStream &)
 No copying.
 
IOStreamoperator= (const IOStream &)
 No assignment.
 

Private Attributes

char_buffer
 data buffer
 
char_head
 where we insert bytes into the buffer
 
char_tail
 where we remove bytes from the buffer
 
size_t _buflen
 The actual buffer size.
 
size_t _halflen
 
char_eob
 One past end of buffer.
 
bool _newInput
 Was the previous read performed on a newly opened file?
 
long long _nbytesIn
 
long long _nbytesOut
 
size_t _nEAGAIN
 

Detailed Description

A base class for buffering data.

Constructor & Destructor Documentation

◆ IOStream() [1/2]

IOStream::IOStream ( IOChannel & input,
size_t buflen = 8192 )

Create IOStream.

Parameters
inputreference to an Input object providing the physical IO
buflenlength of buffer, in bytes.

References reallocateBuffer().

◆ ~IOStream()

IOStream::~IOStream ( )
virtual

References _buffer.

◆ IOStream() [2/2]

nidas::core::IOStream::IOStream ( const IOStream & )
private

No copying.

Member Function Documentation

◆ addNumInputBytes()

void nidas::core::IOStream::addNumInputBytes ( int val)
inline

References _nbytesIn, and nidas::core::getSample().

Referenced by readBuf().

◆ addNumOutputBytes()

void nidas::core::IOStream::addNumOutputBytes ( int val)
inline

References _nbytesOut, and nidas::core::getSample().

Referenced by flush(), and write().

◆ available()

size_t nidas::core::IOStream::available ( ) const
inline

Number of bytes available to be copied from the buffer of IOStream.

References _head, and _tail.

Referenced by read(), nidas::dynld::SampleInputStream::read(), read(), readBuf(), nidas::dynld::SampleInputStream::readSamples(), readUntil(), and skip().

◆ backup() [1/2]

size_t IOStream::backup ( )
throw ( )

Move the read buffer pointer backwards to the beginning of the buffer.

References _buffer, _tail, and backup().

Referenced by backup().

◆ backup() [2/2]

size_t IOStream::backup ( size_t len)
throw ( )

Move the read buffer pointer backwards by len number of bytes, so that the next readBuf will return data that was previously read.


This just backs over bytes in the current buffer, and does not reposition the physical device.

Returns
The number of bytes backed over, which may be less than the number requested if there are fewer than len number of bytes in the buffer.

References nidas::util::addBackslashSequences(), DLOG, nidas::core::getSample(), len, and WLOG.

Referenced by nidas::dynld::SampleInputStream::handleNewInput(), and nidas::dynld::SampleInputStream::parseInputHeader().

◆ createFile()

dsm_time_t nidas::core::IOStream::createFile ( dsm_time_t t,
bool exact )
inline

Request that IOChannel object open a new file, with a name based on a time.

Parameters
tTime to use when creating file name.
exactUse exact time when creating file name, else the time is truncated down to an even time interval.
Returns
Start time of next file.
Exceptions
nidas::util::IOException

References _iochannel, nidas::core::IOChannel::createFile(), and flush().

◆ flush()

void IOStream::flush ( )

Flush buffer to physical device.

This is not done automatically by the destructor - the user must call flush before destroying this IOStream.

Exceptions
nidas::util::IOException

References _buffer, _head, _iochannel, _tail, addNumOutputBytes(), nidas::core::IOChannel::flush(), nidas::core::getSample(), and nidas::core::IOChannel::write().

Referenced by createFile(), nidas::dynld::SampleOutputStream::flush(), write(), and write().

◆ getName()

const std::string & nidas::core::IOStream::getName ( ) const
inline

◆ getNumInputBytes()

long long nidas::core::IOStream::getNumInputBytes ( ) const
inline

◆ getNumOutputBytes()

long long nidas::core::IOStream::getNumOutputBytes ( ) const
inline

Total number of bytes written with this IOStream.

References _nbytesOut.

◆ isNewInput()

bool nidas::core::IOStream::isNewInput ( ) const
inline

Did last read(), or read(buf,len) call result in a new file being opened?

References _newInput.

Referenced by nidas::dynld::SampleInputStream::read().

◆ operator=()

IOStream & nidas::core::IOStream::operator= ( const IOStream & )
private

No assignment.

◆ read() [1/2]

size_t IOStream::read ( )

Do an IOChannel::read into the internal buffer of IOStream.

Returns
number of bytes read. If there is still data in the IOStream buffer, then no physical read will be done, and a length of 0 is returned.
Exceptions
nidas::util::IOException

References _buffer, _eob, _head, _iochannel, _nbytesIn, _newInput, _tail, available(), nidas::core::getSample(), nidas::core::IOChannel::isNewInput(), nidas::core::IOChannel::read(), and VLOG.

Referenced by nidas::dynld::SampleInputStream::read(), read(), nidas::dynld::SampleInputStream::readInputHeader(), readUntil(), and skip().

◆ read() [2/2]

size_t IOStream::read ( void * buf,
size_t len )

Read len bytes of data into buf.

Returns
Number of bytes read. This will be less than len if an end of file is encountered, or if the IOChannel is configured for non-blocking reads and no data is available. This method may perform 0 or more physical reads of the IOChannel.
Exceptions
nidas::util::IOException

References _iochannel, _newInput, available(), nidas::core::getSample(), nidas::core::IOChannel::isNewInput(), len, read(), and readBuf().

◆ readBuf()

size_t nidas::core::IOStream::readBuf ( void * buf,
size_t len )
throw ( )
inline

Copy available bytes from the internal buffer to buf, returning the number of bytes copied, which may be less then len.

An IOChannel::read() is not done, even if the internal buffer is empty.

References _tail, addNumInputBytes(), available(), nidas::core::getSample(), and len.

Referenced by nidas::dynld::SampleInputStream::read(), and read().

◆ readUntil()

size_t IOStream::readUntil ( void * buf,
size_t len,
char term )

Read into the user buffer until a terminating character is found or len-1 bytes have been read.

The buffer is NULL terminated.

Returns
number of bytes read, not including the NULL character. Therefore the return value will not be more than len-1. This method will do an IOChannel::read() until the termination character is found of the buffer is full. Using this method on a non-blocking device may cause a system lockup. If an IOChannel::isNewInput is encountered before the readUntil() is satisfied, then any previous contents of the buffer are discarded and the reading proceeds with the new input.
Todo
throw an exception if used with a non-blocking physical device.
Exceptions
nidas::util::IOException

References _head, _iochannel, _nbytesIn, _newInput, _tail, available(), nidas::core::getSample(), nidas::core::IOChannel::isNewInput(), len, and read().

◆ reallocateBuffer()

void IOStream::reallocateBuffer ( size_t len)
protected

◆ skip()

size_t IOStream::skip ( size_t len)

If the internal buffer is empty, do an IOChannel::read into the buffer.

Then skip over len bytes in the user buffer.

Returns
number of bytes skipped. May be less than the user asked for.
Exceptions
nidas::util::IOException

References _nbytesIn, _newInput, _tail, available(), nidas::core::getSample(), len, and read().

◆ write() [1/2]

size_t IOStream::write ( const struct iovec * iov,
int nbufs,
bool flush )

Write data.

This supports an atomic write of data from multiple buffers into an output buffer. The write either completely succeeds (all buffers written), or completely fails (no buffers written). This prevents partial data samples from being written, and also reduces the need for copying to a temporary buffer.

Parameters
iovArray of pointers to buffers of data to be written.
nbufsNumber of buffers, the length of iov.
flushforce a write to the physical device.
Returns
true: all data in bufs was copied to output buffer; false: no data copied because the buffer was full and the physical device is bogged down. Typically one must chuck the data and proceed.
Exceptions
nidas::util::IOException

References _buffer, _buflen, _eob, _halflen, _head, _iochannel, _nEAGAIN, _tail, addNumOutputBytes(), flush(), getName(), nidas::core::getSample(), reallocateBuffer(), WLOG, and nidas::core::IOChannel::write().

Referenced by nidas::dynld::SampleOutputStream::write(), write(), nidas::dynld::SampleOutputStream::write(), and nidas::core::SampleInputHeader::write().

◆ write() [2/2]

size_t IOStream::write ( const void * buf,
size_t len,
bool flush )

Member Data Documentation

◆ _buffer

char* nidas::core::IOStream::_buffer
private

data buffer

Referenced by backup(), flush(), read(), reallocateBuffer(), write(), and ~IOStream().

◆ _buflen

size_t nidas::core::IOStream::_buflen
private

The actual buffer size.

Referenced by reallocateBuffer(), and write().

◆ _eob

char* nidas::core::IOStream::_eob
private

One past end of buffer.

Referenced by read(), reallocateBuffer(), and write().

◆ _halflen

size_t nidas::core::IOStream::_halflen
private

Referenced by reallocateBuffer(), and write().

◆ _head

char* nidas::core::IOStream::_head
private

where we insert bytes into the buffer

Referenced by available(), flush(), read(), readUntil(), reallocateBuffer(), and write().

◆ _iochannel

IOChannel& nidas::core::IOStream::_iochannel
protected

◆ _nbytesIn

long long nidas::core::IOStream::_nbytesIn
private

◆ _nbytesOut

long long nidas::core::IOStream::_nbytesOut
private

◆ _nEAGAIN

size_t nidas::core::IOStream::_nEAGAIN
private

Referenced by write().

◆ _newInput

bool nidas::core::IOStream::_newInput
private

Was the previous read performed on a newly opened file?

Referenced by isNewInput(), read(), read(), readUntil(), and skip().

◆ _tail

char* nidas::core::IOStream::_tail
private

where we remove bytes from the buffer

Referenced by available(), backup(), flush(), read(), readBuf(), readUntil(), reallocateBuffer(), skip(), and write().


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