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 | |
| 111 | implementation |
| 112 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 113 | uses Thrift.Transport; |
| 114 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 115 | { TThriftStreamAdapterCOM } |
| 116 | |
| 117 | procedure TThriftStreamAdapterCOM.Close; |
| 118 | begin |
| 119 | FStream := nil; |
| 120 | end; |
| 121 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 122 | constructor TThriftStreamAdapterCOM.Create( const aStream: IStream); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 123 | begin |
| 124 | inherited Create; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 125 | FStream := aStream; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 126 | end; |
| 127 | |
| 128 | procedure TThriftStreamAdapterCOM.Flush; |
| 129 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 130 | if IsOpen then begin |
| 131 | if FStream <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 132 | FStream.Commit( STGC_DEFAULT ); |
| 133 | end; |
| 134 | end; |
| 135 | end; |
| 136 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 137 | function TThriftStreamAdapterCOM.Size : Int64; |
| 138 | var statstg: TStatStg; |
| 139 | begin |
| 140 | FillChar( statstg, SizeOf( statstg), 0); |
| 141 | if IsOpen |
| 142 | and Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) |
| 143 | then result := statstg.cbSize |
| 144 | else result := 0; |
| 145 | end; |
| 146 | |
| 147 | function TThriftStreamAdapterCOM.Position : Int64; |
| 148 | var newpos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
| 149 | begin |
| 150 | if SUCCEEDED( FStream.Seek( 0, STREAM_SEEK_CUR, newpos)) |
| 151 | then result := Int64(newpos) |
| 152 | else raise TTransportExceptionEndOfFile.Create('Seek() error'); |
| 153 | end; |
| 154 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 155 | function TThriftStreamAdapterCOM.IsOpen: Boolean; |
| 156 | begin |
| 157 | Result := FStream <> nil; |
| 158 | end; |
| 159 | |
| 160 | procedure TThriftStreamAdapterCOM.Open; |
| 161 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 162 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 163 | end; |
| 164 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 165 | 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] | 166 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 167 | begin |
| 168 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 169 | |
| 170 | if count >= buflen-offset |
| 171 | then count := buflen-offset; |
| 172 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 173 | Result := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 174 | if FStream <> nil then begin |
| 175 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 176 | pTmp := pBuf; |
| 177 | Inc( pTmp, offset); |
| 178 | FStream.Read( pTmp, count, @Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 179 | end; |
| 180 | end; |
| 181 | end; |
| 182 | |
| 183 | function TThriftStreamAdapterCOM.ToArray: TBytes; |
| 184 | var |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 185 | len : Int64; |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 186 | NewPos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 187 | cbRead : Integer; |
| 188 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 189 | len := Self.Size; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 190 | SetLength( Result, len ); |
| 191 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 192 | if len > 0 then begin |
| 193 | if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 194 | FStream.Read( @Result[0], len, @cbRead); |
| 195 | end; |
| 196 | end; |
| 197 | end; |
| 198 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 199 | procedure TThriftStreamAdapterCOM.Write( const pBuf: Pointer; offset: Integer; count: Integer); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 200 | var nWritten : Integer; |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 201 | pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 202 | begin |
| 203 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 204 | if IsOpen then begin |
| 205 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 206 | pTmp := pBuf; |
| 207 | Inc( pTmp, offset); |
| 208 | FStream.Write( pTmp, count, @nWritten); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 209 | end; |
| 210 | end; |
| 211 | end; |
| 212 | |
| 213 | { TThriftStreamImpl } |
| 214 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 215 | 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] | 216 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 217 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 218 | if (offset < 0) or ( offset >= buflen) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 219 | raise ERangeError.Create( SBitsIndexError ); |
| 220 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 221 | if count > buflen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 222 | raise ERangeError.Create( SBitsIndexError ); |
| 223 | end; |
| 224 | end; |
| 225 | end; |
| 226 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 227 | function TThriftStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 228 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 229 | if Length(buffer) > 0 |
| 230 | then Result := Read( @buffer[0], Length(buffer), offset, count) |
| 231 | else Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 232 | end; |
| 233 | |
| 234 | function TThriftStreamImpl.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; |
| 235 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 236 | Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 237 | CheckSizeAndOffset( pBuf, buflen, offset, count ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 238 | end; |
| 239 | |
| 240 | procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 241 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 242 | if Length(buffer) > 0 |
| 243 | then Write( @buffer[0], offset, count); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 244 | end; |
| 245 | |
| 246 | procedure TThriftStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer); |
| 247 | begin |
| 248 | CheckSizeAndOffset( pBuf, offset+count, offset, count); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 249 | end; |
| 250 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 251 | function TThriftStreamImpl.Size : Int64; |
| 252 | begin |
| 253 | ASSERT(FALSE); |
| 254 | raise ENotImplemented.Create(ClassName+'.Size'); |
| 255 | end; |
| 256 | |
| 257 | function TThriftStreamImpl.Position : Int64; |
| 258 | begin |
| 259 | ASSERT(FALSE); |
| 260 | raise ENotImplemented.Create(ClassName+'.Position'); |
| 261 | end; |
| 262 | |
| 263 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 264 | { TThriftStreamAdapterDelphi } |
| 265 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 266 | constructor TThriftStreamAdapterDelphi.Create( const aStream: TStream; aOwnsStream: Boolean); |
| 267 | begin |
| 268 | inherited Create; |
| 269 | FStream := aStream; |
| 270 | FOwnsStream := aOwnsStream; |
| 271 | end; |
| 272 | |
| 273 | destructor TThriftStreamAdapterDelphi.Destroy; |
| 274 | begin |
| 275 | if FOwnsStream |
| 276 | then Close; |
| 277 | |
| 278 | inherited; |
| 279 | end; |
| 280 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 281 | procedure TThriftStreamAdapterDelphi.Close; |
| 282 | begin |
| 283 | FStream.Free; |
| 284 | FStream := nil; |
| 285 | FOwnsStream := False; |
| 286 | end; |
| 287 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 288 | procedure TThriftStreamAdapterDelphi.Flush; |
| 289 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 290 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 291 | end; |
| 292 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 293 | function TThriftStreamAdapterDelphi.Size : Int64; |
| 294 | begin |
| 295 | result := FStream.Size; |
| 296 | end; |
| 297 | |
| 298 | function TThriftStreamAdapterDelphi.Position : Int64; |
| 299 | begin |
| 300 | result := FStream.Position; |
| 301 | end; |
| 302 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 303 | function TThriftStreamAdapterDelphi.IsOpen: Boolean; |
| 304 | begin |
| 305 | Result := FStream <> nil; |
| 306 | end; |
| 307 | |
| 308 | procedure TThriftStreamAdapterDelphi.Open; |
| 309 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 310 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 311 | end; |
| 312 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 313 | 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] | 314 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 315 | begin |
| 316 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 317 | |
| 318 | if count >= buflen-offset |
| 319 | then count := buflen-offset; |
| 320 | |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 321 | if count > 0 then begin |
| 322 | pTmp := pBuf; |
| 323 | Inc( pTmp, offset); |
| 324 | Result := FStream.Read( pTmp^, count) |
| 325 | end |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 326 | else Result := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 327 | end; |
| 328 | |
| 329 | function TThriftStreamAdapterDelphi.ToArray: TBytes; |
| 330 | var |
| 331 | OrgPos : Integer; |
| 332 | len : Integer; |
| 333 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 334 | if FStream <> nil |
| 335 | then len := FStream.Size |
| 336 | else len := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 337 | |
| 338 | SetLength( Result, len ); |
| 339 | |
| 340 | if len > 0 then |
| 341 | begin |
| 342 | OrgPos := FStream.Position; |
| 343 | try |
| 344 | FStream.Position := 0; |
| 345 | FStream.ReadBuffer( Pointer(@Result[0])^, len ); |
| 346 | finally |
| 347 | FStream.Position := OrgPos; |
| 348 | end; |
| 349 | end |
| 350 | end; |
| 351 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 352 | procedure TThriftStreamAdapterDelphi.Write(const pBuf : Pointer; offset, count: Integer); |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 353 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 354 | begin |
| 355 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 356 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 357 | pTmp := pBuf; |
| 358 | Inc( pTmp, offset); |
| 359 | FStream.Write( pTmp^, count) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 360 | end; |
| 361 | end; |
| 362 | |
| 363 | end. |