IdAttachmentMemory.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $Log$
  13. Rev 1.6 6/29/04 12:27:14 PM RLebeau
  14. Updated to remove DotNet conditionals
  15. Updated constructor to call SetDataString()
  16. Rev 1.5 2004.02.03 5:44:52 PM czhower
  17. Name changes
  18. Rev 1.4 2004.02.03 2:12:04 PM czhower
  19. $I path change
  20. Rev 1.3 24/01/2004 19:07:18 CCostelloe
  21. Cleaned up warnings
  22. Rev 1.2 14/12/2003 18:07:16 CCostelloe
  23. Changed GetDataString to avoiud error 'String element cannot be passed to var
  24. parameter'
  25. Rev 1.1 13/05/2003 20:28:04 CCostelloe
  26. Bug fix: remove default values in Create to avoid ambiguities with
  27. Create(TCollection)
  28. Rev 1.0 11/14/2002 02:12:46 PM JPMugaas
  29. }
  30. unit IdAttachmentMemory;
  31. interface
  32. {$I IdCompilerDefines.inc}
  33. uses
  34. Classes, IdAttachment, IdMessageParts, IdGlobal;
  35. type
  36. TIdAttachmentMemory = class(TIdAttachment)
  37. protected
  38. FDataStream: TStream;
  39. FDataStreamBeforeLoadPosition: TIdStreamSize;
  40. function GetDataString: string;
  41. procedure SetDataStream(const Value: TStream);
  42. procedure SetDataString(const Value: string);
  43. public
  44. {CC: Bug fix, remove default values to resolve ambiguities with Create(TCollection).}
  45. constructor Create(Collection: TCollection); overload; override;
  46. constructor Create(Collection: TIdMessageParts; const CopyFrom: TStream); reintroduce; overload;
  47. constructor Create(Collection: TIdMessageParts; const CopyFrom: String); reintroduce; overload;
  48. destructor Destroy; override;
  49. property DataStream: TStream read FDataStream write SetDataStream;
  50. property DataString: string read GetDataString write SetDataString;
  51. function OpenLoadStream: TStream; override;
  52. procedure CloseLoadStream; override;
  53. procedure FinishTempStream; override;
  54. function PrepareTempStream: TStream; override;
  55. end;
  56. implementation
  57. uses
  58. SysUtils;
  59. { TIdAttachmentMemory }
  60. constructor TIdAttachmentMemory.Create(Collection: TCollection);
  61. begin
  62. inherited Create(Collection);
  63. FDataStream := TMemoryStream.Create;
  64. end;
  65. constructor TIdAttachmentMemory.Create(Collection: TIdMessageParts;
  66. const CopyFrom: TStream);
  67. var
  68. LSize: TIdStreamSize;
  69. begin
  70. inherited Create(Collection);
  71. FDataStream := TMemoryStream.Create;
  72. if Assigned(CopyFrom) then begin
  73. LSize := IndyLength(CopyFrom);
  74. if LSize > 0 then begin
  75. FDataStream.CopyFrom(CopyFrom, LSize);
  76. end;
  77. end;
  78. end;
  79. constructor TIdAttachmentMemory.Create(Collection: TIdMessageParts;
  80. const CopyFrom: String);
  81. begin
  82. inherited Create(Collection);
  83. FDataStream := TMemoryStream.Create;
  84. SetDataString(CopyFrom);
  85. end;
  86. destructor TIdAttachmentMemory.Destroy;
  87. begin
  88. FreeAndNil(FDataStream);
  89. inherited Destroy;
  90. end;
  91. procedure TIdAttachmentMemory.CloseLoadStream;
  92. begin
  93. DataStream.Position := FDataStreamBeforeLoadPosition;
  94. end;
  95. function TIdAttachmentMemory.GetDataString: string;
  96. var
  97. Pos: TIdStreamSize;
  98. begin
  99. Pos := FDataStream.Position;
  100. try
  101. FDataStream.Position := 0;
  102. Result := ReadStringFromStream(FDataStream, FDataStream.Size);
  103. finally
  104. FDataStream.Position := Pos;
  105. end;
  106. end;
  107. function TIdAttachmentMemory.OpenLoadStream: TStream;
  108. begin
  109. FDataStreamBeforeLoadPosition := DataStream.Position;
  110. DataStream.Position := 0;
  111. Result := DataStream;
  112. end;
  113. procedure TIdAttachmentMemory.SetDataStream(const Value: TStream);
  114. var
  115. LSize: TIdStreamSize;
  116. begin
  117. FDataStream.Size := 0;
  118. LSize := IndyLength(Value);
  119. if LSize > 0 then begin
  120. FDataStream.CopyFrom(Value, LSize);
  121. end;
  122. end;
  123. procedure TIdAttachmentMemory.SetDataString(const Value: string);
  124. begin
  125. FDataStream.Size := 0;
  126. WriteStringToStream(FDataStream, Value);
  127. end;
  128. procedure TIdAttachmentMemory.FinishTempStream;
  129. begin
  130. DataStream.Position := 0;
  131. end;
  132. function TIdAttachmentMemory.PrepareTempStream: TStream;
  133. begin
  134. DataStream.Size := 0;
  135. Result := DataStream;
  136. end;
  137. end.