简体   繁体   English

Indy10中的DecodeToStream

[英]DecodeToStream in Indy10

I want to upgrade my application from Indy 9 to 10 with Delphi 2007. Now this don't compile anymore as DecodeToStream is not found. 我想用Delphi 2007将我的应用程序从Indy 9升级到10。现在这不再编译,因为找不到DecodeToStream。 The code use Bold framwork as there is reference to BoldElement. 代码使用Bold framwork,因为它引用了BoldElement。

Any alternative methods to call ? 有什么替代方法可以吗?

UPDATE (I think I simplify previous example too much) 更新 (我想我太简化了前面的例子)

Original code: 原始代码:

    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

After my change: 我改变后:

    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

But I'm not confident at all of my changes as I don't know Indy so much. 但是我对所有的变化都没有信心,因为我不太了解Indy。 So all comments are welcome. 所以欢迎所有评论。 One thing I don't understand is the call to CreateBlobStream. 我不明白的一件事是调用CreateBlobStream。 I should check with FastMM so it isn't a memleak. 我应该检查FastMM,这不是memleak。

Yes they changed a lot between 9 and 10. 是的,他们在9到10之间变了很多。

Now you have "DecodeBytes" instead of DecodeToStream I think. 现在你有了“DecodeBytes”而不是DecodeToStream。 So something like this should do it: 所以这样的事情应该这样做:

var
  DecodedBytes: TIdBytes;
begin
  MIMEDecoder := TidDecoderMIME.Create(nil);
  try
    DecodedBytes := MIMEDecoder.DecodeBytes(vString);
    vStream.Write(DecodedBytes[0], Length(DecodedBytes));
  finally
    FreeAndNil(MIMEDecoder);
  end;
end;

Using TIdDecoder.DecodeBegin() is the correct way to decode to a TStream. 使用TIdDecoder.DecodeBegin()是解码为TStream的正确方法。 However, you do not need the intermediate TIdMemoryStream (which, BTW, has not existed in Indy 10 for a long time now - consider upgrading to a newer release). 但是,您不需要中间TIdMemoryStream(BTW,现在很长一段时间不存在于Indy 10中 - 考虑升级到更新的版本)。 You can pass the Blob stream directly instead, for example: 您可以直接传递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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM