IdStreamVCL.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. unit IdStreamVCL;
  17. interface
  18. {$I IdCompilerDefines.inc}
  19. uses
  20. Classes,
  21. IdGlobal;
  22. type
  23. TIdStreamHelperVCL = class
  24. public
  25. class function ReadBytes(
  26. const AStream: TStream;
  27. var VBytes: TIdBytes;
  28. const ACount: Integer = -1;
  29. const AOffset: Integer = 0) : Integer; {$IFDEF DOTNET} static; {$ENDIF}
  30. class function Write(
  31. const AStream: TStream;
  32. const ABytes: TIdBytes;
  33. const ACount: Integer = -1;
  34. const AOffset: Integer = 0) : Integer; {$IFDEF DOTNET} static; {$ENDIF}
  35. class function Seek(
  36. const AStream: TStream;
  37. const AOffset: TIdStreamSize;
  38. const AOrigin: TSeekOrigin) : TIdStreamSize; {$IFDEF DOTNET} static; {$ENDIF}
  39. end;
  40. implementation
  41. // RLebeau: must use a 'var' and not an 'out' for the VBytes parameter,
  42. // or else any preallocated buffer the caller passes in will get wiped out!
  43. class function TIdStreamHelperVCL.ReadBytes(const AStream: TStream; var VBytes: TIdBytes;
  44. const ACount, AOffset: Integer): Integer;
  45. var
  46. LActual: Integer;
  47. begin
  48. Assert(AStream<>nil);
  49. Result := 0;
  50. if VBytes = nil then begin
  51. SetLength(VBytes, 0);
  52. end;
  53. //check that offset<length(buffer)? offset+count?
  54. //is there a need for this to be called with an offset into a nil buffer?
  55. LActual := ACount;
  56. if LActual < 0 then begin
  57. LActual := AStream.Size - AStream.Position;
  58. end;
  59. //this prevents eg reading 0 bytes at Offset=10 from allocating memory
  60. if LActual = 0 then begin
  61. Exit;
  62. end;
  63. if Length(VBytes) < (AOffset+LActual) then begin
  64. SetLength(VBytes, AOffset+LActual);
  65. end;
  66. Assert(VBytes<>nil);
  67. Result := AStream.Read(VBytes[AOffset], LActual);
  68. end;
  69. class function TIdStreamHelperVCL.Write(const AStream: TStream; const ABytes: TIdBytes;
  70. const ACount: Integer; const AOffset: Integer): Integer;
  71. var
  72. LActual: Integer;
  73. begin
  74. Result := 0;
  75. Assert(AStream<>nil);
  76. //should we raise assert instead of this nil check?
  77. if ABytes <> nil then begin
  78. LActual := IndyLength(ABytes, ACount, AOffset);
  79. // TODO: loop the writing, or use WriteBuffer(), to mimic .NET where
  80. // System.IO.Stream.Write() writes all provided bytes in a single operation
  81. if LActual > 0 then begin
  82. Result := AStream.Write(ABytes[AOffset], LActual);
  83. end;
  84. end;
  85. end;
  86. class function TIdStreamHelperVCL.Seek(const AStream: TStream; const AOffset: TIdStreamSize;
  87. const AOrigin: TSeekOrigin): TIdStreamSize;
  88. {$IFNDEF STREAM_SIZE_64}
  89. const
  90. cOrigins: array[TSeekOrigin] of Word = (soFromBeginning, soFromCurrent, soFromEnd);
  91. {$ENDIF}
  92. begin
  93. {$IFDEF STREAM_SIZE_64}
  94. Result := AStream.Seek(AOffset, AOrigin);
  95. {$ELSE}
  96. Result := AStream.Seek(AOffset, cOrigins[AOrigin]);
  97. {$ENDIF}
  98. end;
  99. end.