blob: c08f5eaefde0d2a7ae87e99f65c449df69d8daa5 [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20unit Thrift.Stream;
21
22interface
23
24uses
25 Classes,
26 SysUtils,
27 SysConst,
28 RTLConsts,
29 Thrift.Utils,
30 ActiveX;
31
32type
33
34 IThriftStream = interface
35 ['{732621B3-F697-4D76-A1B0-B4DD5A8E4018}']
36 procedure Write( const buffer: TBytes; offset: Integer; count: Integer);
37 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer;
38 procedure Open;
39 procedure Close;
40 procedure Flush;
41 function IsOpen: Boolean;
42 function ToArray: TBytes;
43 end;
44
45 TThriftStreamImpl = class( TInterfacedObject, IThriftStream)
46 private
47 procedure CheckSizeAndOffset( const buffer: TBytes; offset: Integer; count: Integer);
48 protected
49 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); virtual;
50 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; virtual;
51 procedure Open; virtual; abstract;
52 procedure Close; virtual; abstract;
53 procedure Flush; virtual; abstract;
54 function IsOpen: Boolean; virtual; abstract;
55 function ToArray: TBytes; virtual; abstract;
56 end;
57
58 TThriftStreamAdapterDelphi = class( TThriftStreamImpl )
59 private
60 FStream : TStream;
61 FOwnsStream : Boolean;
62 protected
63 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
64 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
65 procedure Open; override;
66 procedure Close; override;
67 procedure Flush; override;
68 function IsOpen: Boolean; override;
69 function ToArray: TBytes; override;
70 public
Roger Meier333bbf32012-01-08 21:51:08 +000071 constructor Create( const AStream: TStream; AOwnsStream : Boolean);
Jake Farrell7ae13e12011-10-18 14:35:26 +000072 destructor Destroy; override;
73 end;
74
75 TThriftStreamAdapterCOM = class( TThriftStreamImpl)
76 private
77 FStream : IStream;
78 protected
79 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
80 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
81 procedure Open; override;
82 procedure Close; override;
83 procedure Flush; override;
84 function IsOpen: Boolean; override;
85 function ToArray: TBytes; override;
86 public
Roger Meier333bbf32012-01-08 21:51:08 +000087 constructor Create( const AStream: IStream);
Jake Farrell7ae13e12011-10-18 14:35:26 +000088 end;
89
90implementation
91
92{ TThriftStreamAdapterCOM }
93
94procedure TThriftStreamAdapterCOM.Close;
95begin
96 FStream := nil;
97end;
98
Roger Meier333bbf32012-01-08 21:51:08 +000099constructor TThriftStreamAdapterCOM.Create( const AStream: IStream);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000100begin
Jens Geyer718f6ee2013-09-06 21:02:34 +0200101 inherited Create;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000102 FStream := AStream;
103end;
104
105procedure TThriftStreamAdapterCOM.Flush;
106begin
107 if IsOpen then
108 begin
109 if FStream <> nil then
110 begin
111 FStream.Commit( STGC_DEFAULT );
112 end;
113 end;
114end;
115
116function TThriftStreamAdapterCOM.IsOpen: Boolean;
117begin
118 Result := FStream <> nil;
119end;
120
121procedure TThriftStreamAdapterCOM.Open;
122begin
123
124end;
125
126function TThriftStreamAdapterCOM.Read( var buffer: TBytes; offset: Integer; count: Integer): Integer;
127begin
128 inherited;
129 Result := 0;
130 if FStream <> nil then
131 begin
132 if count > 0 then
133 begin
134 FStream.Read( @buffer[offset], count, @Result);
135 end;
136 end;
137end;
138
139function TThriftStreamAdapterCOM.ToArray: TBytes;
140var
141 statstg: TStatStg;
142 len : Integer;
143 NewPos : Int64;
144 cbRead : Integer;
145begin
146 FillChar( statstg, SizeOf( statstg), 0);
147 len := 0;
148 if IsOpen then
149 begin
150 if Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) then
151 begin
152 len := statstg.cbSize;
153 end;
154 end;
155
156 SetLength( Result, len );
157
158 if len > 0 then
159 begin
160 if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then
161 begin
162 FStream.Read( @Result[0], len, @cbRead);
163 end;
164 end;
165end;
166
167procedure TThriftStreamAdapterCOM.Write( const buffer: TBytes; offset: Integer; count: Integer);
168var
169 nWritten : Integer;
170begin
171 inherited;
172 if IsOpen then
173 begin
174 if count > 0 then
175 begin
176 FStream.Write( @buffer[0], count, @nWritten);
177 end;
178 end;
179end;
180
181{ TThriftStreamImpl }
182
183procedure TThriftStreamImpl.CheckSizeAndOffset(const buffer: TBytes; offset,
184 count: Integer);
185var
186 len : Integer;
187begin
188 if count > 0 then
189 begin
190 len := Length( buffer );
191 if (offset < 0) or ( offset >= len) then
192 begin
193 raise ERangeError.Create( SBitsIndexError );
194 end;
195 if count > len then
196 begin
197 raise ERangeError.Create( SBitsIndexError );
198 end;
199 end;
200end;
201
202function TThriftStreamImpl.Read(var buffer: TBytes; offset,
203 count: Integer): Integer;
204begin
205 Result := 0;
206 CheckSizeAndOffset( buffer, offset, count );
207end;
208
209procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer);
210begin
211 CheckSizeAndOffset( buffer, offset, count );
212end;
213
214{ TThriftStreamAdapterDelphi }
215
216procedure TThriftStreamAdapterDelphi.Close;
217begin
218 FStream.Free;
219 FStream := nil;
220 FOwnsStream := False;
221end;
222
Roger Meier333bbf32012-01-08 21:51:08 +0000223constructor TThriftStreamAdapterDelphi.Create( const AStream: TStream; AOwnsStream: Boolean);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000224begin
Jens Geyer718f6ee2013-09-06 21:02:34 +0200225 inherited Create;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000226 FStream := AStream;
227 FOwnsStream := AOwnsStream;
228end;
229
230destructor TThriftStreamAdapterDelphi.Destroy;
231begin
232 if FOwnsStream then
233 begin
234 FStream.Free;
235 end;
236 inherited;
237end;
238
239procedure TThriftStreamAdapterDelphi.Flush;
240begin
241
242end;
243
244function TThriftStreamAdapterDelphi.IsOpen: Boolean;
245begin
246 Result := FStream <> nil;
247end;
248
249procedure TThriftStreamAdapterDelphi.Open;
250begin
251
252end;
253
254function TThriftStreamAdapterDelphi.Read(var buffer: TBytes; offset,
255 count: Integer): Integer;
256begin
257 inherited;
258 Result := 0;
259 if count > 0 then
260 begin
261 Result := FStream.Read( Pointer(@buffer[offset])^, count)
262 end;
263end;
264
265function TThriftStreamAdapterDelphi.ToArray: TBytes;
266var
267 OrgPos : Integer;
268 len : Integer;
269begin
270 len := 0;
271 if FStream <> nil then
272 begin
273 len := FStream.Size;
274 end;
275
276 SetLength( Result, len );
277
278 if len > 0 then
279 begin
280 OrgPos := FStream.Position;
281 try
282 FStream.Position := 0;
283 FStream.ReadBuffer( Pointer(@Result[0])^, len );
284 finally
285 FStream.Position := OrgPos;
286 end;
287 end
288end;
289
290procedure TThriftStreamAdapterDelphi.Write(const buffer: TBytes; offset,
291 count: Integer);
292begin
293 inherited;
294 if count > 0 then
295 begin
296 FStream.Write( Pointer(@buffer[offset])^, count)
297 end;
298end;
299
300end.