Search in sources :

Example 1 with EncodingException

use of com.alibaba.rsocket.encoding.EncodingException in project alibaba-rsocket-broker by alibaba.

the class ObjectEncodingHandlerCborImpl method decodeResult.

@Override
@Nullable
public Object decodeResult(ByteBuf data, @Nullable Class<?> targetClass) throws EncodingException {
    if (data.readableBytes() > 0 && targetClass != null) {
        try {
            // Kotlin Cbor Serializer
            if (ktCbor && KotlinSerializerSupport.isKotlinSerializable(targetClass)) {
                byte[] bytes = new byte[data.readableBytes()];
                data.readBytes(bytes);
                return KotlinSerializerSupport.decodeFromCbor(bytes, targetClass);
            }
            return objectMapper.readValue((InputStream) new ByteBufInputStream(data), targetClass);
        } catch (Exception e) {
            throw new EncodingException(RsocketErrorCode.message("RST-700501", "ByteBuf", targetClass.getName()), e);
        }
    }
    return null;
}
Also used : EncodingException(com.alibaba.rsocket.encoding.EncodingException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with EncodingException

use of com.alibaba.rsocket.encoding.EncodingException in project alibaba-rsocket-broker by alibaba.

the class ObjectEncodingHandlerJsonImpl method encodingParams.

@Override
public ByteBuf encodingParams(@Nullable Object[] args) throws EncodingException {
    if (isArrayEmpty(args)) {
        return EMPTY_BUFFER;
    }
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream bos = new ByteBufOutputStream(byteBuf);
        JsonUtils.objectMapper.writeValue((OutputStream) bos, args);
        return byteBuf;
    } catch (Exception e) {
        ReferenceCountUtil.safeRelease(byteBuf);
        throw new EncodingException(RsocketErrorCode.message("RST-700500", "Object[]", "ByteBuf"), e);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException) ByteBuf(io.netty.buffer.ByteBuf) EncodingException(com.alibaba.rsocket.encoding.EncodingException)

Example 3 with EncodingException

use of com.alibaba.rsocket.encoding.EncodingException in project alibaba-rsocket-broker by alibaba.

the class ObjectEncodingHandlerJsonImpl method decodeResult.

@Override
@Nullable
public Object decodeResult(ByteBuf data, @Nullable Class<?> targetClass) throws EncodingException {
    if (data.readableBytes() > 0 && targetClass != null) {
        try {
            if (ktJson && KotlinSerializerSupport.isKotlinSerializable(targetClass)) {
                byte[] bytes = new byte[data.readableBytes()];
                data.readBytes(bytes);
                return KotlinSerializerSupport.decodeFromJson(bytes, targetClass);
            } else {
                return JsonUtils.readJsonValue(data, targetClass);
            }
        } catch (Exception e) {
            throw new EncodingException(RsocketErrorCode.message("RST-700501", "bytebuf", targetClass.getName()), e);
        }
    }
    return null;
}
Also used : EncodingException(com.alibaba.rsocket.encoding.EncodingException) EncodingException(com.alibaba.rsocket.encoding.EncodingException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with EncodingException

use of com.alibaba.rsocket.encoding.EncodingException in project alibaba-rsocket-broker by alibaba.

the class ObjectEncodingHandlerSerializationImpl method objectToByteBuf.

private ByteBuf objectToByteBuf(Object obj) throws EncodingException {
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream bos = new ByteBufOutputStream(byteBuf);
        ObjectOutputStream outputStream = new ObjectOutputStream(bos);
        outputStream.writeObject(obj);
        outputStream.close();
        return byteBuf;
    } catch (Exception e) {
        ReferenceCountUtil.safeRelease(byteBuf);
        throw new EncodingException(RsocketErrorCode.message("RST-700500", obj.getClass().getName(), "byte[]"), e);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException)

Example 5 with EncodingException

use of com.alibaba.rsocket.encoding.EncodingException in project alibaba-rsocket-broker by alibaba.

the class ObjectEncodingHandlerAvorImpl method encodingResult.

@Override
@NotNull
public ByteBuf encodingResult(@Nullable Object result) throws EncodingException {
    if (result instanceof SpecificRecordBase) {
        Class<?> objectClass = result.getClass();
        Method toByteBufferMethod = toByteBufferMethodStore.get(objectClass);
        if (toByteBufferMethod != null) {
            try {
                ByteBuffer byteBuffer = (ByteBuffer) toByteBufferMethod.invoke(result);
                return Unpooled.wrappedBuffer(byteBuffer);
            } catch (Exception e) {
                throw new EncodingException(RsocketErrorCode.message("RST-700500", result.toString(), "ByteBuf"), e);
            }
        }
    }
    return EMPTY_BUFFER;
}
Also used : SpecificRecordBase(org.apache.avro.specific.SpecificRecordBase) EncodingException(com.alibaba.rsocket.encoding.EncodingException) Method(java.lang.reflect.Method) ByteBuffer(java.nio.ByteBuffer) EncodingException(com.alibaba.rsocket.encoding.EncodingException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

EncodingException (com.alibaba.rsocket.encoding.EncodingException)10 ByteBuf (io.netty.buffer.ByteBuf)6 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)6 NotNull (org.jetbrains.annotations.NotNull)3 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)2 Nullable (org.jetbrains.annotations.Nullable)2 HessianSerializerOutput (com.caucho.hessian.io.HessianSerializerOutput)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Method (java.lang.reflect.Method)1 ByteBuffer (java.nio.ByteBuffer)1 SpecificRecordBase (org.apache.avro.specific.SpecificRecordBase)1