From 3f8e9d8cb789a669e06e1833aa4d171b18ae0667 Mon Sep 17 00:00:00 2001 From: Pawel Kaczmarek Date: Mon, 2 Mar 2015 14:21:28 +0100 Subject: [PATCH] [Filesystem] File.openStream, FileStream.read, FileStream.readBytes, FileStream.readBase64 var documentsDir; function onsuccess(files) { for (var i = 0; i < files.length; i++) { console.log("File Name is " + files[i].name); } var testFile = files[0]; if (testFile !== null) { testFile.openStream( "r", function (fs) { var r1 = fs.read(200); console.log('Readed: ', r1); var r2 = fs.readBytes(200); console.log('Readed bytes: ', r2); var r3 = fs.readBase64(200); console.log('Readed base64: ', r3); }, function (e) { console.log("Error " + e.message); }, "UTF-8" ); } } function onerror(error) { console.log("The error " + error.message + " occurred when listing the files in the selected folder"); } tizen.filesystem.resolve( 'documents', function (dir) { documentsDir = dir; dir.listFiles(onsuccess, onerror); }, function (e) { console.log("Error" + e.message); }, "rw"); //should return text/bytes/base64 from first file in documents directory Change-Id: I2c911f8eb37f2a83df8b188332e28197dab1a6b3 Signed-off-by: Pawel Kaczmarek --- src/filesystem/js/file.js | 33 ++++++++----- src/filesystem/js/file_stream.js | 103 +++++++++++++++++++++++++++++++++++---- 2 files changed, 114 insertions(+), 22 deletions(-) diff --git a/src/filesystem/js/file.js b/src/filesystem/js/file.js index b7a7480..f031ed8 100644 --- a/src/filesystem/js/file.js +++ b/src/filesystem/js/file.js @@ -223,20 +223,29 @@ File.prototype.openStream = function(mode, onsuccess, onerror, encoding) { {name: 'encoding', type: types_.STRING, optional: true, nullable: true} ]); - var data = { - mode: args.mode, - encoding: args.encoding - }; + if (this.mode === 'r' && args.mode !== 'r') { + setTimeout(function() { + native_.callIfPossible(args.onerror, + new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, 'Read only mode')); + }, 0); + return; + } - var callback = function(result) { - if (native_.isFailure(result)) { - native_.callIfPossible(args.onerror, native_.getErrorObject(result)); - return; - } - native_.callIfPossible(args.onsuccess, new FileStream(native_.getResultObject(result))); - }; + if (this.isDirectory) { + var directoryMessage = 'This method should be called on file, not directory'; + setTimeout(function() { + native_.callIfPossible(args.onerror, new tizen.WebAPIException(tizen.WebAPIException.IO_ERR, + directoryMessage)); + }, 0); + return; + } + + _checkEncoding(args.encoding); - native_.call('File_openStream', data, callback); + var fileStream = new FileStream(this, args.mode, args.encoding); + setTimeout(function() { + native_.callIfPossible(args.onsuccess, fileStream); + }, 0); }; File.prototype.readAsText = function(onsuccess, onerror, encoding) { diff --git a/src/filesystem/js/file_stream.js b/src/filesystem/js/file_stream.js index ad9629e..ae2716a 100644 --- a/src/filesystem/js/file_stream.js +++ b/src/filesystem/js/file_stream.js @@ -3,22 +3,44 @@ // found in the LICENSE file. -function FileStream(fileDescriptor, nodeMode, nodeEncoding) { +function FileStream(data, mode, encoding) { + var _totalBytes = data.fileSize || 0; + var _position = mode === 'a' ? _totalBytes : 0; + Object.defineProperties(this, { - position: { - get: function() {}, + eof: { + value: false, enumerable: true, writable: false }, - eof: { - get: function() {}, - set: function() {}, + position: { + get: function() { + return _position; + }, + set: function(v) { + _position = Math.max(0, v); + }, enumerable: true }, bytesAvailable: { - get: function() {}, + value: this.eof ? -1 : Math.max(0, _totalBytes - _position), enumerable: true, writable: false + }, + _mode: { + value: mode, + writable: false, + enumerable: false + }, + _encoding: { + value: encoding, + writable: false, + enumerable: false + }, + _file: { + value: data, + writable: false, + enumerable: false } }); } @@ -31,6 +53,18 @@ FileStream.prototype.close = function() { } }; +function _checkReadAccess(mode) { + if (mode !== 'r' && mode !== 'rw') { + throw new tizen.WebAPIException(tizen.WebAPIException.IO_ERR, 'Stream is not in read mode.'); + } +} + +function _checkWriteAccess(mode) { + if (mode !== 'a' && mode !== 'w' && mode !== 'rw') { + throw new tizen.WebAPIException(tizen.WebAPIException.IO_ERR, 'Stream is not in write mode.'); + } +} + FileStream.prototype.read = function() { var args = validator_.validateArgs(arguments, [ { @@ -44,11 +78,24 @@ FileStream.prototype.read = function() { 'Argument "charCount" must be greater than 0'); } - var result = native_.callSync('FileStream_read', {}); + _checkReadAccess(this._mode); + var _count = this.bytesAvailable; + + var data = { + location: commonFS_.toRealPath(this._file.fullPath), + offset: this.position || 0, + length: args.charCount > _count ? _count : args.charCount + }; + + var result = native_.callSync('File_readSync', data); if (native_.isFailure(result)) { throw native_.getErrorObject(result); } + var encoded = native_.getResultObject(result); + var decoded = Base64.decode(encoded); + + return decoded; }; FileStream.prototype.readBytes = function() { @@ -64,11 +111,29 @@ FileStream.prototype.readBytes = function() { 'Argument "byteCount" must be greater than 0'); } - var result = native_.callSync('FileStream_readBytes', {}); + _checkReadAccess(this._mode); + + var _count = this.bytesAvailable; + var data = { + location: commonFS_.toRealPath(this._file.fullPath), + offset: this.position || 0, + length: args.byteCount > _count ? _count : args.byteCount + }; + + var result = native_.callSync('File_readSync', data); if (native_.isFailure(result)) { throw native_.getErrorObject(result); } + var encoded = native_.getResultObject(result); + var decoded = Base64.decode(encoded); + var bytes = []; + + for (var i = 0; i < decoded.length; ++i) { + bytes.push(decoded.charCodeAt(i)); + } + + return bytes; }; FileStream.prototype.readBase64 = function() { @@ -84,11 +149,23 @@ FileStream.prototype.readBase64 = function() { 'Argument "byteCount" must be greater than 0'); } - var result = native_.callSync('FileStream_readBase64', {}); + _checkReadAccess(this._mode); + + var _count = this.bytesAvailable; + var data = { + location: commonFS_.toRealPath(this._file.fullPath), + offset: this.position || 0, + length: args.byteCount > _count ? _count : args.byteCount + }; + + var result = native_.callSync('File_readSync', data); if (native_.isFailure(result)) { throw native_.getErrorObject(result); } + var encoded = native_.getResultObject(result); + + return encoded; }; FileStream.prototype.write = function() { @@ -99,6 +176,8 @@ FileStream.prototype.write = function() { } ]); + _checkWriteAccess(this._mode); + var result = native_.callSync('FileStream_write', {}); if (native_.isFailure(result)) { @@ -115,6 +194,8 @@ FileStream.prototype.writeBytes = function() { } ]); + _checkWriteAccess(this._mode); + var result = native_.callSync('FileStream_writeBytes', {}); if (native_.isFailure(result)) { @@ -130,6 +211,8 @@ FileStream.prototype.writeBase64 = function() { } ]); + _checkWriteAccess(this._mode); + var result = native_.callSync('FileStream_writeBase64', {}); if (native_.isFailure(result)) { -- 2.7.4