WOLFRAM LANGUAGE TUTORIAL

Stream Methods

Introduction

Input and output streams are opened using a stream method. A stream method abstracts the stream operations required for reading or writing bytes from the source of those bytes. This makes it possible to read or write from data sources and storage mechanisms such as files, child processes, web servers, and in-memory lists of bytes. A stream method can also perform translations such as compression or encryption. For example, a stream method could allow a user to open a compressed file and read the uncompressed contents. Operating on streams allows operations to be performed on very large files that could otherwise not fit into physical memory.

The Wolfram System supplies stream methods for the most common needs, such as reading and writing from files, web servers, strings, and external commands. New stream methods can also be added to the Wolfram System.

Using Stream Methods

When you open a stream for reading or writing, you can specify the stream method with the Method option, or the Wolfram System can choose the stream method automatically from the stream name.

This opens a stream from a web server, selecting the stream method from the stream name.
In[1]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=
This opens a stream from a web server, selecting the stream method with the Method option.
In[5]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
This loads a Wolfram System package directly from a web server, selecting the stream method from the stream name:
In[9]:=
Click for copyable input
In[10]:=
Click for copyable input
Out[10]=
This loads a Wolfram System package directly from a web server, using an explicitly opened input stream:
In[11]:=
Click for copyable input
Out[11]=
In[12]:=
Click for copyable input
In[13]:=
Click for copyable input
In[14]:=
Click for copyable input
Out[14]=
In[15]:=
Click for copyable input
Out[15]=

Input Stream Methods

You can add new input stream methods to the Wolfram System. This would be useful if you wanted to read from a new storage medium, or you wanted to transform the data as it is read, such as uncompressing a file.

Define a stream method that takes its input not from a file but from a list of bytes, passed as a method option :
In[16]:=
Click for copyable input
Out[16]=
This tests the new method.
This defines a stream method that performs a rotation cipher on the stream as it reads. A cryptographically strong decryption could be performed in the same fashion.
In[21]:=
Click for copyable input
Out[23]=
Test the method by first writing an enciphered test file, and then reading it using the new method.
In[24]:=
Click for copyable input
Out[24]=
In[26]:=
Click for copyable input
Out[26]=
The file content differs from what Read returns, because the stream method translates it.
In[27]:=
Click for copyable input
Out[27]=
In[28]:=
Click for copyable input
Out[28]=

Output Stream Methods

You can add new output stream methods to the Wolfram System. This would be useful if you wanted to write to a new storage medium, or you wanted to transform the data as it is written, such as compressing a file.

This defines a minimal stream method that prints the number of bytes written.
In[30]:=
Click for copyable input
Out[30]=
This tests the new method.
In[31]:=
Click for copyable input
Out[31]=
In[32]:=
Click for copyable input
Out[32]=
In[33]:=
Click for copyable input
Out[33]=
In[34]:=
Click for copyable input
Out[34]=
In[35]:=
Click for copyable input
Out[35]=
In[36]:=
Click for copyable input