gpt4 book ai didi

delphi - Indy10 中的 DecodeToStream

转载 作者:行者123 更新时间:2023-12-02 00:42:45 28 4
gpt4 key购买 nike

我想使用 Delphi 2007 将我的应用程序从 Indy 9 升级到 10。现在,由于未找到 DecodeToStream,因此不再编译。该代码使用 Bold 框架,因为引用了 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 核实一下,这样就不会出现内存泄漏。

最佳答案

使用 TIdDecoder.DecodeBegin() 是解码为 TStream 的正确方法。但是,您不需要中间的 TIdMemoryStream(顺便说一句,它在 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;

关于delphi - Indy10 中的 DecodeToStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2028235/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com