MemoryStream

Qualified name: delphivcl.MemoryStream

class MemoryStream

Bases: CustomMemoryStream

TMemoryStream is a stream that stores its data in dynamic memory. Use TMemoryStream to store data in a dynamic memory buffer that is enhanced with file-like access capabilities. TMemoryStream provides the general I/O capabilities of a stream object while introducing methods and properties to manage a dynamic memory buffer. Memory streams are useful as intermediary objects that can hold information as well as read it from or write it to another storage medium. They provide a useful format for comparing the contents of streams, or for manipulating data that is stored in a less accessible medium.

Methods

Clear

Frees the memory buffer, discarding all data associated with the memory stream.

LoadFromFile

Loads the entire contents of a file into the memory buffer.

LoadFromStream

Loads the entire contents of a stream into the memory buffer.

ReadBytes

Read content as bytes.

ReadFloat

Read content as float.

ReadInt

Read content as integer.

ReadString

Read content as string.

SetSize

Sets the Size property of the memory stream.

Write

Writes Count bytes from Buffer to the current position in the memory buffer and updates the current position by Count bytes.

WriteBytes

Write content as bytes.

WriteFloat

Write content as float.

WriteInt

Write content as integer.

WriteString

Write content as string.

Attributes

Capabilities

StreamCapabilities:

ClassName

Returns the TObject.ClassName

Memory

Provides direct access to the memory pool allocated for the memory stream.

Position

Indicates the current offset into the stream for reading and writing.

Size

Indicates the size in bytes of the stream.

ClassName

Returns the TObject.ClassName

Clear()

Frees the memory buffer, discarding all data associated with the memory stream. Use Clear to empty the memory buffer for the memory stream and free all associated memory. In addition to freeing the memory associated with the memory buffer, Clear:

Sets the Memory property to nil (Delphi) or NULL (C++). Sets the Position property to 0. Sets the Size property to 0.

LoadFromFile(FileName: str) None

Loads the entire contents of a file into the memory buffer. Use LoadFromFile to fill the memory stream with the contents of a file. Pass the name of the file as the FileName parameter. LoadFromFile allows an application to read the contents of a file into the memory stream without having to explicitly create and free a file stream object. LoadFromFile reallocates the memory buffer so that the contents of the file will exactly fit. It sets the Size property accordingly, and then reads the entire contents of the file into the memory buffer. Thus, LoadFromFile will discard any pre-existing data stored in the memory stream.

LoadFromStream(Stream: Stream) None

Loads the entire contents of a stream into the memory buffer. Use LoadFromStream to fill the memory stream with the contents of the stream specified by the Stream parameter. LoadFromStream always sets the Position of the source stream to 0, before streaming in the number of bytes indicated by the source stream’s Size property. LoadFromStream reallocates the memory buffer so that the contents of the source stream will exactly fit. It sets the Size property accordingly, and then reads the entire contents of the source stream into the memory buffer. Thus, LoadFromStream will discard any pre-existing data stored in the memory stream. If the source stream is a TFileStream object, LoadFromStream does the same thing as LoadFromFile, except that the application must create and free the TFileStream object. LoadFromStream also allows applications to fill a memory stream object from other types of stream objects.

ReadBytes()

Read content as bytes.

ReadFloat()

Read content as float.

ReadInt()

Read content as integer.

ReadString()

Read content as string.

SetSize(NewSize: int) None

Sets the Size property of the memory stream. Use SetSize to set the Size of a memory stream before filling it with data. SetSize allocates the memory buffer to hold NewSize bytes, preserving as much of the existing data as possible. Use SetSize before filling the memory buffer with data from various sources, or from a portion of another stream. If the intended contents of the memory stream is exactly the same as the contents of another stream or file, use LoadFromStream or LoadFromFile instead. Sets the Size property of the memory stream. Use SetSize to set the Size of a memory stream before filling it with data. SetSize allocates the memory buffer to hold NewSize bytes, preserving as much of the existing data as possible. Use SetSize before filling the memory buffer with data from various sources, or from a portion of another stream. If the intended contents of the memory stream is exactly the same as the contents of another stream or file, use LoadFromStream or LoadFromFile instead.

Write(Buffer, Count: int) int

Writes Count bytes from Buffer to the current position in the memory buffer and updates the current position by Count bytes. Use Write to insert Count bytes into the memory buffer of the memory stream, starting at the current position. Write will increase the size of the memory buffer, if necessary, to accommodate the data being written in. If the current position is not the end of the memory buffer, Write will overwrite the data following the current position. Write updates the Size property to Position + Count, and sets the Position property to the new value of Size. Thus, any data that was stored in the memory stream in the Count bytes after the current position is lost when calling Write. Write always writes the Count bytes in the Buffer, unless there is a memory failure. Thus, for TMemoryStream, Write is equivalent to the WriteBuffer method. All other data-writing methods of a memory stream (WriteBuffer, WriteComponent) call Write to do the actual writing.

WriteBytes()

Write content as bytes.

WriteFloat()

Write content as float.

WriteInt()

Write content as integer.

WriteString()

Write content as string.