DataView

DataView 뷰는 플랫폼의 자체 엔디언(바이트 정렬 방법)에 신경 쓰지 않으면서 ArrayBuffer에서 다양한 숫자 자료형의 데이터를 읽고 쓰기 위한 저수준 인터페이스를 제공합니다.

설명

엔디언

다중 바이트 숫자 자료형은 장치의 아키텍처에 따라 메모리에 다르게 표현됩니다. 엔디언 용어 사전을 참고하세요. DataView 접근자는 코드를 실행하는 컴퓨터의 엔디언에 관계 없이 데이터의 접근법을 명시적으로 통제할 수 있습니다.

var littleEndian = (function() {
  var buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
  // Int16Array uses the platform's endianness.
  return new Int16Array(buffer)[0] === 256;
})();
console.log(littleEndian); // true or false

64비트 정수 자료형

JavaScript는 64비트 정수형 값에 대한 표준 지원을 아직 제공하지 않으므로 DataView도 64비트 연산을 지원하지 않습니다. 필요한 경우 최대 정확도 Number.MAX_SAFE_INTEGER의 값을 가져올 수 있는 getUint64() 함수를 만들 수는 있으며, 어느 정도까지는 충분할 것입니다.

function getUint64(dataview, byteOffset, littleEndian) {
  // split 64-bit number into two 32-bit (4-byte) parts
  const left =  dataview.getUint32(byteOffset, littleEndian);
  const right = dataview.getUint32(byteOffset+4, littleEndian);

  // combine the two 32-bit values
  const combined = littleEndian? left + 2**32*right : 2**32*left + right;

  if (!Number.isSafeInteger(combined))
    console.warn(combined, 'exceeds MAX_SAFE_INTEGER. Precision may be lost');

  return combined;
}

완전한 범위의 64비트를 지원해야 할 경우 BigInt를 사용하세요. 단, BigInt는 사용자 계층 라이브러리 구현체보다는 훨씬 빠르지만, 가변 크기라는 성질로 인해 JavaScript 32비트 정수보다는 항상 느릴 수밖에 없습니다.

const BigInt = window.BigInt, bigThirtyTwo = BigInt(32), bigZero = BigInt(0);
function getUint64BigInt(dataview, byteOffset, littleEndian) {
  // split 64-bit number into two 32-bit (4-byte) parts
  const left = BigInt(dataview.getUint32(byteOffset|0, !!littleEndian)>>>0);
  const right = BigInt(dataview.getUint32((byteOffset|0) + 4|0, !!littleEndian)>>>0);

  // combine the two 32-bit values and return
  return littleEndian ? (right<<bigThirtyTwo)|left : (left<<bigThirtyTwo)|right;
}

생성자

DataView()
새로운 DataView 객체를 생성합니다.

속성

DataView.prototype.constructor
객체의 프로토타입을 생성한 함수를 나타냅니다. 초깃값은 내장 DataView 생성자입니다.
DataView.prototype.buffer Read only
뷰가 참조하는 ArrayBuffer입니다.
DataView.prototype.byteLength Read only
뷰의 시작점부터 측정한 ArrayBuffer의 길이(바이트 단위)입니다.
DataView.prototype.byteOffset Read only
뷰의 시작점 오프셋(바이트 단위)입니다.

메서드

읽기

DataView.prototype.getInt8()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 8비트 정수(byte)를 가져옵니다.
DataView.prototype.getUint8()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 8비트 부호 없는 정수(unsigned byte)를 가져옵니다.
DataView.prototype.getInt16()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 16비트 정수(short)를 가져옵니다.
DataView.prototype.getUint16()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 16비트 부호 없는 정수(unsigned short)를 가져옵니다.
DataView.prototype.getInt32()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 32비트 정수(long)를 가져옵니다.
DataView.prototype.getUint32()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 32비트 부호 없는 정수(unsigned short)를 가져옵니다.
DataView.prototype.getFloat32()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 32비트 실수(float)를 가져옵니다.
DataView.prototype.getFloat64()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 64비트 실수(double)를 가져옵니다.
DataView.prototype.getBigInt64()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 64비트 정수(long long)를 가져옵니다.
DataView.prototype.getBigUint64()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 64비트 부호 없는 정수(unsigned long long)를 가져옵니다.

