rochars /
minibuffer
| 1 | /* |
||
| 2 | * Copyright (c) 2018 Rafael da Silva Rocha. |
||
| 3 | * |
||
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
||
| 5 | * a copy of this software and associated documentation files (the |
||
| 6 | * "Software"), to deal in the Software without restriction, including |
||
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
||
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
||
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
||
| 10 | * the following conditions: |
||
| 11 | * |
||
| 12 | * The above copyright notice and this permission notice shall be |
||
| 13 | * included in all copies or substantial portions of the Software. |
||
| 14 | * |
||
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||
| 19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||
| 20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||
| 21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
| 22 | * |
||
| 23 | */ |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @fileoverview A minimalist buffer reader and writer. |
||
| 27 | * @see https://github.com/rochars/minibuffer |
||
| 28 | */ |
||
| 29 | |||
| 30 | import {unpackFrom, packTo, unpackString, packStringTo} from 'byte-data'; |
||
| 31 | |||
| 32 | const RANGE_EROR = "RangeError: Source is too large"; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A class to read and write to buffers. |
||
| 36 | */ |
||
| 37 | export default class MiniBuffer { |
||
| 38 | |||
| 39 | constructor() { |
||
| 40 | /** |
||
| 41 | * @type {number} |
||
| 42 | */ |
||
| 43 | this.head = 0; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Read a number from a buffer. |
||
| 48 | * @param {!Uint8Array} buffer The bufefr. |
||
| 49 | * @param {!Object} typeDefinition The type definition. |
||
| 50 | * @param {?number=} index The index to read. |
||
| 51 | * @return {number} The number. |
||
| 52 | */ |
||
| 53 | read(buffer, typeDefinition, index=null) { |
||
| 54 | index = index === null ? this.head : index; |
||
| 55 | /** @type {number} */ |
||
| 56 | let size = typeDefinition.bits / 8; |
||
| 57 | /** @type {number} */ |
||
| 58 | let num = unpackFrom(buffer, typeDefinition, index); |
||
| 59 | this.head += size + index; |
||
| 60 | return num; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Write a number to a buffer. |
||
| 65 | * @param {!Uint8Array} buffer The buffer. |
||
| 66 | * @param {!Object} typeDefinition The type definition. |
||
| 67 | * @param {number} num The number to write. |
||
| 68 | * @param {?number=} index The buffer index to write. |
||
| 69 | */ |
||
| 70 | write(buffer, typeDefinition, num, index=null) { |
||
| 71 | index = index === null ? this.head : index; |
||
| 72 | /** @type {number} */ |
||
| 73 | let size = typeDefinition.bits / 8; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 74 | this.head = packTo(num, typeDefinition, buffer, index); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Read a ASCII string from a buffer. |
||
| 79 | * @param {!Uint8Array} buffer The buffer. |
||
| 80 | * @param {number} size the max size of the string. |
||
| 81 | * @param {?number=} index The buffer index to read. |
||
| 82 | * @return {string} The string. |
||
| 83 | * @throws {Error} If size + index > buffer.length |
||
| 84 | */ |
||
| 85 | readStr(buffer, size, index=null) { |
||
| 86 | this.head = index === null ? this.head : index; |
||
| 87 | size = this.head + size; |
||
| 88 | if (size > buffer.length) { |
||
| 89 | throw new Error(RANGE_EROR); |
||
| 90 | } |
||
| 91 | /** @type {string} */ |
||
| 92 | let str = ''; |
||
| 93 | for (; this.head<size; this.head++) { |
||
| 94 | str += unpackString(buffer, this.head, 1); |
||
| 95 | } |
||
| 96 | return str; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Write a ASCII string to a buffer. If the string is smaller |
||
| 101 | * than the max size the output buffer is filled with 0s. |
||
| 102 | * @param {!Uint8Array} buffer The buffer. |
||
| 103 | * @param {string} str The string to be written as bytes. |
||
| 104 | * @param {number=} size The size of the string. |
||
| 105 | * @param {?number=} index The buffer index to write. |
||
| 106 | * @throws {Error} If size + index > buffer.length |
||
| 107 | */ |
||
| 108 | writeStr(buffer, str, size=str.length, index=null) { |
||
| 109 | index = index === null ? this.head : index; |
||
| 110 | /** @type {number} */ |
||
| 111 | let limit = index + size; |
||
| 112 | if (limit > buffer.length) { |
||
| 113 | throw new Error(RANGE_EROR); |
||
| 114 | } |
||
| 115 | this.head = packStringTo(str, buffer, index); |
||
| 116 | if (this.head < index + size) { |
||
| 117 | for (; this.head<limit; this.head++) { |
||
| 118 | buffer[this.head] = 0; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 |