use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class HelloRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final ByteBuffer bb = DataEncoderHelper.intToByteBuffer(protocolVersion);
final byte[] peerCapabilitiesBytes = peerCapabilities.toByteArray();
// Body length
final long bodyLength = bb.capacity() + peerCapabilitiesBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
outputStream.write(bb.array());
outputStream.write(peerCapabilitiesBytes);
return headerLength + bodyLength;
} catch (Exception e) {
throw new PackageEncodeException("Got exception while converting package into bytes", e);
}
}
use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class InsertTupleRequest method decodeTuple.
/**
* Decode the encoded tuple into a object
*
* @param encodedPackage
* @return
* @throws IOException
* @throws PackageEncodeException
*/
public static InsertTupleRequest decodeTuple(final ByteBuffer encodedPackage) throws IOException, PackageEncodeException {
final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_INSERT_TUPLE);
if (decodeResult == false) {
throw new PackageEncodeException("Unable to decode package");
}
final TupleAndTable tupleAndTable = NetworkTupleEncoderDecoder.decode(encodedPackage);
if (encodedPackage.remaining() != 0) {
throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
}
final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
final TupleStoreName ssTableName = new TupleStoreName(tupleAndTable.getTable());
return new InsertTupleRequest(sequenceNumber, routingHeader, ssTableName, tupleAndTable.getTuple());
}
use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class KeepAliveRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] tableBytes = tablename.getBytes();
final ByteBuffer bb = ByteBuffer.allocate(8);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.putShort((short) tableBytes.length);
bb.put(NetworkConst.UNUSED_BYTE);
bb.put(NetworkConst.UNUSED_BYTE);
bb.putInt(tuples.size());
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(bb.array());
bos.write(tableBytes);
for (final Tuple tuple : tuples) {
final byte[] keyByteArray = tuple.getKey().getBytes();
final byte[] boundingBoxBytes = tuple.getBoundingBoxBytes();
final ByteBuffer keyLengthBytes = DataEncoderHelper.intToByteBuffer(keyByteArray.length);
final ByteBuffer boundingBoxLength = DataEncoderHelper.intToByteBuffer(boundingBoxBytes.length);
final ByteBuffer versionBytes = DataEncoderHelper.longToByteBuffer(tuple.getVersionTimestamp());
bos.write(keyLengthBytes.array());
bos.write(keyByteArray);
bos.write(boundingBoxLength.array());
bos.write(boundingBoxBytes);
bos.write(versionBytes.array());
}
bos.flush();
bos.close();
final byte[] bodyBytes = bos.toByteArray();
final int bodyLength = bodyBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
outputStream.write(bodyBytes);
return headerLength + bodyLength;
} catch (Exception e) {
throw new PackageEncodeException("Got exception while converting package into bytes", e);
}
}
use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class LockTupleRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] tablenameBytes = tablename.getBytes();
final byte[] keyBytes = key.getBytes();
final ByteBuffer bb = ByteBuffer.allocate(12);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.putShort((short) tablenameBytes.length);
bb.putShort((short) keyBytes.length);
bb.putLong(version);
// Body length
final long bodyLength = bb.capacity() + tablenameBytes.length + keyBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(tablenameBytes);
outputStream.write(keyBytes);
return headerLength + bodyLength;
} catch (IOException e) {
throw new PackageEncodeException("Got exception while converting package into bytes", e);
}
}
use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class QueryBoundingBoxRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] tableBytes = table.getFullnameBytes();
final byte[] bboxBytes = box.toByteArray();
final ByteBuffer bb = ByteBuffer.allocate(12);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.put(getQueryType());
if (pagingEnabled) {
bb.put((byte) 1);
} else {
bb.put((byte) 0);
}
bb.putShort(tuplesPerPage);
bb.putShort((short) tableBytes.length);
bb.put(NetworkConst.UNUSED_BYTE);
bb.put(NetworkConst.UNUSED_BYTE);
bb.putInt((int) bboxBytes.length);
final long bodyLength = bb.capacity() + tableBytes.length + bboxBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(tableBytes);
outputStream.write(bboxBytes);
return headerLength + bodyLength;
} catch (IOException e) {
throw new PackageEncodeException("Got exception while converting package into bytes", e);
}
}
Aggregations