Does StringOf make a copy of the data passed to it?
Yes, according to the docs: 'Converts a byte array into a Unicode string using the default system locale.'
If you just want to access the TBytes as a string, why not cast it to a PChar (if it's Unicode) or PAnsiChar if it's an AnsiString?
Example code:
var
MyBuffer: TBytes;
BufferLength: integer;
BufferAsString: PChar;
BuferAsAnsiString: PAnsiChar;
begin
MyBuffer:= TFile.ReadAllBytes(Filename);
BufferLength:= SizeOf(MyBuffer);
BufferAsString:= PChar(@MyBuffer[0]);
BufferAsAnsiString:= PAnsiChar(@MyBuffer[0]);
//if there's no #0 at the end, make sure not to read past the end of the buffer!
EDIT
I'm a bit puzzled, why you're not just using TFile.OpenRead
to get a FileStream.
Let's assume you've got gigabyte(s) of data and you're in a hurry.
The Filestream will allow you to just read a small chunk of the data speeding things up.
This example code reads the whole file, but can easily be modified to only get a small part:
var
MyData: TFileStream
MyString: string; {or AnsiString}
FileSize: integer;
Index: integer;
begin
MyData:= TFile.OpenRead(Filename);
try
FileSize:= MyData.GetSize;
SetLength(MyString,FileSize+1); //Preallocate the string;
Index:= 0;
MyData.Read(PChar(MyString[Index])^, FileSize);
finally
MyData.Free;
end;
//Do stuff with your newly read string.
Note that the last example still reads all data from disk first (which may or may not be what your want).
However you can also read the data in chunks.
All of this is simpler with AnsiStrings because 1 char = 1 byte there :-).