Passed
Push — master ( f9d700...6df533 )
by Rafael S.
01:19
created

index.js ➔ clear   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
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
/** @module minibuffer */
31
32
import {unpack, packTo, unpackString, packStringTo} from 'byte-data';
33
34
/**
35
 * @type {string}
36
 * @private
37
 */
38
const RANGE_EROR = "Range error";
39
40
/**
41
 * A class to read and write to buffers.
42
 */
43
export default class MiniBuffer {
44
  
45
  constructor() {
46
    /**
47
     * @type {number}
48
     */
49
    this.head = 0;
50
  }
51
52
  /**
53
   * Set the MiniBuffer head to zero.
54
   */
55
  clear() {
56
    this.head = 0;
57
  }
58
59
  /**
60
   * Read a number from a buffer.
61
   * @param {!Uint8Array} buffer The buffer.
62
   * @param {!Object} typeDefinition The type definition.
63
   * @param {?number=} index The index to read.
64
   * @return {number|undefined} The number.
65
   * @throws {Error} If word size + index > buffer.length
66
   */
67
  read(buffer, typeDefinition, index=null) {
68
    index = index === null ? this.head : index;
69
    /** @type {number} */
70
    let size = typeDefinition.bits / 8;
71
    if (index + size > buffer.length) {
72
      throw new Error(RANGE_EROR);
73
    }
74
    /** @type {number|undefined} */
75
    let num = unpack(buffer, typeDefinition, index);
76
    this.head = size + index;
77
    return num;
78
  }
79
80
  /**
81
   * Write a number to a buffer.
82
   * @param {!Uint8Array} buffer The buffer.
83
   * @param {!Object} typeDefinition The type definition.
84
   * @param {number} num The number to write.
85
   * @param {?number=} index The buffer index to write.
86
   * @throws {Error} If word size + index > buffer.length
87
   */
88
  write(buffer, typeDefinition, num, index=null) {
89
    index = index === null ? this.head : index;
90
    /** @type {number} */
91
    let size = typeDefinition.bits / 8;
92
    if (index + size > buffer.length) {
93
      throw new Error(RANGE_EROR);
94
    }
95
    this.head = packTo(num, typeDefinition, buffer, index);
96
  }
97
98
  /**
99
   * Read a UTF-8 string from a buffer.
100
   * @param {!Uint8Array} buffer The buffer.
101
   * @param {number} size the max size of the string.
102
   * @param {?number=} index The buffer index to read.
103
   * @return {string} The string.
104
   * @throws {Error} If size + index > buffer.length
105
   */
106
  readStr(buffer, size, index=null) {
107
    index = index === null ? this.head : index;
108
    size = index + size;
109
    if (size > buffer.length) {
110
      throw new Error(RANGE_EROR);
111
    }
112
    /** @type {string} */
113
    let str = '';
114
    for (; index<size; index++) {
115
      str += unpackString(buffer, index, 1);
116
    }
117
    this.head = index;
118
    return str;
119
  }
120
121
  /**
122
   * Write a UTF-8 string to a buffer. If the string is smaller
123
   * than the max size the output buffer is filled with 0s.
124
   * @param {!Uint8Array} buffer The buffer.
125
   * @param {string} str The string to be written as bytes.
126
   * @param {number=} size The size of the string.
127
   * @param {?number=} index The buffer index to write.
128
   * @throws {Error} If size + index > buffer.length
129
   */
130
  writeStr(buffer, str, size=str.length, index=null) {
131
    index = index === null ? this.head : index;
132
    /** @type {number} */
133
    let limit = index + size;
134
    if (limit > buffer.length) {
135
      throw new Error(RANGE_EROR);
136
    }
137
    this.head = packStringTo(str, buffer, index);
138
    if (this.head < index + size) {
139
      for (; this.head<limit; this.head++) {
140
        buffer[this.head] = 0;
141
      }
142
    }
143
  }
144
}
145