README.md (5469B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # ArrayBuffer 22 23 > [Constructor][mdn-arraybuffer] which returns an object used to represent a generic, fixed-length raw binary data buffer. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var ArrayBuffer = require( '@stdlib/array/buffer' ); 41 ``` 42 43 #### ArrayBuffer( size ) 44 45 Returns an [`ArrayBuffer`][mdn-arraybuffer] having a specified number of bytes. 46 47 <!-- eslint-disable stdlib/require-globals --> 48 49 ```javascript 50 var buf = new ArrayBuffer( 5 ); 51 // returns <ArrayBuffer> 52 ``` 53 54 * * * 55 56 ### Properties 57 58 #### ArrayBuffer.length 59 60 Number of input arguments the constructor accepts. 61 62 <!-- eslint-disable stdlib/require-globals --> 63 64 ```javascript 65 var len = ArrayBuffer.length; 66 // returns 1 67 ``` 68 69 #### ArrayBuffer.prototype.byteLength 70 71 **Read-only** property which returns the length (in bytes) of the [`ArrayBuffer`][mdn-arraybuffer]. 72 73 <!-- eslint-disable stdlib/require-globals --> 74 75 ```javascript 76 var buf = new ArrayBuffer( 5 ); 77 var byteLength = buf.byteLength; 78 // returns 5 79 ``` 80 81 * * * 82 83 ### Methods 84 85 #### ArrayBuffer.isView( arr ) 86 87 Static method which returns a `boolean` indicating if provided a buffer view. 88 89 <!-- eslint-disable stdlib/require-globals --> 90 91 ```javascript 92 var Float64Array = require( '@stdlib/array/float64' ); 93 var view = new Float64Array( 10 ); 94 95 var bool = ArrayBuffer.isView( view ); 96 // returns true 97 ``` 98 99 #### ArrayBuffer.prototype.slice( \[start\[, end]] ) 100 101 Copies the bytes of an `ArrayBuffer` to a new [`ArrayBuffer`][mdn-arraybuffer]. 102 103 <!-- eslint-disable stdlib/require-globals --> 104 105 ```javascript 106 var b1 = new ArrayBuffer( 10 ); 107 108 var b2 = b1.slice(); 109 // returns <ArrayBuffer> 110 111 var bool = ( b2 === b1 ); 112 // returns false 113 ``` 114 115 By default, the method copies from the beginning of the [`ArrayBuffer`][mdn-arraybuffer]. To beginning copying from a different byte index, provide a `start` argument, specifying the starting byte index (inclusive). 116 117 <!-- eslint-disable stdlib/require-globals --> 118 119 ```javascript 120 var b1 = new ArrayBuffer( 10 ); 121 var b2 = b1.slice( 2 ); 122 123 var nbytes = b2.byteLength; 124 // returns 8 125 ``` 126 127 If `start < 0`, the index is relative to the end of the [`ArrayBuffer`][mdn-arraybuffer]. 128 129 <!-- eslint-disable stdlib/require-globals --> 130 131 ```javascript 132 var b1 = new ArrayBuffer( 10 ); 133 var b2 = b1.slice( -2 ); 134 135 var nbytes = b2.byteLength; 136 // returns 2 137 ``` 138 139 By default, the method copies to the end of the [`ArrayBuffer`][mdn-arraybuffer]. To copy until a particular byte index, provide an `end` index, specifying the ending byte index (exclusive). 140 141 <!-- eslint-disable stdlib/require-globals --> 142 143 ```javascript 144 var b1 = new ArrayBuffer( 10 ); 145 var b2 = b1.slice( 2, 6 ); 146 147 var nbytes = b2.byteLength; 148 // returns 4 149 ``` 150 151 If `end < 0`, the index is relative to the end of the [`ArrayBuffer`][mdn-arraybuffer]. 152 153 <!-- eslint-disable stdlib/require-globals --> 154 155 ```javascript 156 var b1 = new ArrayBuffer( 10 ); 157 var b2 = b1.slice( 2, -2 ); 158 159 var nbytes = b2.byteLength; 160 // returns 6 161 ``` 162 163 </section> 164 165 <!-- /.usage --> 166 167 * * * 168 169 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 170 171 <section class="notes"> 172 173 </section> 174 175 <!-- /.notes --> 176 177 <!-- Package usage examples. --> 178 179 <section class="examples"> 180 181 ## Examples 182 183 <!-- eslint no-undef: "error" --> 184 185 ```javascript 186 var randu = require( '@stdlib/random/base/randu' ); 187 var Float64Array = require( '@stdlib/array/float64' ); 188 var Uint8Array = require( '@stdlib/array/uint8' ); 189 var toBinaryString = require( '@stdlib/number/uint8/base/to-binary-string' ); 190 var ArrayBuffer = require( '@stdlib/array/buffer' ); 191 192 var bytes; 193 var buf; 194 var arr; 195 var i; 196 197 // Create a new ArrayBuffer: 198 buf = new ArrayBuffer( 64 ); 199 200 // Create a Float64 array buffer view: 201 arr = new Float64Array( buf.byteLength/8 ); 202 for ( i = 0; i < arr.length; i++ ) { 203 arr[ i ] = randu() * 100.0; 204 } 205 206 // Create a "bytes" view of the array buffer: 207 bytes = new Uint8Array( arr.buffer ); 208 209 // Print the bytes: 210 for ( i = 0; i < bytes.length; i++ ) { 211 console.log( 'byte %d: %s', i, toBinaryString( bytes[ i ] ) ); 212 } 213 ``` 214 215 </section> 216 217 <!-- /.examples --> 218 219 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 220 221 <section class="references"> 222 223 </section> 224 225 <!-- /.references --> 226 227 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 228 229 <section class="links"> 230 231 [mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer 232 233 </section> 234 235 <!-- /.links -->