ByteArrayInputStream Class Reference

ByteArrayInputStream implements the ByteInputStream interface by reading data from an existing fixed-size array of bytes. More...

#include <ByteArrayInputStream.h>

Inheritance diagram for ByteArrayInputStream:

ByteInputStream ByteStream ClosableObject List of all members.

Public Member Functions

void resetArray (PConstBuffer pBuffer, uint cbBuffer)
 Resets stream to read from a new array.
uint readBytes (void *pData, uint cbRequested)
 Reads bytes from the stream.
template<class T>
uint readValue (T &value)
 Reads a fixed-size value from the stream.
PConstBuffer getReadPointer (uint cbRequested, uint *pcbActual=NULL)
 Copyless alternative for reading bytes from the stream.
void consumeReadPointer (uint cbUsed)
 Advances stream position after a call to getReadPointer.
void seekForward (uint cb)
 Skips forward in stream.
void seekBackward (uint cb)
 Skips backward in stream.
virtual SharedByteStreamMarker newMarker ()
 Creates a new uninitialized marker for this stream.
virtual void mark (ByteStreamMarker &marker)
 Marks the current stream position in preparation for a future call to reset().
virtual void reset (ByteStreamMarker const &marker)
 Resets stream to a previously marked position.
FileSize getOffset () const
 
Returns:
current offset from beginning of stream

bool isClosed () const
 
Returns:
whether the object has been closed

void close ()
 Closes this object, releasing any unallocated resources.

Static Public Member Functions

static SharedByteArrayInputStream newByteArrayInputStream (PConstBuffer pBuffer, uint cbBuffer)
 Creates a new ByteArrayInputStream.

Protected Member Functions

void setBuffer (PConstBuffer pBuffer, uint cbBuffer)
 Sets the current buffer to be read.
void nullifyBuffer ()
 Nullifies the current buffer, indicating no more data is available.
uint getBytesAvailable () const
 
Returns:
number of bytes remaining in current buffer

uint getBytesConsumed () const
 
Returns:
number of bytes already consumed from current buffer


Protected Attributes

FileSize cbOffset
 Byte position in stream.
bool needsClose

Private Member Functions

virtual void readNextBuffer ()
 Must be implemented by derived class by calling either setBuffer or nullifyBuffer.
virtual void readPrevBuffer ()
 Must be implemented by derived class if seekBackward is to be supported.
virtual void closeImpl ()
 Must be implemented by derived class to release any resources.
 ByteArrayInputStream (PConstBuffer pBuffer, uint cbBuffer)

Detailed Description

ByteArrayInputStream implements the ByteInputStream interface by reading data from an existing fixed-size array of bytes.

Definition at line 35 of file ByteArrayInputStream.h.


Constructor & Destructor Documentation

ByteArrayInputStream::ByteArrayInputStream ( PConstBuffer  pBuffer,
uint  cbBuffer 
) [explicit, private]

Definition at line 38 of file ByteArrayInputStream.cpp.

References ByteInputStream::setBuffer().

Referenced by newByteArrayInputStream().

00041 {
00042     setBuffer(pBuffer,cbBuffer);
00043 }


Member Function Documentation

void ByteArrayInputStream::readNextBuffer (  )  [private, virtual]

Must be implemented by derived class by calling either setBuffer or nullifyBuffer.

Implements ByteInputStream.

Definition at line 45 of file ByteArrayInputStream.cpp.

References ByteInputStream::nullifyBuffer().

00046 {
00047     nullifyBuffer();
00048 }

void ByteArrayInputStream::readPrevBuffer (  )  [private, virtual]

Must be implemented by derived class if seekBackward is to be supported.

Reimplemented from ByteInputStream.

Definition at line 50 of file ByteArrayInputStream.cpp.

References ByteInputStream::nullifyBuffer().

00051 {
00052     nullifyBuffer();
00053 }

void ByteArrayInputStream::closeImpl (  )  [private, virtual]

Must be implemented by derived class to release any resources.

Implements ClosableObject.

Definition at line 55 of file ByteArrayInputStream.cpp.

00056 {
00057     // nothing to do
00058 }

SharedByteArrayInputStream ByteArrayInputStream::newByteArrayInputStream ( PConstBuffer  pBuffer,
uint  cbBuffer 
) [static]

Creates a new ByteArrayInputStream.

Parameters:
pBuffer bytes to read
cbBuffer number of bytes to read
Returns:
shared_ptr to new ByteArrayInputStream

Definition at line 29 of file ByteArrayInputStream.cpp.

References ByteArrayInputStream().

Referenced by SpillOutputStream::getInputStream().

00032 {
00033     return SharedByteArrayInputStream(
00034         new ByteArrayInputStream(pBuffer,cbBuffer),
00035         ClosableObjectDestructor());
00036 }

