Vault  4.1
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends
VStream Class Reference

VStream is an abstract base class that defines a stream-oriented i/o API. More...

#include <vstream.h>

Inheritance diagram for VStream:
VAbstractFileStream VMemoryStream VSocketStream VBufferedFileStream VDirectIOFileStream VReadOnlyMemoryStream VWriteBufferedStream

List of all members.

Public Member Functions

 VStream ()
 Default constructor.
 VStream (const VString &name)
 Constructor that gives the stream a name for debugging purposes.
virtual ~VStream ()
 Destructor.
void readGuaranteed (Vu8 *targetBuffer, Vs64 numBytesToRead)
 Reads a specified number of bytes from the stream, and throws a VException if they cannot be read.
Vu8 readGuaranteedByte ()
 Reads one byte from the stream, and throws a VException if it cannot be read.
virtual Vs64 read (Vu8 *targetBuffer, Vs64 numBytesToRead)=0
 Attempts to read a specified number of bytes from the stream.
virtual Vs64 write (const Vu8 *buffer, Vs64 numBytesToWrite)=0
 Writes bytes to the stream.
virtual void flush ()=0
 Flushes any pending or buffered write data to the stream.
virtual bool skip (Vs64 numBytesToSkip)=0
 Skips forward in the stream a specified number of bytes.
virtual bool seek (Vs64 offset, int whence)=0
 Seeks in the stream using Unix seek() semantics.
bool seek0 ()
 This is a convenience function that means seek(0, SEEK_SET).
virtual Vs64 getIOOffset () const =0
 Returns the "current" "offset" in the stream.
virtual Vs64 available () const =0
 Returns the number of bytes that are available to be read from this stream.
const VStringgetName () const
 Returns the name of the stream that it was given when constructed.
void setName (const VString &name)
 Sets the stream name.

Static Public Member Functions

static Vs64 streamCopy (VStream &fromStream, VStream &toStream, Vs64 numBytesToCopy, Vs64 tempBufferSize=16384)
 Efficiently copies bytes from one stream to another, no matter which concrete stream types are being used.
static Vs64 streamCopy (VIOStream &fromStream, VIOStream &toStream, Vs64 numBytesToCopy, Vs64 tempBufferSize=16384)
static Vs64 streamCopy (VIOStream &fromStream, VStream &toStream, Vs64 numBytesToCopy, Vs64 tempBufferSize=16384)
static Vs64 streamCopy (VStream &fromStream, VIOStream &toStream, Vs64 numBytesToCopy, Vs64 tempBufferSize=16384)
static bool needSizeConversion (Vs64 sizeValue)
 Returns true if the specified size value requires conversion given the compiler's definition of size_t and the actual value given.
static void copyMemory (Vu8 *toBuffer, const Vu8 *fromBuffer, Vs64 numBytesToCopy)
 A direct replacement for memcpy() that allows for 64-bit lengths that we use everywhere, and deals with situations where the native sizes used by memcpy() are smaller.
static Vu8newNewBuffer (Vs64 bufferSize)
 The compiler- and size-safe way to do new[bufferSize] when the buffer size is a Vs64.
static Vu8mallocNewBuffer (Vs64 bufferSize)
 This is provided for consistency with newNewBuffer() when a buffer needs to be allocated with malloc() instead of operator new.

Protected Member Functions

virtual Vu8_getReadIOPtr () const
 Returns a pointer to the current read i/o position in the stream's buffer, or NULL if the stream does not have a buffer or support direct copying from that buffer (for example, a file or socket stream).
virtual Vu8_getWriteIOPtr () const
 Returns a pointer to the current write i/o position in the stream's buffer, or NULL if the stream does not have a buffer or support direct copying to that buffer (for example, a file or socket stream).
virtual Vs64 _prepareToRead (Vs64 numBytesToRead) const
 Returns the number of bytes available for reading from the stream's buffer, or zero by default for streams without buffers.
virtual void _prepareToWrite (Vs64 numBytesToWrite)
 Preflights the stream's buffer so that it can have the specified number of bytes written to it subsequently.
virtual void _finishRead (Vs64 numBytesRead)
 Postflights a copy by advancing the i/o offset to reflect the specified number of bytes having just been read.
virtual void _finishWrite (Vs64 numBytesWritten)
 Postflights a copy by advancing the i/o offset to reflect the specified number of bytes having just been written.

Protected Attributes

VString mName
 A name for use when debugging stream.

Friends

class VWriteBufferedStream

Detailed Description

VStream is an abstract base class that defines a stream-oriented i/o API.

You will generally use VSocketStream for socket i/o, VBufferedFileStream for file i/o, and VMemoryStream for memory i/o. VStream also defines and implements a static function streamCopy() for efficiently copying data between any two streams, no matter their types; this is useful for doing zero-copy file and socket i/o, or buffering such i/o, without having to specialize the code.

However, it is generally better to use one of the VIOStream-derived classes to do your actual i/o and avoid calling VStream methods directly. The VIOStream classes take a VStream in their constructors and use that stream, letting you do more well-typed i/o, as well as the same operations provided here. For binary data you will use VBinaryIOStream, and for text data (such as a text file or a line-oriented wire protocol) you will use VTextIOStream.

There are two ways to read raw bytes. The readGuaranteed() method will throw an exception if the stream does not contain the requested number of bytes. The read() method will return the actual number of bytes that were read. readGuaranteed() is implemented by calling read() and throwing if it returns an unsatisfactory number of bytes.

You can also write raw bytes. After you have completed your sequence of writes, you should call flush to allow special classes to clean up or write whatever they may have buffered.

Depending on the actual type of stream, you may be able to seek around in the stream. Here are some examples of which streams allow what kind of seeks. Really, the only limitations are obvious: on socket streams you cannot seek anywhere but forward relative to the current position. If you need to do some kind of pseudo-random-access i/o on a socket stream, then you'll just need to buffer it (using a VMemoryStream would make this very easy).

  SEEK_SET SEEK_CUR offset>=0 SEEK_CUR offset<0 SEEK_END
VMemoryStream yes yes yes yes
VAbstractFileStream -derived yes yes yes yes
VSocketStream no yes no no

If you just want to skip over some bytes while reading, use the skip() method, which is equivalent to SEEK_CUR forward, and works on all streams. Unfortunately, because of how the different underlying native implementations differ, you can only get a boolean return value indicating whether you could succesfully seek to the desired location.

See also:
VBinaryIOStream
VTextIOStream
VMemoryStream
VAbstractFileStream
VBufferedFileStream
VDirectIOFileStream
VSocketStream

Definition at line 199 of file vstream.h.


Member Function Documentation

void VStream::readGuaranteed ( Vu8 targetBuffer,
Vs64  numBytesToRead 
)

Reads a specified number of bytes from the stream, and throws a VException if they cannot be read.

Parameters:
targetBufferthe buffer to read into
numBytesToReadthe number of bytes to read

Definition at line 25 of file vstream.cpp.

References read().

Vu8 VStream::readGuaranteedByte ( )

Reads one byte from the stream, and throws a VException if it cannot be read.

Returns:
the byte that was read

Definition at line 33 of file vstream.cpp.

References readGuaranteed().

virtual Vs64 VStream::read ( Vu8 targetBuffer,
Vs64  numBytesToRead 
) [pure virtual]

Attempts to read a specified number of bytes from the stream.

Parameters:
targetBufferthe buffer to read into
numBytesToReadthe number of bytes to read
Returns:
the actual number of bytes that could be read

Implemented in VMemoryStream, VBufferedFileStream, VDirectIOFileStream, VSocketStream, and VWriteBufferedStream.

virtual Vs64 VStream::write ( const Vu8 buffer,
Vs64  numBytesToWrite 
) [pure virtual]

Writes bytes to the stream.

Parameters:
bufferthe buffer containing the data
numBytesToWritethe number of bytes to write to the stream
Returns:
the actual number of bytes written

Implemented in VReadOnlyMemoryStream, VMemoryStream, VBufferedFileStream, VDirectIOFileStream, and VSocketStream.

virtual void VStream::flush ( ) [pure virtual]

Flushes any pending or buffered write data to the stream.

Until you call flush, you cannot guarantee that your data has actually been written to the underlying physical stream.

Implemented in VReadOnlyMemoryStream, VMemoryStream, VBufferedFileStream, VDirectIOFileStream, VSocketStream, and VWriteBufferedStream.

virtual bool VStream::skip ( Vs64  numBytesToSkip) [pure virtual]

Skips forward in the stream a specified number of bytes.

