Home  |  Delphi .net Home  |  System.IO.MemoryStream  |  Read Method
Read  
Method  
Returns a block of bytes from the current position in the Memory Stream
MemoryStream Class
System.IO NameSpace
CF1.  Function Read ( Bytes:Array of ByteBytes : Array of Byte; TargetStartIndex : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The Read method tries to read Count bytes from the current Memory Stream position into the target Bytes array, starting at TargetStartIndex.
 
The actual count of bytes written to the array is returned.
 
The Memory stream position is updated accordingly.
Notes
Warning : an exception is thrown if the array does not have capacity for the next block of bytes - even if that number of bytes may not be returned.
Microsoft MSDN Links
System.IO
System.IO.MemoryStream
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

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

begin
  // Create our Memory Stream
  Memory := System.IO.MemoryStream.Create;

  // Write to the stream
  Memory.WriteByte(1);
  Memory.WriteByte(2);
  Memory.WriteByte(3);
  Memory.WriteByte(4);
  Memory.WriteByte(5);
  Memory.WriteByte(6);
  Memory.WriteByte(7);
  Memory.WriteByte(8);
  Memory.WriteByte(9);

  // Move to the start of the stream
  Memory.Seek(0, SeekOrigin.Begin);

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

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

  // Display our array contents
  for Index := 0 to Length(ByteArray)-1 do
    Console.WriteLine(ByteArray[Index].ToString);

  // Close the memory stream
  Memory.Close;

  Console.Readline;
end.
Show full unit code
  1
  2
  3
  4
  5
  6
  7
  8
  9
  0
  0
  0
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author