Home  |  Delphi .net Home  |  System.IO.BufferedStream  |  SetLength Method
SetLength  
Method  
Sets the length of the stream (not the buffer it uses)
BufferedStream Class
System.IO NameSpace
NotCF1.  Procedure SetLength ( StreamSize : Int64; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The current BufferedStream is contracted or expanded to the specified StreamSize.
 
Data is lost from the end of the stream is it is contracted.
 
If expanded, the added bytes have no predictable value.
Notes
When testing this method, expanding the stream seemed to have no discernable effect. Extending a 5 byte stream to 10 bytes, seeking to offset -1 from Origin.End and writing another byte seemed to have no effect - the stream still appeared to have only 5 bytes.
Microsoft MSDN Links
System.IO
System.IO.BufferedStream
 
 
Reducing the size of the stream
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Memory    : System.IO.MemoryStream;
  Stream    : System.IO.BufferedStream;
  ByteAsInt : 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(1);
  Stream.WriteByte(2);
  Stream.WriteByte(3);
  Stream.WriteByte(4);
  Stream.WriteByte(5);

  // Now set the stream to a length of just 2 bytes
  Stream.SetLength(2);

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

  // Now display the contents
  ByteAsInt := Stream.ReadByte;

  while ByteAsInt > 0 do
  begin
    Console.WriteLine(ByteAsInt.ToString);
    ByteAsInt := Stream.ReadByte;
  end;

  // Close the stream
  Stream.Close;

  Console.Readline;
end.
Show full unit code
  1
  2
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author