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 | |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 22 | {$IF CompilerVersion >= 23.0} |
| 23 | {$LEGACYIFEND ON} |
| 24 | {$IFEND} |
| 25 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 26 | interface |
| 27 | |
| 28 | uses |
| 29 | Classes, |
| 30 | SysUtils, |
| 31 | SysConst, |
| 32 | RTLConsts, |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 33 | {$IF CompilerVersion < 23.0} |
| 34 | ActiveX, |
| 35 | {$ELSE} |
| 36 | Winapi.ActiveX, |
| 37 | {$IFEND} |
| 38 | Thrift.Utils; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 39 | |
| 40 | type |
| 41 | |
| 42 | IThriftStream = interface |
| 43 | ['{732621B3-F697-4D76-A1B0-B4DD5A8E4018}'] |
| 44 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); |
| 45 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 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 |
| 55 | procedure CheckSizeAndOffset( const buffer: TBytes; offset: Integer; count: Integer); |
| 56 | protected |
| 57 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); virtual; |
| 58 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; virtual; |
| 59 | procedure Open; virtual; abstract; |
| 60 | procedure Close; virtual; abstract; |
| 61 | procedure Flush; virtual; abstract; |
| 62 | function IsOpen: Boolean; virtual; abstract; |
| 63 | function ToArray: TBytes; virtual; abstract; |
| 64 | end; |
| 65 | |
| 66 | TThriftStreamAdapterDelphi = class( TThriftStreamImpl ) |
| 67 | private |
| 68 | FStream : TStream; |
| 69 | FOwnsStream : Boolean; |
| 70 | protected |
| 71 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 72 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
| 73 | procedure Open; override; |
| 74 | procedure Close; override; |
| 75 | procedure Flush; override; |
| 76 | function IsOpen: Boolean; override; |
| 77 | function ToArray: TBytes; override; |
| 78 | public |
| 79 | constructor Create( const AStream: TStream; AOwnsStream : Boolean); |
| 80 | destructor Destroy; override; |
| 81 | end; |
| 82 | |
| 83 | TThriftStreamAdapterCOM = class( TThriftStreamImpl) |
| 84 | private |
| 85 | FStream : IStream; |
| 86 | protected |
| 87 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 88 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
| 89 | procedure Open; override; |
| 90 | procedure Close; override; |
| 91 | procedure Flush; override; |
| 92 | function IsOpen: Boolean; override; |
| 93 | function ToArray: TBytes; override; |
| 94 | public |
| 95 | constructor Create( const AStream: IStream); |
| 96 | end; |
| 97 | |
| 98 | implementation |
| 99 | |
| 100 | { TThriftStreamAdapterCOM } |
| 101 | |
| 102 | procedure TThriftStreamAdapterCOM.Close; |
| 103 | begin |
| 104 | FStream := nil; |
| 105 | end; |
| 106 | |
| 107 | constructor TThriftStreamAdapterCOM.Create( const AStream: IStream); |
| 108 | begin |
| 109 | inherited Create; |
| 110 | FStream := AStream; |
| 111 | end; |
| 112 | |
| 113 | procedure TThriftStreamAdapterCOM.Flush; |
| 114 | begin |
| 115 | if IsOpen then |
| 116 | begin |
| 117 | if FStream <> nil then |
| 118 | begin |
| 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 |
| 131 | |
| 132 | end; |
| 133 | |
| 134 | function TThriftStreamAdapterCOM.Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 135 | begin |
| 136 | inherited; |
| 137 | Result := 0; |
| 138 | if FStream <> nil then |
| 139 | begin |
| 140 | if count > 0 then |
| 141 | begin |
| 142 | FStream.Read( @buffer[offset], count, @Result); |
| 143 | end; |
| 144 | end; |
| 145 | end; |
| 146 | |
| 147 | function TThriftStreamAdapterCOM.ToArray: TBytes; |
| 148 | var |
| 149 | statstg: TStatStg; |
| 150 | len : Integer; |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 151 | NewPos : {$IF CompilerVersion >= 29.0} UInt64 {$ELSE} Int64 {$IFEND}; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 152 | cbRead : Integer; |
| 153 | begin |
| 154 | FillChar( statstg, SizeOf( statstg), 0); |
| 155 | len := 0; |
| 156 | if IsOpen then |
| 157 | begin |
| 158 | if Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) then |
| 159 | begin |
| 160 | len := statstg.cbSize; |
| 161 | end; |
| 162 | end; |
| 163 | |
| 164 | SetLength( Result, len ); |
| 165 | |
| 166 | if len > 0 then |
| 167 | begin |
| 168 | if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then |
| 169 | begin |
| 170 | FStream.Read( @Result[0], len, @cbRead); |
| 171 | end; |
| 172 | end; |
| 173 | end; |
| 174 | |
| 175 | procedure TThriftStreamAdapterCOM.Write( const buffer: TBytes; offset: Integer; count: Integer); |
| 176 | var |
| 177 | nWritten : Integer; |
| 178 | begin |
| 179 | inherited; |
| 180 | if IsOpen then |
| 181 | begin |
| 182 | if count > 0 then |
| 183 | begin |
| 184 | FStream.Write( @buffer[0], count, @nWritten); |
| 185 | end; |
| 186 | end; |
| 187 | end; |
| 188 | |
| 189 | { TThriftStreamImpl } |
| 190 | |
| 191 | procedure TThriftStreamImpl.CheckSizeAndOffset(const buffer: TBytes; offset, |
| 192 | count: Integer); |
| 193 | var |
| 194 | len : Integer; |
| 195 | begin |
| 196 | if count > 0 then |
| 197 | begin |
| 198 | len := Length( buffer ); |
| 199 | if (offset < 0) or ( offset >= len) then |
| 200 | begin |
| 201 | raise ERangeError.Create( SBitsIndexError ); |
| 202 | end; |
| 203 | if count > len then |
| 204 | begin |
| 205 | raise ERangeError.Create( SBitsIndexError ); |
| 206 | end; |
| 207 | end; |
| 208 | end; |
| 209 | |
| 210 | function TThriftStreamImpl.Read(var buffer: TBytes; offset, |
| 211 | count: Integer): Integer; |
| 212 | begin |
| 213 | Result := 0; |
| 214 | CheckSizeAndOffset( buffer, offset, count ); |
| 215 | end; |
| 216 | |
| 217 | procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 218 | begin |
| 219 | CheckSizeAndOffset( buffer, offset, count ); |
| 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 |
| 240 | if FOwnsStream then |
| 241 | begin |
| 242 | FStream.Free; |
| 243 | end; |
| 244 | inherited; |
| 245 | end; |
| 246 | |
| 247 | procedure TThriftStreamAdapterDelphi.Flush; |
| 248 | begin |
| 249 | |
| 250 | end; |
| 251 | |
| 252 | function TThriftStreamAdapterDelphi.IsOpen: Boolean; |
| 253 | begin |
| 254 | Result := FStream <> nil; |
| 255 | end; |
| 256 | |
| 257 | procedure TThriftStreamAdapterDelphi.Open; |
| 258 | begin |
| 259 | |
| 260 | end; |
| 261 | |
| 262 | function TThriftStreamAdapterDelphi.Read(var buffer: TBytes; offset, |
| 263 | count: Integer): Integer; |
| 264 | begin |
| 265 | inherited; |
| 266 | Result := 0; |
| 267 | if count > 0 then |
| 268 | begin |
| 269 | Result := FStream.Read( Pointer(@buffer[offset])^, count) |
| 270 | end; |
| 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 | |
| 298 | procedure TThriftStreamAdapterDelphi.Write(const buffer: TBytes; offset, |
| 299 | count: Integer); |
| 300 | begin |
| 301 | inherited; |
| 302 | if count > 0 then |
| 303 | begin |
| 304 | FStream.Write( Pointer(@buffer[offset])^, count) |
| 305 | end; |
| 306 | end; |
| 307 | |
| 308 | end. |