# wxStreamBuffer Class Reference
[Streams]

`#include <wx/stream.h>`

## Detailed Description

wxStreamBuffer is a cache manager for wxStreamBase: it manages a stream buffer linked to a stream.

Each stream always has one autoinitialized stream buffer, but you may attach more of them to the same stream.

##

Library: wxBase

Category: Streams

See also:
  wxStreamBase, Stream classes overview

## Public Member Functions
| wxStreamBuffer (wxStreamBase &stream, BufMode mode)
| Constructor, creates a new stream buffer using stream as a parent stream and mode as the IO mode.
| wxStreamBuffer (size_t bufsize, wxInputStream &stream)
| Constructor for an input buffer of the specified size.
| wxStreamBuffer (size_t bufsize, wxOutputStream &stream)
| Constructor for an output buffer of the specified size.
| wxStreamBuffer (BufMode mode)
| Constructor; creates a new empty stream buffer which won't flush any data to a stream.
| wxStreamBuffer (const wxStreamBuffer &buffer)
| Copy constructor.
| wxStreamBuffer ()
| Destructor.
bool | FillBuffer ()
| Fill the IO buffer.
void | Fixed (bool fixed)
| Toggles the fixed flag.
bool | FlushBuffer ()
| Flushes the IO buffer.
void | Flushable (bool flushable)
| Toggles the flushable flag.
void * | GetBufferEnd () const
| Returns a pointer on the end of the stream buffer.
void * | GetBufferPos () const
| Returns a pointer on the current position of the stream buffer.
size_t | GetBufferSize () const
| Returns the size of the buffer.
void * | GetBufferStart () const
| Returns a pointer on the start of the stream buffer.
virtual char | GetChar ()
| Gets a single char from the stream buffer.
size_t | GetDataLeft ()
| Returns the amount of available data in the buffer.
size_t | GetIntPosition () const
| Returns the current position (counted in bytes) in the stream buffer.
size_t | GetLastAccess () const
| Returns the amount of bytes read during the last IO call to the parent stream.
virtual void | PutChar (char c)
| Puts a single char to the stream buffer.
virtual size_t | Read (void *buffer, size_t size)
| Reads a block of the specified size and stores the data in buffer.
size_t | Read (wxStreamBuffer *buffer)
| Copies data to buffer.
void | ResetBuffer ()
| Resets to the initial state variables concerning the buffer.
virtual wxFileOffset | Seek (wxFileOffset pos, wxSeekMode mode)
| Changes the current position.
void | SetBufferIO (void *start, void *end, bool takeOwnership=false)
| Specifies which pointers to use for stream buffering.
void | SetBufferIO (size_t bufsize)
| Destroys or invalidates the previous IO buffer and allocates a new one of the specified size.
void | SetIntPosition (size_t pos)
| Sets the current position (in bytes) in the stream buffer.
wxStreamBase * | Stream ()
| Returns the parent stream of the stream buffer.
virtual wxFileOffset | Tell () const
| Gets the current position in the stream.
void | Truncate ()
| Truncates the buffer to the current position.
virtual size_t | Write (const void *buffer, size_t size)
| Writes a block of the specified size using data of buffer.
size_t | Write (wxStreamBuffer *buffer)
| See Read().

List of all members.

## Constructor & Destructor Documentation

wxStreamBuffer::wxStreamBuffer | ( | wxStreamBase & | stream,
|  | BufMode | mode |
| ) |  |  |

Constructor, creates a new stream buffer using stream as a parent stream and mode as the IO mode.

Parameters:

  | stream | The parent stream.
  | mode | Can be: wxStreamBuffer::read, wxStreamBuffer::write, wxStreamBuffer::read_write.

One stream can have many stream buffers but only one is used internally to pass IO call (e.g. wxInputStream::Read() -> wxStreamBuffer::Read()), but you can call directly wxStreamBuffer::Read without any problems. Note that all errors and messages linked to the stream are stored in the stream, not the stream buffers:

            streambuffer.Read(...);
            streambuffer2.Read(...);
                // This call erases previous error messages set by 'streambuffer'
                // assuming that both instances are stream buffers for the same stream

