sitelink1 http://siphon9.net/loune/2011/05/javascr...avascript/ 
sitelink2 http://dean.edwards.name/my/base64-ie.html 
sitelink3  
sitelink4  
extra_vars4  
extra_vars5  
extra_vars6  

Javascript ArrayBuffer – Binary handling in javascript

Leave a reply

As javascript and HTML5 venture into applications never contemplated, there was one basic feature sorely missing from its API line up. The feature I’m talking about is of course native support for binary. Currently, if you want to manipulate binary in javascript, you have to make do with an array of numbers to store each byte in the “byte array”. This was horribly ineffecient given that each byte of number type probably occupied 4 or 8 bytes, so you would be using 4 or 8 times the space. Another alternative is to use base64, but that meant your binary blob was 33% larger and was only good for serialisation and storage.

 

This changed however, with the advent of WebGL, the straw that broke the camels back. WebGL required efficient processing of byte arrays which herald the creation of the ArrayBuffer. Although designed for WebGL, the ArrayBuffer can be used anywhere in your javascript. WebGL is currently available for Firefox 4 and Chrome. The ArrayBuffer allows you to allocate a opaque chunk of memory. To manipulate the buffer, we have to “cast” or map the array to a Typed Array. There are various typed arrays such as Int16Array, Float32Array, but the one that interests us is probably Uint8Array, allowing us to view our ArrayBuffer as a byte array.

 

1

2

3

4

5

var buf = new ArrayBuffer(1024);

var bytes = new Uint8Array(buf);

for (var i = 0; i < bytes.length; i++) {

  bytes[i] = 0xFF;

}

 

The Typed Array may seem like a foreign concept in a dynamic-typed language such as javascript, but it’s neccesarry to provide good performance for binary handling. If you try to assign a Uint8Array element a number greater than 255, it will be truncated, and if you put a string, it’ll become 0, proving that this is more than just your average JS array.

 

Already there is talk of using these in HTML File API and Web Sockets API. WebSockets currently only support plain text UTF-8 frames, which means you can’t talk binary over the wire. Given that a lot of exisiting protocols are binary, this was a severely limitation. A web based proxy websockify that proxies native protocols into websocket frames needs the base64 each frame. Array Buffer support would allow us to do away the base64 overhead.

 

Base64 Encoded Images for Internet Explorer

<img src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////
wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4ML
wWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="
alt="Base64 encoded image" width="150" height="150"/>

 

That ghastly mess will produce the following image:

Base64 encoded image

Explanation: /weblog/2005/06/base64-ie/

 

번호 제목 글쓴이 날짜 조회 수
206 JavaScript Closures for Dummies 황제낙엽 2009.04.08 744
205 Faster JavaScript Memoization For Improved Application Performance 황제낙엽 2011.11.04 739
204 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 736
203 외부 라이브러리 (.js) 의 바람직한 동적 로딩 (The best way to load external JavaScript) 황제낙엽 2009.10.05 713
202 JScript Memory Leaks 황제낙엽 2011.11.28 701
201 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 698
200 Why does this simple Javascript prototype not work in IE? 황제낙엽 2011.03.24 685
199 Javascript 내장객체 String 황제낙엽 2007.04.10 670
198 unshift() Method 황제낙엽 2009.03.02 669
197 링크모음 황제낙엽 2011.03.25 666
196 배열에 대한 루프문 조회 (loop iterator) 황제낙엽 2023.03.01 658
195 Function.apply and Function.call in JavaScript 황제낙엽 2011.10.07 646
194 IFrames and cross-domain security file 황제낙엽 2012.01.13 644
193 Barcode Detection API 황제낙엽 2023.08.06 638
192 CORS(Cross-Origin Resource Sharing) - 5 file 황제낙엽 2017.03.07 636
191 Reference Count (순환참조) 황제낙엽 2011.11.24 625
190 insertAdjacentHTML Method 황제낙엽 2005.12.19 619
189 char to hex string 황제낙엽 2011.11.29 616
188 브라우저에서 뒤로 가기 막기와 펑션키(function key) 막기 황제낙엽 2005.10.21 606
187 체크박스에 체크된 항목 개수 구하기 황제낙엽 2023.06.10 602