我应该如何调整我的代码以适应TBytes和TIdBytes之间的兼容性?

时间:2023-01-13 23:15:20

I am having the same problem as mentioned in "Delphi XE4 Indy compatibility issue between TBytes and TidBytes ", i.e. compatibility issues between TBytes(Delphi RTL) and TIdBytes(Indy) datatypes when compiling with the Delphi XE4. The source of my problem is that the code is not exactly according to Indy's interface and some of functions use TBytes, instead of TIdBytes, when calling native Indy IO procedures.

我遇到的问题与“TBytes和TidBytes之间的Delphi XE4 Indy不兼容问题”中提到的一样,即在使用Delphi XE4编译时TBytes(Delphi RTL)和TidBytes (Indy)数据类型之间的兼容性问题。我的问题的根源在于,在调用本机Indy IO过程时,代码并不完全符合Indy的接口,有些函数使用TBytes,而不是TIdBytes。

So I was wondering what will the best fix be?

所以我想知道最好的解决办法是什么?

As I see it there are two approaches:

在我看来,有两种方法:

  1. Refactor all functions in the project to use TIdBytes rather than TBytes.

    重构项目中的所有函数以使用TIdBytes而不是TBytes。

  2. Implement a TBytesToTidBytes conversion procedure (converts the TBytes to TIdBytes) and call that procedure prior to making the mentioned native Indy calls.

    实现一个tby睾丸肽字节转换过程(将TBytes转换为TIdBytes),并在进行上述本机Indy调用之前调用该过程。

Which of the approaches is better/best? Do you have any other ideas on how I can do that?

哪一种方法更好?关于我该怎么做,你还有别的想法吗?

FYI: The project I am trying to configure with the XE4 is available online on sourceforge : http://sourceforge.net/projects/indy10clieservr/?source=directory

我正在尝试使用XE4配置的项目可以在sourceforge: http://sourceforge.net/projects/indy10clieserv/?

The suggested conversion procedure should be something like:

建议的转换过程应该是:

procedure TBytesToTIdBytes(const Input:TBytes, var Output: TIdBytes)
var 
    i,L : Integer;
    allocate : Boolean;
begin
    L := Length(Input);
    if(Length(Output) <> L) then 
    begin 
        SetLength(Output,L);
    end;
    if(L > 0) then 
        move(Pointer(Input)^,Pointer(Output)^,L);
end;

2 个解决方案

#1


5  

TBytes and TIdBytes are both implemented as dynamic arrays, they are simply declared differently. The "politically correct" solution is to make a copy of the bytes. But that can waste memory for large arrays. A simpler solution is to use a typecast so you can utilize the array's internal reference count, eg:

TBytes和TIdBytes都是作为动态数组实现的,它们只是以不同的方式声明。“政治正确”的解决方案是复制字节。但这会浪费大数组的内存。一个更简单的解决方案是使用一个typecast,这样您就可以利用数组的内部引用计数(例如:

type
  PIdBytes = ^TIdBytes;
var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := PIdBytes(@B1)^;
end;

Or simply:

或者仅仅是:

var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := TIdBytes(B1);
end;

#2


1  

Both types are not the same at implementation level, in newer Delphi versions (TBytes is a simple alias for TArray<Byte> in recent Delphi releases).

在较新的Delphi版本中,这两种类型在实现级别上都不相同(在最近的Delphi版本中,TBytes是TArray 的简单别名)。

So I guess you can use such a procedure:

所以我猜你可以使用这样的程序:

procedure TBytesToTIdBytes(const Input: TBytes; var Output: TIdBytes);
var L: integer;
begin
  L := Length(Input);
  SetLength(Output,L);
  move(Input[0],Output[0],L);
end;

Here move() is faster than a loop.

这里移动()比循环快。

#1


5  

TBytes and TIdBytes are both implemented as dynamic arrays, they are simply declared differently. The "politically correct" solution is to make a copy of the bytes. But that can waste memory for large arrays. A simpler solution is to use a typecast so you can utilize the array's internal reference count, eg:

TBytes和TIdBytes都是作为动态数组实现的,它们只是以不同的方式声明。“政治正确”的解决方案是复制字节。但这会浪费大数组的内存。一个更简单的解决方案是使用一个typecast,这样您就可以利用数组的内部引用计数(例如:

type
  PIdBytes = ^TIdBytes;
var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := PIdBytes(@B1)^;
end;

Or simply:

或者仅仅是:

var
  B1: TBytes;
  B2: TIdBytes;
begin
  B1 := ...;
  B2 := TIdBytes(B1);
end;

#2


1  

Both types are not the same at implementation level, in newer Delphi versions (TBytes is a simple alias for TArray<Byte> in recent Delphi releases).

在较新的Delphi版本中,这两种类型在实现级别上都不相同(在最近的Delphi版本中,TBytes是TArray 的简单别名)。

So I guess you can use such a procedure:

所以我猜你可以使用这样的程序:

procedure TBytesToTIdBytes(const Input: TBytes; var Output: TIdBytes);
var L: integer;
begin
  L := Length(Input);
  SetLength(Output,L);
  move(Input[0],Output[0],L);
end;

Here move() is faster than a loop.

这里移动()比循环快。