J/Link examples


ImportURL

Modification of the GetURL example of the J/Link manual:

ImportURL imports a text file directly from an HTTP Input stream as a string into Mathematica, without a intermediate temporary file:
ImportURL[url_String]  :=   JavaBlock[
                Module[{u, s="", stream, numRead, buf},
                        InstallJava[];
                        u = JavaNew["java.net.URL", url];
                        stream = u@openStream[];
                        If[stream === $Failed, Return[$Failed]];
                        buf = JavaNew["[B", 5000]; (* 5000 is an arbitrary buffer size. *)                     
                        While[(numRead = stream@read[buf]) > 0,
                        s = (s<>FromCharacterCode[(If[#<0,#+256,#]&)/@
                        Take[Val[buf],numRead]])]
                        stream@close[];
                s]];
ImportRemoteNB[nb_String] := NotebookPut[Import[StringToStream[ImportURL[nb]]],"NB"]


Here a usage example:
ImportRemoteNB["http://www.mertig.com/mathdepot/ButtonTools.nb"]