TByteArrayWrapper

Overview

The TByteArrayWrapper class allows you to use an arbitrary chunk of bytes as a read-only TStream.

Uses case

program TByteArrayWrapper_Demo;
{$APPTYPE CONSOLE}

uses
  Classes, SysUtils, uROIpTcpServer;

const
  USE_WIDE_STRING = True;

{$IF USE_WIDE_STRING}
  type    StringType = WideString;  StringElem = WideChar;
{$ELSE}
  type    StringType = AnsiString;  StringElem = AnsiChar;
{$IFEND}

procedure doDemo;
type
  TDataRepository = array [0..3] of TByteArray;
var
  storage         : TByteArrayWrapper;
  dataRepository  : TDataRepository;
  i, len          : integer;
  s               : StringType;
begin
  for i := Low (dataRepository) to High (dataRepository) do
  begin
    s := Format ('line %*d', [i + 2, i]);
    len := Length (s) * sizeof (StringElem);
    SetLength (dataRepository [i], len);
    Move (PChar (s)^, dataRepository [i] [0], len);
    WriteLn (QuotedStr (s), ' was stored at ', i);
  end;

  for i := High (dataRepository) downto Low (dataRepository) do
  begin
    storage := TByteArrayWrapper.Create (dataRepository [i]);
    len := storage.Size;
    SetLength (s, len div sizeof (StringElem));
    storage.Read (PChar (s)^, len);
    storage.Free;
    WriteLn (QuotedStr (s), ' was read from ', i);
  end;
end;

begin
  doDemo;
end.
 

Location

Instance Methods


constructor Create

Initializes the instance to use aData as a source of data.

constructor Create(const aData: TByteArray)

Parameters:

  • aData:

Read  override

Implements the abstract TStream.Read method by copying Count bytes into the Buffer variable. If there are not enough unread bytes, only the available size will be copied. Returns the number of bytes actually read.

function Read(var Buffer: ; Count: Integer): Integer

Parameters:

  • Buffer:
  • Count:

Seek  override

Moves the current position within the memory stream by the indicated offset.

Seek returns the new value of the Position property.
Note: Seek does no error checking on the value provided for Offset. Do not call seek with an offset that would move the current position to less than 0 (before the start of Memory) or greater than Size (beyond the end of the memory buffer).

function Seek(Offset: Integer; Origin: Word): Integer

Parameters:

  • Offset: If Offset is a negative number, the seek goes backward from the specified origin.
  • Origin: Direction: ** soFromBeginning - Offset is from the beginning of the Memory. Seek moves to the position Offset. Offset must be >= 0. ** soFromCurrent - Offset is from the current position. Seek moves to Position + Offset. ** soFromEnd - Offset is from the end of Memory. Offset must be <= 0 to indicate a number of bytes before the end of the memory buffer.

Write  override

Raises an Exception with the message Not Implemented.

function Write(const Buffer: ; Count: Integer): Integer

Parameters:

  • Buffer: Buffer
  • Count: Count