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/

 

번호 제목 글쓴이 날짜 조회 수
166 getBoundingClientRect in FF3 file 황제낙엽 2013.01.11 424
165 연속해서 스트림 받기 (flush data from servlet to jsp with ajax) 황제낙엽 2013.01.04 5099
164 Stack (스택) 예제 프로그램 황제낙엽 2012.12.27 408
163 상속과 Super 로의 접근 황제낙엽 2012.09.18 400
162 inherits() 를 이용한 상속 황제낙엽 2012.07.18 434
161 Javascript delete 황제낙엽 2012.06.11 445
160 delete 연산자에 대한 고찰 황제낙엽 2012.06.11 456
159 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 황제낙엽 2012.05.31 1079
158 연산자 (===, ==, >=, <=) 황제낙엽 2012.05.30 417
157 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 691
156 JavaScript 재입문 황제낙엽 2012.05.29 389
155 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 664
154 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 782
» Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 766
152 오류:호출자(서버 응용 프로그램이 아닌 서버)가 사용될 수 없어서 사라졌습니다. file 황제낙엽 2012.03.14 2219
151 Alert 에서의 개행처리 황제낙엽 2012.03.09 420
150 env.js 사용시 부족한 부분 file 황제낙엽 2012.02.15 360
149 Rhino 와 env.js 를 사용해서 자바 서버에서 javascript 를 구동해보자 file 황제낙엽 2012.02.15 423
148 외부 라이브러리 (.js) 의 바람직하지 않은 동적 로딩 (eval함수 이용) 황제낙엽 2012.01.18 2169
147 IFrames and cross-domain security file 황제낙엽 2012.01.13 565