我想使用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(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;

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

10-10 17:50