-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathbyteStream.js
More file actions
134 lines (116 loc) · 4.46 KB
/
byteStream.js
File metadata and controls
134 lines (116 loc) · 4.46 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
import sharedCopy from './sharedCopy.js';
import { readFixedString } from './byteArrayParser.js';
import { invalidParameter, missingParameter } from './util/errors.js';
/**
*
* Internal helper class to assist with parsing. Supports reading from a byte
* stream contained in a Uint8Array. Example usage:
*
* var byteArray = new Uint8Array(32);
* var byteStream = new dicomParser.ByteStream(dicomParser.littleEndianByteArrayParser, byteArray);
*
* */
export default class ByteStream {
/**
* Constructor for ByteStream objects.
* @param byteArrayParser a parser for parsing the byte array
* @param {Uint8Array | Buffer} byteArray a Uint8Array containing the byte stream
* @param {number} position (optional) the position to start reading from. 0 if not specified
* @constructor
* @throws {TypeError} missing or invalid `byteArrayParser` or `byteArray` parameter
* @throws {RangeError} incorrect start `position` parameter
*/
constructor (byteArrayParser, byteArray, position = 0) {
if (byteArrayParser === undefined) {
throw new TypeError(missingParameter('byteArrayParser'));
}
if (byteArray === undefined) {
throw new TypeError(missingParameter('byteArray'));
}
if ((byteArray instanceof Uint8Array) === false &&
((typeof Buffer === 'undefined') ||
(byteArray instanceof Buffer) === false)) {
throw new TypeError(invalidParameter('byteArray', 'should be of type Uint8Array or Buffer'));
}
if (position < 0) {
throw new RangeError(`ByteStream start position should be a positive integer or 0, got ${position.toString(10)}`);
}
if (position >= byteArray.length) {
throw new RangeError('ByteStream start position should be a positive integer strictly smaller than byteArray' +
`length (${byteArray.length}), got ${position.toString(10)}`);
}
this.byteArrayParser = byteArrayParser;
this.byteArray = byteArray;
this.position = position;
this.warnings = []; // array of string warnings encountered while parsing
}
/**
* Safely seeks through the byte stream.
* @param {number} offset the number of bytes to add to the position
* @throws {RangeError} seek would cause position to be outside the byteArray
*/
seek (offset) {
const newPosition = this.position + offset;
if (newPosition < 0) {
throw new RangeError(
`ByteStream cannot seek outside the byteArray tried to read position ${newPosition.toString(10)}`
);
}
this.position = newPosition;
}
/**
* Returns a new ByteStream object from the current position and of the requested number of bytes
* @param numBytes the length of the byte array for the ByteStream to contain
* @returns {dicomParser.ByteStream}
* @throws {RangeError} error if buffer overflow would occur
*/
readByteStream (numBytes) {
if (this.position + numBytes > this.byteArray.length) {
throw new RangeError('Attempted to read stream past buffer end');
}
var byteArrayView = sharedCopy(this.byteArray, this.position, numBytes);
this.position += numBytes;
return new ByteStream(this.byteArrayParser, byteArrayView);
}
getSize () {
return this.byteArray.length;
}
/**
*
* Parses an unsigned int 16 from a byte array and advances
* the position by 2 bytes
*
* @returns {*} the parsed unsigned int 16
* @throws {RangeError} tried to read outside the buffer
*/
readUint16 () {
var result = this.byteArrayParser.readUint16(this.byteArray, this.position);
this.position += 2;
return result;
}
/**
* Parses an unsigned int 32 from a byte array and advances
* the position by 2 bytes
*
* @returns {*} the parse unsigned int 32
* @throws {RangeError} tried to read outside the buffer
*/
readUint32 () {
var result = this.byteArrayParser.readUint32(this.byteArray, this.position);
this.position += 4;
return result;
}
/**
* Reads a string of 8-bit characters from an array of bytes and advances
* the position by length bytes. A null terminator will end the string
* but will not effect advancement of the position.
* @param length the maximum number of bytes to parse
* @returns {string} the parsed string
* @throws {RangeError} tried to read outside the buffer
*/
readFixedString (length) {
var result = readFixedString(this.byteArray, this.position, length);
this.position += length;
return result;
}
}