unit Unit2;
interface
uses
System.SysUtils,
System.Classes;
type
TMyHelpToTBytes = record helper for TBytes
private
function MyGetLen: integer;
public
property MyLen: integer read MyGetLen;
function MyGetBytes(const AStream: TStream; const AStart: integer = 0; ACount: integer = 32): TBytes;
function MyBytesAsString: string;
end;
implementation
{ TMyHelpToTBytes }
function TMyHelpToTBytes.MyGetLen: integer;
begin
result := Length(Self);
end;
function TMyHelpToTBytes.MyGetBytes(const AStream: TStream; const AStart: integer = 0; ACount: integer = 32): TBytes;
begin
result := [];
//
if (AStream = nil) or {(AStart < 0 ) or} (ACount < 1) then
exit;
//
// maybe some protection here to avoid "AV"...?
if ((AStart + ACount) > AStream.Size) then
ACount := AStream.Size - AStart;
//
if ACount > 0 then
begin
Setlength(result, ACount);
//
AStream.Position := AStart;
AStream.Read(result, ACount);
end;
end;
function TMyHelpToTBytes.MyBytesAsString: string;
begin
result := '';
//
for var B in Self do
result := result + ',(' + B.ToString + ')';
//
result := result.Remove(0, 1);
end;
end.
uses
Unit2;
procedure TForm1.Button1Click(Sender: TObject);
var
MyBytes : TBytes;
MyStream: TMemoryStream;
begin
Memo1.Text := 'MyBytes Len = ' + MyBytes.MyLen.ToString + ', time: ' + TimeToStr(now);
//
MyStream := TMemoryStream.Create;
try
MyStream.LoadFromFile('..\..\Unit1.pas');
//
MyBytes := MyBytes.MyGetBytes(MyStream, random(100), random(1000));
//
Memo1.Lines.Add('MyBytes Len = ' + MyBytes.MyLen.ToString);
Memo1.Lines.Add(MyBytes.MyBytesAsString);
//
MyBytes := MyBytes.MyGetBytes(MyStream, random(100), random(500));
//
Memo1.Lines.Add('MyBytes Len = ' + MyBytes.MyLen.ToString);
Memo1.Lines.Add(MyBytes.MyBytesAsString);
//
MyBytes := MyBytes.MyGetBytes(MyStream, random(100), random(1500));
//
Memo1.Lines.Add('MyBytes Len = ' + MyBytes.MyLen.ToString);
Memo1.Lines.Add(MyBytes.MyBytesAsString);
finally
MyStream.Free;
end;
end;
initialization
ReportMemoryLeaksOnShutdown := true;
end.