use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class QueryInsertTimeRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] tableBytes = table.getFullnameBytes();
final ByteBuffer bb = ByteBuffer.allocate(14);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.put(getQueryType());
if (pagingEnabled) {
bb.put((byte) 1);
} else {
bb.put((byte) 0);
}
bb.putShort(tuplesPerPage);
bb.putLong(timestamp);
bb.putShort((short) tableBytes.length);
final long bodyLength = bb.capacity() + tableBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(tableBytes);
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 QueryJoinRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
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.putInt(tables.size());
bb.putInt(bboxBytes.length);
final ByteArrayOutputStream bStream = new ByteArrayOutputStream();
for (int i = 0; i < tables.size(); i++) {
final byte[] tablename = tables.get(i).getFullnameBytes();
bStream.write(DataEncoderHelper.shortToByteBuffer((short) tablename.length).array());
bStream.write(tablename);
}
final byte[] tablesArray = bStream.toByteArray();
final long bodyLength = bb.capacity() + tablesArray.length + bboxBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(bboxBytes);
outputStream.write(tablesArray);
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 QueryKeyRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] tableBytes = table.getFullnameBytes();
final byte[] keyBytes = key.getBytes();
final ByteBuffer bb = ByteBuffer.allocate(8);
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.putShort((short) keyBytes.length);
final long bodyLength = bb.capacity() + tableBytes.length + keyBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(tableBytes);
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 QueryKeyRequest method decodeTuple.
/**
* Decode the encoded package into a object
*
* @param encodedPackage
* @return
* @throws PackageEncodeException
* @throws IOException
*/
public static QueryKeyRequest decodeTuple(final ByteBuffer encodedPackage) throws PackageEncodeException, IOException {
final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_QUERY);
if (decodeResult == false) {
throw new PackageEncodeException("Unable to decode package");
}
final byte queryType = encodedPackage.get();
if (queryType != NetworkConst.REQUEST_QUERY_KEY) {
throw new PackageEncodeException("Wrong query type: " + queryType);
}
boolean pagingEnabled = false;
if (encodedPackage.get() != 0) {
pagingEnabled = true;
}
final short tuplesPerPage = encodedPackage.getShort();
final short tableLength = encodedPackage.getShort();
final short keyLength = encodedPackage.getShort();
final byte[] tableBytes = new byte[tableLength];
encodedPackage.get(tableBytes, 0, tableBytes.length);
final String table = new String(tableBytes);
final byte[] keyBytes = new byte[keyLength];
encodedPackage.get(keyBytes, 0, keyBytes.length);
final String key = new String(keyBytes);
if (encodedPackage.remaining() != 0) {
throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
}
final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
return new QueryKeyRequest(sequenceNumber, routingHeader, table, key, pagingEnabled, tuplesPerPage);
}
use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class QueryVersionTimeRequest method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] tableBytes = table.getFullnameBytes();
final ByteBuffer bb = ByteBuffer.allocate(14);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.put(getQueryType());
if (pagingEnabled) {
bb.put((byte) 1);
} else {
bb.put((byte) 0);
}
bb.putShort(tuplesPerPage);
bb.putLong(timestamp);
bb.putShort((short) tableBytes.length);
final long bodyLength = bb.capacity() + tableBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(tableBytes);
return headerLength + bodyLength;
} catch (IOException e) {
throw new PackageEncodeException("Got exception while converting package into bytes", e);
}
}
Aggregations