{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) {
// 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
}
});
}
}
};
+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, [
{
'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() {
'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() {
'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() {
}
]);
+ _checkWriteAccess(this._mode);
+
var result = native_.callSync('FileStream_write', {});
if (native_.isFailure(result)) {
}
]);
+ _checkWriteAccess(this._mode);
+
var result = native_.callSync('FileStream_writeBytes', {});
if (native_.isFailure(result)) {
}
]);
+ _checkWriteAccess(this._mode);
+
var result = native_.callSync('FileStream_writeBase64', {});
if (native_.isFailure(result)) {