Changeset 291


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.
Location:
CoolStreaming
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • CoolStreaming/CoolStreaming.lpk

    r250 r291  
    11<?xml version="1.0"?>
    22<CONFIG>
    3   <Package Version="3">
     3  <Package Version="4">
    44    <PathDelim Value="\"/>
    55    <Name Value="CoolStreaming"/>
     6    <AddToProjectUsesSection Value="True"/>
    67    <Author Value="Chronos"/>
    78    <CompilerOptions>
    8       <Version Value="9"/>
     9      <Version Value="10"/>
    910      <PathDelim Value="\"/>
    1011      <SearchPaths>
    1112        <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
    1213      </SearchPaths>
     14      <Linking>
     15        <Debugging>
     16          <GenerateDebugInfo Value="True"/>
     17          <DebugInfoType Value="dsAuto"/>
     18        </Debugging>
     19      </Linking>
    1320      <Other>
    1421        <CompilerPath Value="$(CompPath)"/>
     
    4956    </Files>
    5057    <Type Value="RunAndDesignTime"/>
    51     <RequiredPkgs Count="3">
     58    <RequiredPkgs Count="4">
    5259      <Item1>
    53         <PackageName Value="LCL"/>
     60        <PackageName Value="Common"/>
    5461      </Item1>
    5562      <Item2>
    56         <PackageName Value="TemplateGenerics"/>
     63        <PackageName Value="LCL"/>
    5764      </Item2>
    5865      <Item3>
     66        <PackageName Value="TemplateGenerics"/>
     67      </Item3>
     68      <Item4>
    5969        <PackageName Value="FCL"/>
    6070        <MinVersion Major="1" Valid="True"/>
    61       </Item3>
     71      </Item4>
    6272    </RequiredPkgs>
    6373    <UsageOptions>
  • 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;
  • CoolStreaming/UStreamHelper.pas

    r269 r291  
    3939    function ReadCardinal: Cardinal;
    4040    function ReadInt64: Int64;
    41     function ReadString: string;
     41    function ReadString(Length: Integer): string;
    4242    function ReadShortString: string;
    4343    function ReadAnsiString: string;
     
    7373begin
    7474  FStream.ReadBuffer(StringLength, SizeOf(StringLength));
    75   SetLength(Result, StringLength);
    76   if StringLength > 0 then begin
    77     FStream.ReadBuffer(Result[1], StringLength);
    78   end;
     75  Result := ReadString(StringLength);
    7976end;
    8077
     
    119116end;
    120117
    121 function TStreamHelper.ReadString:string;
    122 begin
    123   SetLength(Result, FStream.Size - FStream.Position);
    124   if (FStream.Size - FStream.Position) > 0 then
    125     FStream.Read(Result[1], FStream.Size - FStream.Position)
    126     else Result := '';
     118function TStreamHelper.ReadString(Length: Integer): string;
     119begin
     120  if Length > 0 then begin
     121    SetLength(Result, Length);
     122    FStream.Read(Result[1], Length);
     123  end else Result := '';
    127124end;
    128125
     
    132129begin
    133130  FStream.ReadBuffer(Count, 1);
    134   SetLength(Result, Count);
    135   FStream.ReadBuffer(Result[1], Count);
     131  Result := ReadString(Count);
    136132end;
    137133
Note: See TracChangeset for help on using the changeset viewer.