How to get a byte array data into DoubleBuffer

2019-03-02 18:54发布

问题:

I would like to extract a set of coordinates from a byte array into a DoubleBuffer.

Below is an example of how I extract a set of coordinates from the main byte array into another byte array.

byte intPoints[] = new byte[4];
byte geomCoords[];
...
is = new ByteArrayInputStream(stmt.column_bytes(0)); //reads the polygon from db
...
is.read(intPoints); //intPoints now holds the number of points in the polygon
//After this is read the actual coordinate list is next

//Set the size of geomCoords to hold all coordinates,
//There are 2 coordinates per point and each coordinate is a double value(8 bytes)
geomCoords = new byte[ByteBuffer.wrap(intPoints).order(endian).getInt() * 2 * 8];

is.read(geomCoords); //geomCoords now holds all the coordinates for the polygon

My questions are:
How can I get geomCoords byte array into a DoubleBuffer?
Or
Can I get this data into a DoubleBuffer without the creation of geomCoords? Speed and efficiency is key, so any shortcut or optimization is most welcome!

回答1:

If you know that the 8 bytes in the bytes buffer are indeed Doubles, then simply

DoubleBuffer dbls = new ByteBuffer(geomCoords).asDoubleBuffer();

Now each point can be extracted with dbls.get();