Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1 | (* |
| 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 | |
| 20 | unit Thrift.Stream; |
| 21 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 22 | {$I Thrift.Defines.inc} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 23 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 24 | interface |
| 25 | |
| 26 | uses |
| 27 | Classes, |
| 28 | SysUtils, |
| 29 | SysConst, |
| 30 | RTLConsts, |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 31 | {$IFDEF OLD_UNIT_NAMES} |
| 32 | ActiveX, |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 33 | {$ELSE} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 34 | Winapi.ActiveX, |
| 35 | {$ENDIF} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 36 | Thrift.Utils; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 37 | |
| 38 | type |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 39 | IThriftStream = interface |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 40 | ['{3A61A8A6-3639-4B91-A260-EFCA23944F3A}'] |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 41 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); overload; |
| 42 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); overload; |
| 43 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; overload; |
| 44 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 45 | procedure Open; |
| 46 | procedure Close; |
| 47 | procedure Flush; |
| 48 | function IsOpen: Boolean; |
| 49 | function ToArray: TBytes; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 50 | function Size : Int64; |
| 51 | function Position : Int64; |
| 52 | end; |
| 53 | |
| 54 | |
| 55 | TThriftStreamImpl = class abstract( TInterfacedObject, IThriftStream) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 56 | strict private |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 57 | procedure CheckSizeAndOffset( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer); overload; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 58 | strict protected |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 59 | // IThriftStream |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 60 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); overload; inline; |
| 61 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); overload; virtual; |
| 62 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; overload; inline; |
| 63 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload; virtual; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 64 | procedure Open; virtual; abstract; |
| 65 | procedure Close; virtual; abstract; |
| 66 | procedure Flush; virtual; abstract; |
| 67 | function IsOpen: Boolean; virtual; abstract; |
| 68 | function ToArray: TBytes; virtual; abstract; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 69 | function Size : Int64; virtual; |
| 70 | function Position : Int64; virtual; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 71 | end; |
| 72 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 73 | TThriftStreamAdapterDelphi = class( TThriftStreamImpl) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 74 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 75 | FStream : TStream; |
| 76 | FOwnsStream : Boolean; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 77 | strict protected |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 78 | // IThriftStream |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 79 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override; |
| 80 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 81 | procedure Open; override; |
| 82 | procedure Close; override; |
| 83 | procedure Flush; override; |
| 84 | function IsOpen: Boolean; override; |
| 85 | function ToArray: TBytes; override; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 86 | function Size : Int64; override; |
| 87 | function Position : Int64; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 88 | public |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 89 | constructor Create( const aStream: TStream; aOwnsStream : Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 90 | destructor Destroy; override; |
| 91 | end; |
| 92 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 93 | TThriftStreamAdapterCOM = class( TThriftStreamImpl) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 94 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 95 | FStream : IStream; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 96 | strict protected |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 97 | // IThriftStream |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 98 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override; |
| 99 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 100 | procedure Open; override; |
| 101 | procedure Close; override; |
| 102 | procedure Flush; override; |
| 103 | function IsOpen: Boolean; override; |
| 104 | function ToArray: TBytes; override; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 105 | function Size : Int64; override; |
| 106 | function Position : Int64; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 107 | public |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 108 | constructor Create( const aStream: IStream); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 109 | end; |
| 110 | |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 111 | |
| 112 | TThriftMemoryStream = class(TMemoryStream) |
| 113 | strict protected |
| 114 | FInitialCapacity : NativeInt; |
| 115 | public |
| 116 | constructor Create( const aInitialCapacity : NativeInt = 4096); |
| 117 | |
| 118 | // reimplemented |
| 119 | procedure Clear; |
| 120 | |
| 121 | // make it publicly visible |
| 122 | property Capacity; |
| 123 | end; |
| 124 | |
| 125 | |
| 126 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 127 | implementation |
| 128 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 129 | uses Thrift.Transport; |
| 130 | |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 131 | |
| 132 | { TThriftMemoryStream } |
| 133 | |
| 134 | constructor TThriftMemoryStream.Create( const aInitialCapacity : NativeInt); |
| 135 | begin |
| 136 | inherited Create; |
| 137 | FInitialCapacity := aInitialCapacity; |
| 138 | Clear; |
| 139 | end; |
| 140 | |
| 141 | |
| 142 | procedure TThriftMemoryStream.Clear; |
| 143 | // reimplemented to keep initial capacity |
| 144 | begin |
| 145 | Position := 0; |
| 146 | Size := 0; |
| 147 | |
| 148 | // primary goal: minimize costly reallocations (performance!) |
| 149 | // secondary goal: prevent costly ressource over-allocations |
| 150 | if (FInitialCapacity >= 1024*1024) // if we are talking about MB |
| 151 | or ((Capacity div 2) > FInitialCapacity) // or the allocated buffer is really large |
| 152 | or (Capacity < FInitialCapacity) // or we are actually below the limit |
| 153 | then Capacity := FInitialCapacity; |
| 154 | end; |
| 155 | |
| 156 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 157 | { TThriftStreamAdapterCOM } |
| 158 | |
| 159 | procedure TThriftStreamAdapterCOM.Close; |
| 160 | begin |
| 161 | FStream := nil; |
| 162 | end; |
| 163 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 164 | constructor TThriftStreamAdapterCOM.Create( const aStream: IStream); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 165 | begin |
| 166 | inherited Create; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 167 | FStream := aStream; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 168 | end; |
| 169 | |
| 170 | procedure TThriftStreamAdapterCOM.Flush; |
| 171 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 172 | if IsOpen then begin |
| 173 | if FStream <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 174 | FStream.Commit( STGC_DEFAULT ); |
| 175 | end; |
| 176 | end; |
| 177 | end; |
| 178 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 179 | function TThriftStreamAdapterCOM.Size : Int64; |
| 180 | var statstg: TStatStg; |
| 181 | begin |
| 182 | FillChar( statstg, SizeOf( statstg), 0); |
| 183 | if IsOpen |
| 184 | and Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) |
| 185 | then result := statstg.cbSize |
| 186 | else result := 0; |
| 187 | end; |
| 188 | |
| 189 | function TThriftStreamAdapterCOM.Position : Int64; |
| 190 | var newpos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
| 191 | begin |
| 192 | if SUCCEEDED( FStream.Seek( 0, STREAM_SEEK_CUR, newpos)) |
| 193 | then result := Int64(newpos) |
| 194 | else raise TTransportExceptionEndOfFile.Create('Seek() error'); |
| 195 | end; |
| 196 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 197 | function TThriftStreamAdapterCOM.IsOpen: Boolean; |
| 198 | begin |
| 199 | Result := FStream <> nil; |
| 200 | end; |
| 201 | |
| 202 | procedure TThriftStreamAdapterCOM.Open; |
| 203 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 204 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 205 | end; |
| 206 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 207 | function TThriftStreamAdapterCOM.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 208 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 209 | begin |
| 210 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 211 | |
| 212 | if count >= buflen-offset |
| 213 | then count := buflen-offset; |
| 214 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 215 | Result := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 216 | if FStream <> nil then begin |
| 217 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 218 | pTmp := pBuf; |
| 219 | Inc( pTmp, offset); |
| 220 | FStream.Read( pTmp, count, @Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 221 | end; |
| 222 | end; |
| 223 | end; |
| 224 | |
| 225 | function TThriftStreamAdapterCOM.ToArray: TBytes; |
| 226 | var |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 227 | len : Int64; |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 228 | NewPos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 229 | cbRead : Integer; |
| 230 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 231 | len := Self.Size; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 232 | SetLength( Result, len ); |
| 233 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 234 | if len > 0 then begin |
| 235 | if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 236 | FStream.Read( @Result[0], len, @cbRead); |
| 237 | end; |
| 238 | end; |
| 239 | end; |
| 240 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 241 | procedure TThriftStreamAdapterCOM.Write( const pBuf: Pointer; offset: Integer; count: Integer); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 242 | var nWritten : Integer; |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 243 | pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 244 | begin |
| 245 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 246 | if IsOpen then begin |
| 247 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 248 | pTmp := pBuf; |
| 249 | Inc( pTmp, offset); |
| 250 | FStream.Write( pTmp, count, @nWritten); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 251 | end; |
| 252 | end; |
| 253 | end; |
| 254 | |
| 255 | { TThriftStreamImpl } |
| 256 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 257 | procedure TThriftStreamImpl.CheckSizeAndOffset( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 258 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 259 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 260 | if (offset < 0) or ( offset >= buflen) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 261 | raise ERangeError.Create( SBitsIndexError ); |
| 262 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 263 | if count > buflen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 264 | raise ERangeError.Create( SBitsIndexError ); |
| 265 | end; |
| 266 | end; |
| 267 | end; |
| 268 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 269 | function TThriftStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 270 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 271 | if Length(buffer) > 0 |
| 272 | then Result := Read( @buffer[0], Length(buffer), offset, count) |
| 273 | else Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 274 | end; |
| 275 | |
| 276 | function TThriftStreamImpl.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; |
| 277 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 278 | Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 279 | CheckSizeAndOffset( pBuf, buflen, offset, count ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 280 | end; |
| 281 | |
| 282 | procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 283 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 284 | if Length(buffer) > 0 |
| 285 | then Write( @buffer[0], offset, count); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 286 | end; |
| 287 | |
| 288 | procedure TThriftStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer); |
| 289 | begin |
| 290 | CheckSizeAndOffset( pBuf, offset+count, offset, count); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 291 | end; |
| 292 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 293 | function TThriftStreamImpl.Size : Int64; |
| 294 | begin |
| 295 | ASSERT(FALSE); |
| 296 | raise ENotImplemented.Create(ClassName+'.Size'); |
| 297 | end; |
| 298 | |
| 299 | function TThriftStreamImpl.Position : Int64; |
| 300 | begin |
| 301 | ASSERT(FALSE); |
| 302 | raise ENotImplemented.Create(ClassName+'.Position'); |
| 303 | end; |
| 304 | |
| 305 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 306 | { TThriftStreamAdapterDelphi } |
| 307 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 308 | constructor TThriftStreamAdapterDelphi.Create( const aStream: TStream; aOwnsStream: Boolean); |
| 309 | begin |
| 310 | inherited Create; |
| 311 | FStream := aStream; |
| 312 | FOwnsStream := aOwnsStream; |
| 313 | end; |
| 314 | |
| 315 | destructor TThriftStreamAdapterDelphi.Destroy; |
| 316 | begin |
| 317 | if FOwnsStream |
| 318 | then Close; |
| 319 | |
| 320 | inherited; |
| 321 | end; |
| 322 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 323 | procedure TThriftStreamAdapterDelphi.Close; |
| 324 | begin |
| 325 | FStream.Free; |
| 326 | FStream := nil; |
| 327 | FOwnsStream := False; |
| 328 | end; |
| 329 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 330 | procedure TThriftStreamAdapterDelphi.Flush; |
| 331 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 332 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 333 | end; |
| 334 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 335 | function TThriftStreamAdapterDelphi.Size : Int64; |
| 336 | begin |
| 337 | result := FStream.Size; |
| 338 | end; |
| 339 | |
| 340 | function TThriftStreamAdapterDelphi.Position : Int64; |
| 341 | begin |
| 342 | result := FStream.Position; |
| 343 | end; |
| 344 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 345 | function TThriftStreamAdapterDelphi.IsOpen: Boolean; |
| 346 | begin |
| 347 | Result := FStream <> nil; |
| 348 | end; |
| 349 | |
| 350 | procedure TThriftStreamAdapterDelphi.Open; |
| 351 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 352 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 353 | end; |
| 354 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 355 | function TThriftStreamAdapterDelphi.Read(const pBuf : Pointer; const buflen : Integer; offset, count: Integer): Integer; |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 356 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 357 | begin |
| 358 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 359 | |
| 360 | if count >= buflen-offset |
| 361 | then count := buflen-offset; |
| 362 | |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 363 | if count > 0 then begin |
| 364 | pTmp := pBuf; |
| 365 | Inc( pTmp, offset); |
| 366 | Result := FStream.Read( pTmp^, count) |
| 367 | end |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 368 | else Result := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 369 | end; |
| 370 | |
| 371 | function TThriftStreamAdapterDelphi.ToArray: TBytes; |
| 372 | var |
| 373 | OrgPos : Integer; |
| 374 | len : Integer; |
| 375 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 376 | if FStream <> nil |
| 377 | then len := FStream.Size |
| 378 | else len := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 379 | |
| 380 | SetLength( Result, len ); |
| 381 | |
| 382 | if len > 0 then |
| 383 | begin |
| 384 | OrgPos := FStream.Position; |
| 385 | try |
| 386 | FStream.Position := 0; |
| 387 | FStream.ReadBuffer( Pointer(@Result[0])^, len ); |
| 388 | finally |
| 389 | FStream.Position := OrgPos; |
| 390 | end; |
| 391 | end |
| 392 | end; |
| 393 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 394 | procedure TThriftStreamAdapterDelphi.Write(const pBuf : Pointer; offset, count: Integer); |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 395 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 396 | begin |
| 397 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 398 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 399 | pTmp := pBuf; |
| 400 | Inc( pTmp, offset); |
| 401 | FStream.Write( pTmp^, count) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 402 | end; |
| 403 | end; |
| 404 | |
| 405 | end. |