开发者

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 开发者_StackOverflow社区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?


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.

How can I do a Get on an InputStream?


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.


Just to keep things up to date:

Get works with streams since V9.0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