VCL类学习之(八) TCustomMemoryStream

TCustomMemoryStream is an abstract base class used as the common ancestor for memory streams.
Unit
Classes
Description
Use TCustomMemoryStream as a base class when defining a stream object that can transfer data that is stored in memory. Memory streams are useful for providing file-like access to data that is stored in a less accessible medium. Data can be moved to an internal memory buffer when the memory stream is created. After manipulating the data in a memory stream, the data can be written out to its actual storage medium when the memory stream is destroyed.
Do not instantiate an instance of TCustomMemoryStream. It is an abstract class that implements behavior common to all memory streams. To work with an instance of a memory stream, use one of the descendants of TCustomMemoryStream, such as TMemoryStream or TResourceStream.
  1. { TCustomMemoryStream abstract class }
  2.   TCustomMemoryStream = class(TStream)
  3.   private
  4.     FMemory: Pointer;
  5.     FSize, FPosition: Longint;   //大小,当前位置
  6.   protected
  7.     procedure SetPointer(Ptr: Pointer; Size: Longint);
  8.   public
  9.     function Read(var Buffer; Count: Longint): Longint; override;
  10.     function Seek(Offset: Longint; Origin: Word): Longint; override;
  11.     procedure SaveToStream(Stream: TStream);
  12.     procedure SaveToFile(const FileName: string);
  13.     property Memory: Pointer read FMemory;
  14.   end;
  15. { TCustomMemoryStream }
  16. procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; Size: Longint);
  17. begin
  18.   FMemory := Ptr;
  19.   FSize := Size;
  20. end;
  21. function TCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
  22. begin
  23.   if (FPosition >= 0and (Count >= 0then
  24.   begin
  25.     Result := FSize - FPosition;
  26.     if Result > 0 then
  27.     begin
  28.       if Result > Count then Result := Count;
  29.       Move(Pointer(Longint(FMemory) + FPosition)^, Buffer, Result);    //复制内存
  30.       Inc(FPosition, Result);
  31.       Exit;
  32.     end;
  33.   end;
  34.   Result := 0;
  35. end;
  36. function TCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
  37. begin
  38.   case Origin of
  39.     soFromBeginning: FPosition := Offset;
  40.     soFromCurrent: Inc(FPosition, Offset);
  41.     soFromEnd: FPosition := FSize + Offset;
  42.   end;
  43.   Result := FPosition;
  44. end;
  45. procedure TCustomMemoryStream.SaveToStream(Stream: TStream);
  46. begin
  47.   if FSize <> 0 then Stream.WriteBuffer(FMemory^, FSize);
  48. end;
  49. procedure TCustomMemoryStream.SaveToFile(const FileName: string);
  50. var
  51.   Stream: TStream;
  52. begin
  53.   Stream := TFileStream.Create(FileName, fmCreate);
  54.   try
  55.     SaveToStream(Stream);
  56.   finally
  57.     Stream.Free;
  58.   end;
  59. end;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值