use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.
the class BinaryDecoderTest method testDecodeNullArray.
@Test(description = "a null array, once encoded, should decode to a null array")
public void testDecodeNullArray() {
Argument argument = new Argument("test", Identifiers.Int16, 1, null, LocalizedText.NULL_VALUE);
@SuppressWarnings("unchecked") OpcUaBinaryDataTypeCodec<Argument> codec = (OpcUaBinaryDataTypeCodec<Argument>) OpcUaDataTypeManager.getInstance().getCodec(OpcUaDefaultBinaryEncoding.ENCODING_NAME, Argument.TYPE_ID.toNodeId(new NamespaceTable()).get());
assertNotNull(codec);
codec.encode(new TestSerializationContext(), writer, argument);
Argument decoded = codec.decode(new TestSerializationContext(), reader);
assertEquals(decoded.getName(), argument.getName());
assertNull(decoded.getArrayDimensions());
}
use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.
the class OpcUaBinaryStreamDecoder method readMessage.
@Override
public UaMessage readMessage(String field) throws UaSerializationException {
NodeId encodingId = readNodeId();
OpcUaBinaryDataTypeCodec<?> binaryCodec = (OpcUaBinaryDataTypeCodec<?>) context.getDataTypeManager().getCodec(encodingId);
if (binaryCodec != null) {
return (UaMessage) binaryCodec.decode(context, this);
} else {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + encodingId);
}
}
use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.
the class OpcUaBinaryStreamEncoder method writeStruct.
@Override
public void writeStruct(String field, Object value, NodeId dataTypeId) throws UaSerializationException {
try {
@SuppressWarnings("unchecked") OpcUaBinaryDataTypeCodec<Object> codec = (OpcUaBinaryDataTypeCodec<Object>) context.getDataTypeManager().getCodec(OpcUaDefaultBinaryEncoding.ENCODING_NAME, dataTypeId);
if (codec == null) {
throw new UaSerializationException(StatusCodes.Bad_EncodingError, "no codec registered: " + dataTypeId);
}
codec.encode(context, this, value);
} catch (ClassCastException e) {
throw new UaSerializationException(StatusCodes.Bad_EncodingError, e);
}
}
use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.
the class OpcUaDefaultBinaryEncoding method decode.
@Override
public Object decode(SerializationContext context, Object encodedBody, NodeId encodingId) {
try {
@SuppressWarnings("unchecked") OpcUaBinaryDataTypeCodec<Object> codec = (OpcUaBinaryDataTypeCodec<Object>) context.getDataTypeManager().getCodec(encodingId);
if (codec == null) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered for encodingId=" + encodingId);
}
ByteString binaryBody = (ByteString) encodedBody;
byte[] bs = binaryBody.bytesOrEmpty();
ByteBuf buffer = Unpooled.wrappedBuffer(bs);
OpcUaBinaryStreamDecoder reader = new OpcUaBinaryStreamDecoder(context).setBuffer(buffer);
return codec.decode(context, reader);
} catch (ClassCastException e) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
}
}
use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.
the class OpcUaBinaryStreamDecoder method readStructArray.
@Override
public Object[] readStructArray(String field, NodeId dataTypeId) throws UaSerializationException {
int length = readInt32();
if (length == -1) {
return null;
} else {
checkArrayLength(length);
OpcUaBinaryDataTypeCodec<?> binaryCodec = (OpcUaBinaryDataTypeCodec<?>) context.getDataTypeManager().getCodec(OpcUaDefaultBinaryEncoding.ENCODING_NAME, dataTypeId);
if (binaryCodec == null) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + dataTypeId);
}
Class<?> clazz = binaryCodec.getType();
Object array = Array.newInstance(clazz, length);
for (int i = 0; i < length; i++) {
Object value = binaryCodec.decode(context, this);
Array.set(array, i, value);
}
return (Object[]) array;
}
}
Aggregations