# Titanium.Stream

> Stream module containing stream utility methods.

- Source: https://titaniumsdk.com/docs/sdk/main/Titanium.Stream
- Titanium SDK version: main
- Kind: module
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 1.7
- Extends: Titanium.Module
- Inheritance: Titanium.Module > Titanium.Proxy

This module provides a set of methods for interacting with
[IOStream](https://titaniumsdk.com/docs/sdk/Titanium.IOStream.md) 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](https://titaniumsdk.com/docs/sdk/Titanium.IOStream.md)
* [Titanium.BlobStream](https://titaniumsdk.com/docs/sdk/Titanium.BlobStream.md)
* [Titanium.BufferStream](https://titaniumsdk.com/docs/sdk/Titanium.BufferStream.md)
* [Titanium.Filesystem.FileStream](https://titaniumsdk.com/docs/sdk/Titanium.Filesystem.FileStream.md)
* [Titanium.Network.Socket.TCP](https://titaniumsdk.com/docs/sdk/Titanium.Network.Socket.TCP.md)

## Example: Stream.pump Example

The following example shows how to use the [pump](Titanium.Stream.pump) method.

``` js
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);
```

## Example: Write Stream to File

The following example uses the [writeStream](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`.

``` js
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

### MODE_APPEND

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

Use with [createStream](https://titaniumsdk.com/docs/sdk/Titanium.Stream.md#createStream) to open a stream in append
mode.

### MODE_READ

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

Use with [createStream](https://titaniumsdk.com/docs/sdk/Titanium.Stream.md#createStream) to open a stream in read
mode.

### MODE_WRITE

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

Use with [createStream](https://titaniumsdk.com/docs/sdk/Titanium.Stream.md#createStream) to open a stream in write
mode.

## Methods

### createStream

- Platforms: Android, iPhone, iPad, macOS

Creates stream from a `Buffer` or `Blob` object.

Returns [Titanium.BufferStream](https://titaniumsdk.com/docs/sdk/Titanium.BufferStream.md) or [Titanium.BlobStream](https://titaniumsdk.com/docs/sdk/Titanium.BlobStream.md) depending on whether a
`Buffer` or `Blob` is provided as the `source` property in `params`.


`Blob` objects are read only. Throws an exception if `MODE_WRITE` or `MODE_APPEND` is
specified along with a blob object.

Parameters:

- `params` (CreateStreamArgs): Parameter object, for specifying the source object and mode.

Returns: Titanium.IOStream

### pump

- Platforms: Android, iPhone, iPad, macOS

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:

- `inputStream` (Titanium.IOStream): Stream to pump from.
- `handler` (Callback<PumpCallbackArgs>): Handler method that will receive data from `inputStream`.
- `maxChunkSize` (Number): Maximum number of bytes to pass to `handler` in a single call.
- `isAsync` (Boolean, optional): Specifies if the `pump` operation should run asynchronously. Default: `false`.

### read

- Platforms: Android, iPhone, iPad, macOS

Asynchronously reads data from an [IOStream](https://titaniumsdk.com/docs/sdk/Titanium.IOStream.md) 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:

- `sourceStream` (Titanium.IOStream): Stream to read from.
- `buffer` (Titanium.Buffer): Buffer to read into.
- `offset` (Number, optional): Offset to start reading stream data from, in bytes. If specified, `length` must be specified as well. Default: `0`.
- `length` (Number, optional): Number of bytes of data to read from the stream. If specified, `offset` must be specified as well. Default: `All available data or the length of the buffer, whichever is smaller.`.
- `resultsCallback` (Callback<ReadCallbackArgs>): Function to call with the results of the read operation.

### readAll

- Platforms: Android, iPhone, iPad, macOS

Reads all data from the specified [IOStream](https://titaniumsdk.com/docs/sdk/Titanium.IOStream.md).

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:

- `sourceStream` (Titanium.IOStream): Stream to read from.
- `buffer` (Titanium.Buffer, optional): Buffer to read into. If specified, `resultsCallback` must be specified as well.
- `resultsCallback` (Callback<ReadCallbackArgs>, optional): Function to call with the results of the read operation. If specified, `resultsCallback` must be specified as well.

Returns: Titanium.Buffer

### write

- Platforms: Android, iPhone, iPad, macOS

Asynchronously writes data from a buffer to an [IOStream](https://titaniumsdk.com/docs/sdk/Titanium.IOStream.md).

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:

- `outputStream` (Titanium.IOStream): Stream to write to.
- `buffer` (Titanium.Buffer): Buffer to write from.
- `offset` (Number, optional): Offset to start writing buffer data from. If specified, `length` must be specified as well. Default: `0`.
- `length` (Number, optional): Bytes of data to write. If specified, `offset` must be specified as well. Default: `Length of the supplied buffer.`.
- `resultsCallback` (Callback<WriteCallbackArgs>): Function to call with the results of the write operation.

### writeStream

- Platforms: Android, iPhone, iPad, macOS

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:

- `inputStream` (Titanium.IOStream): Stream to read from.
- `outputStream` (Titanium.IOStream): Stream to write to.
- `maxChunkSize` (Number): Maximum number of bytes to copy from `inputStream` to `outputStream` in a single operation.
- `resultsCallback` (Callback<WriteStreamCallbackArgs>, optional): Function to call with the results of the `writeStream` operation.

## Inherited members

7 members are inherited. Each is documented on the type that declares it.

- [Titanium.Proxy](https://titaniumsdk.com/docs/sdk/main/Titanium.Proxy.md): `addEventListener`, `apiName`, `applyProperties`, `bubbleParent`, `fireEvent`, `lifecycleContainer`, `removeEventListener`

---

- This corpus documents Titanium SDK 13.4.1. Anything here that does not name a version describes 13.4.1.
- Titanium is maintained by TiDev. The Appcelerator toolchain is retired: there is no `appc` CLI, no Appcelerator Studio and no hosted build service. The supported CLI is `titanium` on npm, invoked as `ti`.

