एन्कोडिंग एपीआई का इस्तेमाल करके, आसान arrayBuffer से स्ट्रिंग कन्वर्ज़न में

दो साल पहले, Renato Mangini ने रॉ ArrayBuffers और उस डेटा से जुड़े स्ट्रिंग को एक-दूसरे से बदलने का तरीका बताया. पोस्ट के आखिर में, Renato ने बताया कि कन्वर्ज़न को हैंडल करने वाले आधिकारिक स्टैंडर्ड एपीआई को ड्राफ़्ट किया जा रहा है. जानकारी अब पूरी हो चुकी है. साथ ही, Firefox और Google Chrome, दोनों में TextDecoder और TextEncoder इंटरफ़ेस के लिए नेटिव सपोर्ट जोड़ा गया है.

जैसा कि इस लाइव सैंपल में बताया गया है, एन्कोडिंग एपीआई की मदद से, रॉ बाइट और नेटिव JavaScript स्ट्रिंग के बीच आसानी से अनुवाद किया जा सकता है. भले ही, आपको किसी भी स्टैंडर्ड एन्कोडिंग के साथ काम करना हो.

<pre id="results"></pre>

<script>
    if ('TextDecoder' in window) {
    // The local files to be fetched, mapped to the encoding that they're using.
    var filesToEncoding = {
        'utf8.bin': 'utf-8',
        'utf16le.bin': 'utf-16le',
        'macintosh.bin': 'macintosh'
    };

    Object.keys(filesToEncoding).forEach(function(file) {
        fetchAndDecode(file, filesToEncoding[file]);
    });
    } else {
    document.querySelector('#results').textContent = 'Your browser does not support the Encoding API.'
    }

    // Use XHR to fetch `file` and interpret its contents as being encoded with `encoding`.
    function fetchAndDecode(file, encoding) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', file);
    // Using 'arraybuffer' as the responseType ensures that the raw data is returned,
    // rather than letting XMLHttpRequest decode the data first.
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
        if (this.status == 200) {
        // The decode() method takes a DataView as a parameter, which is a wrapper on top of the ArrayBuffer.
        var dataView = new DataView(this.response);
        // The TextDecoder interface is documented at http://encoding.spec.whatwg.org/#interface-textdecoder
        var decoder = new TextDecoder(encoding);
        var decodedString = decoder.decode(dataView);
        // Add the decoded file's text to the <pre> element on the page.
        document.querySelector('#results').textContent += decodedString + '\n';
        } else {
        console.error('Error while requesting', file, this);
        }
    };
    xhr.send();
    }
</script>

ऊपर दिया गया सैंपल, सुविधा की पहचान करने की सुविधा का इस्तेमाल करता है. इससे यह पता चलता है कि ज़रूरी TextDecoder इंटरफ़ेस, मौजूदा ब्राउज़र में उपलब्ध है या नहीं. अगर ऐसा नहीं है, तो गड़बड़ी का मैसेज दिखाता है. असली ऐप्लिकेशन में, नेटिव सहायता उपलब्ध न होने पर, आप आम तौर पर कोई अन्य तरीका इस्तेमाल करना चाहेंगे. अच्छी बात यह है कि रेनाटो ने अपने मूल लेख में जिस टेक्स्ट-कोडिंग लाइब्रेरी के बारे में बताया है उसे अब भी बेहतर बनाया जा सकता है. यह लाइब्रेरी, उन ब्राउज़र पर नेटिव तरीकों का इस्तेमाल करती है जो इन ब्राउज़र के साथ काम करते हैं. साथ ही, जिन ब्राउज़र पर अभी तक सहायता नहीं जोड़ी गई है उन पर, Encrypt API के लिए पॉलीफ़िल की सुविधा उपलब्ध है.

अपडेट, सितंबर 2014: यह दिखाने के लिए सैंपल में बदलाव किया गया था कि मौजूदा ब्राउज़र में एन्कोडिंग एपीआई उपलब्ध है या नहीं.