File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Memory contents as file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory contents as file" Watch "Memory contents as file" New topic
Author

Memory contents as file

Chad Cravens
Greenhorn

Joined: Oct 16, 2010
Posts: 2
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Oct 16, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Memory contents as file