26
$\begingroup$

Version 9 introduced the WolframStreamsLibrary.h header for LibraryLink. It contains MInputStream and MOutputStream.

What can I do with this header? How can I use these functions? Are the documented anywhere?


Possibly related:


Update 2018 March: Partial spelunking results, someone else may want to pick up the trail.

Currently, my best guess is that this is for extending Mathematica with new streams. I believe it to be the analogue of DefineInputStreamMethod and DefineOutputStreamMethod. The struct MInputStream contains most of the functions that would be passed to DefineInputStreamMethod. The actual analogue is registerInputStreamMethod in WolframLibray.h, which I believe sets the equivalents of the ConstructorFunction and NameTestFunction. My (unverified!) guesses for the parameters of registerInputStreamMethod are:

  • const char *name, same as name in DefineInputStreamMethod
  • void (*ctor)(MInputStream, const char* msgHead, void* optionsIn), same as "ConstructorFunction". Should fill out the MInputStream struct passed to it. But in what format does it receive the options?
  • mbool (*handlerTest)(void*, char*), probably the same as "NameTestFunction". But what is the void * parameter for? The methodData?
  • void* methodData, possibly pointer to private data used by the stream implementation.
  • void (*destroyMethod)(void* methodData), possibly for freeing the methodData? This is not the "CloseFunction".

There are still many key questions remaining before one could actually implement everything that is needed.

What is the methodData? A state unique to each instance of the stream, or shared by all of them? (I assume this is shared, and the individual state is in the MInputStream struct.) What exactly should each of these functions do?

An example implementation is woefully needed. After some more spelunking, I found one in SystemFiles/Links/SocketLink/Source/C. This example may provide enough information for us to write our own implementation, but I have not yet had the time/motivation to do all this. I still wanted to share my findings with others who may be interested in continuing the spelunking.

Note that the SocketLink example won't answer all questions: it often passes NULL pointers or simply ignores arguments (e.g the options in the constructor).

$\endgroup$
8
  • 1
    $\begingroup$ I think these items are largely to do with the "data" (Hadoop, etc.) functionality being written by Taliesin Beynon and presumably to appear in version 10. (Other traces of this project are also visible already in version 9.) It wouldn't surprise me if they weren't documented until then, and as such, it may be that nobody can answer the question due to NDA. OTOH, maybe Joel Klein can comment at least on the stream functionality. $\endgroup$ Sep 4, 2013 at 9:33
  • 1
    $\begingroup$ @OleksandrR. I was thinking they might also be related to the AsynchronousTasks functionality of v9, and the DataStore might provide a way to temporarily store the results before they're ready to be returned. $\endgroup$
    – Szabolcs
    Sep 4, 2013 at 14:41
  • $\begingroup$ Szabolcs, what's up with the sudden large bounties? :) $\endgroup$
    – Mr.Wizard
    Dec 10, 2016 at 16:33
  • 1
    $\begingroup$ @Mr.Wizard Don't know what to do with all my reputation ... Also, previously I didn't really believe that bounties would work (or that they would motivate people—they don't motivate me). But in the last week I got more than one unexpectedly good answer to questions I set a bounty on. If you know any worthy questions, I'm willing to "donate" reputation for bounties :) $\endgroup$
    – Szabolcs
    Dec 10, 2016 at 16:35
  • 2
    $\begingroup$ @Szabolcs I see. I find bounties motivating only in that it indicates the question is particularly important to someone, and as from the beginning my biggest motivation in being here is the sense (hopefully not false) that I am helping people at least in some small way. Regarding "donation" I tried something like that myself once. I couldn't really tell if people appreciated that or not. If they did I am happy to "donate" more bounties myself. $\endgroup$
    – Mr.Wizard
    Dec 10, 2016 at 16:46

1 Answer 1

-4
$\begingroup$

MInputStream and MOutputStream are two classes in the LibraryLink API for Mathematica that provide functionality for reading from and writing to streams, respectively.

MInputStream is a class that represents an input stream, and it provides methods for reading data from the stream. Some of the common methods that can be used with MInputStream include:

read: Reads a byte from the stream and returns it as an integer. readDouble: Reads a double-precision floating-point number from the stream. readInt: Reads an integer from the stream. readString: Reads a string from the stream. MOutputStream, on the other hand, is a class that represents an output stream, and it provides methods for writing data to the stream. Some of the common methods that can be used with MOutputStream include:

write: Writes a byte to the stream. writeDouble: Writes a double-precision floating-point number to the stream. writeInt: Writes an integer to the stream. writeString: Writes a string to the stream. Both MInputStream and MOutputStream are useful when working with data streams, such as files or network sockets. For example, when writing a LibraryLink function that reads from a file, you can create an MInputStream object to represent the file stream, and then use the read methods to read data from the file. Similarly, when writing a LibraryLink function that writes to a file, you can create an MOutputStream object to represent the file stream, and then use the write methods to write data to the file.

$\endgroup$
1
  • 7
    $\begingroup$ You should include the chat gpt prompt you used to generate the answer as well $\endgroup$
    – Jason B.
    Feb 19 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.