[Gdal-dev] Java bindings and wkb support

Andrea Aime aaime at openplans.org
Sun Feb 4 16:51:06 EST 2007

Hi all,
while using the java bindings I noticed that trying to
export geometries to WKB format makes the virtual
machine crash pretty consistently, whilst doing the
same with WKT seems to work just fine.

I'm using the above formats to convert an OGR geometry
to a JTS one. I would like to use WKB for the translation
since its production and parsing are a lot more efficient.

The code I'm using is:

char[][] charBuffArray = { new char[geom.WkbSize()] };
geom.ExportToWkb(charBuffArray, WKB_XDR);
char[] charBuffer = charBuffArray[0];
byte[] byteBuffer = new byte[charBuffer.length * 2];
for (int i = 0, j = 0; i < charBuffer.length; i++) {
     int buf = (int) charBuffer[i];
     byteBuffer[j] = (byte) (buf & 0xFF);
     byteBuffer[j] = (byte) ((buf & 0xFF00) >> 8);
Geometry g = wkbReader.read(byteBuffer);

and it tears apart the java virtual machine after a few geometries
are parsed.
Now, I noticed two things:
* the C++ interface uses char type, which is one byte in C++,
   but it's two bytes in java. In fact I get back two populated
   bytes for each char, that's why the above code splits each
   char into two bytes.
* preallocating the buffer does not seem to help much, apparently
   no matter what I do, the call to OGR will create another one
   and replace it (that is, if I create the char[][] as new char[1][]
   it crashes just the same, at the same point, on the same geometry,
   after having parsed a few others).

Does anyone have experience on this one?

More information about the Gdal-dev mailing list