4

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?

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
P. Fonseca
  • 549
  • 4
  • 12

3 Answers3

6

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

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • 1
    @P. Fonseca In support to this suggestion: in really `StringToStream` just does the same thing: it creates a temporary file in the temporary directory, writes string in it and then operates with the file in the ordinary way. You can ascertain this fact by yourself using an utility line [Process Monitor](http://en.wikipedia.org/wiki/Process_Monitor#FileMon) for Windows. – Alexey Popkov Aug 28 '11 at 14:04
  • @alexey I remember your comment on this discussion on `ImportString`: http://stackoverflow.com/questions/6482607/is-put-get-cycle-in-mathematica-always-deterministic/6486999#6486999. The question is: Can you `Get` a `StringToStream` stream? Methinks not. – Sjoerd C. de Vries Aug 29 '11 at 21:36
  • I apologize for misleading but in the comment above I was wrong: I just mixed up `StringToStream` with `ImportString`. I have checked now and found that `StringToStream` does not create a temporary file while `ImportString` does. Sorry for confusion. – Alexey Popkov Aug 30 '11 at 07:29
  • @Sjoerd - almost perfect: can force a machineID, but not a specific Key. Works fine in Mathematica; doesn't work on the Free Player (I think it'sbecause it doesn't create the temporary exported file) but probably it also should not work because of the FREE part of the license agreement; I hope it works on the future player pro. Thank you. – P. Fonseca Aug 31 '11 at 10:34
  • @P.Fonseca Your question didn't mention CDF-player compatibility. If you're looking for that there's not much hope. The player doesn't allow exports, see http://stackoverflow.com/q/5592819/615464. – Sjoerd C. de Vries Aug 31 '11 at 11:56
3

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.

ragfield
  • 2,486
  • 13
  • 15
0

Just to keep things up to date:

Get works with streams since V9.0.

Kuba
  • 791
  • 8
  • 14