This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory contents as file

 
Chad Cravens
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have contents in memory. Let's say I am storing the string "Hi There" as String str = "Hi There";

I have an executable program that reads contents from a file. I would like to take the contents from memory ("Hi There"), and have it read "from the filesystem" by the executable program, without having to actually write the contents on disk. Something like this:

Process p = Runtime.getRuntime().exec("executable -readfrom file");

Where "file" is a virtual file containing the contents in memory (str).

Can anyone point me in the right direction? I've tried several topics and I'm not sure how to google something like this.

Thanks!!!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If reaching into the memory space of another program was allowed it would be a HUGE security risk. Operating system designers spend a lot of effort making sure it can't happen.

What exactly do you think you would gain by this hack?

Note that java.io.File has perfectly usable methods for creating and deleting temporary file.

Bill
 
Chad Cravens
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry maybe the explanation of my problem was not well described.

I am not asking that another executable have access to the memory space of my running program, but rather, does Java have an API for creating virtual files where another program would access one of these virtual files as if it was accessing the file from the file system. Using this virtual file, I could stream data to its handle from content created dynamically in memory or from a database or from the network (etc etc). Since the other executable requires a file or directory as input, it would be fed this information from my running Java process.

The reason one may want to do this is that disk IO is very expensive, and files written to disk are not "dynamic" in nature, as the file must be removed and completely rewritten to the disk. There are a few concrete examples I could give if anyone is interested.

Some google searches of "java virtual file" yield some interesting topics:
http://tech.puredanger.com/2008/11/10/virtual-filesystem/
http://commons.apache.org/vfs/filesystems.html
http://www.jcp.org/en/jsr/detail?id=203

I'll post as I learn more. Would love any feedback anyone can provide regarding this if you have experience with this type of issue.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, there are just three virtual streams like that. In Java you may know them as System.in, System.out and System.err. Other (console) programs have the same streams, but they may be called differently (e.g., in C they are stdin, stdout and stderr after including stdio.h).

You can use p.getOutputStream() to get an OutputStream. Anything you write to this (remember to flush() to ensure the data is sent) will be available as stdin in the called process. Likewise, anything that the process writes to stdout is available as p.getInputStream(), and anything the process writes to stderr is available as p.getErrorStream(). You can use these to communicate with the process, but only if the process is coded to read from stdin instead of a file.

No matter how you solve this, you should read When Runtime.exec() won't by Michael C. Daconta. The article is almost 10 years old but is still the #1 article to read when using java.lang.Process, even if you don't need p.waitFor().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic