Home  |  Delphi .net Home  |  System.IO.BufferedStream  |  Read Method
  
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
Description
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.
Notes
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
System.IO
System.IO.BufferedStream
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Reading blocks of bytes
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

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

begin
  // 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(66);
  Stream.WriteByte(67);
  Stream.WriteByte(68);
  Stream.WriteByte(69);
  Stream.WriteByte(70);
  Stream.WriteByte(71);
  Stream.WriteByte(72);
  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
  Console.WriteLine(ByteArray);

  // Close the stream
  Stream.Close;

  Console.Readline;
end.
   ABCDEFGHI
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author