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/

 

번호 제목 글쓴이 날짜 조회 수
197 XMLHttpRequest.timeout 황제낙엽 2018.11.03 248
196 IFrames and cross-domain security file 황제낙엽 2012.01.13 246
195 UTF-8 한글 초성 추출 (자바스크립트) 황제낙엽 2019.05.07 243
194 Why does this simple Javascript prototype not work in IE? 황제낙엽 2011.03.24 242
193 Function.apply and Function.call in JavaScript 황제낙엽 2011.10.07 238
192 브라우저에서 뒤로 가기 막기와 펑션키(function key) 막기 황제낙엽 2005.10.21 236
191 두 서버의 자원을 접근하는 클라이언트 프레임웍(Next.js)에서의 CORS오류 file 황제낙엽 2021.12.05 231
190 JavaScript Closures for Dummies 황제낙엽 2009.04.08 227
» Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
188 javascript array contains method 황제낙엽 2011.08.19 210
187 char to hex string 황제낙엽 2011.11.29 206
186 How to use Rhino to script Java classes. 황제낙엽 2008.07.14 198
185 Page Refresh/Reload 황제낙엽 2007.08.24 197
184 [펌] 아사페릴의 사생활 - Code Conventions for the JavaScript Programming Language 황제낙엽 2009.04.02 194
183 Reference Count (순환참조) 황제낙엽 2011.11.24 191
182 JavaScript Form Validation file 황제낙엽 2008.11.24 186
181 code compressor & decompressor 황제낙엽 2015.01.02 181
180 자바스크립트의 데이터 타입과 변수 황제낙엽 2008.08.06 179
179 SelectBox 밑에 CheckBox가 포함된 리스트 만들기 file 황제낙엽 2007.01.16 178
178 서비스 워커 file 황제낙엽 2020.05.25 177