问题描述
使用TStringStream,使用其 Bytes
属性的 bytes
与 bytes 使用
TStream.Read
提取的code>。如下所示:
With TStringStream, the bytes
using its Bytes
property differs from the bytes
extracted using TStream.Read
. As shown below:
- 使用
TStream提取的
代表正确的数据。个字节
读 -
字节
使用其Bytes
属性包含更多数据。
- the
bytes
extracted usingTStream.Read
represents correct data. - the
bytes
using itsBytes
property contains more data.
您能否评论可能的原因?
Could you help to comment about the possible reason? Thank you very much for your help!
PS:Delphi XE,Windows7。(看来Delphi 7中的TStringStream没有LoadFromFile或SaveToFile。)
PS: Delphi XE, Windows 7. (It seems TStringStream back in Delphi 7 doesn't have LoadFromFile or SaveToFile.)
PS:可以从SkyDrive下载示例文件:。 (Zlib压缩的图像文件。)。
PS: The sample files can be download from SkyDrive: REF_EncodedSample & REF_DecodedSample. (Zlib-compressed image file.).
procedure CompareBytes_2;
var
ss_1: TStringStream;
ss_2: TStringStream;
sbytes_Read: TBytes;
sbytes_Property: TBytes;
len_sbytes_Read: Integer;
len_sbytes_Property: Integer;
filename: string;
begin
filename := 'REF_EncodedSample'; // textual data
// filename := 'REF_DecodedSample'; // non-textual data
ss_1 := TStringStream.Create;
ss_1.LoadFromFile(filename);
ss_2 := TStringStream.Create;
ss_2.LoadFromFile(filename);
ss_1.SaveToFile(filename+ '_CopyByStringStream_1');
ss_2.SaveToFile(filename+ '_CopyByStringStream_2');
len_sbytes_Read := ss_1.Size;
SetLength(sbytes_Read, len_sbytes_Read);
ss_1.Read(sbytes_Read[0], len_sbytes_Read);
sbytes_Property := ss_2.Bytes;
ShowMessage(
BoolToStr(
Length(sbytes_Read) = Length(sbytes_Property),
True));
ShowMessage(
BoolToStr(
sbytes_Read[len_sbytes_Read - 1] = sbytes_Property[len_sbytes_Read - 1],
True));
ss_1.Free;
ss_2.Free;
end;
推荐答案
字符串流状态:
假定缓冲区已分配为容纳更多的空间。实际需要。
Presumably the buffer has been allocated to hold more space than it actually needs. Only the first Size bytes of the buffer contain valid content.
此外,对ss_1的调用毫无意义。因为调用SetLength后Length(sbytes_Read)不变。从流中读取时,应使用ReadBuffer而不是Read。对于WriteBuffer也是如此。
Also, the call to ss_1.Read is a little pointless since Length(sbytes_Read) does not change after the call to SetLength. And when reading from a stream you are to use ReadBuffer rather than Read. Likewise for WriteBuffer.
这篇关于为什么TStringStream.Bytes与从'TStream.Read'获得的内容不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!