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.
—-which stream classes can be used for writing to a file
ofstream?
fstream?
ostream?
——which ios modes are used for reading a file

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.

ifstream /with-f/ and istream — typedef specializations revealed

Background — “cin” is an instance of istream (no “f”); whereas ifstream is the most common file input utility.

“istream is a specialization (not a template instantiation) of the class template basic_istream” — many people say that but if the generic template is named “basic_istream” a specialization can’t have a different name! When you look at the code you realize it’s yet another typedef —

typedef basic_istream istream

Therefore, istream is an alias to a specialized basic_istream.

Now back to basics. Stripping layers of typedef and template specialization, the core structure is

———> basic_ifstream extends basic_istream extends ….

-) ifstream is a typedef of a specialization of basic_ifstream
-) istream is a typedef of a specialization of basic_istream

typedef basic_istream istream;
typedef basic_ifstream ifstream;

c++ stream format flags and manipulators – key words

bitVector — probably there’s a hidden bitArray of boolean flags for each stream instance. http://www.cplusplus.com/reference/iostream/ios_base/flags/

1-to-1 — one flag for one manipulator function

Concrete eg — http://www.cplusplus.com/reference/iostream/manipulators/showpos/

transformer — a typical manipulator is a function accepting a stream by reference, and returning the same stream object by reference