void ByteArrayInputStream::resetArray ( PConstBuffer  pBuffer,
uint  cbBuffer 
)

Resets stream to read from a new array.

Parameters:
pBuffer bytes to read
cbBuffer number of bytes to read

Definition at line 60 of file ByteArrayInputStream.cpp.

References ByteStream::cbOffset, and ByteInputStream::setBuffer().

00063 {
00064     setBuffer(pBuffer,cbBuffer);
00065     cbOffset = 0;
00066 }

void ByteInputStream::setBuffer ( PConstBuffer  pBuffer,
uint  cbBuffer 
) [inline, protected, inherited]

Sets the current buffer to be read.

Parameters:
pBuffer receives start address of new buffer
cbBuffer number of bytes in buffer

Definition at line 241 of file ByteInputStream.h.

References ByteInputStream::pEndByte, ByteInputStream::pFirstByte, and ByteInputStream::pNextByte.

Referenced by ByteArrayInputStream(), SegInputStream::lockBuffer(), CrcSegInputStream::lockBufferParanoid(), ByteInputStream::nullifyBuffer(), and resetArray().

00244 {
00245     pFirstByte = pBuffer;
00246     pEndByte = pBuffer + cbBuffer;
00247     pNextByte = pFirstByte;
00248 }

void ByteInputStream::nullifyBuffer (  )  [inline, protected, inherited]

Nullifies the current buffer, indicating no more data is available.

Definition at line 250 of file ByteInputStream.h.

References ByteInputStream::setBuffer().

Referenced by ByteInputStream::ByteInputStream(), CrcSegInputStream::lockBuffer(), SegInputStream::readNextBuffer(), readNextBuffer(), SegInputStream::readPrevBuffer(), and readPrevBuffer().

00251 {
00252     setBuffer(NULL,0);
00253 }

uint ByteInputStream::getBytesAvailable (  )  const [inline, protected, inherited]

Returns:
number of bytes remaining in current buffer

Definition at line 199 of file ByteInputStream.h.

References ByteInputStream::pEndByte, and ByteInputStream::pNextByte.

Referenced by ByteInputStream::consumeReadPointer(), ByteInputStream::getReadPointer(), ByteInputStream::readBytes(), and SegInputStream::seekSegPos().

00200 {
00201     return static_cast<uint>(pEndByte - pNextByte);
00202 }

uint ByteInputStream::getBytesConsumed (  )  const [inline, protected, inherited]

Returns:
number of bytes already consumed from current buffer

Definition at line 204 of file ByteInputStream.h.

References ByteInputStream::pFirstByte, and ByteInputStream::pNextByte.

Referenced by SegInputStream::getSegPos(), and ByteInputStream::seekBackward().

00205 {
00206     return static_cast<uint>(pNextByte - pFirstByte);
00207 }

uint ByteInputStream::readBytes ( void *  pData,
uint  cbRequested 
) [inherited]

Reads bytes from the stream.

Parameters:
pData target buffer to read into
cbRequested number of bytes to read
Returns:
number of bytes actually read; 0 indicates end-of-stream

Definition at line 34 of file ByteInputStream.cpp.

References ByteStream::cbOffset, ByteInputStream::getBytesAvailable(), ByteInputStream::pEndByte, ByteInputStream::pNextByte, and ByteInputStream::readNextBuffer().

Referenced by FtrsTableWriter::redoLogicalAction(), and ByteInputStream::seekForward().

00036 {
00037     uint cbActual = 0;
00038     if (pNextByte == pEndByte) {
00039         readNextBuffer();
00040     }
00041     for (;;) {
00042         uint cbAvailable = getBytesAvailable();
00043         if (!cbAvailable) {
00044             break;
00045         }
00046         if (cbRequested <= cbAvailable) {
00047             if (pData) {
00048                 memcpy(pData,pNextByte,cbRequested);
00049             }
00050             pNextByte += cbRequested;
00051             cbActual += cbRequested;
00052             break;
00053         }
00054         if (pData) {
00055             memcpy(pData,pNextByte,cbAvailable);
00056             pData = static_cast<char *>(pData) + cbAvailable;
00057         }
00058         cbRequested -= cbAvailable;
00059         cbActual += cbAvailable;
00060         readNextBuffer();
00061     }
00062     cbOffset += cbActual;
00063     return cbActual;
00064 }

template<class T>
uint ByteInputStream::readValue ( T &  value  )  [inline, inherited]

Reads a fixed-size value from the stream.

Parameters:
value value to write; type must be memcpy-safe
Returns:
number of bytes actually read

Definition at line 112 of file ByteInputStream.h.