See also:
  SetBufferIO()

wxStreamBuffer::wxStreamBuffer | ( | size_t | bufsize,
|  | wxInputStream & | stream |
| ) |  |  |

Constructor for an input buffer of the specified size.

Using it is equivalent to using the constructor above with read mode and calling SetBufferIO() but is more convenient.

Since:
  2.9.0

Parameters:

  | bufsize | The size of buffer in bytes.
  | stream | The associated input stream, the buffer will be used in read mode.

wxStreamBuffer::wxStreamBuffer | ( | size_t | bufsize,
|  | wxOutputStream & | stream |
| ) |  |  |

Constructor for an output buffer of the specified size.

Using it is equivalent to using the constructor above with write mode and calling SetBufferIO() but is more convenient.

Since:
  2.9.0

Parameters:

  | bufsize | The size of buffer in bytes.
  | stream | The associated output stream, the buffer will be used in write mode.

wxStreamBuffer::wxStreamBuffer | ( | BufMode | mode | ) |

Constructor; creates a new empty stream buffer which won't flush any data to a stream.

mode specifies the type of the buffer (read, write, read_write).

This stream buffer has the advantage to be stream independent and to work only on memory buffers but it is still compatible with the rest of the wxStream classes. You can write, read to this special stream and it will grow (if it is allowed by the user) its internal buffer. Briefly, it has all functionality of a "normal" stream.

Warning:
  The "read_write" mode doesn't currently work for standalone stream buffers.

See also:
  SetBufferIO()

wxStreamBuffer::wxStreamBuffer | ( | const wxStreamBuffer & | buffer | ) |

Copy constructor.

This method initializes the stream buffer with the data of the specified stream buffer. The new stream buffer has the same attributes, size, position and they share the same buffer. This will cause problems if the stream to which the stream buffer belong is destroyed and the newly cloned stream buffer continues to be used, trying to call functions in the (destroyed) stream. It is advised to use this feature only in very local area of the program.

wxStreamBuffer::wxStreamBuffer | ( |  | ) |

Destructor.

It finalizes all IO calls and frees all internal buffers if necessary.

## Member Function Documentation

bool wxStreamBuffer::FillBuffer | ( |  | ) |

Fill the IO buffer.

void wxStreamBuffer::Fixed | ( | bool | fixed | ) |

Toggles the fixed flag.

Usually this flag is toggled at the same time as flushable. This flag allows (when it has the false value) or forbids (when it has the true value) the stream buffer to resize dynamically the IO buffer.

See also:
  SetBufferIO()

void wxStreamBuffer::Flushable | ( | bool | flushable | ) |

Toggles the flushable flag.

If flushable is disabled, no data are sent to the parent stream.

bool wxStreamBuffer::FlushBuffer | ( |  | ) |

Flushes the IO buffer.

void* wxStreamBuffer::GetBufferEnd | ( |  | ) | const

Returns a pointer on the end of the stream buffer.

void* wxStreamBuffer::GetBufferPos | ( |  | ) | const

Returns a pointer on the current position of the stream buffer.

size_t wxStreamBuffer::GetBufferSize | ( |  | ) | const

Returns the size of the buffer.

void* wxStreamBuffer::GetBufferStart | ( |  | ) | const

Returns a pointer on the start of the stream buffer.

virtual char wxStreamBuffer::GetChar | ( |  | ) | `[virtual]`

Gets a single char from the stream buffer.

It acts like the Read() call.

Warning:
  You aren't directly notified if an error occurred during the IO call.

See also:
  Read()

size_t wxStreamBuffer::GetDataLeft | ( |  | ) |

Returns the amount of available data in the buffer.

size_t wxStreamBuffer::GetIntPosition | ( |  | ) | const

Returns the current position (counted in bytes) in the stream buffer.

size_t wxStreamBuffer::GetLastAccess | ( |  | ) | const

