Search in sources :

Example 16 with PackageEncodeException

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);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 17 with PackageEncodeException

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);
    }
}
Also used : NetworkResponsePackage(org.bboxdb.network.packages.NetworkResponsePackage) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 18 with PackageEncodeException

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);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities)

Example 19 with PackageEncodeException

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);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 20 with PackageEncodeException

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);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ArrayList(java.util.ArrayList) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple) TupleAndTable(org.bboxdb.storage.entity.TupleAndTable) Tuple(org.bboxdb.storage.entity.Tuple) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple)

Aggregations

PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)68 IOException (java.io.IOException)31 ByteBuffer (java.nio.ByteBuffer)21 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)16 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)14 RoutingHeader (org.bboxdb.network.routing.RoutingHeader)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ArrayList (java.util.ArrayList)9 BoundingBox (org.bboxdb.commons.math.BoundingBox)9 List (java.util.List)8 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)6 Tuple (org.bboxdb.storage.entity.Tuple)6 StreamClientQuery (org.bboxdb.network.server.StreamClientQuery)5 OperatorTreeBuilder (org.bboxdb.storage.queryprocessor.OperatorTreeBuilder)5 NetworkRequestPackage (org.bboxdb.network.packages.NetworkRequestPackage)4 Operator (org.bboxdb.storage.queryprocessor.operator.Operator)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ExceptionSafeRunnable (org.bboxdb.commons.concurrent.ExceptionSafeRunnable)3 CompressionEnvelopeRequest (org.bboxdb.network.packages.request.CompressionEnvelopeRequest)3 ClientQuery (org.bboxdb.network.server.ClientQuery)3