title: Reading bytes from stream, and convert to string
body: <p>Im having a problem while reading a stream from a Client. Im using Delphi XE3 with Indy 10.
  This Client, send me a tagged text data stream, that is in XML format. My problem start when i read data with a TIdTCPServer. With this code:</p>
  
  <pre><code>if Acontext.Connection.IoHandler.InputbufferisEmpty then
  begin
   Acontext.Connection.IoHandler.CheckforDataonSource(1000);
   Acontext.Connection.IoHandler.CheckForDisconnect;
   if Acontext.Connection.IoHandler.InputbufferisEmpty then Exit; 
  end;
  Acontext.Connection.Iohandler.ReadBytes(Abuffer,      
    Acontext.connection.ioHandler.Inputbuffer.size);
  data := BytesToString(Abuffer, Tencoding.bigendianUnicode);
  </code></pre>
  
  <p>Data is encoded in BigEndianUnicode. I noticed that when im recieving data, the server read and show me the data in 2 'blocks' diferents. If the server recieve the same data all the time, when i parse with BytesToString the data get "cut" at same char everytime.</p>
  
  <p>Here is a result of what im getting:</p>
  
  <pre><code>&lt;rootNode&gt;
    &lt;Node1&gt;&lt;/Node1&gt;
    &lt;Node2&gt;&lt;/Node2&gt;
    &lt;
     Node3&gt;&lt;/Node3&gt;
    &lt;Node4&gt;&lt;/Node4&gt;
  </code></pre>
  
  <p></p>
  
  <p>The problem get bigger, when i assign that WideString to an XMLDocument. I get an Invalid at the top level of the document error.</p>
  
  <p>Sometimes, i run the server and get all the data correct, without this error, but when i close the aplication and run it again, the server recieve the data corrupted.</p>
  
  <p>Sorry, for my english. I need some help with this.</p>