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 |
| 39 | |
| 40 | IThriftStream = interface |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 41 | ['{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 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 | |
| 53 | TThriftStreamImpl = class( TInterfacedObject, IThriftStream) |
| 54 | private |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 55 | procedure CheckSizeAndOffset( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer); overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 56 | protected |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 57 | 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 61 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 73 | 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 75 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 89 | 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 91 | 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 | |
| 100 | implementation |
| 101 | |
| 102 | { TThriftStreamAdapterCOM } |
| 103 | |
| 104 | procedure TThriftStreamAdapterCOM.Close; |
| 105 | begin |
| 106 | FStream := nil; |
| 107 | end; |
| 108 | |
| 109 | constructor TThriftStreamAdapterCOM.Create( const AStream: IStream); |
| 110 | begin |
| 111 | inherited Create; |
| 112 | FStream := AStream; |
| 113 | end; |
| 114 | |
| 115 | procedure TThriftStreamAdapterCOM.Flush; |
| 116 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 117 | if IsOpen then begin |
| 118 | if FStream <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 119 | FStream.Commit( STGC_DEFAULT ); |
| 120 | end; |
| 121 | end; |
| 122 | end; |
| 123 | |
| 124 | function TThriftStreamAdapterCOM.IsOpen: Boolean; |
| 125 | begin |
| 126 | Result := FStream <> nil; |
| 127 | end; |
| 128 | |
| 129 | procedure TThriftStreamAdapterCOM.Open; |
| 130 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 131 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 132 | end; |
| 133 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 134 | function TThriftStreamAdapterCOM.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 135 | begin |
| 136 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 137 | |
| 138 | if count >= buflen-offset |
| 139 | then count := buflen-offset; |
| 140 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 141 | Result := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 142 | if FStream <> nil then begin |
| 143 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 144 | FStream.Read( @(PByteArray(pBuf)^[offset]), count, @Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 145 | end; |
| 146 | end; |
| 147 | end; |
| 148 | |
| 149 | function TThriftStreamAdapterCOM.ToArray: TBytes; |
| 150 | var |
| 151 | statstg: TStatStg; |
| 152 | len : Integer; |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 153 | NewPos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 154 | cbRead : Integer; |
| 155 | begin |
| 156 | FillChar( statstg, SizeOf( statstg), 0); |
| 157 | len := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 158 | if IsOpen then begin |
| 159 | if Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 160 | len := statstg.cbSize; |
| 161 | end; |
| 162 | end; |
| 163 | |
| 164 | SetLength( Result, len ); |
| 165 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 166 | if len > 0 then begin |
| 167 | if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 168 | FStream.Read( @Result[0], len, @cbRead); |
| 169 | end; |
| 170 | end; |
| 171 | end; |
| 172 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 173 | procedure TThriftStreamAdapterCOM.Write( const pBuf: Pointer; offset: Integer; count: Integer); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 174 | var nWritten : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 175 | begin |
| 176 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 177 | if IsOpen then begin |
| 178 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 179 | FStream.Write( @(PByteArray(pBuf)^[offset]), count, @nWritten); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 180 | end; |
| 181 | end; |
| 182 | end; |
| 183 | |
| 184 | { TThriftStreamImpl } |
| 185 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 186 | 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] | 187 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 188 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 189 | if (offset < 0) or ( offset >= buflen) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 190 | raise ERangeError.Create( SBitsIndexError ); |
| 191 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 192 | if count > buflen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 193 | raise ERangeError.Create( SBitsIndexError ); |
| 194 | end; |
| 195 | end; |
| 196 | end; |
| 197 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 198 | function TThriftStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 199 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 200 | if Length(buffer) > 0 |
| 201 | then Result := Read( @buffer[0], Length(buffer), offset, count) |
| 202 | else Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 203 | end; |
| 204 | |
| 205 | function TThriftStreamImpl.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; |
| 206 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 207 | Result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 208 | CheckSizeAndOffset( pBuf, buflen, offset, count ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 209 | end; |
| 210 | |
| 211 | procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 212 | begin |
Jens Geyer | a76e6c7 | 2017-09-08 21:03:30 +0200 | [diff] [blame] | 213 | if Length(buffer) > 0 |
| 214 | then Write( @buffer[0], offset, count); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 215 | end; |
| 216 | |
| 217 | procedure TThriftStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer); |
| 218 | begin |
| 219 | CheckSizeAndOffset( pBuf, offset+count, offset, count); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 220 | end; |
| 221 | |
| 222 | { TThriftStreamAdapterDelphi } |
| 223 | |
| 224 | procedure TThriftStreamAdapterDelphi.Close; |
| 225 | begin |
| 226 | FStream.Free; |
| 227 | FStream := nil; |
| 228 | FOwnsStream := False; |
| 229 | end; |
| 230 | |
| 231 | constructor TThriftStreamAdapterDelphi.Create( const AStream: TStream; AOwnsStream: Boolean); |
| 232 | begin |
| 233 | inherited Create; |
| 234 | FStream := AStream; |
| 235 | FOwnsStream := AOwnsStream; |
| 236 | end; |
| 237 | |
| 238 | destructor TThriftStreamAdapterDelphi.Destroy; |
| 239 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 240 | if FOwnsStream |
| 241 | then Close; |
| 242 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 243 | inherited; |
| 244 | end; |
| 245 | |
| 246 | procedure TThriftStreamAdapterDelphi.Flush; |
| 247 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 248 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 249 | end; |
| 250 | |
| 251 | function TThriftStreamAdapterDelphi.IsOpen: Boolean; |
| 252 | begin |
| 253 | Result := FStream <> nil; |
| 254 | end; |
| 255 | |
| 256 | procedure TThriftStreamAdapterDelphi.Open; |
| 257 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 258 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 259 | end; |
| 260 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 261 | function TThriftStreamAdapterDelphi.Read(const pBuf : Pointer; const buflen : Integer; offset, count: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 262 | begin |
| 263 | inherited; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 264 | |
| 265 | if count >= buflen-offset |
| 266 | then count := buflen-offset; |
| 267 | |
| 268 | if count > 0 |
| 269 | then Result := FStream.Read( PByteArray(pBuf)^[offset], count) |
| 270 | else Result := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 271 | end; |
| 272 | |
| 273 | function TThriftStreamAdapterDelphi.ToArray: TBytes; |
| 274 | var |
| 275 | OrgPos : Integer; |
| 276 | len : Integer; |
| 277 | begin |
| 278 | len := 0; |
| 279 | if FStream <> nil then |
| 280 | begin |
| 281 | len := FStream.Size; |
| 282 | end; |
| 283 | |
| 284 | SetLength( Result, len ); |
| 285 | |
| 286 | if len > 0 then |
| 287 | begin |
| 288 | OrgPos := FStream.Position; |
| 289 | try |
| 290 | FStream.Position := 0; |
| 291 | FStream.ReadBuffer( Pointer(@Result[0])^, len ); |
| 292 | finally |
| 293 | FStream.Position := OrgPos; |
| 294 | end; |
| 295 | end |
| 296 | end; |
| 297 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 298 | procedure TThriftStreamAdapterDelphi.Write(const pBuf : Pointer; offset, count: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 299 | begin |
| 300 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 301 | if count > 0 then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 302 | FStream.Write( PByteArray(pBuf)^[offset], count) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 303 | end; |
| 304 | end; |
| 305 | |
| 306 | end. |