Search in sources :

Example 6 with EncodingException

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

the class ObjectEncodingHandlerCborImpl 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);
        objectMapper.writeValue((OutputStream) bos, args[0]);
        return byteBuf;
    } catch (Exception e) {
        ReferenceCountUtil.safeRelease(byteBuf);
        throw new EncodingException(RsocketErrorCode.message("RST-700500", Arrays.toString(args), "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 7 with EncodingException

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

the class ObjectEncodingHandlerCborImpl method encodingResult.

@Override
@NotNull
public ByteBuf encodingResult(@Nullable Object result) throws EncodingException {
    if (result != null) {
        ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
        try {
            ByteBufOutputStream bos = new ByteBufOutputStream(byteBuf);
            objectMapper.writeValue((OutputStream) bos, result);
            return byteBuf;
        } catch (Exception e) {
            ReferenceCountUtil.safeRelease(byteBuf);
            throw new EncodingException(RsocketErrorCode.message("RST-700500", result.toString(), "Bytebuf"), e);
        }
    }
    return EMPTY_BUFFER;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException) ByteBuf(io.netty.buffer.ByteBuf) EncodingException(com.alibaba.rsocket.encoding.EncodingException) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with EncodingException

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

the class ObjectEncodingHandlerHessianImpl method encode.

public static ByteBuf encode(Object obj) throws EncodingException {
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream bos = new ByteBufOutputStream(byteBuf);
        HessianSerializerOutput output = new HessianSerializerOutput(bos);
        output.writeObject(obj);
        output.flush();
        return byteBuf;
    } catch (Exception e) {
        ReferenceCountUtil.safeRelease(byteBuf);
        throw new EncodingException(RsocketErrorCode.message("RST-700500", obj.getClass().getName(), "byte[]"), e);
    }
}
Also used : HessianSerializerOutput(com.caucho.hessian.io.HessianSerializerOutput) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) EncodingException(com.alibaba.rsocket.encoding.EncodingException)

Example 9 with EncodingException

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

the class ObjectEncodingHandlerJsonImpl method encodingResult.

@Override
@NotNull
public ByteBuf encodingResult(@Nullable Object result) throws EncodingException {
    if (result == null) {
        return EMPTY_BUFFER;
    }
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        if (ktJson && KotlinSerializerSupport.isKotlinSerializable(result.getClass())) {
            return Unpooled.wrappedBuffer(KotlinSerializerSupport.encodeAsJson(result));
        } else {
            ByteBufOutputStream bos = new ByteBufOutputStream(byteBuf);
            JsonUtils.objectMapper.writeValue((OutputStream) bos, result);
            return byteBuf;
        }
    } catch (Exception e) {
        ReferenceCountUtil.safeRelease(byteBuf);
        throw new EncodingException(RsocketErrorCode.message("RST-700500", result.getClass().getCanonicalName(), "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) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with EncodingException

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

the class ObjectEncodingHandlerSerializationImpl method byteBufToObject.

private Object byteBufToObject(ByteBuf data) throws EncodingException {
    try {
        ObjectInputStream inputStream = new ObjectInputStream(new ByteBufInputStream(data));
        Object object = inputStream.readObject();
        inputStream.close();
        return object;
    } catch (Exception e) {
        throw new EncodingException(RsocketErrorCode.message("RST-700501", "byte[]", "Object"), e);
    }
}
Also used : EncodingException(com.alibaba.rsocket.encoding.EncodingException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) EncodingException(com.alibaba.rsocket.encoding.EncodingException) ObjectInputStream(java.io.ObjectInputStream)

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