source: Generics/NativeGenerics/Specialized/SpecializedStream.pas

Last change on this file was 496, checked in by chronos, 7 years ago
  • Modified: New native generics classes working under FPC 3.0 transformed from TemplateGenerics package.
File size: 4.2 KB
Line 
1unit SpecializedStream;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, SpecializedList, DateUtils, GenericStream, GenericList;
9
10type
11 TStreamByte = class(TGStream<Byte>)
12 function ReadBuffer(var Buffer; Count: Integer): Integer; virtual; abstract;
13 function WriteBuffer(var Buffer; Count: Integer): Integer; virtual; abstract;
14 end;
15
16 { TMemoryStreamByte }
17
18 TMemoryStreamByte = class(TStreamByte)
19 private
20 FList: TGList<Byte>;
21 FOwnsList: Boolean;
22 FPosition: Integer;
23 public
24 procedure Assign(Source: TGStream<Byte>); override;
25 procedure Write(Item: Byte); override;
26 procedure WriteArray(Values: array of Byte); override;
27 procedure WriteList(List: TListByte);
28 function WriteBuffer(var Buffer; Count: Integer): Integer; override;
29 procedure WriteStream(Stream: TGStream<Byte>; Count: Integer); override;
30 function Read: Byte; override;
31 function ReadArray(Count: Integer): TItemArray; override;
32 function ReadList(List: TListByte; Count: Integer): Integer;
33 function ReadBuffer(var Buffer; Count: Integer): Integer; override;
34 function ReadStream(Stream: TGStream<Byte>; Count: Integer): Integer; override;
35 function Insert(Count: Integer): Integer; override;
36 function Remove(Count: Integer): Integer; override;
37 function Seek(Offset: Integer; Origin: TSeekOrigin = soCurrent): Integer; override;
38 constructor Create; override; overload;
39 constructor Create(AList: TListByte); overload;
40 destructor Destroy; override;
41 property OwnsList: Boolean read FOwnsList write FOwnsList;
42 property List: TGList<Byte> read FList;
43 end;
44
45
46implementation
47
48{ TMemoryStreamByte }
49
50procedure TMemoryStreamByte.Assign(Source: TGStream<Byte>);
51begin
52 inherited;
53 if Source is TMemoryStreamByte then begin
54 FList.Assign(TMemoryStreamByte(Source).FList);
55 FPosition := TMemoryStreamByte(Source).FPosition;
56 end;
57end;
58
59procedure TMemoryStreamByte.Write(Item: Byte);
60begin
61 if FList.Count < (FPosition + 1) then
62 FList.Count := FPosition + 1;
63 FList[FPosition] := Item;
64 Inc(FPosition);
65end;
66
67procedure TMemoryStreamByte.WriteArray(Values: array of Byte);
68begin
69 if FList.Count < (FPosition + Length(Values)) then
70 FList.Count := FPosition + Length(Values);
71 FList.ReplaceArray(FPosition, Values);
72 Inc(FPosition, Length(Values));
73end;
74
75procedure TMemoryStreamByte.WriteList(List: TListByte);
76begin
77 FList.ReplaceList(FPosition, List);
78end;
79
80procedure TMemoryStreamByte.WriteStream(Stream: TGStream<Byte>; Count: Integer);
81begin
82
83end;
84
85function TMemoryStreamByte.WriteBuffer(var Buffer; Count: Integer): Integer;
86begin
87
88end;
89
90function TMemoryStreamByte.Read: Byte;
91begin
92 Result := FList[FPosition];
93 Inc(FPosition);
94end;
95
96function TMemoryStreamByte.ReadArray(Count: Integer): TItemArray;
97begin
98 Result := FList.GetArray(FPosition, Count);
99end;
100
101function TMemoryStreamByte.ReadList(List: TListByte; Count: Integer): Integer;
102begin
103 if (FPosition + Count) > FList.Count then
104 Count := FList.Count - FPosition;
105 FList.GetList(List, FPosition, Count);
106 Result := Count;
107end;
108
109function TMemoryStreamByte.ReadBuffer(var Buffer; Count: Integer): Integer;
110begin
111
112end;
113
114function TMemoryStreamByte.ReadStream(Stream: TGStream<Byte>; Count: Integer
115 ): Integer;
116begin
117
118end;
119
120function TMemoryStreamByte.Insert(Count: Integer): Integer;
121begin
122 FList.InsertCount(FPosition, Count);
123 Result := Count;
124end;
125
126function TMemoryStreamByte.Remove(Count: Integer): Integer;
127begin
128 Result := FList.Count - FPosition;
129 if Count < Result then Result := Count;
130 FList.DeleteItems(FPosition, Count);
131end;
132
133function TMemoryStreamByte.Seek(Offset: Integer; Origin: TSeekOrigin): Integer;
134begin
135 case Origin of
136 soBegin: FPosition := Offset;
137 soCurrent: FPosition := FPosition + Offset;
138 soEnd: FPosition := FList.Count + Offset;
139 end;
140 if FPosition > FList.Count then FPosition := FList.Count;
141 if FPosition < 0 then FPosition := 0;
142 Result := FPosition;
143end;
144
145constructor TMemoryStreamByte.Create;
146begin
147 inherited;
148 FList := TListByte.Create;
149 OwnsList := True;
150end;
151
152constructor TMemoryStreamByte.Create(AList: TListByte);
153begin
154 inherited Create;
155 FList := AList;
156 OwnsList := False;
157end;
158
159destructor TMemoryStreamByte.Destroy;
160begin
161 if OwnsList then FList.Free;
162 inherited Destroy;
163end;
164
165
166end.
Note: See TracBrowser for help on using the repository browser.