[英]DecodeToStream in Indy10
我想用Delphi 2007將我的應用程序從Indy 9升級到10。現在這不再編譯,因為找不到DecodeToStream。 代碼使用Bold framwork,因為它引用了BoldElement。
有什么替代方法可以嗎?
更新 (我想我太簡化了前面的例子)
原始代碼:
BlobStreamStr : String;
MIMEDecoder : TidDecoderMIME;
if (BoldElement is TBATypedBlob) then
begin
BlobStreamStr := copy(ChangeValue,pos(']',ChangeValue)+1,maxint);
(BoldElement as TBATypedBlob).ContentType := copy(ChangeValue,2,pos(']',ChangeValue)-2);
MIMEDecoder := TidDecoderMIME.Create(nil);
try
MIMEDecoder.DecodeToStream(BlobStreamStr,(BoldElement as TBATypedBlob).CreateBlobStream(bmWrite));
finally
FreeAndNil(MIMEDecoder);
end;
end
我改變后:
BlobStreamStr : String;
MIMEDecoder : TidDecoderMIME;
LStream : TIdMemoryStream;
if (BoldElement is TBATypedBlob) then
begin
BlobStreamStr := copy(ChangeValue, pos(']', ChangeValue) + 1, maxint);
(BoldElement as TBATypedBlob).ContentType := copy(ChangeValue, 2, pos(']',ChangeValue)-2);
MIMEDecoder := TidDecoderMIME.Create(nil);
LStream := TIdMemoryStream.Create;
try
MIMEDecoder.DecodeBegin(LStream);
MIMEDecoder.Decode(BlobStreamStr, 0, Length(BlobStreamStr));
LStream.Position := 0;
ReadTIdBytesFromStream(LStream, DecodedBytes, Length(BlobStreamStr));
// Should memory for this stream be released ??
(BoldElement as TBATypedBlob).CreateBlobStream(bmWrite).Write(DecodedBytes[0], Length(DecodedBytes));
finally
MIMEDecoder.DecodeEnd;
FreeAndNil(LStream);
FreeAndNil(MIMEDecoder);
end;
end
但是我對所有的變化都沒有信心,因為我不太了解Indy。 所以歡迎所有評論。 我不明白的一件事是調用CreateBlobStream。 我應該檢查FastMM,這不是memleak。
是的,他們在9到10之間變了很多。
現在你有了“DecodeBytes”而不是DecodeToStream。 所以這樣的事情應該這樣做:
var
DecodedBytes: TIdBytes;
begin
MIMEDecoder := TidDecoderMIME.Create(nil);
try
DecodedBytes := MIMEDecoder.DecodeBytes(vString);
vStream.Write(DecodedBytes[0], Length(DecodedBytes));
finally
FreeAndNil(MIMEDecoder);
end;
end;
使用TIdDecoder.DecodeBegin()是解碼為TStream的正確方法。 但是,您不需要中間TIdMemoryStream(BTW,現在很長一段時間不存在於Indy 10中 - 考慮升級到更新的版本)。 您可以直接傳遞Blob流,例如:
var
BlobElement : TBATypedBlob;
BlobStreamStr : String;
BlobStream : TStream;
MIMEDecoder : TidDecoderMIME;
begin
...
if BoldElement is TBATypedBlob then
begin
BlobElement := BoldElement as TBATypedBlob;
BlobStreamStr := Copy(ChangeValue, Pos(']',ChangeValue)+1, Maxint);
BlobElement.ContentType := Copy(ChangeValue, 2, Pos(']',ChangeValue)-2);
BlobStream := BlobElement.CreateBlobStream(bmWrite);
try
MIMEDecoder := TidDecoderMIME.Create(nil);
try
MIMEDecoder.DecodeBegin(BlobStream);
try
MIMEDecoder.Decode(BlobStreamStr);
finally
MIMEDecoder.DecodeEnd;
end;
finally
FreeAndNil(MIMEDecoder);
end;
finally
FreeAndNil(BlobStream);
end;
end;
...
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.