- Timestamp:
- Jul 20, 2018, 10:25:06 AM (6 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 6 6 compiled 7 7 heaptrclog.trc 8 LazFuck.dbg
-
- Property svn:ignore
-
trunk/Packages/TemplateGenerics/Additional/UBinarySerializer.pas
r72 r93 1 1 unit UBinarySerializer; 2 2 3 {$mode objfpc}{$H+}3 {$mode delphi}{$H+} 4 4 5 5 interface … … 15 15 TBinarySerializer = class 16 16 private 17 FStream: TStreamByte; 17 FGrow: Boolean; 18 FList: TListByte; 18 19 FEndianness: TEndianness; 19 20 SwapData: Boolean; 21 procedure SetList(const AValue: TListByte); 20 22 procedure SetEndianness(const AValue: TEndianness); 21 23 procedure ReverseByteOrder(var Buffer; Count: Integer); 22 24 public 25 Position: Integer; 26 OwnsList: Boolean; 27 procedure Write(var Buffer; Count: Integer); //inline; 28 procedure Read(var Buffer; Count: Integer); //inline; 29 23 30 procedure Assign(Source: TBinarySerializer); 24 31 procedure WriteByte(Data: Byte); … … 48 55 procedure ReadStream(AStream: TStream; Count: Integer); 49 56 procedure ReadStreamPart(AStream: TStream; Count: Integer); 57 procedure ReadList(List: TListByte; StartIndex, Count: Integer); 50 58 constructor Create; overload; 51 constructor Create(AStream: TStreamByte); overload;52 59 procedure Clear; 53 60 destructor Destroy; override; 54 61 property Endianness: TEndianness read FEndianness write SetEndianness; 55 property Stream: TStreamByte read FStream write FStream; 62 property List: TListByte read FList write SetList; 63 property Grow: Boolean read FGrow write FGrow; 56 64 end; 57 65 … … 65 73 StringLength: Longint; 66 74 begin 67 StringLength 68 FStream.ReadBuffer(StringLength, SizeOf(StringLength));75 StringLength := 0; 76 Read(StringLength, SizeOf(StringLength)); 69 77 Result := ReadString(StringLength); 70 78 end; … … 76 84 OldPosition: Integer; 77 85 begin 78 OldPosition := FStream.Position;86 OldPosition := Position; 79 87 Result := ''; 80 88 I := 1; 81 89 repeat 82 if FStream.Position >= FStream.Sizethen Break;90 if Position >= FList.Count then Break; 83 91 Data := Chr(ReadByte); 84 92 if Data <> Terminator[I] then begin … … 89 97 if not (I > Length(Terminator)) then begin 90 98 Result := ''; 91 FStream.Position := OldPosition;99 Position := OldPosition; 92 100 end; 93 101 end; … … 96 104 begin 97 105 Result := 0; 98 FStream.ReadBuffer(Result, SizeOf(Byte));106 Read(Result, SizeOf(Byte)); 99 107 end; 100 108 … … 102 110 begin 103 111 Result := 0; 104 FStream.ReadBuffer(Result, SizeOf(Cardinal));112 Read(Result, SizeOf(Cardinal)); 105 113 if SwapData then Result := SwapEndian(Result); 106 114 end; … … 109 117 begin 110 118 Result := 0; 111 FStream.ReadBuffer(Result, SizeOf(Int64));119 Read(Result, SizeOf(Int64)); 112 120 if SwapData then Result := SwapEndian(Result); 113 121 end; … … 117 125 if Length > 0 then begin 118 126 SetLength(Result, Length); 119 FStream.ReadBuffer(Result[1], Length);127 Read(Result[1], Length); 120 128 end else Result := ''; 121 129 end; … … 126 134 begin 127 135 Count := 0; 128 FStream.ReadBuffer(Count, 1);136 Read(Count, 1); 129 137 Result := ReadString(Count); 130 138 end; … … 136 144 if Count > 0 then begin 137 145 SetLength(Buffer, Count); 138 FStream.ReadBuffer(Buffer[0], Count);146 Read(Buffer[0], Count); 139 147 AStream.Size := Count; 140 148 AStream.Position := 0; … … 149 157 if Count > 0 then begin 150 158 SetLength(Buffer, Count); 151 FStream.ReadBuffer(Buffer[0], Count);159 Read(Buffer[0], Count); 152 160 if AStream.Size < (AStream.Position + Count) then 153 161 AStream.Size := AStream.Position + Count; 154 AStream.Write(Buffer[0], Count); 162 Write(Buffer[0], Count); 163 end; 164 end; 165 166 procedure TBinarySerializer.ReadList(List: TListByte; StartIndex, Count: Integer 167 ); 168 var 169 Buffer: array of Byte; 170 begin 171 if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size 172 if Count > 0 then begin 173 SetLength(Buffer, Count); 174 Read(Pointer(Buffer)^, Count); 175 List.ReplaceBuffer(StartIndex, Pointer(Buffer)^, Count); 155 176 end; 156 177 end; … … 164 185 SetLength(Buffer, Count); 165 186 AStream.ReadBuffer(Pointer(Buffer)^, Count); 166 FStream.WriteBuffer(Pointer(Buffer)^, Count);187 Write(Pointer(Buffer)^, Count); 167 188 end; 168 189 end; … … 172 193 Buffer: array of Byte; 173 194 begin 174 if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size175 if Count > 0 then begin 176 SetLength(Buffer, Count); 177 List. ReadBuffer(Pointer(Buffer)^, Count);178 FStream.WriteBuffer(Pointer(Buffer)^, Count);195 // if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size 196 if Count > 0 then begin 197 SetLength(Buffer, Count); 198 List.GetBuffer(StartIndex, PByte(Buffer)^, Count); 199 Write(Pointer(Buffer)^, Count); 179 200 end; 180 201 end; … … 184 205 inherited; 185 206 Endianness := enLittle; 186 FStream := nil; 187 end; 188 189 constructor TBinarySerializer.Create(AStream: TStreamByte); 190 begin 191 inherited Create; 192 Endianness := enLittle; 193 FStream := AStream; 207 FList := nil; 208 FGrow := True; 194 209 end; 195 210 196 211 procedure TBinarySerializer.Clear; 197 212 begin 198 Stream.Size := 0; 213 FList.Count := 0; 214 Position := 0; 199 215 end; 200 216 201 217 destructor TBinarySerializer.Destroy; 202 218 begin 219 if OwnsList then FList.Free; 203 220 inherited Destroy; 204 221 end; … … 212 229 begin 213 230 Result := 0; 214 FStream.ReadBuffer(Result, SizeOf(Double));231 Read(Result, SizeOf(Double)); 215 232 end; 216 233 … … 218 235 begin 219 236 Result := 0; 220 FStream.ReadBuffer(Result, SizeOf(Single));237 Read(Result, SizeOf(Single)); 221 238 end; 222 239 … … 224 241 begin 225 242 Result := 0; 226 FStream.ReadBuffer(Result, SizeOf(Word));243 Read(Result, SizeOf(Word)); 227 244 if SwapData then Result := SwapEndian(Result); 245 end; 246 247 procedure TBinarySerializer.SetList(const AValue: TListByte); 248 begin 249 if OwnsList then FList.Free; 250 FList := AValue; 228 251 end; 229 252 … … 254 277 end; 255 278 279 procedure TBinarySerializer.Write(var Buffer; Count: Integer); 280 var 281 NewCount: Integer; 282 begin 283 if FGrow then begin 284 NewCount := Position + Count; 285 if FList.Count < NewCount then 286 FList.Count := NewCount; 287 end; 288 FList.ReplaceBuffer(Position, Buffer, Count); 289 Inc(Position, Count); 290 end; 291 292 procedure TBinarySerializer.Read(var Buffer; Count: Integer); 293 begin 294 FList.GetBuffer(Position, Buffer, Count); 295 Inc(Position, Count); 296 end; 297 256 298 procedure TBinarySerializer.Assign(Source: TBinarySerializer); 257 299 begin 258 F Stream := Source.FStream;300 FList := Source.FList; 259 301 end; 260 302 … … 270 312 procedure TBinarySerializer.WriteByte(Data: Byte); 271 313 begin 272 Data := 0; 273 FStream.WriteBuffer(Data, SizeOf(Byte)); 314 Write(Data, SizeOf(Byte)); 274 315 end; 275 316 … … 307 348 SetLength(Buffer, Count); 308 349 AStream.ReadBuffer(Pointer(Buffer)^, Count); 309 FStream.WriteBuffer(Pointer(Buffer)^, Count);350 Write(Pointer(Buffer)^, Count); 310 351 end; 311 352 end;
Note:
See TracChangeset
for help on using the changeset viewer.