Ignore:
Timestamp:
Nov 4, 2011, 11:46:15 AM (13 years ago)
Author:
george
Message:
  • Modified: Method of StreamHelper for reading string data with defined length.
  • Modified: BitStream now use class TPositionMemory from package Common instead of TMemoryStream for storing data. TMemoryStream allocate too huge data (4 kB) even if only few bytes are used.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CoolStreaming/UBitStream.pas

    r249 r291  
    88
    99uses
    10   Classes, SysUtils, RtlConsts, Math;
     10  Classes, SysUtils, RtlConsts, Math, UMemory;
    1111
    1212type
     
    4747  TMemoryBitStream = class(TBitStream)
    4848  private
    49     FStream: TMemoryStream;
     49    FMemory: TPositionMemory;
    5050    FPosition: LongInt;
    5151    FSize: LongInt;
     
    6161    constructor Create;
    6262    destructor Destroy; override;
    63     property Stream: TMemoryStream read FStream;
     63    property Memory: TPositionMemory read FMemory;
    6464  end;
    6565
     
    175175function TBitStream.ReadNumber(Count: Byte): QWord;
    176176begin
     177  Result := 0;
    177178  Read(Result, Count);
    178179  Result := Result and ((QWord(1) shl Count) - 1);
     
    224225begin
    225226  FSize := AValue;
    226   Stream.Size := Ceil(AValue / 8);
     227  FMemory.Size := Ceil(AValue / 8);
    227228  if FPosition > FSize then FPosition := FSize;
    228229end;
     
    251252    ByteCount := Ceil(Count / 8);
    252253    PosInByte := FPosition mod 8;
    253     Stream.Position := Trunc(FPosition / 8);
    254     Data := Stream.ReadByte; // Read first byte
     254    FMemory.Position := Trunc(FPosition / 8);
     255    Data := FMemory.ReadByte; // Read first byte
    255256    for I := 0 to ByteCount - 1 do begin
    256257      TBytes(Buffer)[I] := (Data shr PosInByte) and ((1 shl (8 - PosInByte)) - 1);
    257       if (I < ByteCount) and (Stream.Position < Stream.Size) then
    258         Data := Stream.ReadByte else Data := 0;
     258      if (I < ByteCount) and (FMemory.Position < FMemory.Size) then begin
     259        Data := FMemory.ReadByte;
     260      end else Data := 0;
    259261      if PosInByte > 0 then
    260262        TBytes(Buffer)[I] := TBytes(Buffer)[I] or
     
    293295  BitCount := Count;
    294296  ByteCount := Ceil(Count / 8);
    295   Stream.Position := Trunc(FPosition / 8);
     297  FMemory.Position := Trunc(FPosition / 8);
    296298  BytePos := FPosition mod 8;
    297299  I := 0;
    298300  while (I < ByteCount) or (RestBitCount > 0) do begin
    299301    WriteBitCount := Min(8 - BytePos, BitCount);
    300     if (Stream.Position < Stream.Size) and (WriteBitCount < 8) then begin
    301       Data := Stream.ReadByte;
    302       Stream.Position := Stream.Position - 1;
     302    if (FMemory.Position < FMemory.Size) and (WriteBitCount < 8) then begin
     303      Data := FMemory.ReadByte;
     304      FMemory.Position := FMemory.Position - 1;
    303305    end else Data := 0;
    304306
     
    311313    // Write part up to one byte from source to target
    312314    Dec(BitCount, WriteToByte(Data, TBytes(Buffer)[I], BytePos, WriteBitCount));
    313     Stream.WriteByte(Data);
     315    FMemory.WriteByte(Data);
    314316
    315317    RestBitCount := Min(8 - WriteBitCount, BitCount);
     
    334336constructor TMemoryBitStream.Create;
    335337begin
    336   FStream := TMemoryStream.Create;
     338  FMemory := TPositionMemory.Create;
    337339  FPosition := 0;
    338340  FSize := 0;
     
    341343destructor TMemoryBitStream.Destroy;
    342344begin
    343   FStream.Free;
     345  FMemory.Free;
    344346  inherited Destroy;
    345347end;
Note: See TracChangeset for help on using the changeset viewer.