the "stream" concept in c++/c#/java

C had ascii/binary read/write functions. C++ introduced a family of “stream” classes as a complete replacement.

java and c# embraced and expanded the stream concepts and kissed goodby to the C legacy. I guess stream must be an immensely successful invention. Before we look at all the new stream classes like MemoryStream, GzipStream, StringStream… it’s good to understand the fundamental innovation. See http://www.cprogramming.com/tutorial/c++-iostreams.html.

I feel streams have become more versatile over the decades. Initially IO constructs for file and socket only. Then IPC. Then serialization. Then …

I particularly like the ostringstream — the “string builder” in c++.

Can we say the producer-consumer pattern is also stream-based? Probably not. Streams usually hold bytes or characters, not commands/tasks.

c++ stream — IKM findings

——tellg and tellp?
http://answers.yahoo.com/question/index?qid=20110507033952AAXZBz2 is a short explanation.

tellg(void) and tellp(void) both return their pointer’s position
——
ostream& endl (ostream& os);
——
stream::rdbuf() changes the filebuf… http://www.cplusplus.com/reference/ios/ios/rdbuf/
——

endl – flush() implicitly
——
If your variable to “populate” is an int, then extraction operator stops when hitting ….”any character that couldn’t be part of an int” which is not limited to white space.

IPC sockets, memory mapped files and sysV shmem

Requirement — Large volume data sharing across jvm and other unix processes.

1) DB is the most common solution.
2) sockets are well supported in java, c, perl, python but still requires copying lots of data. I think only the IPC socket is relevant, here, not inet sockets.
3) memory mapped files as a RandomAccessFile and MappedByteBuffer? Pure java solution — No JNI needed. I feel not so “popular”. May work in specific contexts.
4) sysV shmem? Presumably faster than data copying solutions like pipes, message queues, file-based. But JNI needed.

java getResourceAsStream() – easist(?) way to read any file

You can easily read a file in the same dir (esp. in a jar) as your class or anywhere(?) on file system. Methods below are usually conveniently called on the Class object, like

1) simplest usage:
InputStream is = this.getClass().getResourceAsStream(“any file such as a.txt”);
InputStream is = AnyClass.class.getResourceAsStream(“any file such as a.txt”);

InputStream is = GetResourceTest.class.getResourceAsStream(“../../test.txt”);
if (is==null) System.err.println(“file not found”);

Behind the scene, these methods delegate to the classloader’s getResourceAsStream()
http://mindprod.com/jgloss/getresourceasstream.html has good examples.