use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class AbstractBodyResponse method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final byte[] bodyBytes = body.getBytes();
final ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.putShort((short) bodyBytes.length);
// Write body length
final long bodyLength = bb.capacity() + bodyBytes.length;
final long headerLength = appendResponsePackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(bodyBytes);
return headerLength;
} 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 CompressionEnvelopeResponse method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
if (compressionType != NetworkConst.COMPRESSION_TYPE_GZIP) {
throw new PackageEncodeException("Unknown compression method: " + compressionType);
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final OutputStream os = new GZIPOutputStream(baos);
// Write packages
for (final NetworkResponsePackage networkResponsePackage : networkResponsePackages) {
networkResponsePackage.writeToOutputStream(os);
}
os.close();
final byte[] compressedBytes = baos.toByteArray();
// Header
final ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.put(compressionType);
bb.putShort((short) networkResponsePackages.size());
// Body length
final long bodyLength = bb.capacity() + compressedBytes.length;
// Write body length
final long headerLength = appendResponsePackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(compressedBytes);
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 HelloResponse method decodePackage.
/**
* Decode the encoded package into a object
*
* @param encodedPackage
* @return
* @throws PackageEncodeException
*/
public static HelloResponse decodePackage(final ByteBuffer encodedPackage) throws PackageEncodeException {
final short requestId = NetworkPackageDecoder.getRequestIDFromResponsePackage(encodedPackage);
final boolean decodeResult = NetworkPackageDecoder.validateResponsePackageHeader(encodedPackage, NetworkConst.RESPONSE_TYPE_HELLO);
if (decodeResult == false) {
throw new PackageEncodeException("Unable to decode package");
}
final int protocolVersion = encodedPackage.getInt();
final byte[] capabilityBytes = new byte[PeerCapabilities.CAPABILITY_BYTES];
encodedPackage.get(capabilityBytes, 0, capabilityBytes.length);
if (encodedPackage.remaining() != 0) {
throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
}
final PeerCapabilities peerCapabilities = new PeerCapabilities(capabilityBytes);
peerCapabilities.freeze();
return new HelloResponse(requestId, protocolVersion, peerCapabilities);
}
use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.
the class JoinedTupleResponse method writeToOutputStream.
@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
final ByteArrayOutputStream bStream = new ByteArrayOutputStream();
final ByteBuffer encodedTupleAmount = DataEncoderHelper.intToByteBuffer(joinedTuple.getNumberOfTuples());
bStream.write(encodedTupleAmount.array());
for (int i = 0; i < joinedTuple.getNumberOfTuples(); i++) {
final byte[] encodedBytes = NetworkTupleEncoderDecoder.encode(joinedTuple.getTuple(i), joinedTuple.getTupleStoreName(i));
bStream.write(encodedBytes);
}
bStream.close();
final byte[] allTupleBytes = bStream.toByteArray();
final long headerLength = appendResponsePackageHeader(allTupleBytes.length, outputStream);
outputStream.write(allTupleBytes);
return headerLength + allTupleBytes.length;
} 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 JoinedTupleResponse method decodePackage.
/**
* Decode the encoded package into a object
*
* @param encodedPackage
* @return
* @throws PackageEncodeException
*/
public static JoinedTupleResponse decodePackage(final ByteBuffer encodedPackage) throws PackageEncodeException {
final short requestId = NetworkPackageDecoder.getRequestIDFromResponsePackage(encodedPackage);
final boolean decodeResult = NetworkPackageDecoder.validateResponsePackageHeader(encodedPackage, NetworkConst.RESPONSE_TYPE_JOINED_TUPLE);
if (decodeResult == false) {
throw new PackageEncodeException("Unable to decode package");
}
final List<String> tupleStoreNames = new ArrayList<>();
final List<Tuple> tuples = new ArrayList<>();
final int numberOfTuples = encodedPackage.getInt();
for (int i = 0; i < numberOfTuples; i++) {
final TupleAndTable tupleAndTable = NetworkTupleEncoderDecoder.decode(encodedPackage);
tupleStoreNames.add(tupleAndTable.getTable());
tuples.add(tupleAndTable.getTuple());
}
final JoinedTuple joinedTuple = new JoinedTuple(tuples, tupleStoreNames);
if (encodedPackage.remaining() != 0) {
throw new PackageEncodeException("Some bytes are left after encoding: " + encodedPackage.remaining());
}
return new JoinedTupleResponse(requestId, joinedTuple);
}
Aggregations