Class ABufferedStream

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type ABufferedStream = class(AStreamWrapper)

Description

This class serves as the basis for a pair of classes that wrap around other streams and provide buffered input and output of those streams. This class is not meant to be instantiated directly, as it does not implement the abstract methods inherited from AStream; instead, you should use ABufferedInputStream to buffer input from another stream or ABufferedOutputStream to buffer output to another stream.

Hierarchy

Overview

Fields

Protected myBuffer: AnsiString;
Protected myBufferPosition: pchar;
Protected myBufferEnd: pchar;
Protected myBufferStreamOffset: TStreamOffset;

Methods

Protected function refreshBuffer: TStreamIOSize; virtual; abstract;
Public constructor around(const ThisTarget: AStream; const takeOwnershipOfTarget: boolean = true; const bufferSize: longword = bfstDefaultBufferSize); reintroduce; virtual;
Public function init: boolean; override;
Public destructor destroy; override;
Public function shallowCopyFrom(const Other: AnObject): boolean; override;
Public function buffer: pchar; virtual;
Public function bufferSize: longword; virtual;
Public function bufferPosition: pchar; virtual;
Public function position: TStreamOffset; override;

Description

Fields

Protected myBuffer: AnsiString;

Refers to the buffer used to buffer stream data

Protected myBufferPosition: pchar;

Stores the current position within the buffer

Protected myBufferEnd: pchar;

Refers to the end of the buffer

Protected myBufferStreamOffset: TStreamOffset;

Stores the position in the stream at which the buffer begins

Methods

Protected function refreshBuffer: TStreamIOSize; virtual; abstract;

Refresh the buffer.

This method is meant to be implemented by descendant classes. For descendants that support reading from the underlying stream, it should read from the stream into the buffer and return the number of bytes read. For descendants that support writing to the underlying stream, it should flush the buffer to the underlying stream and report the number of bytes written.

Public constructor around(const ThisTarget: AStream; const takeOwnershipOfTarget: boolean = true; const bufferSize: longword = bfstDefaultBufferSize); reintroduce; virtual;

Construct a new buffered stream instance that will buffer input or output from the specified stream.

If takeOwnershipOfTarget is True, then the buffered stream instance will assume ownership of Target and will free the stream when it is itself freed.

bufferSize specifies the size of the buffer to use, in bytes. If it is omitted or zero (0), then the default value specified by bfstDefaultBufferSize is used. If memory for the buffer cannot be allocated, this method will raise an exception.

Exceptions raised
ABufferedStreamError
if a buffer could not be allocated.
Public function init: boolean; override;

Initializer

Public destructor destroy; override;

Free the buffered stream instance and the buffer it uses.

If ABufferedStream.ownsTarget is True, then this method will free the target stream.

Public function shallowCopyFrom(const Other: AnObject): boolean; override;

Construct a shallow copy of the other object.

This method overrides the behavior inherited from AStreamWrapper.shallowCopyFrom: it calls that routine, then checks to see whether Other is an instance of ABufferedStream. If so, it copies the values of

from Other to Self, overwriting any current values in Self.

NOTE: The end result in this case is an almost-exact copy of Other; however, the copy will not assume ownership of the target stream, and read and write operations on the copy will not propagate to Other. The copy's buffer will update independently of the original! This means that if you read or write from the copy and then read or write from the original, you may end up with duplicate data (if both buffers have the same information), or you may confuse your your position in the source stream.

This method should therefore be used with care.

Public function buffer: pchar; virtual;

Retrieve a reference to the buffer used to buffer data for the target stream. This buffer is allocated on the heap.

The reference returned by this routine may be used to manipulate the contents of the buffer directly, but it should NOT be freed. That will be done by the buffered stream instance when it is itself freed.

Public function bufferSize: longword; virtual;

Retrieve the size, in bytes, of the buffer used to buffer data for the target stream.

Public function bufferPosition: pchar; virtual;

Retrieve a reference to the current position of the buffered stream within its buffer. This is the position in the buffer at which the next read or write operation will occur.

The reference returned by this routine may be used to manipulate the contents of the buffer directly, but it should NOT be freed. That will be done by the buffered stream instance when it is itself freed.

Public function position: TStreamOffset; override;

Retrieve the current position within the stream.

This method calculates the current position in the stream by taking the value of ABufferedStream.myBufferStreamOffset and adding it to the current buffer position.


Generated by PasDoc 0.13.0 on 2015-01-10 17:13:18