The TStream object is the ancestor for all streaming objects, i.e. objects that have the capability to
store and retrieve data.
It defines a number of methods that are common to all objects that implement streaming, many of
them are virtual, and are only implemented in the descendrnt types.
Programs should not instantiate objects of type TStream directly, but instead instantiate a
descendant type, such as TDosStream, TMemoryStream.
This is the full declaration of the TStream object:
TYPE
TStream = OBJECT (TObject)
Status : Integer; { Stream status }
ErrorInfo : Integer; { Stream error info }
StreamSize: LongInt; { Stream current size }
Position : LongInt; { Current position }
FUNCTION Get: PObject;
FUNCTION StrRead: PChar;
FUNCTION GetPos: Longint; Virtual;
FUNCTION GetSize: Longint; Virtual;
FUNCTION ReadStr: PString;
PROCEDURE Open (OpenMode: Word); Virtual;
PROCEDURE Close; Virtual;
PROCEDURE Reset;
PROCEDURE Flush; Virtual;
PROCEDURE Truncate; Virtual;
PROCEDURE Put (P: PObject);
PROCEDURE StrWrite (P: PChar);
PROCEDURE WriteStr (P: PString);
PROCEDURE Seek (Pos: LongInt); Virtual;
PROCEDURE Error (Code, Info: Integer); Virtual;
PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
PROCEDURE CopyFrom (Var S: TStream; Count: Longint);
END;
PStream = ^TStream;
|
-
Declaration:
- Function TStream.Get : PObject;
-
Description:
- Get reads an object definition from a stream, and returns a pointer to an instance of this
object.
-
Errors:
- On error, TStream.Status is set, and NIL is returned.
-
See also:
- Put (571)
Listing: objectex/ex9.pp
-
Declaration:
- Function TStream.StrRead: PChar;
-
Description:
- StrRead reads a string from the stream, allocates memory for it, and returns a pointer to
a null-terminated copy of the string on the heap.
-
Errors:
- On error, Nil is returned.
-
See also:
- StrWrite (572), ReadStr (569)
Listing: objectex/ex10.pp
-
Declaration:
- TSTream.GetPos : Longint; Virtual;
-
Description:
- If the stream’s status is stOk, GetPos returns the current position in the stream. Otherwise
it returns -1
-
Errors:
- -1 is returned if the status is an error condition.
-
See also:
- Seek (572), GetSize (568)
Listing: objectex/ex11.pp
-
Declaration:
- Function TStream.GetSize: Longint; Virtual;
-
Description:
- If the stream’s status is stOk then GetSize returns the size of the stream, otherwise it
returns -1.
-
Errors:
- -1 is returned if the status is an error condition.
-
See also:
- Seek (572), GetPos (568)
Listing: objectex/ex12.pp
-
Declaration:
- Function TStream.ReadStr: PString;
-
Description:
- ReadStr reads a string from the stream, copies it to the heap and returns a pointer to this
copy. The string is saved as a pascal string, and hence is NOT null terminated.
-
Errors:
- On error (e.g. not enough memory), Nil is returned.
-
See also:
- StrRead (567)
Listing: objectex/ex13.pp
-
Declaration:
- Procedure TStream.Open (OpenMode: Word); Virtual;
-
Description:
- Open is an abstract method, that should be overridden by descendent objects. Since opening
a stream depends on the stream’s type this is not surprising.
-
Errors:
- None.
-
See also:
- Close (570), Reset (571)
For an example, see TDosStream.Open (580).
-
Declaration:
- Procedure TStream.Close; Virtual;
-
Description:
- Close is an abstract method, that should be overridden by descendent objects. Since Closing
a stream depends on the stream’s type this is not surprising.
-
Errors:
- None.
-
See also:
- Open (570), Reset (571)
for an example, see TDosStream.Open (580).
-
Declaration:
- PROCEDURE TStream.Reset;
-
Description:
- Reset sets the stream’s status to 0, as well as the ErrorInfo
-
Errors:
- None.
-
See also:
- Open (570), Close (570)
-
Declaration:
- Procedure TStream.Flush; Virtual;
-
Description:
- Flush is an abstract method that should be overridden by descendent objects. It serves to
enable the programmer to tell streams that implement a buffer to clear the buffer.
-
Errors:
- None.
-
See also:
- Truncate (571)
for an example, see TBufStream.Flush (584).
-
Declaration:
- Procedure TStream.Truncate; Virtual;
-
Description:
- Truncate is an abstract procedure that should be overridden by descendent objects. It
serves to enable the programmer to truncate the size of the stream to the current file position.
-
Errors:
- None.
-
See also:
- Seek (572)
For an example, see TDosStream.Truncate (578).
-
Declaration:
- Procedure TStream.Put (P: PObject);
-
Description:
- Put writes the object pointed to by P. P should be non-nil. The object type must have been
registered with RegisterType (549).
After the object has been written, it can be read again with Get (566).
-
Errors:
- No check is done whether P is Nil or not. Passing Nil will cause a run-time error 216 to
be generated. If the object has not been registered, the status of the stream will be set to
stPutError.
-
See also:
- Get (566)
For an example, see TStream.Get (566);
-
Declaration:
- Procedure TStream.StrWrite (P: PChar);
-
Description:
- StrWrite writes the null-terminated string P to the stream. P can only be 65355 bytes long.
-
Errors:
- None.
-
See also:
- WriteStr (572), StrRead (567), ReadStr (569)
For an example, see TStream.StrRead (567).
-
Declaration:
- Procedure TStream.WriteStr (P: PString);
-
Description:
- StrWrite writes the pascal string pointed to by P to the stream.
-
Errors:
- None.
-
See also:
- StrWrite (572), StrRead (567), ReadStr (569)
For an example, see TStream.ReadStr (569).
-
Declaration:
- PROCEDURE TStream.Seek (Pos: LongInt); Virtual;
-
Description:
- Seek sets the position to Pos. This position is counted from the beginning, and is zero
based. (i.e. seeek(0) sets the position pointer on the first byte of the stream)
-
Errors:
- If Pos is larger than the stream size, Status is set to StSeekError.
-
See also:
- GetPos (568), GetSize (568)
For an example, see TDosStream.Seek (579).
-
Declaration:
- Procedure TStream.Error (Code, Info: Integer); Virtual;
-
Description:
- Error sets the stream’s status to Code and ErrorInfo to Info. If the StreamError
procedural variable is set, Error executes it, passing Self as an argument.
This method should not be called directly from a program. It is intended to be used in
descendent objects.
-
Errors:
- None.
-
See also:
-
Declaration:
- Procedure TStream.Read (Var Buf; Count: Sw_Word); Virtual;
-
Description:
- Read is an abstract method that should be overridden by descendent objects.
Read reads Count bytes from the stream into Buf. It updates the position pointer, increasing
it’s value with Count. Buf must be large enough to contain Count bytes.
-
Errors:
- No checking is done to see if Buf is large enough to contain Count bytes.
-
See also:
- Write (574), ReadStr (569), StrRead (567)
Listing: objectex/ex18.pp
-
Declaration:
- Procedure TStream.Write (Var Buf; Count: Sw_Word); Virtual;
-
Description:
- Write is an abstract method that should be overridden by descendent objects.
Write writes Count bytes to the stream from Buf. It updates the position pointer, increasing
it’s value with Count.
-
Errors:
- No checking is done to see if Buf actually contains Count bytes.
-
See also:
- Read (573), WriteStr (572), StrWrite (572)
For an example, see TStream.Read (573).
-
Declaration:
- Procedure TStream.CopyFrom (Var S: TStream; Count: Longint);
-
Description:
- CopyFrom reads Count bytes from stream S and stores them in the current stream. It uses
the Read (573) method to read the data, and the Write (574) method to write in the current
stream.
-
Errors:
- None.
-
See also:
- Read (573), Write (574)
Listing: objectex/ex19.pp