2013-06-30 20 views
1

我想從字節數組中提取一組座標到DoubleBuffer中。如何將字節數組數據導入DoubleBuffer

下面是我如何從主字節數組中提取一組座標到另一個字節數組的示例。

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 

我的問題是:
我怎樣才能得到geomCoords字節數組爲雙緩衝?

如果不創建geomCoords,我可以將這些數據存入DoubleBuffer嗎?速度和效率是關鍵,所以任何捷徑或優化是最受歡迎的!

+1

提取'新的字節緩衝區(字節).asDoubleBuffer()' –

+0

@MarkoTopolnik所以呢?:DoubleBuffer dbuf = new ByteBuffer(geomCoords).asDoubleBuffer(); – Hank

+1

如何在一個字節數組中編碼座標? – Joni

回答

0

如果你知道,在字節的緩衝區的8個字節確實是雙打,後來乾脆

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

現在每個點都可以用dbls.get();

+0

這個想法。無法實例化ByteBuffer類型。 這是爲什麼? 搜索並找到下面這行: dbls = ByteBuffer.allocateDirect(geomCoords.length).order(endian).asDoubleBuffer(); – Hank

+0

@Ayman你不能用構造函數創建一個'ByteBuffer'。你必須使用靜態工廠方法之一。 – Jeffrey

+0

糟糕。是的,你需要包裝字節數組或使用另一個靜態工廠。 – Ayman