1 | unit SpecializedStream;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, SpecializedList, DateUtils, GenericStream, GenericList;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
46 | implementation
|
---|
47 |
|
---|
48 | { TMemoryStreamByte }
|
---|
49 |
|
---|
50 | procedure TMemoryStreamByte.Assign(Source: TGStream<Byte>);
|
---|
51 | begin
|
---|
52 | inherited;
|
---|
53 | if Source is TMemoryStreamByte then begin
|
---|
54 | FList.Assign(TMemoryStreamByte(Source).FList);
|
---|
55 | FPosition := TMemoryStreamByte(Source).FPosition;
|
---|
56 | end;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TMemoryStreamByte.Write(Item: Byte);
|
---|
60 | begin
|
---|
61 | if FList.Count < (FPosition + 1) then
|
---|
62 | FList.Count := FPosition + 1;
|
---|
63 | FList[FPosition] := Item;
|
---|
64 | Inc(FPosition);
|
---|
65 | end;
|
---|
66 |
|
---|
67 | procedure TMemoryStreamByte.WriteArray(Values: array of Byte);
|
---|
68 | begin
|
---|
69 | if FList.Count < (FPosition + Length(Values)) then
|
---|
70 | FList.Count := FPosition + Length(Values);
|
---|
71 | FList.ReplaceArray(FPosition, Values);
|
---|
72 | Inc(FPosition, Length(Values));
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TMemoryStreamByte.WriteList(List: TListByte);
|
---|
76 | begin
|
---|
77 | FList.ReplaceList(FPosition, List);
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure TMemoryStreamByte.WriteStream(Stream: TGStream<Byte>; Count: Integer);
|
---|
81 | begin
|
---|
82 |
|
---|
83 | end;
|
---|
84 |
|
---|
85 | function TMemoryStreamByte.WriteBuffer(var Buffer; Count: Integer): Integer;
|
---|
86 | begin
|
---|
87 |
|
---|
88 | end;
|
---|
89 |
|
---|
90 | function TMemoryStreamByte.Read: Byte;
|
---|
91 | begin
|
---|
92 | Result := FList[FPosition];
|
---|
93 | Inc(FPosition);
|
---|
94 | end;
|
---|
95 |
|
---|
96 | function TMemoryStreamByte.ReadArray(Count: Integer): TItemArray;
|
---|
97 | begin
|
---|
98 | Result := FList.GetArray(FPosition, Count);
|
---|
99 | end;
|
---|
100 |
|
---|
101 | function TMemoryStreamByte.ReadList(List: TListByte; Count: Integer): Integer;
|
---|
102 | begin
|
---|
103 | if (FPosition + Count) > FList.Count then
|
---|
104 | Count := FList.Count - FPosition;
|
---|
105 | FList.GetList(List, FPosition, Count);
|
---|
106 | Result := Count;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | function TMemoryStreamByte.ReadBuffer(var Buffer; Count: Integer): Integer;
|
---|
110 | begin
|
---|
111 |
|
---|
112 | end;
|
---|
113 |
|
---|
114 | function TMemoryStreamByte.ReadStream(Stream: TGStream<Byte>; Count: Integer
|
---|
115 | ): Integer;
|
---|
116 | begin
|
---|
117 |
|
---|
118 | end;
|
---|
119 |
|
---|
120 | function TMemoryStreamByte.Insert(Count: Integer): Integer;
|
---|
121 | begin
|
---|
122 | FList.InsertCount(FPosition, Count);
|
---|
123 | Result := Count;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | function TMemoryStreamByte.Remove(Count: Integer): Integer;
|
---|
127 | begin
|
---|
128 | Result := FList.Count - FPosition;
|
---|
129 | if Count < Result then Result := Count;
|
---|
130 | FList.DeleteItems(FPosition, Count);
|
---|
131 | end;
|
---|
132 |
|
---|
133 | function TMemoryStreamByte.Seek(Offset: Integer; Origin: TSeekOrigin): Integer;
|
---|
134 | begin
|
---|
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;
|
---|
143 | end;
|
---|
144 |
|
---|
145 | constructor TMemoryStreamByte.Create;
|
---|
146 | begin
|
---|
147 | inherited;
|
---|
148 | FList := TListByte.Create;
|
---|
149 | OwnsList := True;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | constructor TMemoryStreamByte.Create(AList: TListByte);
|
---|
153 | begin
|
---|
154 | inherited Create;
|
---|
155 | FList := AList;
|
---|
156 | OwnsList := False;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | destructor TMemoryStreamByte.Destroy;
|
---|
160 | begin
|
---|
161 | if OwnsList then FList.Free;
|
---|
162 | inherited Destroy;
|
---|
163 | end;
|
---|
164 |
|
---|
165 |
|
---|
166 | end.
|
---|