Referenced by FtrsTableWriterFactory::loadIndex(), LogicalTxnTest::loadParticipant(), FtrsTableWriterFactory::loadParticipant(), BTreeRecoveryFactory::loadParticipant(), TupleProjection::readPersistent(), TupleDescriptor::readPersistent(), LogicalTxnTest::redoLogicalAction(), DatabaseTest::redoLogicalAction(), LogicalTxnTest::undoLogicalAction(), and DatabaseTest::undoLogicalAction().

00113     {
00114         return readBytes(&value,sizeof(value));
00115     }

PConstBuffer ByteInputStream::getReadPointer ( uint  cbRequested,
uint pcbActual = NULL 
) [inline, inherited]

Copyless alternative for reading bytes from the stream.

Provides direct access to the stream's internal buffer, but doesn't move the stream position (see consumeReadPointer).

Note that it is in general dangerous to assume that getReadPointer will be able to access desired data items contiguously. For example, a stream created by calling ByteOutputStream::write is likely to have data items split across buffers. The assumption MAY be valid for streams created by calling ByteOutputStream::getWritePointer with matching values for cbRequested; it depends on the stream implementation.

Parameters:
cbRequested number of contiguous bytes to access; if a non-zero number of bytes are currently available in the buffer, cbRequested must be no greater than the number of available bytes (otherwise an assertion violation will result)
pcbActual if non-NULL, receives actual number of contiguous bytes available, which will always be greater than or equal to cbRequested except 0 for end-of-stream
Returns:
pointer to cbActual bytes of available data, or NULL for end-of-stream

Definition at line 209 of file ByteInputStream.h.

References ByteInputStream::getBytesAvailable(), ByteInputStream::pEndByte, ByteInputStream::pNextByte, and ByteInputStream::readNextBuffer().

Referenced by BTreeWriter::deleteLogged(), BTreeWriter::insertLogged(), and BTreeBuildLevel::processInput().

00211 {
00212     if (getBytesAvailable() < cbRequested) {
00213         assert(pNextByte == pEndByte);
00214         readNextBuffer();
00215         if (pNextByte == pEndByte) {
00216             if (pcbActual) {
00217                 *pcbActual = 0;
00218             }
00219             return NULL;
00220         }
00221     }
00222     if (pcbActual) {
00223         *pcbActual = getBytesAvailable();
00224     }
00225     return pNextByte;
00226 }

void ByteInputStream::consumeReadPointer ( uint  cbUsed  )  [inline, inherited]

Advances stream position after a call to getReadPointer.

Parameters:
cbUsed number of bytes to advance; must be less than or equal to the value of cbActual returned by the last call to getReadPointer

Definition at line 234 of file ByteInputStream.h.

References ByteStream::cbOffset, ByteInputStream::getBytesAvailable(), and ByteInputStream::pNextByte.

Referenced by BTreeWriter::deleteLogged(), BTreeWriter::insertLogged(), BTreeBuildLevel::processInput(), and SegInputStream::seekSegPos().

00235 {
00236     assert(cbUsed <= getBytesAvailable());
00237     pNextByte += cbUsed;
00238     cbOffset += cbUsed;
00239 }

void ByteInputStream::seekForward ( uint  cb  )  [inline, inherited]

Skips forward in stream.

Parameters:
cb number of bytes to advance

Definition at line 228 of file ByteInputStream.h.

References ByteInputStream::readBytes().

Referenced by ByteInputStream::reset().

00229 {
00230     uint cbActual = readBytes(NULL,cb);
00231     assert(cbActual == cb);
00232 }

void ByteInputStream::seekBackward ( uint  cb  )  [inherited]

Skips backward in stream.

Not all stream implementations support this behavior.

Parameters:
cb number of bytes backward to seek

Definition at line 71 of file ByteInputStream.cpp.

References ByteStream::cbOffset, ByteInputStream::getBytesConsumed(), ByteInputStream::pEndByte, ByteInputStream::pFirstByte, ByteInputStream::pNextByte, and ByteInputStream::readPrevBuffer().

Referenced by ByteInputStream::reset().

00072 {
00073     assert(cb <= cbOffset);
00074     cbOffset -= cb;
00075     if (pNextByte == pFirstByte) {
00076         readPrevBuffer();
00077         pNextByte = pEndByte;
00078     }
00079     for (;;) {
00080         uint cbAvailable = getBytesConsumed();
00081         assert(cbAvailable);
00082         if (cb <= cbAvailable) {
00083             pNextByte -= cb;
00084             break;
00085         }
00086         cb -= cbAvailable;
00087         readPrevBuffer();
00088         pNextByte = pEndByte;
00089     }
00090 }

SharedByteStreamMarker ByteInputStream::newMarker (  )  [virtual, inherited]

Creates a new uninitialized marker for this stream.

The returned marker must be passed to mark() in order to initialize it.

