Home  |  Delphi .net Home  |  System.IO.MemoryStream  |  ToArray Method
ToArray  
Method  
Extracts the current Memory Streaminto an Array of Bytes
MemoryStream Class
System.IO NameSpace
CF1.  Function ToArray ( ) : Array of Byte;
CF : Methods with this mark are Compact Framework Compatible
Description
The whole stream is extracted into an array of Bytes, whose size is dynamically determined by this method.
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);

  // Get the stream into a byte array
  ByteArray := Memory.ToArray;

  // 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
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author