Bulk I/O for Titanium Arrays
This page documents the Bulk I/O extensions implemented in Titanium for high-performance I/O on Titanium Arrays.
This library is now available in the tc compiler (starting at version 1.47), and is currently the fastest way to perform Ti Array I/O in Titanium.
All I/O operations in this library are synchronous (i.e. they block the caller until the operation completes) and work only on Ti Arrays. For information on asynchronous I/O or bulk I/O on Java Arrays, see the Bulk I/O page.
Please direct any usage questions or difficulties to Dan Bonachea.
Intro
Bulk I/O in Titanium works through two new methods on Titanium arrays:
.readFrom( ) and .writeTo( )
Declarations
Assuming you have a Titanium array 'A', whose elements are of atomic type - this means the element type is a primitive type (e.g. int, double, etc.) or an atomic immutable class (an immutable class whose members are all of primitives type or other atomic immutable classes):
This method performs a bulk read of data into the elements of array A from the 'infile' object, which must be an object of class java.io.RandomAccessFile, java.io.DataInputStream, or some subclass of one of these (for example, ti.io.BulkRandomAccessFile and ti.io.BulkDataInputStream also qualify). The number of elements read will be equal to A.domain().size() and they are read sequentially in row-major order. The method may throw java.io.IOException in the case of end-of-file or other I/O errors.
This method performs a bulk write of data from the elements of array A to the 'outfile' object, which must be an object of class java.io.RandomAccessFile, java.io.DataOutputStream, or some subclass of one of these (for example, ti.io.BulkRandomAccessFile and ti.io.BulkDataOutputStream also qualify). The number of elements written will be equal to A.domain().size() and they are written sequentially in row-major order. The method may throw java.io.IOException in the case of disk full or other I/O errors.
Note on Usage - Performing I/O on Subarrays
If you want to read/write a proper subset of the elements in a Ti array, then you should first use the regular .slice() .restrict() etc. methods on Ti arrays and make the I/O calls on the resultant array that includes just the elements you care about (these operations are implemented very efficiently without performing a copy).
Simple Example
import java.io.*;
...
// construct a buffered
DataInputStream
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("myinputfile")));
// construct a RandomAccessFile for output
RandomAccessFile raf = new RandomAccessFile("myoutputfile", "rw");
int width = dis.readInt();
// read some header
info
int height = dis.readInt();
double [2d] myTiArray = new double[1:width,1:height]; // allocate a buffer to hold input
myTiArray.readFrom(dis); // read a large array of doubles
... // do
some work on the array....
raf.seek(0);
// seek to some destination in the output file
raf.writeInt(width);
// output some header
info
raf.writeInt(height);
myTiArray.writeTo(raf); // write the large buffer of doubles
... // do some more work on the array...
myTiArray.restrict([2:7,4:10]).readFrom(dis); // now read into a small square of the grid
myTiArray.slice(1,4).writeTo(raf); // and write the entire fourth row
raf.close();
dis.close();
Extended Examples
taraftest.ti - A diagnostic tool that exercises the Ti Array bulk I/O methods with RandomAccessFile's
taddtest.ti - A diagnostic tool that exercises the Ti Array bulk I/O methods with DataInputStream's and DataOutputStream's
taglobtest.ti - A diagnostic tool that exercises the capabilities of the Ti Array bulk I/O methods when arrays are not local
(In order to compile these programs, you'll also need ExnType.ti)
Back to Bulk I/O page