쓰기

DataView.prototype.setInt8()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 8비트 정수(byte)를 저장합니다.
DataView.prototype.setUint8()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 8비트 부호 없는 정수(unsigned byte)를 저장합니다.
DataView.prototype.setInt16()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 16비트 정수(short)를 저장합니다.
DataView.prototype.setUint16()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 16비트 부호 없는 정수(unsigned short)를 저장합니다.
DataView.prototype.setInt32()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 32비트 정수(long)를 저장합니다.
DataView.prototype.setUint32()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 32비트 부호 없는 정수(unsigned long)를 저장합니다.
DataView.prototype.setFloat32()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 32비트 실수(float)를 저장합니다.
DataView.prototype.setFloat64()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 64비트 실수(double)를 저장합니다.
DataView.prototype.setBigInt64()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 64비트 정수(long long)를 저장합니다.
DataView.prototype.setBigUint64()
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 64비트 부호 없는 정수(unsigned long long)를 저장합니다.

예제

var buffer = new ArrayBuffer(16);
var view = new DataView(buffer, 0);

view.setInt16(1, 42);
view.getInt16(1); //42

명세

Specification
ECMAScript Latest Draft (ECMA-262)
The definition of 'DataView' in that specification.

브라우저 호환성

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
DataViewChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
DataView() constructorChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
bufferChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
byteLengthChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
byteOffsetChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getBigInt64Chrome Full support 67Edge No support NoFirefox Full support 68IE No support NoOpera Full support 54Safari No support NoWebView Android Full support 67Chrome Android Full support 67Firefox Android Full support 68Opera Android Full support 48Safari iOS No support NoSamsung Internet Android Full support 9.0nodejs Full support 10.4.0
getBigUint64Chrome Full support 67Edge No support NoFirefox Full support 68IE No support NoOpera Full support 54Safari No support NoWebView Android Full support 67Chrome Android Full support 67Firefox Android Full support 68Opera Android Full support 48Safari iOS No support NoSamsung Internet Android Full support 9.0nodejs Full support 10.4.0
getFloat32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getFloat64Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getInt16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getInt32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getInt8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getUint16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getUint32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
getUint8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
DataView() without new throwsChrome Full support 11Edge Full support 13Firefox Full support 40IE No support NoOpera Full support 15Safari Full support 5.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 40Opera Android Full support 14Safari iOS Full support 5.1Samsung Internet Android Full support 1.0nodejs Full support 0.12
setBigInt64Chrome Full support 67Edge No support NoFirefox Full support 68IE No support NoOpera Full support 54Safari No support NoWebView Android Full support 67Chrome Android Full support 67Firefox Android Full support 68Opera Android Full support 48Safari iOS No support NoSamsung Internet Android Full support 9.0nodejs Full support 10.4.0
setBigUint64Chrome Full support 67Edge No support NoFirefox Full support 68IE No support NoOpera Full support 54Safari No support NoWebView Android Full support 67Chrome Android Full support 67Firefox Android Full support 68Opera Android Full support 48Safari iOS No support NoSamsung Internet Android Full support 9.0nodejs Full support 10.4.0
setFloat32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setFloat64Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setInt16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setInt32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setInt8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setUint16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setUint32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
setUint8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 15Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
SharedArrayBuffer accepted as bufferChrome Full support 60Edge No support NoFirefox Full support 55IE No support NoOpera Full support 47Safari No support 10.1 — 11.1WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 55Opera Android Full support 44Safari iOS No support 10.3 — 11.3Samsung Internet Android Full support 8.0nodejs ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

같이 보기