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 | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 40 | ['{DBE61E28-2A77-42DB-A5A3-3CCB8A2D09FA}'] |
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 | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 45 | procedure CheckReadBytesAvailable( const value : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 46 | procedure Open; |
| 47 | procedure Close; |
| 48 | procedure Flush; |
| 49 | function IsOpen: Boolean; |
| 50 | function ToArray: TBytes; |
| 51 | end; |
| 52 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 53 | |
| 54 | IThriftStream2 = interface( IThriftStream) |
| 55 | ['{1F55D9FE-F617-4B80-B8CA-4A300D8E33F6}'] |
| 56 | function Size : Int64; |
| 57 | function Position : Int64; |
| 58 | end; |
| 59 | |
| 60 | |
| 61 | TThriftStreamImpl = class abstract( TInterfacedObject, IThriftStream) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 62 | strict private |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 63 | 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] | 64 | strict protected |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 65 | // IThriftStream |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 66 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); overload; inline; |
| 67 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); overload; virtual; |
| 68 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; overload; inline; |
| 69 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload; virtual; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 70 | procedure CheckReadBytesAvailable( const value : Integer); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 71 | procedure Open; virtual; abstract; |
| 72 | procedure Close; virtual; abstract; |
| 73 | procedure Flush; virtual; abstract; |
| 74 | function IsOpen: Boolean; virtual; abstract; |
| 75 | function ToArray: TBytes; virtual; abstract; |
| 76 | end; |
| 77 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 78 | TThriftStreamAdapterDelphi = class( TThriftStreamImpl, IThriftStream2) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 79 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 80 | FStream : TStream; |
| 81 | FOwnsStream : Boolean; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 82 | strict protected |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 83 | // IThriftStream |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 84 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override; |
| 85 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 86 | procedure CheckReadBytesAvailable( const value : Integer); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 87 | procedure Open; override; |
| 88 | procedure Close; override; |
| 89 | procedure Flush; override; |
| 90 | function IsOpen: Boolean; override; |
| 91 | function ToArray: TBytes; override; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 92 | |
| 93 | // IThriftStream2 |
| 94 | function Size : Int64; |
| 95 | function Position : Int64; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 96 | public |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 97 | constructor Create( const aStream: TStream; aOwnsStream : Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 98 | destructor Destroy; override; |
| 99 | end; |
| 100 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 101 | TThriftStreamAdapterCOM = class( TThriftStreamImpl, IThriftStream2) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 102 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 103 | FStream : IStream; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 104 | strict protected |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 105 | // IThriftStream |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 106 | procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override; |
| 107 | function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 108 | procedure CheckReadBytesAvailable( const value : Integer); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 109 | procedure Open; override; |
| 110 | procedure Close; override; |
| 111 | procedure Flush; override; |
| 112 | function IsOpen: Boolean; override; |
| 113 | function ToArray: TBytes; override; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 114 | |
| 115 | // IThriftStream2 |
| 116 | function Size : Int64; |
| 117 | function Position : Int64; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 118 | public |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 119 | constructor Create( const aStream: IStream); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 120 | end; |
| 121 | |
| 122 | implementation |
| 123 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 124 | uses Thrift.Transport; |
| 125 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 126 | { TThriftStreamAdapterCOM } |
| 127 | |
| 128 | procedure TThriftStreamAdapterCOM.Close; |
| 129 | begin |
| 130 | FStream := nil; |
| 131 | end; |
| 132 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 133 | constructor TThriftStreamAdapterCOM.Create( const aStream: IStream); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 134 | begin |
| 135 | inherited Create; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 136 | FStream := aStream; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 137 | end; |
| 138 | |
| 139 | procedure TThriftStreamAdapterCOM.Flush; |
| 140 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 141 | if IsOpen then begin |
| 142 | if FStream <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 143 | FStream.Commit( STGC_DEFAULT ); |
| 144 | end; |
| 145 | end; |
| 146 | end; |
| 147 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 148 | function TThriftStreamAdapterCOM.Size : Int64; |
| 149 | var statstg: TStatStg; |
| 150 | begin |
| 151 | FillChar( statstg, SizeOf( statstg), 0); |
| 152 | if IsOpen |
| 153 | and Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) |
| 154 | then result := statstg.cbSize |
| 155 | else result := 0; |
| 156 | end; |
| 157 | |
| 158 | function TThriftStreamAdapterCOM.Position : Int64; |
| 159 | var newpos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
| 160 | begin |
| 161 | if SUCCEEDED( FStream.Seek( 0, STREAM_SEEK_CUR, newpos)) |
| 162 | then result := Int64(newpos) |
| 163 | else raise TTransportExceptionEndOfFile.Create('Seek() error'); |
| 164 | end; |
| 165 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 166 | function TThriftStreamAdapterCOM.IsOpen: Boolean; |
| 167 | begin |
| 168 | Result := FStream <> nil; |
| 169 | end; |
| 170 | |
| 171 | procedure TThriftStreamAdapterCOM.Open; |
| 172 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 173 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 174 | end; |
| 175 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 176 | 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] | 177 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 178 | begin |
| 179 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 180 | |
| 181 | if count >= buflen-offset |
| 182 | then count := buflen-offset; |
| 183 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 184 | Result := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 185 | if FStream <> nil then begin |
| 186 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 187 | pTmp := pBuf; |
| 188 | Inc( pTmp, offset); |
| 189 | FStream.Read( pTmp, count, @Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 190 | end; |
| 191 | end; |
| 192 | end; |
| 193 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 194 | procedure TThriftStreamAdapterCOM.CheckReadBytesAvailable( const value : Integer); |
| 195 | var nRemaining : Int64; |
| 196 | begin |
| 197 | nRemaining := Self.Size - Self.Position; |
| 198 | if nRemaining < value |
| 199 | then raise TTransportExceptionEndOfFile.Create('Not enough input data'); |
| 200 | end; |
| 201 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 202 | function TThriftStreamAdapterCOM.ToArray: TBytes; |
| 203 | var |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 204 | len : Int64; |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 205 | NewPos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 206 | cbRead : Integer; |
| 207 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 208 | len := Self.Size; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 209 | SetLength( Result, len ); |
| 210 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 211 | if len > 0 then begin |
| 212 | if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 213 | FStream.Read( @Result[0], len, @cbRead); |
| 214 | end; |
| 215 | end; |
| 216 | end; |
| 217 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 218 | procedure TThriftStreamAdapterCOM.Write( const pBuf: Pointer; offset: Integer; count: Integer); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 219 | var nWritten : Integer; |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 220 | pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 221 | begin |
| 222 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 223 | if IsOpen then begin |
| 224 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 225 | pTmp := pBuf; |
| 226 | Inc( pTmp, offset); |
| 227 | FStream.Write( pTmp, count, @nWritten); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 228 | end; |
| 229 | end; |
| 230 | end; |
| 231 | |
| 232 | { TThriftStreamImpl } |
| 233 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 234 | 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] | 235 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 236 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 237 | if (offset < 0) or ( offset >= buflen) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 238 | raise ERangeError.Create( SBitsIndexError ); |
| 239 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 240 | if count > buflen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 241 | raise ERangeError.Create( SBitsIndexError ); |
| 242 | end; |
| 243 | end; |
| 244 | end; |
| 245 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 246 | function TThriftStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 247 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 248 | if Length(buffer) > 0 |
| 249 | then Result := Read( @buffer[0], Length(buffer), offset, count) |
| 250 | else Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 251 | end; |
| 252 | |
| 253 | function TThriftStreamImpl.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; |
| 254 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 255 | Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 256 | CheckSizeAndOffset( pBuf, buflen, offset, count ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 257 | end; |
| 258 | |
| 259 | procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 260 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 261 | if Length(buffer) > 0 |
| 262 | then Write( @buffer[0], offset, count); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 263 | end; |
| 264 | |
| 265 | procedure TThriftStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer); |
| 266 | begin |
| 267 | CheckSizeAndOffset( pBuf, offset+count, offset, count); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 268 | end; |
| 269 | |
| 270 | { TThriftStreamAdapterDelphi } |
| 271 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 272 | constructor TThriftStreamAdapterDelphi.Create( const aStream: TStream; aOwnsStream: Boolean); |
| 273 | begin |
| 274 | inherited Create; |
| 275 | FStream := aStream; |
| 276 | FOwnsStream := aOwnsStream; |
| 277 | end; |
| 278 | |
| 279 | destructor TThriftStreamAdapterDelphi.Destroy; |
| 280 | begin |
| 281 | if FOwnsStream |
| 282 | then Close; |
| 283 | |
| 284 | inherited; |
| 285 | end; |
| 286 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 287 | procedure TThriftStreamAdapterDelphi.Close; |
| 288 | begin |
| 289 | FStream.Free; |
| 290 | FStream := nil; |
| 291 | FOwnsStream := False; |
| 292 | end; |
| 293 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 294 | procedure TThriftStreamAdapterDelphi.Flush; |
| 295 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 296 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 297 | end; |
| 298 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 299 | function TThriftStreamAdapterDelphi.Size : Int64; |
| 300 | begin |
| 301 | result := FStream.Size; |
| 302 | end; |
| 303 | |
| 304 | function TThriftStreamAdapterDelphi.Position : Int64; |
| 305 | begin |
| 306 | result := FStream.Position; |
| 307 | end; |
| 308 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 309 | function TThriftStreamAdapterDelphi.IsOpen: Boolean; |
| 310 | begin |
| 311 | Result := FStream <> nil; |
| 312 | end; |
| 313 | |
| 314 | procedure TThriftStreamAdapterDelphi.Open; |
| 315 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 316 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 317 | end; |
| 318 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 319 | 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] | 320 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 321 | begin |
| 322 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 323 | |
| 324 | if count >= buflen-offset |
| 325 | then count := buflen-offset; |
| 326 | |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 327 | if count > 0 then begin |
| 328 | pTmp := pBuf; |
| 329 | Inc( pTmp, offset); |
| 330 | Result := FStream.Read( pTmp^, count) |
| 331 | end |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 332 | else Result := 0; |
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 | procedure TThriftStreamAdapterDelphi.CheckReadBytesAvailable( const value : Integer); |
| 336 | var nRemaining : Int64; |
| 337 | begin |
| 338 | nRemaining := FStream.Size - FStream.Position; |
| 339 | if nRemaining < value then raise TTransportExceptionEndOfFile.Create('Not enough input data'); |
| 340 | end; |
| 341 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 342 | function TThriftStreamAdapterDelphi.ToArray: TBytes; |
| 343 | var |
| 344 | OrgPos : Integer; |
| 345 | len : Integer; |
| 346 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 347 | if FStream <> nil |
| 348 | then len := FStream.Size |
| 349 | else len := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 350 | |
| 351 | SetLength( Result, len ); |
| 352 | |
| 353 | if len > 0 then |
| 354 | begin |
| 355 | OrgPos := FStream.Position; |
| 356 | try |
| 357 | FStream.Position := 0; |
| 358 | FStream.ReadBuffer( Pointer(@Result[0])^, len ); |
| 359 | finally |
| 360 | FStream.Position := OrgPos; |
| 361 | end; |
| 362 | end |
| 363 | end; |
| 364 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 365 | procedure TThriftStreamAdapterDelphi.Write(const pBuf : Pointer; offset, count: Integer); |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 366 | var pTmp : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 367 | begin |
| 368 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 369 | if count > 0 then begin |
Jens Geyer | 5089b0a | 2018-02-01 22:37:18 +0100 | [diff] [blame] | 370 | pTmp := pBuf; |
| 371 | Inc( pTmp, offset); |
| 372 | FStream.Write( pTmp^, count) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 373 | end; |
| 374 | end; |
| 375 | |
| 376 | end. |