Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a Get on an InputStream?

One annoying thing of encoded packages is that they have to be in a separate file. If we want to distribute a simple self contained app (encoded), we need to supply two files: the app "interface", and the app package.

If I place all the content of the encoded file inside a string, and transform that string into an InputStream, I'm halfway to view that package content as a file.

But Get, that to my knowledge is the only operation (also used by Needs) that has the decoding function, doesn't work on Streams. It only works on real files.

Can someone figure out a way to Get a Stream?

like image 350
P. Fonseca Avatar asked Jan 19 '23 21:01

P. Fonseca


2 Answers

Waiting for Mathematica to arrive on my iPhone so couldn't test anything, but why don't you write the string to a temporary file and Get that?

Update

Here's how to do it:

encoded = ToFileName[$TemporaryDirectory, "encoded"];

Export[encoded, "code string", "Text"]; (*export encrypted code to temp file *)

It's important to copy the contents of the code string from the ASCII file containing the encoded code using an ASCII editor and paste it between existing empty quotes (""). Mathematica will then do automatic escaping of backslashes and quotes that may be in the code. This file has been made earlier using Encode. Can't do it here in the sample code as SO's Markdown messes with the string.

Get[encoded] (* get encrypted code and decode *) 

DeleteFile[encoded] (* Remove temp  file *)

Final Answer

Get doesn't appear to be necessary for decoding. ImportString does work as well:

ImportString["code string", "NB"] 

As above, paste your encoded tekst from an ASCII editor straight between the "" and let MMA do the escaping.

enter image description here

like image 100
Sjoerd C. de Vries Avatar answered Mar 08 '23 18:03

Sjoerd C. de Vries


I don't know of a way to Get a Stream, but you could store the encoded data in your single package, write it out to a temp file, then read the temp file back in with Get.

like image 40
ragfield Avatar answered Mar 08 '23 17:03

ragfield