blob: 3308c53a55f62dffc5a652105af59a404615463e [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
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
Jens Geyer9f7f11e2016-04-14 21:37:11 +020022{$I Thrift.Defines.inc}
Nick4f5229e2016-04-14 16:43:22 +030023
Jens Geyerd5436f52014-10-03 19:50:38 +020024interface
25
26uses
27 Classes,
28 SysUtils,
29 SysConst,
30 RTLConsts,
Jens Geyer9f7f11e2016-04-14 21:37:11 +020031 {$IFDEF OLD_UNIT_NAMES}
32 ActiveX,
Nick4f5229e2016-04-14 16:43:22 +030033 {$ELSE}
Jens Geyer9f7f11e2016-04-14 21:37:11 +020034 Winapi.ActiveX,
35 {$ENDIF}
Nick4f5229e2016-04-14 16:43:22 +030036 Thrift.Utils;
Jens Geyerd5436f52014-10-03 19:50:38 +020037
38type
39
40 IThriftStream = interface
Jens Geyer17c3ad92017-09-05 20:31:27 +020041 ['{2A77D916-7446-46C1-8545-0AEC0008DBCA}']
42 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); overload;
43 procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); overload;
44 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; overload;
45 function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload;
Jens Geyerd5436f52014-10-03 19:50:38 +020046 procedure Open;
47 procedure Close;
48 procedure Flush;
49 function IsOpen: Boolean;
50 function ToArray: TBytes;
51 end;
52
53 TThriftStreamImpl = class( TInterfacedObject, IThriftStream)
54 private
Jens Geyer17c3ad92017-09-05 20:31:27 +020055 procedure CheckSizeAndOffset( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer); overload;
Jens Geyerd5436f52014-10-03 19:50:38 +020056 protected
Jens Geyer17c3ad92017-09-05 20:31:27 +020057 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); overload; inline;
58 procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); overload; virtual;
59 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; overload; inline;
60 function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload; virtual;
Jens Geyerd5436f52014-10-03 19:50:38 +020061 procedure Open; virtual; abstract;
62 procedure Close; virtual; abstract;
63 procedure Flush; virtual; abstract;
64 function IsOpen: Boolean; virtual; abstract;
65 function ToArray: TBytes; virtual; abstract;
66 end;
67
68 TThriftStreamAdapterDelphi = class( TThriftStreamImpl )
69 private
70 FStream : TStream;
71 FOwnsStream : Boolean;
72 protected
Jens Geyer17c3ad92017-09-05 20:31:27 +020073 procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override;
74 function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override;
Jens Geyerd5436f52014-10-03 19:50:38 +020075 procedure Open; override;
76 procedure Close; override;
77 procedure Flush; override;
78 function IsOpen: Boolean; override;
79 function ToArray: TBytes; override;
80 public
81 constructor Create( const AStream: TStream; AOwnsStream : Boolean);
82 destructor Destroy; override;
83 end;
84
85 TThriftStreamAdapterCOM = class( TThriftStreamImpl)
86 private
87 FStream : IStream;
88 protected
Jens Geyer17c3ad92017-09-05 20:31:27 +020089 procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override;
90 function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override;
Jens Geyerd5436f52014-10-03 19:50:38 +020091 procedure Open; override;
92 procedure Close; override;
93 procedure Flush; override;
94 function IsOpen: Boolean; override;
95 function ToArray: TBytes; override;
96 public
97 constructor Create( const AStream: IStream);
98 end;
99
100implementation
101
102{ TThriftStreamAdapterCOM }
103
104procedure TThriftStreamAdapterCOM.Close;
105begin
106 FStream := nil;
107end;
108
109constructor TThriftStreamAdapterCOM.Create( const AStream: IStream);
110begin
111 inherited Create;
112 FStream := AStream;
113end;
114
115procedure TThriftStreamAdapterCOM.Flush;
116begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200117 if IsOpen then begin
118 if FStream <> nil then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200119 FStream.Commit( STGC_DEFAULT );
120 end;
121 end;
122end;
123
124function TThriftStreamAdapterCOM.IsOpen: Boolean;
125begin
126 Result := FStream <> nil;
127end;
128
129procedure TThriftStreamAdapterCOM.Open;
130begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200131 // nothing to do
Jens Geyerd5436f52014-10-03 19:50:38 +0200132end;
133
Jens Geyer17c3ad92017-09-05 20:31:27 +0200134function TThriftStreamAdapterCOM.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyer5089b0a2018-02-01 22:37:18 +0100135var pTmp : PByte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200136begin
137 inherited;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200138
139 if count >= buflen-offset
140 then count := buflen-offset;
141
Jens Geyerd5436f52014-10-03 19:50:38 +0200142 Result := 0;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200143 if FStream <> nil then begin
144 if count > 0 then begin
Jens Geyer5089b0a2018-02-01 22:37:18 +0100145 pTmp := pBuf;
146 Inc( pTmp, offset);
147 FStream.Read( pTmp, count, @Result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200148 end;
149 end;
150end;
151
152function TThriftStreamAdapterCOM.ToArray: TBytes;
153var
154 statstg: TStatStg;
155 len : Integer;
Nick4f5229e2016-04-14 16:43:22 +0300156 NewPos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND};
Jens Geyerd5436f52014-10-03 19:50:38 +0200157 cbRead : Integer;
158begin
159 FillChar( statstg, SizeOf( statstg), 0);
160 len := 0;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200161 if IsOpen then begin
162 if Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200163 len := statstg.cbSize;
164 end;
165 end;
166
167 SetLength( Result, len );
168
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200169 if len > 0 then begin
170 if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200171 FStream.Read( @Result[0], len, @cbRead);
172 end;
173 end;
174end;
175
Jens Geyer17c3ad92017-09-05 20:31:27 +0200176procedure TThriftStreamAdapterCOM.Write( const pBuf: Pointer; offset: Integer; count: Integer);
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200177var nWritten : Integer;
Jens Geyer5089b0a2018-02-01 22:37:18 +0100178 pTmp : PByte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200179begin
180 inherited;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200181 if IsOpen then begin
182 if count > 0 then begin
Jens Geyer5089b0a2018-02-01 22:37:18 +0100183 pTmp := pBuf;
184 Inc( pTmp, offset);
185 FStream.Write( pTmp, count, @nWritten);
Jens Geyerd5436f52014-10-03 19:50:38 +0200186 end;
187 end;
188end;
189
190{ TThriftStreamImpl }
191
Jens Geyer17c3ad92017-09-05 20:31:27 +0200192procedure TThriftStreamImpl.CheckSizeAndOffset( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200193begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200194 if count > 0 then begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200195 if (offset < 0) or ( offset >= buflen) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 raise ERangeError.Create( SBitsIndexError );
197 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200198 if count > buflen then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200199 raise ERangeError.Create( SBitsIndexError );
200 end;
201 end;
202end;
203
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200204function TThriftStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200205begin
Jens Geyera76e6c72017-09-08 21:03:30 +0200206 if Length(buffer) > 0
207 then Result := Read( @buffer[0], Length(buffer), offset, count)
208 else Result := 0;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200209end;
210
211function TThriftStreamImpl.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
212begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200213 Result := 0;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200214 CheckSizeAndOffset( pBuf, buflen, offset, count );
Jens Geyerd5436f52014-10-03 19:50:38 +0200215end;
216
217procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer);
218begin
Jens Geyera76e6c72017-09-08 21:03:30 +0200219 if Length(buffer) > 0
220 then Write( @buffer[0], offset, count);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200221end;
222
223procedure TThriftStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer);
224begin
225 CheckSizeAndOffset( pBuf, offset+count, offset, count);
Jens Geyerd5436f52014-10-03 19:50:38 +0200226end;
227
228{ TThriftStreamAdapterDelphi }
229
230procedure TThriftStreamAdapterDelphi.Close;
231begin
232 FStream.Free;
233 FStream := nil;
234 FOwnsStream := False;
235end;
236
237constructor TThriftStreamAdapterDelphi.Create( const AStream: TStream; AOwnsStream: Boolean);
238begin
239 inherited Create;
240 FStream := AStream;
241 FOwnsStream := AOwnsStream;
242end;
243
244destructor TThriftStreamAdapterDelphi.Destroy;
245begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200246 if FOwnsStream
247 then Close;
248
Jens Geyerd5436f52014-10-03 19:50:38 +0200249 inherited;
250end;
251
252procedure TThriftStreamAdapterDelphi.Flush;
253begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200254 // nothing to do
Jens Geyerd5436f52014-10-03 19:50:38 +0200255end;
256
257function TThriftStreamAdapterDelphi.IsOpen: Boolean;
258begin
259 Result := FStream <> nil;
260end;
261
262procedure TThriftStreamAdapterDelphi.Open;
263begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200264 // nothing to do
Jens Geyerd5436f52014-10-03 19:50:38 +0200265end;
266
Jens Geyer17c3ad92017-09-05 20:31:27 +0200267function TThriftStreamAdapterDelphi.Read(const pBuf : Pointer; const buflen : Integer; offset, count: Integer): Integer;
Jens Geyer5089b0a2018-02-01 22:37:18 +0100268var pTmp : PByte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200269begin
270 inherited;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200271
272 if count >= buflen-offset
273 then count := buflen-offset;
274
Jens Geyer5089b0a2018-02-01 22:37:18 +0100275 if count > 0 then begin
276 pTmp := pBuf;
277 Inc( pTmp, offset);
278 Result := FStream.Read( pTmp^, count)
279 end
Jens Geyer17c3ad92017-09-05 20:31:27 +0200280 else Result := 0;
Jens Geyerd5436f52014-10-03 19:50:38 +0200281end;
282
283function TThriftStreamAdapterDelphi.ToArray: TBytes;
284var
285 OrgPos : Integer;
286 len : Integer;
287begin
288 len := 0;
289 if FStream <> nil then
290 begin
291 len := FStream.Size;
292 end;
293
294 SetLength( Result, len );
295
296 if len > 0 then
297 begin
298 OrgPos := FStream.Position;
299 try
300 FStream.Position := 0;
301 FStream.ReadBuffer( Pointer(@Result[0])^, len );
302 finally
303 FStream.Position := OrgPos;
304 end;
305 end
306end;
307
Jens Geyer17c3ad92017-09-05 20:31:27 +0200308procedure TThriftStreamAdapterDelphi.Write(const pBuf : Pointer; offset, count: Integer);
Jens Geyer5089b0a2018-02-01 22:37:18 +0100309var pTmp : PByte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200310begin
311 inherited;
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200312 if count > 0 then begin
Jens Geyer5089b0a2018-02-01 22:37:18 +0100313 pTmp := pBuf;
314 Inc( pTmp, offset);
315 FStream.Write( pTmp^, count)
Jens Geyerd5436f52014-10-03 19:50:38 +0200316 end;
317end;
318
319end.