DefineInputStreamMethod

DefineInputStreamMethod["name", {"fname1"->function1, "fname2"->function2, ... }]
defines a custom input stream method with the specified name, allowing Mathematica to call the stream functions for opening and reading from an input stream.

Details and OptionsDetails and Options

  • An input stream method can provide a new source of bytes, a way to filter a stream of bytes, or a combination of both.
  • The following stream functions can be provided:
  • "NameTestFunction"whether a stream of the given name can be opened
    "ConstructorFunction"called when a new stream is opened
    "CloseFunction"close the stream and release its resources
    "ReadFunction"read bytes from the stream
    "EndOfFileQFunction"indicate whether the stream has no more input to read
    "WaitForInputFunction"wait until there is input to read
    "SeekFunction"reposition the point in the stream from which input is read
    "StreamPositionFunction"the current point in the stream from which input is read
    "StreamSizeFunction"the number of bytes that can be read from the stream
    "SeekableQFunction"indicate whether the stream method can reposition the point from which input is read
    "ErrorTextFunction"indicate whether there has been an error
    "ClearErrorFunction"clear the error indication
    "OptionChangesFunction"modify the options returned by Options[stream]
  • Only the must be supplied to DefineInputStreamMethod. Other functions have a default definition if a function is not provided.
  • Each InputStream opened with a stream method has a current state. This is where a stream method can store information such as a handle to the underlying stream resource, the current stream position, the latest error message, and other information needed by the stream method functions. The initial state expression is returned by . Other functions take the current state as a parameter and return a new state value.
  • If a function returning a new state value is not provided, the default definition returns the given state unmodified.
  • Functions for opening and closing an input stream are:
  • NameParametersReturn Value
    "NameTestFunction"{streamname}True|False
    "ConstructorFunction"{streamname,caller,opts}
    "CloseFunction"{state}ignored
  • is needed when the stream is opened with Method->Automatic, and Mathematica needs to choose a stream method to use based on streamname. A stream method will be used if its returns True. If no is provided, the default behavior is to return False.
  • When a stream is opened for reading, the is called. It opens a stream indicated by streamname, which could be a file name, URL, or other resource identification appropriate to the stream method. The caller argument is a symbol for issuing messages. The calling function, such as OpenRead, will complete its creation of an InputStream if success is True. The state is used as the initial value of the stream method's state.
  • is used for a stream st when Close[st] is called. Resources allocated in the should be deallocated here. The default definition does nothing.
  • Functions for reading from the stream include:
  • NameParametersReturn Value
    "ReadFunction"{state,n}{{byte1,...,byten},newstate}
    "EndOfFileQFunction"{state}
    "WaitForInputFunction"{state}{ignored,newstate}
  • is the essential function for reading from the stream. It takes an argument n and returns a list of n bytes, integers ranging from 0 to 255. Fewer bytes may be returned if the stream is at the end of file, or if there is an error while reading. In that case, and are used to determine the cause. If no is provided, the default definition returns an empty list of bytes.
  • is used in the process of reading from the stream, and a stream method should typically provide this function. This function should return True when the last byte of the stream has been returned by the . The default definition returns True.
  • is used in the process of reading from a stream. This method is useful for stream methods that read from a source where input may be available intermittently, such as from a pipe to another process. In such a case, a stream method's should ensure that it never waits for input and only returns as many bytes as available; is the place where it waits for input. The default definition does nothing.
  • Functions for reading and setting the stream size and position are as follows:
  • NameParametersReturn Value
    "StreamPositionFunction"{state}{position,newstate}
    "StreamSizeFunction"{state}
    "SeekableQFunction"{state}
    "SeekFunction"{state,offset}
  • is used to support StreamPosition. The position result is an integer that specifies the position of the current point in the open stream. A position of indicates that the stream position is unknown. The default definition returns a position of .
  • is used in the process of reading from the stream. A stream method that provides this function with an accurate stream size may perform faster than a stream method that does not. The size result is an integer that specifies the total number of bytes in the stream, or if the size is unknown. The default definition returns a size of .
  • is used to support SetStreamPosition. A stream method that provides a should provide a that returns True. The default definition returns False.
  • is used to support SetStreamPosition. If a stream method does not support setting the stream position, this function does not need to be provided, but should return False. This function is called with offset, which is either a non-negative integer indicating a byte position relative to the start of the stream, or Infinity indicating the position at the end of the stream. This function should return True to indicate that the seek succeeded.
  • Functions for handling errors are as follows:
  • NameParametersReturn Value
    "ErrorTextFunction"{state,nbytes}{result,newstate}
    "ClearErrorFunction"{state}
  • is used in the process of reading from the stream. It should return an error message String if there is an error condition and Null if there is no error. Mathematica will call this function if other functions indicate failure, for example, if returns fewer bytes than requested and returns False. The default definition returns a result of Null.
  • is called if returned an error string and is called to clear the error state. After this, should return Null until there is another error reading from the stream. The result of this function is ignored. It is recommended that a stream method providing an should also provide a definition.
  • is used to edit the options for a stream st from the list returned by Options[st, Method]. For example, a stream method may take an option that includes a password or other piece of sensitive security data. By default, Options will return that setting. This function provides a way to remove that option field. The format of this function is:
  • "OptionChangesFunction"->Function[{state,options},body;{newoptions,newstate}]

ExamplesExamplesopen allclose all

Basic Examples (1)Basic Examples (1)

Define a stream method that takes its input from a list of bytes, passed as a method option :

In[1]:=
Click for copyable input
Out[1]=

Open a stream to a list of bytes, which could have come from anywhere:

In[2]:=
Click for copyable input
Out[2]=

Read from the stream:

In[3]:=
Click for copyable input
Out[3]=

Close the stream:

In[4]:=
Click for copyable input
Out[4]=
New in 9
New to Mathematica? Find your learning path »
Have a question? Ask support »