private static ByteBuffer addCrsId(byte[] wkb, byte orderByte, int typeCode, int crsId) { ByteBuffer buffer = ByteBuffer.allocate( wkb.length + 4 ); // original capacity + 4 bytes for the CRS ID buffer.setByteOrder( ByteOrder.valueOf( orderByte ) ); buffer.put( orderByte ); // write byte order buffer.putUInt( typeCode | POSTGIS_SRID_FLAG ); // set SRID flag buffer.putInt( crsId ); // write CRS ID // write remaining data for ( int i = 5; i < wkb.length; i++ ) { buffer.put( wkb[i] ); } buffer.rewind(); return buffer; }
@Override public <P extends Position> Geometry<P> decode(ByteBuffer byteBuffer, CoordinateReferenceSystem<P> crs) { byteBuffer.rewind(); try { prepare(byteBuffer); Geometry<P> geom = decodeGeometry(byteBuffer, crs); byteBuffer.rewind(); return geom; } catch (BufferAccessException e) { throw new WkbDecodeException(e); } }
/** * Returns this instance as a hexadecimal string. * * @return A string representation of this ByteBuffer in hexadecimal form */ public String toString() { StringBuilder builder = new StringBuilder(); int savedPosition = buffer.position(); rewind(); for (int i = 0; i < limit(); i++) { int hexValue = readByte(); appendHexByteRepresentation(builder, hexValue); } buffer.position(savedPosition); return builder.toString(); }
/** * Used for testing purposes. * * @param other another <code>ByteBuffer</code> * @return true if both buffers contain the same bytes, false otherwise */ public boolean hasSameContent(ByteBuffer other) { if (other == null) return false; if (this.limit() != other.limit()) return false; int thisSavedPosition = this.buffer.position(); int otherSavedPosition = other.buffer.position(); this.rewind(); other.rewind(); for (int i = 0; i < this.limit(); i++) { if (this.get() != other.get()) return false; } this.buffer.position(thisSavedPosition); other.buffer.position(otherSavedPosition); return true; }
/** * Encodes a <code>Geometry</code> into a WKB representation using the specified byte-order. * * @param geometry The <code>Geometry</code> to be encoded as WKB. * @param byteOrder The WKB byte order, either {@link org.geolatte.geom.ByteOrder#XDR XDR} or {@link org.geolatte.geom.ByteOrder#NDR NDR} * @return A buffer of bytes that contains the WKB-encoded <code>Geometry</code>. */ @Override public <P extends Position> ByteBuffer encode(Geometry<P> geometry, ByteOrder byteOrder) { ByteBuffer output = ByteBuffer.allocate(calculateSize(geometry, true)); if (byteOrder != null) { output.setByteOrder(byteOrder); } writeGeometry(geometry, output); output.rewind(); return output; }
@Override public <P extends Position> ByteBuffer encode(Geometry<P> geometry, ByteOrder byteOrder) { if (geometry == null || hasEmpty(geometry)) return null; //size is size for WKB + 4 bytes for the SRID ByteBuffer output = ByteBuffer.allocate(calculateSize(geometry, false) + 4); if (byteOrder != null) { output.setByteOrder(byteOrder); } output.putInt(geometry.getSRID() == -1 ? 0 : geometry.getSRID()); writeGeometry(geometry, output); output.rewind(); return output; }