Returns the amount of bytes read during the last IO call to the parent stream.

virtual void wxStreamBuffer::PutChar | ( | char | c | ) | `[virtual]`

Puts a single char to the stream buffer.

Warning:
  You aren't directly notified if an error occurred during the IO call.

See also:
  Read()

size_t wxStreamBuffer::Read | ( | wxStreamBuffer * | buffer | ) |

Copies data to buffer.

The function returns when buffer is full or when there isn't any more data in the current buffer.

See also:
  Write()

virtual size_t wxStreamBuffer::Read | ( | void * | buffer,
|  | size_t | size |
| ) |  |  | `[virtual]`

Reads a block of the specified size and stores the data in buffer.

This function tries to read from the buffer first and if more data has been requested, reads more data from the associated stream and updates the buffer accordingly until all requested data is read.

Returns:
  It returns the size of the data read. If the returned size is different of the specified size, an error has occurred and should be tested using GetLastError().

void wxStreamBuffer::ResetBuffer | ( |  | ) |

Resets to the initial state variables concerning the buffer.

virtual wxFileOffset wxStreamBuffer::Seek | ( | wxFileOffset | pos,
|  | wxSeekMode | mode |
| ) |  |  | `[virtual]`

Changes the current position.

Parameter mode may be one of the following:

- wxFromStart: The position is counted from the start of the stream.
- wxFromCurrent: The position is counted from the current position of the stream.
- wxFromEnd: The position is counted from the end of the stream.

Returns:
  Upon successful completion, it returns the new offset as measured in bytes from the beginning of the stream. Otherwise, it returns wxInvalidOffset.

void wxStreamBuffer::SetBufferIO | ( | size_t | bufsize | ) |

Destroys or invalidates the previous IO buffer and allocates a new one of the specified size.

Warning:
  All previous pointers aren't valid anymore.

Remarks:
  The created IO buffer is growable by the object.

See also:
  Fixed(), Flushable()

void wxStreamBuffer::SetBufferIO | ( | void * | start,
|  | void * | end,
|  | bool | takeOwnership = `false` |
| ) |  |  |

Specifies which pointers to use for stream buffering.

You need to pass a pointer on the start of the buffer end and another on the end. The object will use this buffer to cache stream data. It may be used also as a source/destination buffer when you create an empty stream buffer (See wxStreamBuffer::wxStreamBuffer).

Remarks:
  When you use this function, you will have to destroy the IO buffers yourself after the stream buffer is destroyed or don't use it anymore. In the case you use it with an empty buffer, the stream buffer will not resize it when it is full.

See also:
  wxStreamBuffer(), Fixed(), Flushable()

void wxStreamBuffer::SetIntPosition | ( | size_t | pos | ) |

Sets the current position (in bytes) in the stream buffer.

Warning:
  Since it is a very low-level function, there is no check on the position: specifying an invalid position can induce unexpected results.

wxStreamBase* wxStreamBuffer::Stream | ( |  | ) |

Returns the parent stream of the stream buffer.

Deprecated:
  use GetStream() instead

virtual wxFileOffset wxStreamBuffer::Tell | ( |  | ) | const`[virtual]`

Gets the current position in the stream.

This position is calculated from the real position in the stream and from the internal buffer position: so it gives you the position in the real stream counted from the start of the stream.

Returns:
  Returns the current position in the stream if possible, wxInvalidOffset in the other case.

void wxStreamBuffer::Truncate | ( |  | ) |

Truncates the buffer to the current position.

Note:
  Truncate() cannot be used to enlarge the buffer. This is usually not needed since the buffer expands automatically.

size_t wxStreamBuffer::Write | ( | wxStreamBuffer * | buffer | ) |

See Read().

virtual size_t wxStreamBuffer::Write | ( | const void * | buffer,
|  | size_t | size |
| ) |  |  | `[virtual]`

Writes a block of the specified size using data of buffer.

The data are cached in a buffer before being sent in one block to the stream.

|  | [ top ]