Returns:
shared pointer to new marker

Reimplemented in SegInputStream.

Definition at line 92 of file ByteInputStream.cpp.

00093 {
00094     return SharedByteStreamMarker(new SequentialByteStreamMarker(*this));
00095 }

void ByteInputStream::mark ( ByteStreamMarker marker  )  [virtual, inherited]

Marks the current stream position in preparation for a future call to reset().

How long this marker remains valid depends upon the implementation of the ByteInputStream.

Parameters:
marker memento object created with newMarker() on the same stream; receives the marked position (forgetting any previously marked position)

Reimplemented in SegInputStream.

Definition at line 97 of file ByteInputStream.cpp.

References SequentialByteStreamMarker::cbOffset, ByteStream::getOffset(), and ByteStreamMarker::getStream().

00098 {
00099     assert(&(marker.getStream()) == this);
00100 
00101     SequentialByteStreamMarker &seqMarker =
00102         dynamic_cast<SequentialByteStreamMarker &>(marker);
00103     seqMarker.cbOffset = getOffset();
00104 }

void ByteInputStream::reset ( ByteStreamMarker const &  marker  )  [virtual, inherited]

Resets stream to a previously marked position.

The base implementation uses seekForward/seekBackward (i.e. sequential access), making it inefficient for large streams. Derived classes may override with more efficient implementations such as random access.

Parameters:
marker memento previously memorized with mark()

Reimplemented in SegInputStream.

Definition at line 106 of file ByteInputStream.cpp.

References ByteStream::cbOffset, SequentialByteStreamMarker::cbOffset, ByteStreamMarker::getStream(), isMAXU(), ByteInputStream::seekBackward(), and ByteInputStream::seekForward().

00107 {
00108     assert(&(marker.getStream()) == this);
00109 
00110     SequentialByteStreamMarker const &seqMarker =
00111         dynamic_cast<SequentialByteStreamMarker const &>(marker);
00112     assert(!isMAXU(seqMarker.cbOffset));
00113     if (cbOffset == seqMarker.cbOffset) {
00114         // expedite common case where stream has not moved since mark
00115         return;
00116     } else if (cbOffset > seqMarker.cbOffset) {
00117         seekBackward(cbOffset - seqMarker.cbOffset);
00118     } else {
00119         seekForward(seqMarker.cbOffset - cbOffset);
00120     }
00121 }

FileSize ByteStream::getOffset (  )  const [inline, inherited]

Returns:
current offset from beginning of stream

Definition at line 110 of file ByteStream.h.

References ByteStream::cbOffset.

Referenced by SpillOutputStream::getInputStream(), and ByteInputStream::mark().

00111 {
00112     return cbOffset;
00113 }

bool ClosableObject::isClosed (  )  const [inline, inherited]

Returns:
whether the object has been closed

Definition at line 58 of file ClosableObject.h.

00059     {
00060         return !needsClose;
00061     }

void ClosableObject::close (  )  [inherited]

Closes this object, releasing any unallocated resources.

Reimplemented in CollectExecStream, CorrelationJoinExecStream, LcsClusterAppendExecStream, and LcsClusterReplaceExecStream.

Definition at line 39 of file ClosableObject.cpp.

References ClosableObject::closeImpl(), and ClosableObject::needsClose.

Referenced by CacheImpl< PageT, VictimPolicyT >::allocatePages(), LcsRowScanBaseExecStream::closeImpl(), ExecStreamGraphImpl::closeImpl(), FlatFileBuffer::open(), ClosableObjectDestructor::operator()(), and Segment::~Segment().

00040 {
00041     if (!needsClose) {
00042         return;
00043     }
00044     needsClose = false;
00045     closeImpl();
00046 }


Member Data Documentation

FileSize ByteStream::cbOffset [protected, inherited]

Byte position in stream.

Definition at line 41 of file ByteStream.h.

Referenced by ByteStream::ByteStream(), ByteInputStream::consumeReadPointer(), ByteOutputStream::consumeWritePointer(), ByteStream::getOffset(), SegOutputStream::getSegPos(), SegInputStream::getSegPos(), ByteInputStream::readBytes(), ByteInputStream::reset(), resetArray(), ByteInputStream::seekBackward(), SegInputStream::seekSegPos(), and ByteOutputStream::writeBytes().

bool ClosableObject::needsClose [protected, inherited]

Definition at line 44 of file ClosableObject.h.

Referenced by SegStreamAllocation::beginWrite(), ExecStreamGraphImpl::clear(), ClosableObject::ClosableObject(), ClosableObject::close(), FlatFileBuffer::open(), ExecStreamGraphImpl::open(), ExecStream::open(), and ClosableObject::~ClosableObject().


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:26 2009 for Fennel by  doxygen 1.5.1