|
JNIPort for Dolphin Smalltalk |
|
Back to Goodies |
Examples of Basic JNIPortThis section is an example of using the lowish-level interfaces of package 'CU Java Base', which are described here. We read a .ZIP file using Java's zip file handling classes. A parallel example uses the higher-level facilities provided by automatic wrapper generation (and especially, ghost classes) to perform the same tasks. One general point about this example: we use the low-level methods directly throughout, which is certainly not how you would use them in practice. The better way is to define wrapper classes and provide convenient Smalltalk-style methods that are built on top of the low-level API. In fact you'd probably want to use the Wrapper Wizard to help with this, rather than do it all by hand. Ensure that a JMV is running, and open a new workspace. We start, as always, by getting a reference to the JVM object:
Now we need to get a reference to the class static standing for the Java class,
Now we'll open a ZIP file. For this example, we'll use the DolphinJNIHelper.zip file that should be in the Metagnostic\Extras folder. Use the file dialog to find that file:
Now we create a new ZipFile object from that filename, this will be equivalent to the Java code:
At the level of this API, we have to build an argument array explicitly. First we convert
the Smalltalk String to a Java String, then allocate a
Now we want to call the constructor. All references to Java methods (and fields, in fact) must have a “signature” string (necessary because Java allows method overloading). The signature for a constructor that takes a String argument is '(Ljava/lang/String;)V'. For an explanation of this odd construction, see the rules for forming signatures. Once we have worked out the proper signature, we can use that and the argument vector to call the constructor:
If there are no arguments to the constructor, then a simpler form can be used (since the
signature is necessarily '()V'), the method is
We may as well find out how many entries there are in the ZIP file, in this case the Java
method takes no arguments and returns an
Now we'll iterate over the elements of the ZipFile; we start by getting the Java
The
Which is ugly, even though we can take advantage of the shortcut method
or, if you prefer:
which is marginally more efficient than the explicit loops, since it avoids repeatedly looking up the method names/signatures.
By the way, the adaptor object only implements a small part of the Smalltalk
<ReadableStream> protocol. You can get a more complete implementation
by either writing the other wrapper methods yourself <grin> or by
asking for a further level of adaptor by sending
Now to read one of the files from inside the ZipFile. We start by getting the appropriate ZipEntry, this will be the equivalent of the Java code:
The signature in this case is '(Ljava/lang/String;)Ljava/util/zip/ZipEntry;', and we also have to build another argument array, so the code is:
By now, you should have a very clear idea of why I wrote the wrapper generation features. If you check the Java documentation then you'll see that you can ask the ZipEntry for a fair raft of data. What you can't do is get the contents of the file, for that you have to go back to the ZipFile (that's just the way the Java class is designed), so:
This answers an object that is of some subclass of
That's almost the end of the example. We may as well clean up properly, though:
Finally, to see an example of what happens when Java code throws exceptions, try getting a ZipEntry again now that the ZipFile has been closed:
Which should give a normal Smalltalk walkback. You can trap the error in various ways, one is:
Another:
|
Copyright © Chris Uppal, 2003-2005
Java, JNI (probably), JVM (possibly), and God knows what else, are trademarks of Sun Microsystems, Inc.