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 | |
| 22 | interface |
| 23 | |
| 24 | uses |
| 25 | Classes, |
| 26 | SysUtils, |
| 27 | SysConst, |
| 28 | RTLConsts, |
| 29 | Thrift.Utils, |
| 30 | ActiveX; |
| 31 | |
| 32 | type |
| 33 | |
| 34 | IThriftStream = interface |
| 35 | ['{732621B3-F697-4D76-A1B0-B4DD5A8E4018}'] |
| 36 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); |
| 37 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 38 | procedure Open; |
| 39 | procedure Close; |
| 40 | procedure Flush; |
| 41 | function IsOpen: Boolean; |
| 42 | function ToArray: TBytes; |
| 43 | end; |
| 44 | |
| 45 | TThriftStreamImpl = class( TInterfacedObject, IThriftStream) |
| 46 | private |
| 47 | procedure CheckSizeAndOffset( const buffer: TBytes; offset: Integer; count: Integer); |
| 48 | protected |
| 49 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); virtual; |
| 50 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; virtual; |
| 51 | procedure Open; virtual; abstract; |
| 52 | procedure Close; virtual; abstract; |
| 53 | procedure Flush; virtual; abstract; |
| 54 | function IsOpen: Boolean; virtual; abstract; |
| 55 | function ToArray: TBytes; virtual; abstract; |
| 56 | end; |
| 57 | |
| 58 | TThriftStreamAdapterDelphi = class( TThriftStreamImpl ) |
| 59 | private |
| 60 | FStream : TStream; |
| 61 | FOwnsStream : Boolean; |
| 62 | protected |
| 63 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 64 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
| 65 | procedure Open; override; |
| 66 | procedure Close; override; |
| 67 | procedure Flush; override; |
| 68 | function IsOpen: Boolean; override; |
| 69 | function ToArray: TBytes; override; |
| 70 | public |
| 71 | constructor Create( const AStream: TStream; AOwnsStream : Boolean); |
| 72 | destructor Destroy; override; |
| 73 | end; |
| 74 | |
| 75 | TThriftStreamAdapterCOM = class( TThriftStreamImpl) |
| 76 | private |
| 77 | FStream : IStream; |
| 78 | protected |
| 79 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 80 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
| 81 | procedure Open; override; |
| 82 | procedure Close; override; |
| 83 | procedure Flush; override; |
| 84 | function IsOpen: Boolean; override; |
| 85 | function ToArray: TBytes; override; |
| 86 | public |
| 87 | constructor Create( const AStream: IStream); |
| 88 | end; |
| 89 | |
| 90 | implementation |
| 91 | |
| 92 | { TThriftStreamAdapterCOM } |
| 93 | |
| 94 | procedure TThriftStreamAdapterCOM.Close; |
| 95 | begin |
| 96 | FStream := nil; |
| 97 | end; |
| 98 | |
| 99 | constructor TThriftStreamAdapterCOM.Create( const AStream: IStream); |
| 100 | begin |
| 101 | inherited Create; |
| 102 | FStream := AStream; |
| 103 | end; |
| 104 | |
| 105 | procedure TThriftStreamAdapterCOM.Flush; |
| 106 | begin |
| 107 | if IsOpen then |
| 108 | begin |
| 109 | if FStream <> nil then |
| 110 | begin |
| 111 | FStream.Commit( STGC_DEFAULT ); |
| 112 | end; |
| 113 | end; |
| 114 | end; |
| 115 | |
| 116 | function TThriftStreamAdapterCOM.IsOpen: Boolean; |
| 117 | begin |
| 118 | Result := FStream <> nil; |
| 119 | end; |
| 120 | |
| 121 | procedure TThriftStreamAdapterCOM.Open; |
| 122 | begin |
| 123 | |
| 124 | end; |
| 125 | |
| 126 | function TThriftStreamAdapterCOM.Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 127 | begin |
| 128 | inherited; |
| 129 | Result := 0; |
| 130 | if FStream <> nil then |
| 131 | begin |
| 132 | if count > 0 then |
| 133 | begin |
| 134 | FStream.Read( @buffer[offset], count, @Result); |
| 135 | end; |
| 136 | end; |
| 137 | end; |
| 138 | |
| 139 | function TThriftStreamAdapterCOM.ToArray: TBytes; |
| 140 | var |
| 141 | statstg: TStatStg; |
| 142 | len : Integer; |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 143 | NewPos : {$IF CompilerVersion >= 30.0} UInt64 {$ELSE} Int64 {$IFEND}; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 144 | cbRead : Integer; |
| 145 | begin |
| 146 | FillChar( statstg, SizeOf( statstg), 0); |
| 147 | len := 0; |
| 148 | if IsOpen then |
| 149 | begin |
| 150 | if Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) then |
| 151 | begin |
| 152 | len := statstg.cbSize; |
| 153 | end; |
| 154 | end; |
| 155 | |
| 156 | SetLength( Result, len ); |
| 157 | |
| 158 | if len > 0 then |
| 159 | begin |
| 160 | if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then |
| 161 | begin |
| 162 | FStream.Read( @Result[0], len, @cbRead); |
| 163 | end; |
| 164 | end; |
| 165 | end; |
| 166 | |
| 167 | procedure TThriftStreamAdapterCOM.Write( const buffer: TBytes; offset: Integer; count: Integer); |
| 168 | var |
| 169 | nWritten : Integer; |
| 170 | begin |
| 171 | inherited; |
| 172 | if IsOpen then |
| 173 | begin |
| 174 | if count > 0 then |
| 175 | begin |
| 176 | FStream.Write( @buffer[0], count, @nWritten); |
| 177 | end; |
| 178 | end; |
| 179 | end; |
| 180 | |
| 181 | { TThriftStreamImpl } |
| 182 | |
| 183 | procedure TThriftStreamImpl.CheckSizeAndOffset(const buffer: TBytes; offset, |
| 184 | count: Integer); |
| 185 | var |
| 186 | len : Integer; |
| 187 | begin |
| 188 | if count > 0 then |
| 189 | begin |
| 190 | len := Length( buffer ); |
| 191 | if (offset < 0) or ( offset >= len) then |
| 192 | begin |
| 193 | raise ERangeError.Create( SBitsIndexError ); |
| 194 | end; |
| 195 | if count > len then |
| 196 | begin |
| 197 | raise ERangeError.Create( SBitsIndexError ); |
| 198 | end; |
| 199 | end; |
| 200 | end; |
| 201 | |
| 202 | function TThriftStreamImpl.Read(var buffer: TBytes; offset, |
| 203 | count: Integer): Integer; |
| 204 | begin |
| 205 | Result := 0; |
| 206 | CheckSizeAndOffset( buffer, offset, count ); |
| 207 | end; |
| 208 | |
| 209 | procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 210 | begin |
| 211 | CheckSizeAndOffset( buffer, offset, count ); |
| 212 | end; |
| 213 | |
| 214 | { TThriftStreamAdapterDelphi } |
| 215 | |
| 216 | procedure TThriftStreamAdapterDelphi.Close; |
| 217 | begin |
| 218 | FStream.Free; |
| 219 | FStream := nil; |
| 220 | FOwnsStream := False; |
| 221 | end; |
| 222 | |
| 223 | constructor TThriftStreamAdapterDelphi.Create( const AStream: TStream; AOwnsStream: Boolean); |
| 224 | begin |
| 225 | inherited Create; |
| 226 | FStream := AStream; |
| 227 | FOwnsStream := AOwnsStream; |
| 228 | end; |
| 229 | |
| 230 | destructor TThriftStreamAdapterDelphi.Destroy; |
| 231 | begin |
| 232 | if FOwnsStream then |
| 233 | begin |
| 234 | FStream.Free; |
| 235 | end; |
| 236 | inherited; |
| 237 | end; |
| 238 | |
| 239 | procedure TThriftStreamAdapterDelphi.Flush; |
| 240 | begin |
| 241 | |
| 242 | end; |
| 243 | |
| 244 | function TThriftStreamAdapterDelphi.IsOpen: Boolean; |
| 245 | begin |
| 246 | Result := FStream <> nil; |
| 247 | end; |
| 248 | |
| 249 | procedure TThriftStreamAdapterDelphi.Open; |
| 250 | begin |
| 251 | |
| 252 | end; |
| 253 | |
| 254 | function TThriftStreamAdapterDelphi.Read(var buffer: TBytes; offset, |
| 255 | count: Integer): Integer; |
| 256 | begin |
| 257 | inherited; |
| 258 | Result := 0; |
| 259 | if count > 0 then |
| 260 | begin |
| 261 | Result := FStream.Read( Pointer(@buffer[offset])^, count) |
| 262 | end; |
| 263 | end; |
| 264 | |
| 265 | function TThriftStreamAdapterDelphi.ToArray: TBytes; |
| 266 | var |
| 267 | OrgPos : Integer; |
| 268 | len : Integer; |
| 269 | begin |
| 270 | len := 0; |
| 271 | if FStream <> nil then |
| 272 | begin |
| 273 | len := FStream.Size; |
| 274 | end; |
| 275 | |
| 276 | SetLength( Result, len ); |
| 277 | |
| 278 | if len > 0 then |
| 279 | begin |
| 280 | OrgPos := FStream.Position; |
| 281 | try |
| 282 | FStream.Position := 0; |
| 283 | FStream.ReadBuffer( Pointer(@Result[0])^, len ); |
| 284 | finally |
| 285 | FStream.Position := OrgPos; |
| 286 | end; |
| 287 | end |
| 288 | end; |
| 289 | |
| 290 | procedure TThriftStreamAdapterDelphi.Write(const buffer: TBytes; offset, |
| 291 | count: Integer); |
| 292 | begin |
| 293 | inherited; |
| 294 | if count > 0 then |
| 295 | begin |
| 296 | FStream.Write( Pointer(@buffer[offset])^, count) |
| 297 | end; |
| 298 | end; |
| 299 | |
| 300 | end. |