# Titanium.Stream
Stream module containing stream utility methods.
# Overview
This module provides a set of methods for interacting with
Titanium.IOStream objects, including asynchronous versions of the
read
and write
methods offered by all stream objects. These
methods should be used in any place where reading from or writing
to a stream might block.
See also:
- Titanium.IOStream
- Titanium.BlobStream
- Titanium.BufferStream
- Titanium.Filesystem.FileStream
- Titanium.Network.Socket.TCP
# Examples
# Stream.pump Example
The following example shows how to use the Titanium.Stream.pump method.
var pumpCallback = function(arg) {
if(arg.bytesProcessed == -1) {
Ti.API.info("At end of stream.");
} else {
Ti.API.info(String.format("Received %.0f bytes. Total: %.0f bytes.",
arg.bytesProcessed, arg.totalBytesProcessed));
// do something useful with the data in arg.buffer
}
}
Ti.Stream.pump(inputStream, pumpCallback, 1024);
# Write Stream to File
The following example uses the Titanium.Stream.writeStream method
to copy the contents of an input stream to a file. This can be used for any kind
of input stream -- BufferStream
, FileStream
, or Socket.TCP
.
var outFileStream = Ti.Filesystem.getFile('outfile.txt').open(Ti.Filesystem.MODE_WRITE);
// writes all data from inputStream to outFileStream in chunks of 1024 bytes
var bytesWritten = Ti.Stream.writeStream(inputStream, outFileStream, 1024);
Ti.API.info('Wrote ' + bytesWritten + ' bytes, closing both streams.');
inputStream.close();
outFileStream.close();
# Properties
# apiName READONLY
The name of the API that this proxy corresponds to.
The value of this property is the fully qualified name of the API. For example, Titanium.UI.Button
returns Ti.UI.Button
.
# bubbleParent
Indicates if the proxy will bubble an event to its parent.
Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.
Default: true
# lifecycleContainer
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.
# Methods
# addEventListener
Adds the specified callback as an event listener for the named event.
Parameters
Name | Type | Description |
---|---|---|
name | String | Name of the event. |
callback | Callback<Titanium.Event> | Callback function to invoke when the event is fired. |
Returns
- Type
- void
# applyProperties
Applies the properties to the proxy.
Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.
Parameters
Name | Type | Description |
---|---|---|
props | Dictionary | A dictionary of properties to apply. |
Returns
- Type
- void
# createStream
Creates stream from a Buffer
or Blob
object.
Returns Titanium.BufferStream or Titanium.BlobStream depending on whether a
Buffer
or Blob
is provided as the source
property in params
.
Blob
obects are read only. Throws an exception if MODE_WRITE
or MODE_APPEND
is
specified along with a blob object.
Parameters
Name | Type | Description |
---|---|---|
params | CreateStreamArgs | Parameter object, for specifying the source object and mode. |
Returns
- Type
- Titanium.IOStream
# fireEvent
Fires a synthesized event to any registered listeners.
Parameters
Name | Type | Description |
---|---|---|
name | String | Name of the event. |
event | Dictionary | A dictionary of keys and values to add to the Titanium.Event object sent to the listeners. |
Returns
- Type
- void
# pump
Reads data from input stream and passes it to a handler method.
After the end of the input stream is reached, the handler is called
a final time with no data and bytesProcessed
set to -1 to indicate
the end of the input data.
Parameters
Name | Type | Description |
---|---|---|
inputStream | Titanium.IOStream | Stream to pump from. |
handler | Callback<PumpCallbackArgs> | Handler method that will receive data from |
maxChunkSize | Number | Maximum number of bytes to pass to |
isAsync | Boolean | Specifies if the |
Returns
- Type
- void
# read
Asynchronously reads data from an Titanium.IOStream into a buffer.
Reads data from the stream and writes it to the buffer. The first byte is
written to buffer[offset]
. Reads until the provided buffer is full, the end of
the stream is detected, or the requested number of bytes have been read. Does not
resize the supplied buffer.
A request must include both offset
and length
parameters, or omit them both.
If the stream is not readable, an error is reported:
-
On iOS, throws an exception.
-
On Android, calls the callback with an error.
If offset
is past the last byte of the buffer, throws an exception.
Parameters
Name | Type | Description |
---|---|---|
sourceStream | Titanium.IOStream | Stream to read from. |
buffer | Titanium.Buffer | Buffer to read into. |
offset | Number | Offset to start reading stream data from, in bytes.
If specified, |
length | Number | Number of bytes of data to read from the stream.
If specified, |
resultsCallback | Callback<ReadCallbackArgs> | Function to call with the results of the read operation. |
Returns
- Type
- void
# readAll
Reads all data from the specified Titanium.IOStream.
Reads data from the stream, either synchronously or asynchronously.
Throws an exception if a read error is encountered or data can't be written to the buffer.
If the optional buffer
and resultsCallback
arguments are specified, the read operation
takes place asynchronously, and the results are passed to the callback.
The data is written to the provided buffer, which is resized if necessary.
If the buffer
and resultsCallback
arguments are omitted, a new buffer is
allocated and returned.
Parameters
Name | Type | Description |
---|---|---|
sourceStream | Titanium.IOStream | Stream to read from. |
buffer | Titanium.Buffer | Buffer to read into. If specified, |
resultsCallback | Callback<ReadCallbackArgs> | Function to call with the results of the read operation.
If specified, |
Returns
- Type
- Titanium.Buffer | void
# removeEventListener
Removes the specified callback as an event listener for the named event.
Multiple listeners can be registered for the same event, so the
callback
parameter is used to determine which listener to remove.
When adding a listener, you must save a reference to the callback function in order to remove the listener later:
var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);
To remove the listener, pass in a reference to the callback function:
window.removeEventListener('click', listener);
Parameters
Name | Type | Description |
---|---|---|
name | String | Name of the event. |
callback | Callback<Titanium.Event> | Callback function to remove. Must be the same function passed to |
Returns
- Type
- void
# write
Asynchronously writes data from a buffer to an Titanium.IOStream.
Data is read from the buffer and written to the stream.
If the offset
and length
arguments are specified, data is read from the buffer
starting at offset
, up to offset+length-1
or the end of the buffer, whichever
comes first.
If offset
and length
are omitted, the entire buffer is written to the stream.
If the output stream is not writable, an error is reported:
-
On iOS, an exception is thrown.
-
On Android, the callback is invoked with an error.
Parameters
Name | Type | Description |
---|---|---|
outputStream | Titanium.IOStream | Stream to write to. |
buffer | Titanium.Buffer | Buffer to write from. |
offset | Number | Offset to start writing buffer data from.
If specified, |
length | Number | Bytes of data to write. If specified, |
resultsCallback | Callback<WriteCallbackArgs> | Function to call with the results of the write operation. |
Returns
- Type
- void
# writeStream
Writes all data from an input stream to an output stream.
If the optional resultsCallback
argument is supplied, the operation is
performed asynchronously.
If the callback is omitted, this method is synchronous, and blocks until all data has been written. The synchronous version Returns the total number of bytes written.
If no data can be read from the input stream because the input stream is at end of stream, behavior varies by platform:
-
iOS throws an exception.
-
Android returns 0, or calls the callback with
bytesProcessed
set to 0.
Parameters
Name | Type | Description |
---|---|---|
inputStream | Titanium.IOStream | Stream to read from. |
outputStream | Titanium.IOStream | Stream to write to. |
maxChunkSize | Number | Maximum number of bytes to copy from |
resultsCallback | Callback<WriteStreamCallbackArgs> | Function to call with the results of the |
Returns
- Type
- void