IdStreamNET.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 IdStreamNET;
  17. interface
  18. uses
  19. Classes,
  20. IdGlobal;
  21. type
  22. TIdStreamHelperNET = class
  23. public
  24. class function ReadBytes(
  25. AStream: TStream;
  26. var VBytes: TIdBytes;
  27. ACount: Integer = -1;
  28. AOffset: Integer = 0): Integer; static;
  29. class function Write(
  30. const AStream: TStream;
  31. const ABytes: TIdBytes;
  32. const ACount: Integer = -1;
  33. const AOffset: Integer = 0): Integer; static;
  34. class function Seek(
  35. const AStream: TStream;
  36. const AOffset: TIdStreamSize;
  37. const AOrigin: TSeekOrigin) : TIdStreamSize; static;
  38. end;
  39. implementation
  40. // RLebeau: must use a 'var' and not an 'out' for the VBytes parameter,
  41. // or else any preallocated buffer the caller passes in will get wiped out!
  42. class function TIdStreamHelperNET.ReadBytes(AStream: TStream; var VBytes: TIdBytes;
  43. ACount, AOffset: Integer): Integer;
  44. var
  45. LActual: Integer;
  46. begin
  47. Assert(AStream<>nil);
  48. Result := 0;
  49. if VBytes = nil then begin
  50. SetLength(VBytes, 0);
  51. end;
  52. //check that offset<length(buffer)? offset+count?
  53. //is there a need for this to be called with an offset into a nil buffer?
  54. LActual := ACount;
  55. if LActual < 0 then begin
  56. LActual := AStream.Size - AStream.Position;
  57. end;
  58. //this prevents eg reading 0 bytes at Offset=10 from allocating memory
  59. if LActual = 0 then begin
  60. Exit;
  61. end;
  62. if Length(VBytes) < (AOffset+LActual) then begin
  63. SetLength(VBytes, AOffset+LActual);
  64. end;
  65. Assert(VBytes<>nil);
  66. Result := AStream.Read(VBytes, AOffset, LActual);
  67. end;
  68. class function TIdStreamHelperNET.Write(const AStream: TStream;
  69. const ABytes: TIdBytes; const ACount: Integer; const AOffset: Integer): Integer;
  70. var
  71. LActual: Integer;
  72. begin
  73. Result := 0;
  74. Assert(AStream<>nil);
  75. //should we raise assert instead of this nil check?
  76. if ABytes <> nil then
  77. begin
  78. LActual := IndyLength(ABytes, ACount, AOffset);
  79. if LActual > 0 then
  80. begin
  81. // RLebeau: Write raises an exception if the buffer can't be written in full
  82. AStream.Write(ABytes, AOffset, LActual);
  83. Result := LActual;
  84. end;
  85. end;
  86. end;
  87. class function TIdStreamHelperNET.Seek(const AStream: TStream; const AOffset: TIdStreamSize;
  88. const AOrigin: TSeekOrigin): TIdStreamSize;
  89. begin
  90. Result := AStream.Seek(AOffset, AOrigin);
  91. end;
  92. end.