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
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:
Explanation: /weblog/2005/06/base64-ie/
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
94 | 입력받은 날짜와 현재 날짜와의 비교 함수 | 황제낙엽 | 2019.08.02 | 909 |
93 | 사용자 모듈 만들기 | 황제낙엽 | 2019.07.09 | 42090 |
92 | charcode 32와 160 차이 (javascript char 160 to 32) | 황제낙엽 | 2019.05.11 | 427 |
91 |
부동소수점 (floating-point)
![]() | 황제낙엽 | 2018.03.26 | 1441 |
90 | 브라우저의 새로고침과 종료에 대한 이벤트 | 황제낙엽 | 2017.08.11 | 3102 |
89 |
자바스크립트 타입 비교 테이블 + 테이블 작성 스크립트
[1] ![]() | 황제낙엽 | 2017.06.23 | 440 |
88 | |= 비트 OR 대입 연산자 (복합대입연산자) | 황제낙엽 | 2017.03.15 | 463 |
87 | Jasmine 테스트 및 CI 구축 가이드 | 황제낙엽 | 2016.11.16 | 542 |
86 |
QUnit을 이용한 JavaScript 단위 테스트
![]() | 황제낙엽 | 2016.11.16 | 418 |
85 | 멤버 연산자 | 황제낙엽 | 2014.12.30 | 418 |
84 | 연산자 this | 황제낙엽 | 2014.12.30 | 387 |
83 | typeof 와 instanceof의 차이, 타입 또는 클래스 구분하기 | 황제낙엽 | 2013.10.24 | 409 |
82 | HTTP Content-Type 정리 | 황제낙엽 | 2013.09.30 | 454 |
81 |
getBoundingClientRect in FF3
![]() | 황제낙엽 | 2013.01.11 | 424 |
80 | Stack (스택) 예제 프로그램 | 황제낙엽 | 2012.12.27 | 408 |
79 | Javascript delete | 황제낙엽 | 2012.06.11 | 444 |
78 | delete 연산자에 대한 고찰 | 황제낙엽 | 2012.06.11 | 456 |
77 | 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 | 황제낙엽 | 2012.05.31 | 1079 |
76 | 연산자 (===, ==, >=, <=) | 황제낙엽 | 2012.05.30 | 416 |
75 | JavaScript 재입문 | 황제낙엽 | 2012.05.29 | 389 |