forked from Jstarfish/JavaKeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint16.js
More file actions
167 lines (158 loc) · 5.58 KB
/
int16.js
File metadata and controls
167 lines (158 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//? if (INT16) {
// types/ints/int16
/**
* Writes a 16bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.writeInt16 = function(value, offset) {
//? RELATIVE();
if (!this.noAssert) {
//? ASSERT_INTEGER('value');
//? ASSERT_OFFSET();
}
//? ENSURE_CAPACITY(2);
//? if (NODE || !DATAVIEW) { var dst = NODE ? 'this.buffer' : 'this.view';
if (this.littleEndian) {
/*?= dst */[offset+1] = (value & 0xFF00) >>> 8;
/*?= dst */[offset ] = value & 0x00FF;
} else {
/*?= dst */[offset] = (value & 0xFF00) >>> 8;
/*?= dst */[offset+1] = value & 0x00FF;
}
//? } else
this.view.setInt16(offset, value, this.littleEndian);
//? RELATIVE(2);
return this;
};
//? if (ALIASES) {
/**
* Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.writeShort = ByteBufferPrototype.writeInt16;
//? }
/**
* Reads a 16bit signed integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.readInt16 = function(offset) {
//? RELATIVE();
if (!this.noAssert) {
//? ASSERT_OFFSET(2);
}
//? if (NODE || !DATAVIEW) { var dst = NODE ? 'this.buffer' : 'this.view';
var value = 0;
if (this.littleEndian) {
value = /*?= dst */[offset ];
value |= /*?= dst */[offset+1] << 8;
} else {
value = /*?= dst */[offset ] << 8;
value |= /*?= dst */[offset+1];
}
if ((value & 0x8000) === 0x8000) value = -(0xFFFF - value + 1); // Cast to signed
//? } else
var value = this.view.getInt16(offset, this.littleEndian);
//? RELATIVE(2);
return value;
};
//? if (ALIASES) {
/**
* Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.readShort = ByteBufferPrototype.readInt16;
//? }
/**
* Writes a 16bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.writeUint16 = function(value, offset) {
//? RELATIVE();
if (!this.noAssert) {
//? ASSERT_INTEGER('value', true);
//? ASSERT_OFFSET();
}
//? ENSURE_CAPACITY(2);
//? if (NODE || !DATAVIEW) { var dst = NODE ? 'this.buffer' : 'this.view';
if (this.littleEndian) {
/*?= dst */[offset+1] = (value & 0xFF00) >>> 8;
/*?= dst */[offset ] = value & 0x00FF;
} else {
/*?= dst */[offset] = (value & 0xFF00) >>> 8;
/*?= dst */[offset+1] = value & 0x00FF;
}
//? } else
this.view.setUint16(offset, value, this.littleEndian);
//? RELATIVE(2);
return this;
};
/**
* Writes a 16bit unsigned integer. This is an alias of {@link ByteBuffer#writeUint16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @throws {TypeError} If `offset` or `value` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.writeUInt16 = ByteBufferPrototype.writeUint16;
/**
* Reads a 16bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.readUint16 = function(offset) {
//? RELATIVE();
if (!this.noAssert) {
//? ASSERT_OFFSET(2);
}
//? if (NODE || !DATAVIEW) { var dst = NODE ? 'this.buffer' : 'this.view';
var value = 0;
if (this.littleEndian) {
value = /*?= dst */[offset ];
value |= /*?= dst */[offset+1] << 8;
} else {
value = /*?= dst */[offset ] << 8;
value |= /*?= dst */[offset+1];
}
//? } else
var value = this.view.getUint16(offset, this.littleEndian);
//? RELATIVE(2);
return value;
};
/**
* Reads a 16bit unsigned integer. This is an alias of {@link ByteBuffer#readUint16}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
* @returns {number} Value read
* @throws {TypeError} If `offset` is not a valid number
* @throws {RangeError} If `offset` is out of bounds
* @expose
*/
ByteBufferPrototype.readUInt16 = ByteBufferPrototype.readUint16;
//? }