Message Boards Message Boards

2
|
3583 Views
|
1 Reply
|
4 Total Likes
View groups...
Share
Share this post:

JLink 3.0 -- ideas?

I think it would be worthwhile to crowdsource some ideas for improving JLink, which is very powerful technology but may be showing a bit of age.

Here are some beginning ideas of mine. If people think this is a worthwhile topic for the Community, add your ideas here or constructively develop those proposed by others.

1.  Better support for Java 7. (no I am not sure what that means, but it appears that Java 7 is a major advance)
2.  Better support for using JLink to access JVM-compliant languages (Scala,Clojure, etc).  Some of the issues here were outlined in the talk I gave at the 2012 WTC
3. Finer control (via Options?) over how arguments are passed (and possibly retrieved) from Mathematica to Java.  For example, some Java side code wants an ArrayList. You may thus have to pre-wrap your arguments with something like this.  It's kind of ugly and kludgey.  Built-in support would be nice.
javaCollection::noadd="The `1` class does not have an add method"
javaCollection[x_List,collection_String]:=With[{j=JavaNew[collection]},If[MemberQ[Map[#@getName[]&,j@getClass[]@getMethods[]],"add"],
(Scan[j@add[If[JavaObjectQ@#,#,MakeJavaObject@#]]&,x];j),Message[javaCollection::noadd,collection]]]
4. A Methods function that does not wrap its output in TableForm, which is an old-fashioned way of presentation.  If one wants TableForm it should be TableForm[NewMethods]
POSTED BY: Seth Chandler
And the documentation could be streamlined.
I had to read it various times years ago before I understood how everything works.
Not every JLink programmer is a MathLink programmer.

There should be more examples at least in the JLink folder.

I would donate two , though maybe someone can help me how to change the Do to ParallelDo, i.e., 
how to do you do parallel (master/worker) evaluation using JLink? ( I gave up ).
Also, how to do encoding right (i.e., what if you have russian characters somewher, etc.)


Here's my little package:
[font='courier new', courier, monospace]
(* Mathematica Package *)

(* Created by the Wolfram Workbench 01.11.2012 *)

BeginPackage["Unzip`",{"JLink`"}]
(* Exported symbols added here with SymbolName::usage *)


CopyRemote::usage="CopyRemote[url, localfilename] copies a file from
an http location to localfilename.";

Unzip::usage="Unzip unzips file.";

Verbose::usage="Verbose is an option to Unzip."

Begin["`Private`"]

InstallJava[];

Set[Options , {CharacterEncoding :> $CharacterEncoding, Verbose -> True}]

Unzip[zipfilein_String?FileExistsQ, dir_: Directory[], OptionsPattern[]] :=
JavaBlock[
Module[ {enum, exdir = dir, saveEntry, startdir, zf, buf, zipfile, comments, targets, target, len},
zipfile = If[DirectoryName === "", FileNameJoin[{Directory[],zipfilein}], zipfilein];
buf = JavaNew["[B", 10000];
startdir = Directory[];
createdirs[di_String] :=
JavaBlock[JavaNew["java.io.File", di][mkdirs[]]; ] /;
FileNames === {};
If[ startdir =!= dir,
If[ FileNames === {},
createdirs
];
exdir = SetDirectory
];
saveEntry[zipfi_, zipentry_, characterencoding_: $CharacterEncoding] :=
JavaBlock[
Block[ {bos, fi, fos, numRead, stream, outStream, fromcharcode, topdirle},
fi = zipentry[getName[]];
If[ zipentry[isDirectory[]],
createdirs[StringJoin[dir, $PathnameSeparator, fi]],
stream =
JavaNew["java.io.BufferedInputStream",
zipfi];
outStream =
JavaNew["java.io.BufferedOutputStream",
JavaNew["java.io.FileOutputStream",
StringJoin[dir, "/", fi]]];
While[(numRead = stream) > 0,
outStream[write[buf, 0, numRead]]];
stream[close[]];
outStream[close[]];
]
]];
zf = JavaNew["java.util.zip.ZipFile", zipfile];
len = zf[size[]];
enum = zf[entries[]];
comments = OptionValue /. Options;
targets = Table[enum[nextElement[]],{len}];
Do[
If[ comments,
Print[StringJoin["extracting: ", exdir,
$PathnameSeparator,
StringReplace[target[getName[]], "/" -> $PathnameSeparator]]]
];
saveEntry[zf, target],
{target, targets}
];
zf @ close[];
If[ startdir =!= dir,
SetDirectory
];
dir
]];



(*
Example usage:
CopyRemote["http://www.mertig.com/mathdepot/buttons/ButtonTools.nb",
ToFileName[{$UserAddOnsDirectory,"SystemFiles","FrontEnd","Palettes"},
"ButtonTools.nb"]]
*)

(* You need JLink 2.0 or higher.
this code is based on the GetRemote example in the JLink
documentation *)

Options={ProxyHost :> None, ProxyPort :> None};

CopyRemote[url_String /; StringMatchQ[url, "http://*.*", IgnoreCase-> True],
localfile_:Automatic, opts___?OptionQ] :=
(
Needs["JLink`"];
JLink`JavaBlock[
Module[ {u, stream, numRead, outFile, buf, prxyHost, prxyPort},
{prxyHost, prxyPort} = {ProxyHost, ProxyPort} /.
Flatten[{opts}] /. Options;
JLink`InstallJava[];
If[ StringQ,
(* Set properties to force use of proxy. *)
JLink`SetInternetProxy[prxyHost, prxyPort]
];
u = JLink`JavaNew["java.net.URL", url];
(* This is where the error will show up if the URL is not valid.
A Java exception will be thrown during openStream, which
causes the method to return $Failed.
*)
stream = u@openStream[];
If[ stream === $Failed,
Return[$Failed]
];
buf = JLink`JavaNew["[B", 5000];
(* 5000 is an arbitrary buffer size *)
If[ StringQ,
outFile = OpenWrite[localfile, DOSTextFormat -> False],
outFile = OpenTemporary[DOSTextFormat->False];
];
While[(numRead = stream@read) > 0,
WriteString[outFile, FromCharacterCode[If[ # < 0,
#+256,
#
]& /@
Take[JLink`Val, numRead]]]
];
stream@close[];
Close
(* Close returns the filename *)
]
] );



End[]

EndPackage[]
POSTED BY: Rolf Mertig
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract