From: Piotr Kosko Date: Thu, 8 Oct 2015 11:44:23 +0000 (+0200) Subject: [Filesystem] Fixed behaviour of Filestream X-Git-Tag: submit/tizen/20151026.073646^2^2~34^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=88e010acf296274b17c8f4985120c4cce02c9c9d;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] Fixed behaviour of Filestream [Feature] Now scenario: 1. create file and open stream 2. write to stream (e.g. "abc") 3. reset position to the beginning (fs.position = 0) 4. read from stream (expected "abc") works correct (position, bytesAvailable and eof properties are fixed to be updated when write is performed) [Verification] Code compiles without errors. TCT passrate is 100% Scenario above was tested in chrome console. Change-Id: I48f89bfe807f7c1a7aa495e459425a97480ef572 Signed-off-by: Piotr Kosko --- diff --git a/src/filesystem/js/file_stream.js b/src/filesystem/js/file_stream.js index b0e8953b..1d9f5a60 100755 --- a/src/filesystem/js/file_stream.js +++ b/src/filesystem/js/file_stream.js @@ -13,6 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +function _checkClosed(stream) { + if (stream._closed) { + throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is closed.'); + } +} + +function _checkReadAccess(mode) { + if (mode !== 'r' && mode !== 'rw') { + throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is not in read mode.'); + } +} + +function _checkWriteAccess(mode) { + if (mode !== 'a' && mode !== 'w' && mode !== 'rw') { + throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is not in write mode.'); + } +} function FileStream(data, mode, encoding) { var _totalBytes = data.fileSize || 0; @@ -20,9 +37,12 @@ function FileStream(data, mode, encoding) { Object.defineProperties(this, { eof: { - value: false, - enumerable: true, - writable: false + get: function() { + return _totalBytes < _position; + }, + set: function(v) { + }, + enumerable: true }, position: { get: function() { @@ -34,9 +54,12 @@ function FileStream(data, mode, encoding) { enumerable: true }, bytesAvailable: { - value: this.eof ? -1 : Math.max(0, _totalBytes - _position), - enumerable: true, - writable: false + get: function() { + return this.eof ? -1 : Math.max(0, _totalBytes - _position); + }, + set: function(v) { + }, + enumerable: true }, _mode: { value: mode, @@ -59,12 +82,69 @@ function FileStream(data, mode, encoding) { enumerable: false } }); -} -function _checkClosed(stream) { - if (stream._closed) { - throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is closed.'); - } + this.write = function() { + xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.FILESYSTEM_WRITE); + + var args = validator_.validateArgs(arguments, [ + { + name: 'stringData', + type: types_.STRING + } + ]); + + _checkClosed(this); + _checkWriteAccess(this._mode); + if (!arguments.length) { + throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, + 'Argument "stringData" missing'); + } + + var data = { + location: commonFS_.toRealPath(this._file.fullPath), + offset: this.position, + length: args.stringData.length, + is_base64: false, + }; + var result = native_.callSyncData('File_writeSync', data, "string", args.stringData); + if (native_.isFailure(result.reply)) { + throw new WebAPIException(WebAPIException.IO_ERR, 'Could not write'); + } + this.position = this.position + result.reply.data_size; + _totalBytes = Math.max(this.position, _totalBytes); + }; + + this.writeBytes = function() { + xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.FILESYSTEM_WRITE); + + var args = validator_.validateArgs(arguments, [ + { + name: 'byteData', + type: types_.ARRAY + } + ]); + _checkClosed(this); + _checkWriteAccess(this._mode); + if (!arguments.length) { + throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, + 'Argument "byteData" missing'); + } + + var data = { + location: commonFS_.toRealPath(this._file.fullPath), + offset: this.position, + length: args.byteData.length, + is_base64: false, + }; + + var result = native_.callSyncData('File_writeSync', data, "octet", args.byteData); + + if (native_.isFailure(result.reply)) { + throw new WebAPIException(WebAPIException.IO_ERR, 'Could not write'); + } + this.position = this.position + result.reply.data_size; + _totalBytes = Math.max(this.position, _totalBytes); + }; } FileStream.prototype.close = function() { @@ -72,18 +152,6 @@ FileStream.prototype.close = function() { this._closed = true; }; -function _checkReadAccess(mode) { - if (mode !== 'r' && mode !== 'rw') { - throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is not in read mode.'); - } -} - -function _checkWriteAccess(mode) { - if (mode !== 'a' && mode !== 'w' && mode !== 'rw') { - throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is not in write mode.'); - } -} - FileStream.prototype.read = function() { xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.FILESYSTEM_READ); @@ -213,68 +281,6 @@ FileStream.prototype.readBase64 = function() { return result.output; }; -FileStream.prototype.write = function() { - xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.FILESYSTEM_WRITE); - - var args = validator_.validateArgs(arguments, [ - { - name: 'stringData', - type: types_.STRING - } - ]); - - _checkClosed(this); - _checkWriteAccess(this._mode); - if (!arguments.length) { - throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, - 'Argument "stringData" missing'); - } - - var data = { - location: commonFS_.toRealPath(this._file.fullPath), - offset: this.position, - length: args.stringData.length, - is_base64: false, - }; - var result = native_.callSyncData('File_writeSync', data, "string", args.stringData); - if (native_.isFailure(result.reply)) { - throw new WebAPIException(WebAPIException.IO_ERR, 'Could not write'); - } - this.position = this.position + result.reply.data_size; -}; - -FileStream.prototype.writeBytes = function() { - xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.FILESYSTEM_WRITE); - - var args = validator_.validateArgs(arguments, [ - { - name: 'byteData', - type: types_.ARRAY - } - ]); - _checkClosed(this); - _checkWriteAccess(this._mode); - if (!arguments.length) { - throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, - 'Argument "byteData" missing'); - } - - var data = { - location: commonFS_.toRealPath(this._file.fullPath), - offset: this.position, - length: args.byteData.length, - is_base64: false, - }; - - var result = native_.callSyncData('File_writeSync', data, "octet", args.byteData); - - if (native_.isFailure(result.reply)) { - throw new WebAPIException(WebAPIException.IO_ERR, 'Could not write'); - } - this.position = this.position + result.reply.data_size; - -}; - function _isBase64(str) { var base64 = new RegExp('^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$'); return base64.test(str);