Overview
Kotlin was designed so that it interoperates seamlessly with Java. Kotlin code can directly call any existing Java code without making any changes to it. This allows the OSS ASN.1 Tools for Java to easily be used with an application written in Kotlin.
Users of the OSS ASN.1/Java Tools typically deal with the following tasks:
1. Work with the API of Java classes generated for an ASN.1 schema. Use constructors, getters, and setters to create, examine, or modify objects that represent messages and their components.
2. Work with the API of ASN.1/Java coding services to serialize or deserialize objects that represent messages to or from encodings.
Using generated Java classes in a Kotlin application
A Kotlin application can invoke the constructors defined in generated Java classes as is, while getters and setters are mapped to Kotlin properties.
For example, this VehicleIdentification Java class
public class VehicleIdentification extends Sequence {
public VehicleIdentification();
public VehicleIdentification(WMInumber wMInumber, VDS vDS);
public WMInumber getWMInumber();
public void setWMInumber(WMInumber wMInumber);
public boolean hasWMInumber();
public void deleteWMInumber();
public VDS getVDS();
public void setVDS(VDS vDS);
public boolean hasVDS();
public void deleteVDS();
}
represents the VehicleIdentification component of the DENM message from the CAM/DENM ASN.1 Schema. Kotlin will map the following getters and setters of the Java class
public WMInumber getWMInumber();
public void setWMInumber(WMInumber wMInumber);
public VDS getVDS();
public void setVDS(VDS vDS);
to the "wmInumber" and "vds" Kotlin properties. The Kotlin application can then use the following code to instantiate the VehicleIdentification object:
val vehicleIdentification = VehicleIdentification().apply {
wmInumber = WMInumber("WVW")
vds = VDS("ZZZ1JZ")
}
Alternatively, the constructor with arguments defined in the generated VehicleIdentification Java class can be used
val vehicleIdentification =
VehicleIdentification(WMInumber("WVW"), VDS("ZZZ1JZ"))
and use the following code to modify or access the "vds" component of the VehicleIdentification:
vehicleIdentification.vds = VDS("ZZZ2JZ")
val vds = vehicleIdentification.vds
Using coding services in a Kotlin application
Using coding services in a Kotlin application is straightforward: just instantiate and configure the appropriate Coder object and invoke its encode() or decode() method to serialize or deserialize the message.
For example, a Kotlin application can use the following code to serialize the top-level CAM message from the CAM/DENM schema to an in-memory buffer:
//Instantiate and configure the appropriate Coder object
val coder = Camdenm.getPERUnalignedCoder().apply {
enableEncoderConstraints()
enableDecoderConstraints()
}
//Create the CAM message using the API of the generated Java classes
val pdu = CAM(...)
//Encode the "pdu" object that represents the CAM message to an
//in-memory buffer
val encoded = ByteArrayOutputStream().use {
coder.encode(pdu, it)
it.toByteArray()
}
The message can also be serialized to a disk file:
//Encode the "pdu" object that represents the CAM message to a
//disk file
val file = File("cam.uper")
file.outputStream().buffered().use {
coder.encode(pdu, it)
}
Use the following code to decode the message from an in-memory encoding and examine its contents:
val decoded = encoded.inputStream().use {
coder.decode(it, CAM())
}
val camBody = decoded.cam
Or the message can be decoded from a disk file:
val file = File("cam.uper")
val decoded = file.inputStream().buffered().use {
coder.decode(it, CAM())
}
You can download a free trial of the OSS ASN.1 Tools for Java and access the online documentation on the OSS Nokalva website. Please contact our technical support at support@oss.com with any questions about support for Kotlin or to receive a sample program that demonstrates how the OSS ASN.1 Tools for Java can be used with Kotlin. Complete details about calling Java from Kotlin are available here.