合并(分割)流
/// <author>cxg 2020-3-15</author>
unit uStream;
interface
uses
SysUtils, Classes;
type
TStreamArray = array of TStream;
type
TynStream = class
private
{ Private declarations }
public
{ Public declarations }
class procedure MergeStream(ms: TStream; mss: TStreamArray);
class procedure SplitStream(ms: TStream; mss: TStreamArray);
end;
var
ynStream: TynStream;
implementation
class procedure TynStream.MergeStream(ms: TStream; mss: TStreamArray);
var
LSize: Int64;
lnum: Integer;
i: Integer;
begin
ms.Position := 0;
lnum := High(mss);
ms.Write(LNum, SizeOf(Integer)); //有几个流
for i := 0 to LNum - 1 do
begin
LSize := mss[i].Size;
ms.Write(LSize, SizeOf(Int64)); // 流的大小
ms.CopyFrom(mss[i], LSize);
end;
end;
class procedure TynStream.SplitStream(ms: TStream; mss: TStreamArray);
var
i: Integer;
LSize: Int64;
LNum: Integer;
begin
ms.Position := 0;
ms.Read(LNum, SizeOf(Integer)); //有几个流
SetLength(mss, LNum);
for i := 0 to LNum - 1 do
begin
ms.Read(LSize, SizeOf(Int64)); //流的大小
mss[i] := TMemoryStream.Create;
mss[i].CopyFrom(ms, LSize);
end;
end;
end.
interface
uses
SysUtils, Classes;
type
TStreamArray = array of TStream;
type
TynStream = class
private
{ Private declarations }
public
{ Public declarations }
class procedure MergeStream(ms: TStream; mss: TStreamArray);
class procedure SplitStream(ms: TStream; mss: TStreamArray);
end;
var
ynStream: TynStream;
implementation
class procedure TynStream.MergeStream(ms: TStream; mss: TStreamArray);
var
LSize: Int64;
lnum: Integer;
i: Integer;
begin
ms.Position := 0;
lnum := High(mss);
ms.Write(LNum, SizeOf(Integer)); //有几个流
for i := 0 to LNum - 1 do
begin
LSize := mss[i].Size;
ms.Write(LSize, SizeOf(Int64)); // 流的大小
ms.CopyFrom(mss[i], LSize);
end;
end;
class procedure TynStream.SplitStream(ms: TStream; mss: TStreamArray);
var
i: Integer;
LSize: Int64;
LNum: Integer;
begin
ms.Position := 0;
ms.Read(LNum, SizeOf(Integer)); //有几个流
SetLength(mss, LNum);
for i := 0 to LNum - 1 do
begin
ms.Read(LSize, SizeOf(Int64)); //流的大小
mss[i] := TMemoryStream.Create;
mss[i].CopyFrom(ms, LSize);
end;
end;
end.