Class TPeekCharStream
Unit
CastleClassUtils
Declaration
type TPeekCharStream = class(TStream)
Description
Abstract class to read another stream, always being able to back one character. This is a purely sequential read-only stream. This means that calling Write, Seek (changing Position) or setting Size will always cause an exception with appropriate descendant of EStreamNotImplemented.
Getting Size and Position is allowed. Getting Size is simply implemented by getting SourceStream.Size (so it works if the underlying source stream supports getting Size). Getting Position always works correctly, as it's just the number of characters read from this stream. The fact that we may read ahead in the underlying source stream (for buffering, for peeking) is completely hidden.
We do not assume anything about the underlying source stream, in particular the underlying source stream may also be purely sequential (so we can only read from it, and we cannot predict when it ends).
The main advantage of using this class is that you get PeekChar routine: you can always peek ahead one character in the stream, without reading it (i.e. next time you will call Read or ReadBuffer you will still get that char). And it works for all source streams, including the ones where seeking is not allowed (so Seek(-1, soCurrent) would not work).
Hierarchy
Overview
Methods
 |
function GetSize: Int64; override; |
 |
procedure SetSize(NewSize: Longint); override; |
 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
 |
function Write(const Buffer; Count: Longint): Longint; override; |
 |
function PeekChar: Integer; virtual; abstract; |
 |
function ReadChar: Integer; virtual; abstract; |
 |
function ReadUpto(const EndingChars: TSetOfChars): string; virtual; abstract; |
 |
constructor Create(ASourceStream: TStream; AOwnsSourceStream: boolean); |
 |
destructor Destroy; override; |
Properties
Description
Methods
 |
function GetSize: Int64; override; |
.
Returns
SourceStream.Size |
 |
procedure SetSize(NewSize: Longint); override; |
This stream doesn't support setting size. (All other versions of SetSize also call this.)
Exceptions raised
- EStreamNotImplementedSetSize
- Always.
|
 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
This stream doesn't support seeking. (SetPosition and all other versions of Seek also call this.)
Exceptions raised
- EStreamNotImplementedSeek
- Always.
|
 |
function Write(const Buffer; Count: Longint): Longint; override; |
This stream doesn't support writing. (WriteBuffer also calls this.)
Exceptions raised
- EStreamNotImplementedWrite
- Always.
|
 |
function PeekChar: Integer; virtual; abstract; |
Peek next character. Returns the next character without making it "already read", so the next call to Read or ReadBuffer will return this char. Subsequent calls to PeekChar (without any Read/ReadBuffer between) will also return the same character.
Returns -1 if stream ended, otherwise returns Ord or given char.
This may call SourceStream.Read, and any exceptions that may be raised in SourceStream.Read are propagated higher from this method.
|
 |
function ReadChar: Integer; virtual; abstract; |
Read next character. A shortcut for Read(c, 1), but it returns -1 if end of stream is reached. So it's consistent with PeekChar. Sometimes it's also more comfortable, and it's a little faster.
|
 |
function ReadUpto(const EndingChars: TSetOfChars): string; virtual; abstract; |
Read characters, until one of EndingChars (or end of stream) is found. The ending character is not "consumed" from the stream. The Result is guaranteed to not contain any char from EndingChars.
|
 |
constructor Create(ASourceStream: TStream; AOwnsSourceStream: boolean); |
|
 |
destructor Destroy; override; |
|
Properties
 |
property SourceStream: TStream read FSourceStream; |
Underlying stream.
|
 |
property OwnsSourceStream: boolean
read FOwnsSourceStream write FOwnsSourceStream; |
Should we free underlying SourceStream at destruction.
|
Generated by PasDoc 0.14.0.
|