Titanium mobile needs support for stream IO operations on appropriate objects such as files, sockets and any other types we decide upon in the future. In order to support generic stream handling and the exchange of data between streams a common stream interface needs to be defined.
This document will outline the proposed additions for supporting stream operations within Titanium mobile, types that will support stream operations, API changes and source examples.
Currently there isn't an easy or generic manner for reading or writing data from one complex type like xxx to another. By providing a Stream interface that these complex types can implement, we can allow developers to simplify the process of moving data between these complex types. In addition to the core stream interface which just allows these types to interact in a generic manner, convenience functions would be provided in a new Stream module that would allow many of the common operations on streams to be simplified further (IE: writing all data from one stream to another).
Ultimately, adding Stream support will enable our developers to simplify their source code and make IO operations for streaming types much more approachable.
- Stream - Generic input / output object that can be used for read / write operations with a variable frequency and amount.
- Buffer - Generally represents temporary storage for data when moving from one location to another (such as reading data off a socket and then writing to a file). In our case, the buffer will be a container for binary data rather than being limited to only string data. Buffer Spec
- Create Stream interface
- Create BufferStream object that implements Stream interface
- Create BlobStream object that implements the Stream interface
- Create FileStream object that implements Stream interface
- Update Socket object to implement Stream interface
Stream objects below only list the methods that are applicable to the IO operations. More methods may exist already that are left out for the sake of focus on the stream operations.
Stream module would expose generic Stream functionality outside of just the types that implement the Stream interface.
Common interface defines which methods all stream objects will need to support.
File stream object used for IO operations on a physical file. Implements TiStream interface (not listing interface methods).
Creating file stream object:
Socket object that supports IO operations. Implements Stream interface (not listing interface methods).
* refer to sockets spec for details on creating a socket
Reading data from file:
Reading data from file using readAll:
Writing data from one file to another
Write data from one stream to another. Useful for when writing large amounts of data that can not or should not be read into memory as a single chunk.
Reading data from one stream into a handler method:
Writing data from a buffer into a file (process is exactly the same if you replace TiBuffer with TiBlob):