MemoryIO.h

Classes

MemoryIO -- Class for IO to a memory buffer. (full description)

class MemoryIO: public ByteIO

Interface

Public Members
explicit MemoryIO (uInt64 initialSize=65536, uInt64 expandSize=32768)
MemoryIO (const void* buffer, uInt64 size)
MemoryIO (void* buffer, uInt64 size, ByteIO::OpenOption, uInt64 expandSize=0, Bool canDelete=False)
~MemoryIO()
virtual void write (uInt size, const void* buf)
virtual Int read (uInt size, void* buf, Bool throwException=True)
void clear()
const uChar* getBuffer() const
virtual Int64 length()
uInt64 allocated() const
uInt64 expandSize() const
virtual Bool isReadable() const
virtual Bool isWritable() const
virtual Bool isSeekable() const
uChar* setBuffer(uInt64 length)
void setUsed(uInt64 bytesUsed)
Private Members
MemoryIO (const MemoryIO& that)
MemoryIO& operator= (const MemoryIO& that)
virtual Int64 doSeek (Int64 offset, ByteIO::ByteIO)
Bool expand (uInt64 minSize)

Description

Review Status

Reviewed By:
Friso Olnon
Date Reviewed:
1996/11/06
Programs:
Tests:

Prerequisite

Synopsis

This class is doing IO in a buffer in memory. It is part of the entire IO framework. It can for instance be used to store data in canonical format in a memory string and obtain it later.

The memory buffer can be dynamic, so it will be expanded when needed. This is done by allocating a larger buffer, copy the contents and throw the old buffer away.
The memory buffer can also be static to be sure that the pointer to the buffer will not change. The expand size determines if the memory buffer is static or dynamic. An expand size zero indicates a static buffer.

The memory buffer is seekable and readable. It depends on the constructor whether it is writable.

There are several ways in which the buffer can be created/passed:

The user can obtain a pointer to the buffer to extract the stored data from it. The length of the data can also be obtained.

Usually this class will be used in combination with, say, CanonicalIO and AipsIO.

Example

    // Create dynamic (expandable) memory buffer of length 100.
    // Use that as the sink of RawIO in AipsIO.
    MemoryIO membuf (100);
    RawIO rawio (&membuf);
    AipsIO stream (&rawio);
    // Write values.
    stream << (Int)10;
    stream << True;
    // Seek to beginning of buffer and read data in.
    stream.setpos (0);
    Int vali;
    Bool valb;
    stream >> vali >> valb;

    // One can obtain the buffer and its length and use it later.
    // (e.g. to write it in a non-AipsIO file).
    uChar* bufptr = membuf.getBuffer();
    uInt64 length = membuf.length();

    // It can also used to construct another MemoryIO object from it.
    // The following memory buffer is static and readonly.
    MemoryIO membuf2 (bufptr, length);
    membuf2.read (sizeof(vali), vali);
    membuf2.read (sizeof(valb), valb);

Motivation

Make it possible to do IO in a memory buffer. The first implementation used strstreambuf from the iostream package. However, that did not allow seeking and it was hard to get the length.

Member Description

explicit MemoryIO (uInt64 initialSize=65536, uInt64 expandSize=32768)

Construct a dynamic object with the given initial length.

MemoryIO (const void* buffer, uInt64 size)

Construct from a buffer with the given length. The buffer is readonly and cannot be expanded.

MemoryIO (void* buffer, uInt64 size, ByteIO::OpenOption, uInt64 expandSize=0, Bool canDelete=False)

Construct from the given buffer with the given length. The Byte::Option determines how the buffer will be used. The seek pointer is set to the beginning of the buffer, unless told otherwise below.

New, NewNoReplace and Scratch
The buffer is empty and is read/write.
Old
The buffer contains size bytes and is readonly.
Update, Delete
The buffer contains size bytes and is read/write.
Append
The buffer contains size bytes and is read/write. The seek pointer is set to the end of the buffer.
When the buffer is writable, it will be expanded if needed. This means that buffer does not point to the data anymore. However, when expandSize==0, the buffer cannot be expanded and the pointer is always valid.
When canDelete is True, buffer expansion means that the old buffer gets deleted.

~MemoryIO()

Delete the Memory object. The data buffer is not deleted when constructed with the constructor taking a buffer pointer.

virtual void write (uInt size, const void* buf)

Write the number of bytes. When needed it expands the buffer. An exception is thrown when the buffer is not writable or when buffer expansion fails or is not possible.

virtual Int read (uInt size, void* buf, Bool throwException=True)

Read size bytes from the memory buffer. Returns the number of bytes actually read. Will throw an Exception (AipsError) if the requested number of bytes could not be read unless throwException is set to False. Will always throw an exception if the buffer is not readable or the buffer pointer is at an invalid position.

void clear()

Clear the buffer; i.e. set the data length and seek pointer to zero.

const uChar* getBuffer() const

Get the buffer containing the data.
The length of the data in the buffer can be obtained using the length() function.

virtual Int64 length()

Get the length of the data in the buffer.

uInt64 allocated() const

Get the allocated length of the buffer.

uInt64 expandSize() const

Get the expand size (0 = not expandable).

virtual Bool isReadable() const

Is the IO stream readable?

virtual Bool isWritable() const

Is the IO stream writable?

virtual Bool isSeekable() const

Is the IO stream seekable?

uChar* setBuffer(uInt64 length)

resize the internal buffer (if necessary) so that it is big enough to hold the specified number of bytes. Returns a non-const pointer to the buffer that can be used to write up to the specified number of bytes into the buffer. If less data is written into the buffer then the setUsed member funtion should be used to indicate how much of the buffer is valid. Throws an exception if the MemoryIO object is not writable or if it needs to increase the size of the internal buffer and the MemoryIO object is not expandable.

Warning You should not use the supplied pointer to write more than length data points to the buffer

void setUsed(uInt64 bytesUsed)

tell the MemoryIO object how much of its internal buffer is valid data. You only need to use this function if you are directly writing to the buffer using the pointer returned by the non-const getBuffer function. This function throws an exception if the number of bytes used is greater than the number allocated or if the MemoryIO object is not writeable.

MemoryIO (const MemoryIO& that)

MemoryIO& operator= (const MemoryIO& that)

virtual Int64 doSeek (Int64 offset, ByteIO::ByteIO)

Reset the position pointer to the given value. It returns the new position. An exception is thrown when seeking before the start of the buffer or when seeking past the end of a readonly buffer. When seeking past the end of a writable buffer, the required amount of bytes is added and initialized to zero.

Bool expand (uInt64 minSize)