arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/www/blackberry10/readAsText.js
plugins/cordova-plugin-file/www/blackberry10/readAsText.jsRaw
 1/*
 2 *
 3 * Licensed to the Apache Software Foundation (ASF) under one
 4 * or more contributor license agreements.  See the NOTICE file
 5 * distributed with this work for additional information
 6 * regarding copyright ownership.  The ASF licenses this file
 7 * to you under the Apache License, Version 2.0 (the
 8 * "License"); you may not use this file except in compliance
 9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22/* 
23 * readAsText
24 * 
25 * IN:
26 *  args
27 *   0 - URL of file to read
28 *   1 - encoding
29 *   2 - start position
30 *   3 - end position
31 * OUT:
32 *  success - text contents of file
33 *  fail - FileError
34 */
35
36
37var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
38    requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
39
40module.exports = function (success, fail, args) {
41    var uri = args[0],
42        start = args[2],
43        end = args[3],
44        onSuccess = function (data) {
45            if (typeof success === 'function') {
46                success(data);
47            }
48        },
49        onFail = function (error) {
50            if (typeof fail === 'function') {
51                if (error && error.code) {
52                    fail(error.code);
53                } else {
54                    fail(error);
55                }
56            }
57        };
58    resolve(function (fs) {
59        requestAnimationFrame(function () {
60            fs.nativeEntry.file(function (file) {
61                var reader = new FileReader()._realReader;
62                reader.onloadend = function () {
63                    var contents = new Uint8Array(this.result).subarray(start, end),
64                        blob = new Blob([contents]),
65                        textReader = new FileReader()._realReader;
66                    textReader.onloadend = function () {
67                        onSuccess(this.result);
68                    };
69                    textReader.onerror = onFail;
70                    textReader.readAsText(blob);
71                };
72                reader.onerror = onFail;
73                reader.readAsArrayBuffer(file);
74            }, onFail);
75        });
76    }, fail, [uri]);
77};