"File in memory"
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   amit_mendelson
Posted On:   Wednesday, July 11, 2001 08:53 AM

Is there a class in java that can represent a file in memory?
I receive an InputStream (via FTP), and have to store it somewhere in memory (NOT in the disk!) so I'll be able to read from it later.
I can't simply use the received InputStream since it causes my FTP Client to remain on hold (data wasn't read from it).
If you don't know a class for holding files, but one for bytes it will also be helpful.
please help!

Re: "File in memory"

Posted By:   Nils_Kulk  
Posted On:   Wednesday, July 11, 2001 09:08 AM

You can copy the content of the
Inputstream
to a byte array by using a java.io.ByteArrayOutputStream. This works (about) like this:

InputStream in = ...; // whereever you
// got it from
ByteArrayOutputStream out =
new ByteArrayOutputStream(1024);
int read;
byte[] tmpBuffer = new byte[1024];
while((read = in.read(tmpBuffer)) != -1) {
out.write(tmpBuffer, 0, read);
}
in.close();
out.flush();
byte[] file = out.toByteArray();
out.close();
...

Now file holds the bytes from
the downloaded file. You can use it (later)
with a java.io.ByteArrayInputStream:

...
InputStream in =
new ByteArrayInputStream(file);
... // use in as any InputStream
About | Sitemap | Contact