For memory and file streams, this means advancing the i/o offset by the specified number of bytes; for socket streams, this means reading and discarding the specified number of bytes.

Parameters:
numBytesToSkipthe number of bytes to skip

Implemented in VBufferedFileStream, VDirectIOFileStream, VMemoryStream, VSocketStream, and VWriteBufferedStream.

virtual bool VStream::seek ( Vs64  offset,
int  whence 
) [pure virtual]

Seeks in the stream using Unix seek() semantics.

VSocketStream has some restrictions in the kinds of seek that are allowed; if you specify an illegal socket seek operation, a VException is thrown.

The following table shows the valid seek parameters for the different stream types:

  SEEK_SET SEEK_CUR offset>=0 SEEK_CUR offset<0 SEEK_END
VMemoryStream yes yes yes yes
VAbstractFileStream-derived yes yes yes yes
VSocketStream no yes no no
Parameters:
offsetthe offset, meaning depends on whence value
whenceSEEK_SET, SEEK_CUR, or SEEK_END
Returns:
true if the seek was successful

Implemented in VBufferedFileStream, VDirectIOFileStream, VMemoryStream, and VSocketStream.

bool VStream::seek0 ( ) [inline]

This is a convenience function that means seek(0, SEEK_SET).

In other words, seek to the start of the stream. It has the same restrictions as noted in seek() above.

Returns:
true if the seek was successful

Definition at line 307 of file vstream.h.

References seek().

virtual Vs64 VStream::getIOOffset ( ) const [pure virtual]

Returns the "current" "offset" in the stream.

Those scare quotes are there because those terms do not quite have consistent or uniform meaning and behavior for all stream types, so you need to be a little careful in using this feature. For file streams, the current offset is simply the i/o mark relative to the start of the file. For memory streams, the current offset is also the i/o mark, relative to the start of the buffer. But for socket streams, which have no buffer, the current offset is simply the accumulated number of bytes that have been read; when used with a socket stream, this function is most useful as a way to determine how much data you have read since last looking at the offset, without having to keep track of each individual read operation (which you might not be able to).

Returns:
the current offset

Implemented in VBufferedFileStream, VDirectIOFileStream, VMemoryStream, and VSocketStream.

virtual Vs64 VStream::available ( ) const [pure virtual]

Returns the number of bytes that are available to be read from this stream.

For file and memory streams, this means the number of bytes from the current i/o mark until the end of the file or buffer. For socket streams, this means the number of bytes that can be read without blocking (that is, the number of bytes that are waiting to be read on the socket at this time).

Returns:
the number of bytes currently available for reading

Implemented in VBufferedFileStream, VDirectIOFileStream, VMemoryStream, and VSocketStream.

Vs64 VStream::streamCopy ( VStream fromStream,
VStream toStream,
Vs64  numBytesToCopy,
Vs64  tempBufferSize = 16384 
) [static]

Efficiently copies bytes from one stream to another, no matter which concrete stream types are being used.

Some examples of using it include reading a file into memory (fromStream is VAbstractFileStream-derived, toStream is a VMemoryStream), writing from memory to a socket (fromStream is a VMemoryStream, toStream is a VSocketStream), and transferring a file to a socket (fromStream is VAbstractFileStream-derived, toStream is a VSocketStream).

If either of the streams is a VMemoryStream, the copy is made directly with no extra copying. If neither stream is a VMemoryStream, a temporary buffer is used to transfer the data with just a single copy.

Of course, this method does not actually know the stream classes, but simply asks the to and from streams about their capabilities.

Parameters:
fromStreamthe source stream that is read
toStreamthe target stream that is written
numBytesToCopythe number of bytes read from fromStream and write to toStream
tempBufferSizethe size of temporary buffer to create, if one is needed
Returns:
the actual number of bytes copied

Definition at line 40 of file vstream.cpp.

References _finishRead(), _finishWrite(), _getReadIOPtr(), _getWriteIOPtr(), _prepareToRead(), _prepareToWrite(), copyMemory(), newNewBuffer(), NULL, read(), V_MIN, and write().

const VString& VStream::getName ( ) const [inline]

Returns the name of the stream that it was given when constructed.

The name is just an arbitrary string that can be useful when debugging to distinguish between different streams' logging output.

Returns:
the stream name

Definition at line 371 of file vstream.h.

References mName.

void VStream::setName ( const VString name) [inline]

Sets the stream name.

The name is just an arbitrary string that can be useful when debugging to distinguish between different streams' logging output.

Parameters:
namethe name with which to label the stream

Definition at line 378 of file vstream.h.

References mName.

bool VStream::needSizeConversion ( Vs64  sizeValue) [static]

Returns true if the specified size value requires conversion given the compiler's definition of size_t and the actual value given.

If size_t is 64 bits, or if the given value fits in 32 bits, then no conversion is necessary, and an operation that takes a size_t can be called with a simple cast; otherwise the caller needs to perform the operation in a loop, processing 32 bits worth of data at a time. This is used internally by various Code Vault stream and i/o functions that provide a consistent 64-bit wide size parameter interface, but need to work on OS's and compilers that might only provide 32 bits of size for certain functions such as read, write, copy, etc.

Parameters:
sizeValuethe value that is being used to describe a size
Returns:
true if the size won't fit in a size_t

Definition at line 149 of file vstream.cpp.

void VStream::copyMemory ( Vu8 toBuffer,
const Vu8 fromBuffer,
Vs64  numBytesToCopy 
) [static]

A direct replacement for memcpy() that allows for 64-bit lengths that we use everywhere, and deals with situations where the native sizes used by memcpy() are smaller.

This could even have optimized versions per platform if the platform supports a native API that is better than memcpy().

Definition at line 154 of file vstream.cpp.

References needSizeConversion(), and V_MIN.

Vu8 * VStream::newNewBuffer ( Vs64  bufferSize) [static]

The compiler- and size-safe way to do new[bufferSize] when the buffer size is a Vs64.

If size_t is 64 bits then it just does new. Otherwise, if the requested buffer size fits in 32 bits the it also does new. Otherwise, (size_t is 32 bits but requested size needs 64 bits) then it throws a std::bad_alloc just like new would if it ran out of memory.

Parameters:
bufferSizethe requested buffer size
Returns:
a pointer to the newly allocated buffer, allocated with new[bufferSize]

Definition at line 191 of file vstream.cpp.

Vu8 * VStream::_getReadIOPtr ( ) const [protected, virtual]

Returns a pointer to the current read i/o position in the stream's buffer, or NULL if the stream does not have a buffer or support direct copying from that buffer (for example, a file or socket stream).

Returns:
the i/o buffer pointer, or NULL

Reimplemented in VMemoryStream.

Definition at line 216 of file vstream.cpp.

References NULL.

Vu8 * VStream::_getWriteIOPtr ( ) const [protected, virtual]

Returns a pointer to the current write i/o position in the stream's buffer, or NULL if the stream does not have a buffer or support direct copying to that buffer (for example, a file or socket stream).

Returns:
the i/o buffer pointer, or NULL

Reimplemented in VMemoryStream.

Definition at line 221 of file vstream.cpp.

References NULL.

Vs64 VStream::_prepareToRead ( Vs64  numBytesToRead) const [protected, virtual]

Returns the number of bytes available for reading from the stream's buffer, or zero by default for streams without buffers.

Parameters:
numBytesToReadthe number of bytes that will be read
Returns:
the number of bytes available to read, or zero

Reimplemented in VMemoryStream.

Definition at line 226 of file vstream.cpp.

void VStream::_prepareToWrite ( Vs64  numBytesToWrite) [protected, virtual]

Preflights the stream's buffer so that it can have the specified number of bytes written to it subsequently.

Throws a VException if the buffer cannot be expanded to accomodate the data.

Parameters:
numBytesToWritethe number of bytes that will be written

Reimplemented in VMemoryStream.

Definition at line 231 of file vstream.cpp.

void VStream::_finishRead ( Vs64  numBytesRead) [protected, virtual]

Postflights a copy by advancing the i/o offset to reflect the specified number of bytes having just been read.

Parameters:
numBytesReadthe number of bytes that were previously read

Reimplemented in VMemoryStream.

Definition at line 235 of file vstream.cpp.

void VStream::_finishWrite ( Vs64  numBytesWritten) [protected, virtual]

Postflights a copy by advancing the i/o offset to reflect the specified number of bytes having just been written.

Parameters:
numBytesWrittenthe number of bytes that were previously written

Reimplemented in VMemoryStream.

Definition at line 239 of file vstream.cpp.


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

Copyright ©1997-2014 Trygve Isaacson. All rights reserved. This documentation was generated with Doxygen.