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/

 

번호 제목 글쓴이 날짜 조회 수
157 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 황제낙엽 2012.05.31 726
156 연산자 (===, ==, >=, <=) 황제낙엽 2012.05.30 47
155 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 412
154 JavaScript 재입문 황제낙엽 2012.05.29 50
153 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 324
152 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 337
» Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
150 오류:호출자(서버 응용 프로그램이 아닌 서버)가 사용될 수 없어서 사라졌습니다. file 황제낙엽 2012.03.14 1950
149 Alert 에서의 개행처리 황제낙엽 2012.03.09 80
148 env.js 사용시 부족한 부분 file 황제낙엽 2012.02.15 33
147 Rhino 와 env.js 를 사용해서 자바 서버에서 javascript 를 구동해보자 file 황제낙엽 2012.02.15 116
146 외부 라이브러리 (.js) 의 바람직하지 않은 동적 로딩 (eval함수 이용) 황제낙엽 2012.01.18 1851
145 IFrames and cross-domain security file 황제낙엽 2012.01.13 246
144 자바스크립트 숫자형 체크 함수 (isFinite() 함수 와 isNaN() 함수) 황제낙엽 2011.12.13 67
143 char to hex string 황제낙엽 2011.11.29 206
142 Understanding delete 황제낙엽 2011.11.28 61
141 JScript Memory Leaks 황제낙엽 2011.11.28 69
140 JavaScript Reserved Words 황제낙엽 2011.11.28 94
139 Reference Count (순환참조) 황제낙엽 2011.11.24 191
138 IE and Memory accumulation in Javascript (document.createElement()) file 황제낙엽 2011.11.24 30