Browse Prior Art Database

Efficient passing of data across Java Native Interface between native code and Java code

IP.com Disclosure Number: IPCOM000235990D
Publication Date: 2014-Apr-01
Document File: 2 page(s) / 32K

Publishing Venue

The IP.com Prior Art Database

Abstract

Disclosed is a method for transferring data from non-Java code to Java in which the non-Java code creates a serialized version of a Java object in the form of a string. The method then passes that string as an argument to a Java method, invoked through the Java* Native Interface (JNI) layer, that de-serializes and instantiates the Java object. The availability of cross-language serialization libraries enables the same transfer mechanism to be applied in the Java to non-Java code direction.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 51% of the total text.

Page 01 of 2

Efficient passing of data across Java Native Interface between native code and Java code

The Java* Native Interface (JNI) provides a means by which programs written in the Java language may interact with software written in other languages (often languages that are compiled to machine-native code). JNI permits Java applications to access code and data in non-Java libraries or routines, and allows non-Java applications to access facilities provided in Java code. The transfer of data across the JNI is a tedious process involving much coding, because accessing data in Java objects through the JNI involves obtaining references to the objects and then obtaining references to accessor methods for the data or references to the data-containing fields within the objects. This data access code can become quite involved, especially when the data access must account for the possibility of errors. The JNI environment itself can be fragile, in that an unhandled error can potentially cause corruption to the environment and can cause the entire application to abort.

Rather than populating the fields of Java objects in the JNI layer, the solution for transferring data from non-Java code to Java is a method in which the non-Java code creates a serialized version of a Java object in the form of a string. The method then passes that string as an argument to a Java method, invoked through the JNI layer, that de-serializes and instantiates the Java object. The benefits to this approach are numerous. Non-Java code used to populate numerous fields in an object and then invoke a Java method to perform an operation in the JNI layer. Now, the JNI code is reduced to creating a Java String from the string containing the serialized object. It then invokes a Java method on that String to de-serialize and instantiate the object and performs the original operation. For transferring data from Java to non-Java code through the JNI layer, similar advantages may be realized, if appropriate de-serialization/parsing facilities exist in the non-Java environment.

The advent of standard, portable serialization formats, such as JavaScript** Object Notation (JSON), that are supported by libraries in multiple languages, makes it easy to use serialized Java objects across environments.

The pseudo-code to populate a Java object with a single String field and to perform an operation on it from the JNI is as follows:
stringClassReference = FindClass("java/lang/String")
if e...