Skip to content

Instantly share code, notes, and snippets.

@billdawson
Created May 2, 2011 21:33
Show Gist options
  • Save billdawson/952428 to your computer and use it in GitHub Desktop.
Save billdawson/952428 to your computer and use it in GitHub Desktop.
Examples - Stream blog post - FileStream
// Get the source file (this one is in Resources).
var infile = Titanium.Filesystem.getFile('emmy.jpg');
if (!infile.exists()) {
Ti.API.error("File not exists()");
return;
}
// Open for reading.
var instream = infile.open(Titanium.Filesystem.MODE_READ);
// Get a file descriptor for output file. (Doesn't need to exist.)
var outfile =
Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'emmy_out.jpg');
// Open for writing.
var outstream = outfile.open(Titanium.Filesystem.MODE_WRITE);
// The last two steps could have been combined into one using this code:
//var outstream =
// Titanium.Filesystem.openStream(Titanium.Filesystem.MODE_WRITE, Titanium.Filesystem.applicationDataDirectory, 'emmy_out.jpg');
// Create a 1K buffer for reading chunks.
var buffer = Titanium.createBuffer({length: 1024});
// Read and write chunks.
var size = 0;
while ((size = instream.read(buffer)) > -1) {
outstream.write(buffer);
Titanium.API.info('Wrote ' + size + ' bytes');
}
// Cleanup.
instream.close();
outstream.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment