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/

 

번호 제목 글쓴이 날짜 조회 수
90 브라우저의 새로고침과 종료에 대한 이벤트 황제낙엽 2017.08.11 2725
89 자바스크립트 타입 비교 테이블 + 테이블 작성 스크립트 [1] file 황제낙엽 2017.06.23 85
88 |= 비트 OR 대입 연산자 (복합대입연산자) 황제낙엽 2017.03.15 73
87 Jasmine 테스트 및 CI 구축 가이드 황제낙엽 2016.11.16 254
86 QUnit을 이용한 JavaScript 단위 테스트 file 황제낙엽 2016.11.16 36
85 멤버 연산자 황제낙엽 2014.12.30 47
84 연산자 this 황제낙엽 2014.12.30 23
83 typeof 와 instanceof의 차이, 타입 또는 클래스 구분하기 황제낙엽 2013.10.24 38
82 HTTP Content-Type 정리 황제낙엽 2013.09.30 68
81 getBoundingClientRect in FF3 file 황제낙엽 2013.01.11 36
80 Stack (스택) 예제 프로그램 황제낙엽 2012.12.27 27
79 Javascript delete 황제낙엽 2012.06.11 20
78 delete 연산자에 대한 고찰 황제낙엽 2012.06.11 42
77 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 황제낙엽 2012.05.31 726
76 연산자 (===, ==, >=, <=) 황제낙엽 2012.05.30 47
75 JavaScript 재입문 황제낙엽 2012.05.29 50
74 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 337
» Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
72 Alert 에서의 개행처리 황제낙엽 2012.03.09 80
71 자바스크립트 숫자형 체크 함수 (isFinite() 함수 와 isNaN() 함수) 황제낙엽 2011.12.13 67