Passed
Push — master ( 6df533...539e21 )
by Rafael S.
01:10
created

index.js ➔ readStr   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
c 0
b 0
f 0
nc 12
dl 0
loc 19
rs 8
nop 3
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
    let zstr = size === -1 ? true : false;
110
    size = zstr ? buffer.length : size;
111
    if (size > buffer.length) {
112
      throw new Error(RANGE_EROR);
113
    }
114
    /** @type {string} */
115
    let str = '';
116
    for (; index<size; index++) {
117
      if (buffer[index] === 0) {
118
        break;
119
      }
120
      str += unpackString(buffer, index, 1);
121
    }
122
    this.head = index;
123
    return str;
124
  }
125
126
  /**
127
   * Write a UTF-8 string to a buffer. If the string is smaller
128
   * than the max size the output buffer is filled with 0s.
129
   * @param {!Uint8Array} buffer The buffer.
130
   * @param {string} str The string to be written as bytes.
131
   * @param {number=} size The size of the string.
132
   * @param {?number=} index The buffer index to write.
133
   * @throws {Error} If size + index > buffer.length
134
   */
135
  writeStr(buffer, str, size=str.length, index=null) {
136
    index = index === null ? this.head : index;
137
    /** @type {number} */
138
    let limit = index + size;
139
    if (limit > buffer.length) {
140
      throw new Error(RANGE_EROR);
141
    }
142
    this.head = packStringTo(str, buffer, index);
143
    if (this.head < index + size) {
144
      for (; this.head<limit; this.head++) {
145
        buffer[this.head] = 0;
146
      }
147
    }
148
  }
149
}
150