readAsText: function()

in src/windows/FileProxy.js [562:603]


    readAsText: function (win, fail, args) {
        const url = args[0];
        const enc = args[1];
        let startPos = args[2];
        let endPos = args[3];

        const fs = getFilesystemFromURL(url);
        const path = pathFromURL(url);
        if (!fs) {
            fail(FileError.ENCODING_ERR);
            return;
        }
        const wpath = cordovaPathToNative(sanitize(fs.winpath + path));

        let encoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
        if (enc === 'Utf16LE' || enc === 'utf16LE') {
            encoding = Windows.Storage.Streams.UnicodeEncoding.utf16LE;
        } else if (enc === 'Utf16BE' || enc === 'utf16BE') {
            encoding = Windows.Storage.Streams.UnicodeEncoding.utf16BE;
        }

        getFileFromPathAsync(wpath).then(function (file) {
            return file.openReadAsync();
        }).then(function (stream) {
            startPos = (startPos < 0) ? Math.max(stream.size + startPos, 0) : Math.min(stream.size, startPos);
            endPos = (endPos < 0) ? Math.max(endPos + stream.size, 0) : Math.min(stream.size, endPos);
            stream.seek(startPos);

            const readSize = endPos - startPos;
            const buffer = new Windows.Storage.Streams.Buffer(readSize);

            return stream.readAsync(buffer, readSize, Windows.Storage.Streams.InputStreamOptions.none);
        }).done(function (buffer) {
            try {
                win(Windows.Security.Cryptography.CryptographicBuffer.convertBinaryToString(encoding, buffer));
            } catch (e) {
                fail(FileError.ENCODING_ERR);
            }
        }, function () {
            fail(FileError.NOT_FOUND_ERR);
        });
    },