Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Stream/Channel in memory; C# MemoryStream equivalent?

807589Dec 23 2008 — edited Dec 30 2008
Alright, I've tried to do my homework before posting this question, so bear with me. I'm coming from more of a C++ and C# background... I've only been pecking away at Java for 2-3 months.

In C#, Stream is an abstract class inherited by MemoryStream, FileStream, and NetworkStream... maybe even some others, who knows. These Streams allow you to read/write data without any concern for allocation. Very cool.

I've explored java.io.* and java.nio.* and I haven't seen anything that seems to be the equivalent of C#'s MemoryStream. Everything seems to either be file-based or have a static capacity (which I get to declare, but still).

I'm working with C library that I've been JNI-ing for a while here and am now trying to allow the Java devs at my company to interface with the files it manipulates without using the disk. What I mean is that by default, the C library reads and writes based on ANSI paths, which is pretty standard. I'm trying to extend it by allowing them to work completely from memory. We do a lot of processing and if I can keep the disk out of the equation, our work will go that much more quickly.

To accomplish this, I'm going to let them pass me a ByteBuffer, or a Channel, or InputStream and OutputStream... or... see, that's where I get lost. Which one? Or some other? I originally started on ByteBuffer, until I realized that the capacity is solid once I create it. The C library needs to write data out sometimes, and if I run out of space in my ByteBuffer, what then? Allocate a new, larger ByteBuffer, copy the old into the new, and move on? Seems clunky. There has to be a better way. I figure I can't be the only person who has ever needed this kind of functionality.

Any guidance you can provide would be highly appreciated!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 27 2009
Added on Dec 23 2008
3 comments
923 views