title: Why does TStringStream.Bytes differ from what one gets from 'TStream.Read'
body: <p>With TStringStream, the <code>bytes</code> using its <code>Bytes</code> property differs from the <code>bytes</code> extracted using <code>TStream.Read</code>. As shown below: </p>
  
  <ol>
  <li>the <code>bytes</code> extracted using <code>TStream.Read</code> represents correct data.</li>
  <li>the <code>bytes</code> using its <code>Bytes</code> property contains more data. <del>(the last byte of correct <code>bytes</code> is different from that of wrong <code>bytes</code>)</del></li>
  </ol>
  
  <p>Could you help to comment about the possible reason? Thank you very much for your help!</p>
  
  <p>PS: Delphi XE, Windows 7. (It seems TStringStream back in Delphi 7 doesn't have LoadFromFile or SaveToFile.)</p>
  
  <p>PS: The sample files can be download from SkyDrive: <a href="http://sdrv.ms/LzLxlZ" rel="nofollow">REF_EncodedSample &amp; REF_DecodedSample</a>. (Zlib-compressed image file.). </p>
  
  <pre><code>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;
  </code></pre>