1. 程式人生 > >合並(分割)流

合並(分割)流

read class data pri sizeof mod end group split

合並(分割)流

/// <author>cxg 2018-9-14</author>
/// 合並流&分割流

unit uStream;

interface

uses
  System.SysUtils, System.Classes;

type
  /// <summary>
  /// stream.size type: int64 (8 bytes)
  /// </summary>
  TSize = packed record
    size: int64;
  end;

type
  TynStream = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }
    /// <summary>
    /// 將AStream1和AStream2兩個流合並為dest一個流,
    /// </summary>
    /// <param name="ADest">合並後的流</param>
    /// <param name="AStream1">流1</param>
    /// <param name="AStream2">流2</param>
    class procedure MergeStream(ADest, AStream1, AStream2: TStream);
    /// <summary>
    /// 將source一個流,分為stream1, stream2兩個流,
    /// </summary>
    /// <param name="ASource">待分割的流</param>
    /// <param name="AStream1">流1</param>
    /// <param name="AStream2">流2</param>
    class procedure SplitStream(ASource, AStream1, AStream2: TStream);
  end;

var
  ynStream: TynStream;

implementation

{%CLASSGROUP ‘Vcl.Controls.TControl‘}

{$R *.dfm}

class procedure TynStream.MergeStream(ADest, AStream1, AStream2: TStream);
var
  LSize: TSize;
begin
  ADest.Position := 0;
  LSize.size := AStream1.Size;
  ADest.Write(LSize, SizeOf(Int64));
  LSize.size := AStream2.Size;
  ADest.Write(LSize, SizeOf(Int64));
  AStream1.Position := 0;
  ADest.CopyFrom(AStream1, AStream1.Size);
  AStream2.Position := 0;
  ADest.CopyFrom(AStream2, AStream2.Size);
end;

class procedure TynStream.SplitStream(ASource, AStream1, AStream2: TStream);
var
  LCount, LCount2: Int64;
  LSize: TSize;
begin
  ASource.Position := 0;
  ASource.Read(LSize, SizeOf(Int64));
  LCount := LSize.size;
  ASource.Read(LSize, SizeOf(Int64));
  LCount2 := LSize.size;
  AStream1.Position := 0;
  AStream1.CopyFrom(ASource, LCount);
  AStream2.Position := 0;
  AStream2.CopyFrom(ASource, LCount2);
end;

end.

  

合並(分割)流