How to get a byte array data into DoubleBuffer

痴心易碎 提交于 2019-12-20 04:17:03

问题


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();



来源:https://stackoverflow.com/questions/17391944/how-to-get-a-byte-array-data-into-doublebuffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!