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;
}
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);
}
}
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;
}
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);
}
}
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;
}
Aggregations