nidas  v1.2-1520
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. More...
 
virtual ~IOStream ()
 
size_t available () const
 Number of bytes available to be copied from the buffer of IOStream. More...
 
bool isNewInput () const
 Did last read(), or read(buf,len) call result in a new file being opened? More...
 
size_t read () throw (nidas::util::IOException)
 Do an IOChannel::read into the internal buffer of IOStream. More...
 
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. More...
 
size_t read (void *buf, size_t len) throw (nidas::util::IOException)
 Read len bytes of data into buf. More...
 
size_t skip (size_t len) throw (nidas::util::IOException)
 If the internal buffer is empty, do an IOChannel::read into the buffer. More...
 
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. More...
 
size_t backup () throw ()
 Move the read buffer pointer backwards to the beginning of the buffer. More...
 
size_t readUntil (void *buf, size_t len, char term) throw (nidas::util::IOException)
 Read into the user buffer until a terminating character is found or len-1 bytes have been read. More...
 
size_t write (const struct iovec *iov, int nbufs, bool flush) throw (nidas::util::IOException)
 Write data. More...
 
size_t write (const void *buf, size_t len, bool flush) throw (nidas::util::IOException)
 
void flush () throw (nidas::util::IOException)
 Flush buffer to physical device. More...
 
dsm_time_t createFile (dsm_time_t t, bool exact) throw (nidas::util::IOException)
 Request that IOChannel object open a new file, with a name based on a time. More...
 
const std::string & getName () const
 
long long getNumInputBytes () const
 Number of bytes read with this IOStream. More...
 
void addNumInputBytes (int val)
 
long long getNumOutputBytes () const
 Total number of bytes written with this IOStream. More...
 
void addNumOutputBytes (int val)
 

Protected Member Functions

void reallocateBuffer (size_t len)
 

Protected Attributes

IOChannel_iochannel
 

Private Member Functions

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

Private Attributes

char * _buffer
 data buffer More...
 
char * _head
 where we insert bytes into the buffer More...
 
char * _tail
 where we remove bytes from the buffer More...
 
size_t _buflen
 The actual buffer size. More...
 
size_t _halflen
 
char * _eob
 One past end of buffer. More...
 
bool _newInput
 Was the previous read performed on a newly opened file? More...
 
long long _nbytesIn
 
long long _nbytesOut
 
size_t _nEAGAIN
 

Detailed Description

A base class for buffering data.

Constructor & Destructor Documentation

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

Create IOStream.

Parameters
input,:reference to an Input object providing the physical IO
buflen,:length of buffer, in bytes.

References reallocateBuffer().

IOStream::~IOStream ( )
virtual

References _buffer.

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

No copying.

Member Function Documentation

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

References _nbytesIn.

Referenced by readBuf().

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

References _nbytesOut.

Referenced by flush().

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(), readBuf(), nidas::dynld::SampleInputStream::readSamples(), and HeaderUtil::run().

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, len, and WLOG.

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

size_t IOStream::backup ( )
throw (
)

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

References _buffer, and _tail.

dsm_time_t nidas::core::IOStream::createFile ( dsm_time_t  t,
bool  exact 
)
throw (nidas::util::IOException
)
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.

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

void IOStream::flush ( )
throw (nidas::util::IOException
)

Flush buffer to physical device.

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

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

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

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

Total number of bytes written with this IOStream.

References _nbytesOut.

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().

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

No assignment.

size_t IOStream::read ( )
throw (nidas::util::IOException
)

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.

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

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

size_t IOStream::read ( void *  buf,
size_t  len 
)
throw (nidas::util::IOException
)

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.

References len.

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(), and len.

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

size_t IOStream::readUntil ( void *  buf,
size_t  len,
char  term 
)
throw (nidas::util::IOException
)

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.

References len.

void IOStream::reallocateBuffer ( size_t  len)
protected

References _buffer, _buflen, _eob, _halflen, _head, _tail, DLOG, getName(), len, and VLOG.

Referenced by IOStream().

size_t IOStream::skip ( size_t  len)
throw (nidas::util::IOException
)

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.

References len.

Referenced by HeaderUtil::run().

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

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.
flush,:force 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.

References nidas::util::Exception::getErrno(), and WLOG.

Referenced by WriterThread::run().

size_t IOStream::write ( const void *  buf,
size_t  len,
bool  flush 
)
throw (nidas::util::IOException
)

References len.

Member Data Documentation

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

data buffer

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

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

The actual buffer size.

Referenced by reallocateBuffer().

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

One past end of buffer.

Referenced by read(), and reallocateBuffer().

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

Referenced by reallocateBuffer().

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

where we insert bytes into the buffer

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

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

Referenced by createFile(), flush(), getName(), and read().

long long nidas::core::IOStream::_nbytesIn
private
long long nidas::core::IOStream::_nbytesOut
private
size_t nidas::core::IOStream::_nEAGAIN
private
bool nidas::core::IOStream::_newInput
private

Was the previous read performed on a newly opened file?

Referenced by isNewInput(), and read().

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

where we remove bytes from the buffer

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


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