Ignore:
Timestamp:
Nov 28, 2012, 7:49:20 AM (12 years ago)
Author:
chronos
Message:
  • Modified: CoolStreaming TStreamHelper can own stream.
  • Added: TVarBlockSerializer support Double and Buffer type.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CoolStreaming/UStreamHelper.pas

    r360 r457  
    66
    77uses
    8   Classes, DateUtils, syncobjs, SysUtils;
     8  Classes, DateUtils, SysUtils;
    99
    1010type
     
    2222    procedure SetEndianness(const AValue: TEndianness);
    2323    procedure SetItem(Index: Integer; const AValue: Byte);
     24    procedure SetStream(AValue: TStream);
    2425  public
    2526    procedure Assign(Source: TStreamHelper);
     
    6263    function Write(const Buffer; Count: Longint): Longint; override;
    6364    property Endianness: TEndianness read FEndianness write SetEndianness;
    64     property Stream: TStream read FStream write FStream;
     65    property Stream: TStream read FStream write SetStream;
    6566    property Items[Index: Integer]: Byte read GetItem write SetItem; default;
     67    property OwnStream: Boolean read FOwnStream write FOwnStream;
    6668  end;
    6769
     
    7476  StringLength: Longint;
    7577begin
     78  StringLength := 0;
    7679  FStream.ReadBuffer(StringLength, SizeOf(StringLength));
    7780  Result := ReadString(StringLength);
     
    103106function TStreamHelper.ReadByte: Byte;
    104107begin
     108  Result := 0;
    105109  FStream.ReadBuffer(Result, SizeOf(Byte));
    106110end;
     
    108112function TStreamHelper.ReadCardinal: Cardinal;
    109113begin
     114  Result := 0;
    110115  FStream.ReadBuffer(Result, SizeOf(Cardinal));
    111116  if SwapData then Result := Swap(Result);
     
    114119function TStreamHelper.ReadInt64: Int64;
    115120begin
     121  Result := 0;
    116122  FStream.ReadBuffer(Result, SizeOf(Int64));
    117123  if SwapData then Result := Swap(Result);
     
    130136  Count: Byte;
    131137begin
     138  Count := 0;
    132139  FStream.ReadBuffer(Count, 1);
    133140  Result := ReadString(Count);
     
    135142
    136143procedure TStreamHelper.ReadStream(AStream: TStream; Count: Integer);
    137 var
    138   Buffer: array of Byte;
    139 begin
    140   if Count > 0 then begin
     144//var
     145//  Buffer: array of Byte;
     146begin
     147  AStream.Position := 0;
     148  AStream.CopyFrom(Self, Count);
     149  (*if Count > 0 then begin
    141150    SetLength(Buffer, Count);
    142151    FStream.ReadBuffer(Buffer[0], Count);
     
    144153    AStream.Position := 0;
    145154    AStream.Write(Buffer[0], Count);
    146   end;
     155  end;*)
    147156end;
    148157
     
    259268function TStreamHelper.ReadDouble: Double;
    260269begin
     270  Result := 0;
    261271  FStream.ReadBuffer(Result, SizeOf(Double));
    262272end;
     
    264274function TStreamHelper.ReadSingle: Single;
    265275begin
     276  Result := 0;
    266277  FStream.ReadBuffer(Result, SizeOf(Single));
    267278end;
     
    285296function TStreamHelper.ReadWord: Word;
    286297begin
     298  Result := 0;
    287299  FStream.ReadBuffer(Result, SizeOf(Word));
    288300  if SwapData then Result := Swap(Result);
     
    311323end;
    312324
     325procedure TStreamHelper.SetStream(AValue: TStream);
     326begin
     327  if FStream = AValue then Exit;
     328  if FOwnStream and Assigned(FStream) then FStream.Free;
     329  FStream := AValue;
     330  FOwnStream := False;
     331end;
     332
    313333procedure TStreamHelper.Assign(Source: TStreamHelper);
    314334var
     
    361381
    362382procedure TStreamHelper.WriteStream(AStream: TStream; Count: Integer);
    363 var
    364   Buffer: array of Byte;
    365 begin
    366   if Count > AStream.Size then Count := AStream.Size; // Limit max. stream size
     383//var
     384//  Buffer: array of Byte;
     385begin
     386  AStream.Position := 0;
     387  CopyFrom(AStream, Count);
     388  (*if Count > AStream.Size then Count := AStream.Size; // Limit max. stream size
    367389  AStream.Position := 0;
    368390  if Count > 0 then begin
     
    370392    AStream.Read(Buffer[0], Count);
    371393    FStream.Write(Buffer[0], Count);
    372   end;
     394  end;*)
    373395end;
    374396
Note: See TracChangeset for help on using the changeset viewer.