Class AStringStream

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type AStringStream = class(AStream, CanStream, CanPrint)

Description

This class allows an AnsiString to be read from and written to as though it was a stream. It is useful in those instances when buffering data in memory is desired, particularly because the class is capable of streaming its contents to and from another stream.

The stream supports reading, writing, and seeking. Read and write operations always occur at the location specified by AStringStream.position; this value can be changed by first calling AStringStream.setPosition.

Regardless of how the parent unit is compiled (with or without AnsiStrings as the default string type), this class will always operate on an AnsiString.

Hierarchy

Overview

Fields

Protected myName: string;
Protected myString: AnsiString;
Protected myPosition: TStreamOffset;

Methods

Public constructor new(const str: AnsiString = ''); reintroduce; virtual;
Public function init: boolean; override;
Public function read(const count: TStreamIOSize; var dest): TStreamIOSize; override;
Public function write(const count: TStreamIOSize; const source): TStreamIOSize; override;
Public procedure clear; virtual;
Public function shallowCopyFrom(const Other: AnObject): boolean; override;
Public function streamingLength: TStreamIOSize; virtual;
Public function writeTo(const Dest: AStream): TStreamIOSize; virtual;
Public function readFrom(const Source: AStream): TStreamIOSize; virtual;
Public function toString: string; override;
Public function printTo(const Dest: ATextOutputStream; prefix: AnsiString = ''; suffix: AnsiString = ''): TStreamIOSize; virtual;
Public function name: string; override;
Public function setName(const newName: string): string; virtual;
Public function position: TStreamOffset; override;
Public function setPosition(newPosition: TStreamOffset): TStreamOffset; override;
Public function length: TStreamLength; override;
Public function text: AnsiString; virtual;
Public function setText(const newText: AnsiString): AnsiString; virtual;

Description

Fields

Protected myName: string;

Stores the name of the stream.

Protected myString: AnsiString;

Stores the string data used by the stream.

Protected myPosition: TStreamOffset;

Stores the current position within the stream.

Methods

Public constructor new(const str: AnsiString = ''); reintroduce; virtual;

Construct a new stream instance.

If str is provided, its value is used as the initial content of the stream. str is not written to the stream; it is simply assigned as the content, which provides a quick way to buffer a string and then iterate over its contents.

Public function init: boolean; override;

Initializer

Public function read(const count: TStreamIOSize; var dest): TStreamIOSize; override;

Read count bytes from the stream and store them in dest. See AStream.read for more information on this method.

Returns

The total number of bytes read from the stream.

Public function write(const count: TStreamIOSize; const source): TStreamIOSize; override;

Write count bytes from source to the stream. See AStream.write for more information on this method.

Note that no checks are made on the data to ensure that what is written will display well on text-based devices.

Returns

The total number of bytes written to the stream.

Public procedure clear; virtual;

Clear the stream. The contents of the underlying string are discarded, and AStringStream.position and AStringStream.length are both reset to zero (0). Subsequent attempts to read from the stream without first writing content to it will fail silently, and AStream.hasEnded will return True.

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

Construct a shallow copy of the other object.

This method overrides the behavior inherited from AnObject.shallowCopyFrom: it calls that method, then checks to see whether Other is an instance of AStringStream. If so, it copies the value of

from Other to Self, overwriting any values in Self. The end result in this case is a string stream that is an exact copy of Other.

Public function streamingLength: TStreamIOSize; virtual;

Determine the total amount of data, in bytes, that will be read or written when either AStringStream.readFrom or AStringStream.writeTo is called.

Public function writeTo(const Dest: AStream): TStreamIOSize; virtual;

Writes the content of the stream to Dest. See CanStream.writeTo for more information on this method.

This method calls AStream.writeString on Dest, which means the length and checksum of the underlying string are also written.

Returns

The total number of bytes written to Dest, which will include the length and checksum information for the underlying string.

Public function readFrom(const Source: AStream): TStreamIOSize; virtual;

Reads the contents of the stream from Source. See CanStream.readFrom for more information on this method.

This method calls AStream.readString on Source, which means that it will first read the length and checksum of the underlying string.

Returns

The total number of bytes read from Source, which will include the length and checksum information for the underlying string.

Public function toString: string; override;

Constructs and returns a string representation of the stream, suitable for output to a text-based device. See CanPrint.toString for more information on this method.

This method simply returns the value of AStringStream.text, which is the underlying content of the stream.

Public function printTo(const Dest: ATextOutputStream; prefix: AnsiString = ''; suffix: AnsiString = ''): TStreamIOSize; virtual;

Print a string representation of the stream to Dest. See CanPrint.printTo for more information on this method.

This method simply prints the value returned by a call to AStringStream.toString to Dest.

Returns

The total number of bytes printed to Dest.

Public function name: string; override;

Retrieve the name of the stream.

This method overrides the behavior inherited from AnObject.name: instead of return the class name of the stream, it returns the name set by a previous call to AStringStream.setName. If no such call has been made, an empty string is returned.

Public function setName(const newName: string): string; virtual;

Set the name of the stream.

This method allows the caller to set the name that is reported when AStringStream.name is called. This behavior is intended to allow a descriptive name to be given to instances of AStringStream for display in debug and diagnostic messages.

Returns

The previous value of AStringStream.name.

Public function position: TStreamOffset; override;

Retrieve the current position within the stream. This is the location at which the next read or write operation will occur.

See AStream.position for more information on this method.

Public function setPosition(newPosition: TStreamOffset): TStreamOffset; override;

Set the current position within the stream. This is the location at which the next read or write operation will occur.

See AStream.setPosition for more information on this method.

Returns

The previous position within the stream.

Public function length: TStreamLength; override;

Retrieve the current size of the stream, in bytes. See AStream.length for more information on this method.

This method returns the length of the underlying string, as determined by a call to System.length.

Public function text: AnsiString; virtual;

Retrieve the content of the stream. The string returned by this routine will contain all values written to the stream, or the content of the string specified by a call to AStringStream.setText.

Public function setText(const newText: AnsiString): AnsiString; virtual;

Set the content of the stream. The current content of the stream will be discarded in favor of the value specified by newText. AStream.position will be reset to zero (0), and AStream.length will return the length of newText.

Returns

The old content of the stream.


Generated by PasDoc 0.13.0 on 2015-06-23 19:40:11