Home  |  Delphi .net Home  |  System.IO.BufferedStream  |  Read Method
Reads the next block of characters from the current stream
BufferedStream Class
System.IO NameSpace
NotCF1.  Function Read ( Bytes : Array of Byte; StartIndex : Integer; Count : Integer ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
If the current stream has not been exhausted a block of Count bytes are stored at StartIndex in the Bytes array.
The stream position is updated accordingly.
In the first syntax, the Read method returns the next stream character is returned as an integer. The integer value is the numeric equivalent of the character, or -1 if the stream was exhausted. Carriage return and Line-feed characters are returned by this method.
In the second syntax, an attempt is made to read up to Count characters. These characters are overlaid onto the Characters array from StartIndex. The returned integer value is the number of characters overlaid. Zero if the stream was already exhausted before the read. Unlike StringReader, in this 2nd syntax, Carriage return and Line-feed characters are returned by this syntax.
You must set the length of the target Character array, and it must have sufficient capacity to hold the returned characters.

Warning : be careful that the StartIndex and Count values do not exceed the Bytes array capacity - these parameters are checked (and an ArgumentException>/b> will be thrown if) before an attempt is made to read - even if the stream remainder will fit into the array.
Microsoft MSDN Links
 Author links

 Buy Website Traffic at

 Buy Proxies at
Reading blocks of bytes
program Project1;


  Memory    : System.IO.MemoryStream;
  Stream    : System.IO.BufferedStream;
  ByteArray : Array of Byte;
  Index     : Integer;

  // Create our memory stream
  Memory := System.IO.MemoryStream.Create;

  // Create out BufferedStream to access this memory stream
  Stream := System.IO.BufferedStream.Create(Memory);

  // Write to the memory stream using the BufferedStream
  Stream.WriteByte(65);  // Chr(56) = 'A'
  Stream.WriteByte(73);  // Chr(73) = 'I'

  // Now seek to the start of the stream
  Stream.Seek(0, SeekOrigin.Begin);

  // Declare our array to hold the returned data
  SetLength(ByteArray, 12);

  // Get the file contents 3 bytes at a time into our array
  Index := 0;
  while Stream.Read(ByteArray, Index, 3) > 0 do Index := Index + 3;

  // Display our array contents

  // Close the stream

Delphi Programming Neil Moffatt All rights reserved.  |  Contact the author