Here is a console app that illustrates my problem. In short, I need to get from a ByteBuffer an array of short integers and add it to an arraylist of short integers as quickly as possible. I presume an .addAll() is the best way to do that.
The nullable types seem to be getting in my way, I think.
Comments embedded explain what I’m seeing.
namespace JavaConsoleApp;
interface
uses
java.util;
type
ConsoleApp = class
public
class method Main(args: array of String);
class method ReadFile(Buffer: java.nio.ByteBuffer; nWords: INT);
end;
AcArrayList<E> = public class (java.util.ArrayList<E>) // inherits constructor
public
procedure removeRange(arg1: Integer; arg2: Integer); override;
end;
INT = {not nullable} SmallInt; // I have the {not nullable} here to play around with alternative declarations of INT that will work. Without success.
IntArrayList0 = AcArrayList<INT>;
DWORD = UInt32;
implementation
class method ConsoleApp.Main(args: array of String);
begin
var nWords: INT := 10;
var wdInfo: IntArrayList0 := new IntArrayList0;
wdInfo.add(100); // Stick something in wdInfo
var Buffer: java.nio.ByteBuffer := java.nio.ByteBuffer.allocate(nWords * sizeOf(INT));
ReadFile(Buffer, nWords);
Buffer.position(0);
// At this point, all I want to do is convert the INTs in the Buffer and add them to wdInfo as fast as possible.
// I presume as fast as possible is with an addAll().
// So I'm trying to get to an array of INT, which this works...
var tempArray: Array of INT := new Array of INT(nWords);
Buffer.asShortBuffer().get(tempArray);
// And now I want as a list, so I can do my wdInfo.addAll()
// But I get: Error (E62) Type mismatch, cannot assign "array of INT" to "array of nullable Short"
var test := Arrays.asList(tempArray);
wdInfo.addAll(test);
wdInfo.add(200);
end;
class method ConsoleApp.ReadFile(Buffer: java.nio.ByteBuffer; nWords: INT);
begin
for iCntr: INT := 1 to nWords do
Buffer.putShort((iCntr - 1) * sizeOf(INT), iCntr);
end;
procedure AcArrayList<E>.removeRange (arg1: Integer; arg2: Integer); {override;}
begin
removeRange(arg1, arg2);
end;
end.
I have it working (essentially) by doing this:
for iCntr := 0 to pred(nWords) do
wdInfo.add(Buffer.getShort(iCntr * iSize));
but nWords is usually large, and this sort of thing happens frequently, and I am running into performance issues. I have to believe .addAll() is going to be faster. If I can just get it to behave. I must be missing something, I’ve tried all sorts of variations on the theme.
Suggestions?