Merge branch 'tizen_4.0' into tizen_5.0
[platform/core/api/webapi-plugins.git] / src / filesystem / js / file_stream.js
index 8e72b0b..8867985 100644 (file)
@@ -47,28 +47,12 @@ function FileStream(data, mode, encoding) {
             set: function(v) {},
             enumerable: true
         },
-        _mode: {
-            value: mode,
-            writable: false,
-            enumerable: false
-        },
-        _encoding: {
-            value: encoding,
-            writable: false,
-            enumerable: false
-        },
-        _file: {
-            value: data,
-            writable: false,
-            enumerable: false
-        },
-        _closed: {
-            value: false,
-            writable: true,
-            enumerable: false
-        },
-        _rewrite: {
-            value: mode === 'w' ? true : false,
+        _mode: { value: mode, writable: false, enumerable: false },
+        _encoding: { value: encoding, writable: false, enumerable: false },
+        _file: { value: data, writable: false, enumerable: false },
+        _closed: { value: false, writable: true, enumerable: false },
+        _truncate: {
+            value: mode === 'w', // 'w' truncates file to zero length
             writable: true,
             enumerable: false
         }
@@ -82,6 +66,11 @@ function _checkClosed(stream) {
 }
 
 function closeFileStream() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.close() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.close() instead.'
+    );
+
     this._closed = true;
 }
 
@@ -101,32 +90,14 @@ function _checkWriteAccess(mode) {
     }
 }
 
-/* returns array of numbers */
-function string_to_array(str) {
-    var output = [];
-    var len = str.length;
-    for (var i = 0; i < len; i++) {
-        output.push(str.charCodeAt(i));
-    }
-    return output;
-}
-
-/* receives array of numbers, returns string */
-function array_to_string(data) {
-    var output = '';
-    var len = data.length;
-    for (var i = 0; i < len; i++) {
-        output += String.fromCharCode(data[i] & 0xff); // conversion to octet
-    }
-    return output;
-}
-
 function read() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.read() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.readString() or FileHandle.readStringNonBlocking() instead.'
+    );
+
     var args = validator_.validateArgs(arguments, [
-        {
-            name: 'charCount',
-            type: types_.LONG
-        }
+        { name: 'charCount', type: types_.LONG }
     ]);
 
     _checkClosed(this);
@@ -186,10 +157,7 @@ FileStream.prototype.read = function() {
 
 function readBytes() {
     var args = validator_.validateArgs(arguments, [
-        {
-            name: 'byteCount',
-            type: types_.LONG
-        }
+        { name: 'byteCount', type: types_.LONG }
     ]);
 
     _checkClosed(this);
@@ -215,7 +183,7 @@ function readBytes() {
         throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Could not read');
     }
 
-    var decoded = string_to_array(native_.getResultObject(result));
+    var decoded = StringToArray(native_.getResultObject(result), Array);
 
     if (decoded.length) {
         can_change_size = true;
@@ -229,10 +197,21 @@ function readBytes() {
 }
 
 FileStream.prototype.readBytes = function() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.readBytes() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.readData() or FileHandle.readDataNonBlocking() instead.'
+    );
+
     return readBytes.apply(this, arguments);
 };
 
 FileStream.prototype.readBase64 = function() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.readBase64() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.readData() or FileHandle.readDataNonBlocking() in ' +
+            'combination with atob() and btoa() functions instead.'
+    );
+
     return base64_encode(readBytes.apply(this, arguments));
 };
 
@@ -249,11 +228,13 @@ function check_characters_outside_latin1(str) {
 }
 
 function write() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.write() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.writeString() or FileHandle.writeStringNonBlocking() instead.'
+    );
+
     var args = validator_.validateArgs(arguments, [
-        {
-            name: 'stringData',
-            type: types_.STRING
-        }
+        { name: 'stringData', type: types_.STRING }
     ]);
 
     _checkClosed(this);
@@ -271,7 +252,7 @@ function write() {
         encoding: this._encoding,
         offset: this.position,
         data: args.stringData,
-        rewrite: this._rewrite
+        truncate: this._truncate
     };
 
     if (data.encoding == 'iso-8859-1') {
@@ -287,7 +268,7 @@ function write() {
     var written_bytes = native_.getResultObject(result);
     this.position = this.position + written_bytes;
     can_change_size = false;
-    this._rewrite = false;
+    this._truncate = false;
 }
 
 FileStream.prototype.write = function() {
@@ -295,12 +276,17 @@ FileStream.prototype.write = function() {
 };
 
 function writeBytes() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.writeBytes() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.writeData() or FileHandle.writeDataNonBlocking() instead.'
+    );
+
     var args = validator_.validateArgs(arguments, [
         {
             name: 'byteData',
             type: types_.ARRAY,
-            values: undefined /* was types_.OCTET, but checking moved to
-                                array_to_string for performance */
+            values: undefined /* was types_.OCTET, but checking moved to ArrayToString for
+                             performance */
         }
     ]);
 
@@ -317,8 +303,8 @@ function writeBytes() {
     var data = {
         location: commonFS_.toRealPath(this._file.fullPath),
         offset: this.position,
-        data: array_to_string(args.byteData),
-        rewrite: this._rewrite
+        data: ArrayToString(args.byteData),
+        truncate: this._truncate
     };
 
     var result = native_.callSync('File_writeBytes', data);
@@ -329,7 +315,7 @@ function writeBytes() {
     can_change_size = true;
     this.position = this.position + args.byteData.length;
     can_change_size = false;
-    this._rewrite = false;
+    this._truncate = false;
 }
 
 FileStream.prototype.writeBytes = function() {
@@ -337,11 +323,14 @@ FileStream.prototype.writeBytes = function() {
 };
 
 function writeBase64() {
+    privUtils_.warn(
+        'DEPRECATION WARNING: FileStream.writeBase64() is deprecated since Tizen 5.0. ' +
+            'Use FileHandle.writeData() or FileHandle.writeDataNonBlocking() in ' +
+            'combination with atob() and btoa() functions instead.'
+    );
+
     var args = validator_.validateArgs(arguments, [
-        {
-            name: 'base64Data',
-            type: types_.STRING
-        }
+        { name: 'base64Data', type: types_.STRING }
     ]);
 
     _checkClosed(this);
@@ -351,7 +340,7 @@ function writeBase64() {
         location: commonFS_.toRealPath(this._file.fullPath),
         offset: this.position,
         data: args.base64Data,
-        rewrite: this._rewrite
+        truncate: this._truncate
     };
 
     var result = native_.callSync('File_writeBase64', data);
@@ -365,7 +354,7 @@ function writeBase64() {
     can_change_size = true;
     this.position += written_bytes;
     can_change_size = false;
-    this._rewrite = false;
+    this._truncate = false;
 }
 
 FileStream.prototype.writeBase64 = function() {