Changeset 291 for CoolStreaming/UBitStream.pas
- Timestamp:
- Nov 4, 2011, 11:46:15 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
CoolStreaming/UBitStream.pas
r249 r291 8 8 9 9 uses 10 Classes, SysUtils, RtlConsts, Math ;10 Classes, SysUtils, RtlConsts, Math, UMemory; 11 11 12 12 type … … 47 47 TMemoryBitStream = class(TBitStream) 48 48 private 49 F Stream: TMemoryStream;49 FMemory: TPositionMemory; 50 50 FPosition: LongInt; 51 51 FSize: LongInt; … … 61 61 constructor Create; 62 62 destructor Destroy; override; 63 property Stream: TMemoryStream read FStream;63 property Memory: TPositionMemory read FMemory; 64 64 end; 65 65 … … 175 175 function TBitStream.ReadNumber(Count: Byte): QWord; 176 176 begin 177 Result := 0; 177 178 Read(Result, Count); 178 179 Result := Result and ((QWord(1) shl Count) - 1); … … 224 225 begin 225 226 FSize := AValue; 226 Stream.Size := Ceil(AValue / 8);227 FMemory.Size := Ceil(AValue / 8); 227 228 if FPosition > FSize then FPosition := FSize; 228 229 end; … … 251 252 ByteCount := Ceil(Count / 8); 252 253 PosInByte := FPosition mod 8; 253 Stream.Position := Trunc(FPosition / 8);254 Data := Stream.ReadByte; // Read first byte254 FMemory.Position := Trunc(FPosition / 8); 255 Data := FMemory.ReadByte; // Read first byte 255 256 for I := 0 to ByteCount - 1 do begin 256 257 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; 259 261 if PosInByte > 0 then 260 262 TBytes(Buffer)[I] := TBytes(Buffer)[I] or … … 293 295 BitCount := Count; 294 296 ByteCount := Ceil(Count / 8); 295 Stream.Position := Trunc(FPosition / 8);297 FMemory.Position := Trunc(FPosition / 8); 296 298 BytePos := FPosition mod 8; 297 299 I := 0; 298 300 while (I < ByteCount) or (RestBitCount > 0) do begin 299 301 WriteBitCount := Min(8 - BytePos, BitCount); 300 if ( Stream.Position < Stream.Size) and (WriteBitCount < 8) then begin301 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; 303 305 end else Data := 0; 304 306 … … 311 313 // Write part up to one byte from source to target 312 314 Dec(BitCount, WriteToByte(Data, TBytes(Buffer)[I], BytePos, WriteBitCount)); 313 Stream.WriteByte(Data);315 FMemory.WriteByte(Data); 314 316 315 317 RestBitCount := Min(8 - WriteBitCount, BitCount); … … 334 336 constructor TMemoryBitStream.Create; 335 337 begin 336 F Stream := TMemoryStream.Create;338 FMemory := TPositionMemory.Create; 337 339 FPosition := 0; 338 340 FSize := 0; … … 341 343 destructor TMemoryBitStream.Destroy; 342 344 begin 343 F Stream.Free;345 FMemory.Free; 344 346 inherited Destroy; 345 347 end;
Note:
See TracChangeset
for help on using the changeset viewer.