Example usage for io.netty.buffer ByteBufAllocator buffer

List of usage examples for io.netty.buffer ByteBufAllocator buffer

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator buffer.

Prototype

ByteBuf buffer(int initialCapacity);

Source Link

Document

Allocate a ByteBuf with the given initial capacity.

Usage

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGraphSONMessageSerializerV2d0.java

License:Apache License

@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage,
        final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;//  w  w w  .ja va2s  .co  m
    try {
        final byte[] payload = mapper.writeValueAsBytes(responseMessage);
        encodedMessage = allocator.buffer(payload.length);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(),
                AbstractGraphSONMessageSerializerV2d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGraphSONMessageSerializerV2d0.java

License:Apache License

@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator)
        throws SerializationException {
    ByteBuf encodedMessage = null;//w  w  w  .ja v  a  2s .c  o  m
    try {
        final byte[] header = obtainHeader();
        final byte[] payload = mapper.writeValueAsBytes(requestMessage);

        encodedMessage = allocator.buffer(header.length + payload.length);
        encodedMessage.writeBytes(header);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(),
                AbstractGraphSONMessageSerializerV2d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGryoMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage,
        final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;//from w  w w  .j  a va  2s  . com
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos, bufferSize);

            // request id - if present
            kryo.writeObjectOrNull(output,
                    responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class);

            // status
            output.writeShort(responseMessage.getStatus().getCode().getValue());
            output.writeString(responseMessage.getStatus().getMessage());
            kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes());

            // result
            kryo.writeClassAndObject(output, serializeToString ? serializeResultToString(responseMessage)
                    : responseMessage.getResult().getData());
            kryo.writeClassAndObject(output, responseMessage.getResult().getMeta());

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(
                        String.format("Message size of %s exceeds allocatable space", size));

            output.flush();
            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(baos.toByteArray());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(),
                GryoMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGryoMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator)
        throws SerializationException {
    ByteBuf encodedMessage = null;//from   w  w w .  j a v a2s . c o m
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos, bufferSize);
            final String mimeType = mimeTypesSupported()[0];
            output.writeByte(mimeType.length());
            output.write(mimeType.getBytes(UTF8));

            kryo.writeObject(output, requestMessage.getRequestId());
            output.writeString(requestMessage.getProcessor());
            output.writeString(requestMessage.getOp());
            kryo.writeObject(output, requestMessage.getArgs());

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(
                        String.format("Message size of %s exceeds allocatable space", size));

            output.flush();
            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(baos.toByteArray());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(),
                GryoMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGryoMessageSerializerV3d0.java

License:Apache License

@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage,
        final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;/*from   w w  w.j  a v a 2 s.c o  m*/
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos, bufferSize);
            final ResponseMessage msgToWrite = !serializeToString ? responseMessage
                    : ResponseMessage.build(responseMessage.getRequestId())
                            .code(responseMessage.getStatus().getCode())
                            .statusAttributes(responseMessage.getStatus().getAttributes())
                            .responseMetaData(responseMessage.getResult().getMeta())
                            .result(serializeResultToString(responseMessage))
                            .statusMessage(responseMessage.getStatus().getMessage()).create();
            kryo.writeObject(output, msgToWrite);

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(
                        String.format("Message size of %s exceeds allocatable space", size));

            output.flush();
            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(baos.toByteArray());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(),
                GryoMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGryoMessageSerializerV3d0.java

License:Apache License

@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator)
        throws SerializationException {
    ByteBuf encodedMessage = null;//  ww  w  .  j a v a 2s  .  c om
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos, bufferSize);
            final String mimeType = mimeTypesSupported()[0];
            output.writeByte(mimeType.length());
            output.write(mimeType.getBytes(UTF8));

            kryo.writeObject(output, requestMessage);

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(
                        String.format("Message size of %s exceeds allocatable space", size));

            output.flush();
            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(baos.toByteArray());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(),
                GryoMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractJsonMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage,
        final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;/*from  w  w w  .  jav a2s.co  m*/
    try {
        final Map<String, Object> result = new HashMap<>();
        result.put(SerTokens.TOKEN_DATA, responseMessage.getResult().getData());
        result.put(SerTokens.TOKEN_META, responseMessage.getResult().getMeta());

        final Map<String, Object> status = new HashMap<>();
        status.put(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage());
        status.put(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue());
        status.put(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes());

        final Map<String, Object> message = new HashMap<>();
        message.put(SerTokens.TOKEN_STATUS, status);
        message.put(SerTokens.TOKEN_RESULT, result);
        message.put(SerTokens.TOKEN_REQUEST,
                responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null);

        final byte[] payload = mapper.writeValueAsBytes(message);
        encodedMessage = allocator.buffer(payload.length);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(),
                AbstractJsonMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractJsonMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator)
        throws SerializationException {
    ByteBuf encodedMessage = null;/*from w w w . j  a va2  s  . co m*/
    try {
        final byte[] header = obtainHeader();
        final byte[] payload = mapper.writeValueAsBytes(requestMessage);

        encodedMessage = allocator.buffer(header.length + payload.length);
        encodedMessage.writeBytes(header);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(),
                AbstractJsonMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage,
        final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;//from   w  ww .j ava  2s.c om
    try {
        final Map<String, Object> result = new HashMap<>();
        result.put(SerTokens.TOKEN_DATA, serializeToString ? serializeResultToString(responseMessage)
                : responseMessage.getResult().getData());
        result.put(SerTokens.TOKEN_META, responseMessage.getResult().getMeta());

        final Map<String, Object> status = new HashMap<>();
        status.put(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage());
        status.put(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue());
        status.put(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes());

        final Map<String, Object> message = new HashMap<>();
        message.put(SerTokens.TOKEN_STATUS, status);
        message.put(SerTokens.TOKEN_RESULT, result);
        message.put(SerTokens.TOKEN_REQUEST,
                responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null);

        final Kryo kryo = kryoThreadLocal.get();
        try (final OutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos);
            kryo.writeClassAndObject(output, message);

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(
                        String.format("Message size of %s exceeds allocatable space", size));

            encodedMessage = allocator.buffer((int) output.total());
            encodedMessage.writeBytes(output.toBytes());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(),
                GryoMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator)
        throws SerializationException {
    ByteBuf encodedMessage = null;/*from   w w w .j  a v a 2 s.  c  om*/
    try {
        final Kryo kryo = kryoThreadLocal.get();
        try (final OutputStream baos = new ByteArrayOutputStream()) {
            final Output output = new Output(baos);
            final String mimeType = serializeToString ? MIME_TYPE_STRINGD : MIME_TYPE;
            output.writeByte(mimeType.length());
            output.write(mimeType.getBytes(UTF8));

            final Map<String, Object> request = new HashMap<>();
            request.put(SerTokens.TOKEN_REQUEST, requestMessage.getRequestId());
            request.put(SerTokens.TOKEN_PROCESSOR, requestMessage.getProcessor());
            request.put(SerTokens.TOKEN_OP, requestMessage.getOp());
            request.put(SerTokens.TOKEN_ARGS, requestMessage.getArgs());

            kryo.writeClassAndObject(output, request);

            final long size = output.total();
            if (size > Integer.MAX_VALUE)
                throw new SerializationException(
                        String.format("Message size of %s exceeds allocatable space", size));

            encodedMessage = allocator.buffer((int) size);
            encodedMessage.writeBytes(output.toBytes());
        }

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(),
                GryoMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}