Created
May 2, 2011 21:33
-
-
Save billdawson/952428 to your computer and use it in GitHub Desktop.
Examples - Stream blog post - FileStream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open for reading. | |
var instream = infile.open(Titanium.Filesystem.MODE_READ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get a file descriptor for output file. (Doesn't need to exist.) | |
var outfile = | |
Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'emmy_out.jpg'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open for writing. | |
var outstream = outfile.open(Titanium.Filesystem.MODE_WRITE); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a 1K buffer for reading chunks. | |
var buffer = Titanium.createBuffer({length: 1024}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Read and write chunks. | |
var size = 0; | |
while ((size = instream.read(buffer)) > -1) { | |
outstream.write(buffer); | |
Titanium.API.info('Wrote ' + size + ' bytes'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cleanup. | |
instream.close(); | |
outstream.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment