Skip to content

Commit 3224be2

Browse files
committed
BinaryWriter added new methods and tests
Renamed OutOfBoundsError to EndOfStreamError Added separate eslint configuration for tests that disables `no-explicit-any` requirement Added out of bounds checking for some of `BinaryWriter` methods + corresponding tests Reworked BinaryWriter magic numbers to use defined constants instead Reworked BinaryWriter tests to use number constants for number range Added more tests and filled in missing functionalities in `BinaryWriter` Wrapped up functionality and tests for BinaryWriter
1 parent ada1204 commit 3224be2

22 files changed

+1280
-316
lines changed

src/BinaryReader.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {Encoding, isValidEncoding} from "./Encoding";
22
import * as bigInt from 'big-integer';
3-
import {OutOfBoundsError} from "./errors/OutOfBoundsError";
3+
import {EndOfStreamError} from "./errors/EndOfStreamError";
44
import * as Utf8 from './Utf8';
55
import * as Int7 from './Int7';
6-
import {EncodingMessageFactory, OutOfBoundsMessageFactory} from "./errors/ErrorMessageFactory";
6+
import {EncodingMessageFactory, EndOfStreamMessageFactory} from "./errors/ErrorMessageFactory";
77
import {EncodingError} from "./errors/EncodingError";
88
import {InvalidArgumentError} from "./errors/InvalidArgumentError";
99

@@ -111,7 +111,7 @@ export class BinaryReader
111111
*
112112
* @link [C# `BinaryReader.ReadBoolean` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readboolean?view=netframework-4.7.2)
113113
* @returns {boolean} `false` if it's zero and `true` if it is not zero.
114-
* @throws `OutOfBoundsError` Thrown when there are no bytes left in the stream.
114+
* @throws `EndOfStreamError` Thrown when there are no bytes left in the stream.
115115
*/
116116
public readBoolean(): boolean
117117
{
@@ -128,7 +128,7 @@ export class BinaryReader
128128
*
129129
* @link [C# `BinaryReader.ReadByte` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readbyte?view=netframework-4.7.2)
130130
* @returns {number} Number between 0 and 255.
131-
* @throws `OutOfBoundsError` Thrown when there are no bytes left in the stream.
131+
* @throws `EndOfStreamError` Thrown when there are no bytes left in the stream.
132132
*/
133133
public readByte(): number
134134
{
@@ -145,7 +145,7 @@ export class BinaryReader
145145
*
146146
* @link [C# `BinaryReader.ReadSByte` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readsbyte?view=netframework-4.7.2)
147147
* @returns {number} Number between -128 and 127.
148-
* @throws `OutOfBoundsError` Thrown when there are no bytes left in the stream.
148+
* @throws `EndOfStreamError` Thrown when there are no bytes left in the stream.
149149
*/
150150
public readSignedByte(): number
151151
{
@@ -164,7 +164,7 @@ export class BinaryReader
164164
*
165165
* @link [C# `BinaryReader.ReadInt16` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readint16?view=netframework-4.7.2)
166166
* @returns {number} Number between -65,536 and 32,767.
167-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
167+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
168168
*/
169169
public readShort(): number
170170
{
@@ -186,7 +186,7 @@ export class BinaryReader
186186
*
187187
* @link [C# `BinaryReader.ReadUInt16` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readuint16?view=netframework-4.7.2)
188188
* @returns {number} Number between 0 and 65,535.
189-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
189+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
190190
*/
191191
public readUnsignedShort(): number
192192
{
@@ -204,7 +204,7 @@ export class BinaryReader
204204
*
205205
* @link [C# `BinaryReader.ReadInt32` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readint32?view=netframework-4.7.2)
206206
* @returns {number} Number between -2,147,483,648 and 2,147,483,647.
207-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
207+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
208208
*/
209209
public readInt(): number
210210
{
@@ -231,7 +231,7 @@ export class BinaryReader
231231
*
232232
* @link [C# `BinaryReader.ReadUInt32` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readuint32?view=netframework-4.7.2)
233233
* @returns {number} Number between 0 and 4,294,967,296.
234-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
234+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
235235
*/
236236
public readUnsignedInt(): number
237237
{
@@ -263,7 +263,7 @@ export class BinaryReader
263263
*
264264
* @link [C# `BinaryReader.ReadInt64` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readint64?view=netframework-4.7.2)
265265
* @returns {string} String representing a number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
266-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
266+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
267267
*/
268268
public readLongString(): string
269269
{
@@ -300,7 +300,7 @@ export class BinaryReader
300300
*
301301
* @link [C# `BinaryReader.ReadInt64` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readint64?view=netframework-4.7.2)
302302
* @returns {number} Number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
303-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
303+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
304304
*/
305305
public readLong(): number
306306
{
@@ -315,7 +315,7 @@ export class BinaryReader
315315
*
316316
* @link [C# `BinaryReader.ReadUInt64` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readuint64?view=netframework-4.7.2)
317317
* @returns {string} String representing a number between 0 and 18,446,744,073,709,551,615.
318-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
318+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
319319
*/
320320
public readUnsignedLongString(): string
321321
{
@@ -350,7 +350,7 @@ export class BinaryReader
350350
*
351351
* @link [C# `BinaryReader.ReadUInt64` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readuint64?view=netframework-4.7.2)
352352
* @returns {number} Number between 0 and 18,446,744,073,709,551,615.
353-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
353+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
354354
*/
355355
public readUnsignedLong(): number
356356
{
@@ -364,7 +364,7 @@ export class BinaryReader
364364
*
365365
* @link [C# `BinaryReader.ReadSingle` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readsingle?view=netframework-4.7.2)
366366
* @returns {number} Float number
367-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
367+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
368368
*/
369369
public readFloat(): number
370370
{
@@ -384,7 +384,7 @@ export class BinaryReader
384384
*
385385
* @link [C# `BinaryReader.ReadDouble` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readdouble?view=netframework-4.7.2)
386386
* @returns {number} Double number
387-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
387+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
388388
*/
389389
public readDouble(): number
390390
{
@@ -406,7 +406,7 @@ export class BinaryReader
406406
* @param {Encoding} encoding The encoding to use when reading the chars.
407407
* @returns {string} A single character read from the stream
408408
* @throws `EncodingError` Thrown when an unknown encoding is provided as the argument.
409-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
409+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
410410
* @throws `InvalidUtf8CharacterError` Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.
411411
*/
412412
public readChar(encoding: Encoding): string
@@ -416,7 +416,7 @@ export class BinaryReader
416416
}
417417

418418
if (this.remainingBytes === 0) {
419-
throw new OutOfBoundsError(OutOfBoundsMessageFactory.readCharZeroBytesLeft());
419+
throw new EndOfStreamError(EndOfStreamMessageFactory.readCharZeroBytesLeft());
420420
}
421421

422422
const result = Utf8.readUtf8StringFromBytes(this._view, this._position, 1);
@@ -433,7 +433,7 @@ export class BinaryReader
433433
* @returns {string} A string read from the stream.
434434
* @throws `InvalidArgumentError` Thrown when `charactersToRead` is not a number nor numeric string or when it is less than 1.
435435
* @throws `EncodingError` Thrown when an unknown encoding is provided as the argument.
436-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
436+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.
437437
* @throws `InvalidUtf8CharacterError` Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.
438438
* @link [C# `BinaryReader.ReadChars` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readchars?view=netframework-4.7.2)
439439
*/
@@ -454,7 +454,7 @@ export class BinaryReader
454454
}
455455

456456
if (this.remainingBytes === 0) {
457-
throw new OutOfBoundsError(OutOfBoundsMessageFactory.readCharZeroBytesLeft());
457+
throw new EndOfStreamError(EndOfStreamMessageFactory.readCharZeroBytesLeft());
458458
}
459459

460460
const result = Utf8.readUtf8StringFromBytes(this._view, this._position, charactersToRead);
@@ -471,7 +471,7 @@ export class BinaryReader
471471
* @returns {string} A string read from the stream.
472472
* @throws `InvalidArgumentError` Thrown when `bytesToRead` is not a number nor numeric string or when it is less than 1.
473473
* @throws `EncodingError` Thrown when an unknown encoding is provided as the argument.
474-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream or when the function stops reading in the middle of a
474+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream or when the function stops reading in the middle of a
475475
* character sequence in multibyte character encodings. Position of the stream does not change if this exception is thrown.
476476
* @throws `InvalidUtf8CharacterError` Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.
477477
*/
@@ -492,7 +492,7 @@ export class BinaryReader
492492
}
493493

494494
if (this.remainingBytes === 0) {
495-
throw new OutOfBoundsError(OutOfBoundsMessageFactory.readCharZeroBytesLeft());
495+
throw new EndOfStreamError(EndOfStreamMessageFactory.readCharZeroBytesLeft());
496496
}
497497

498498
const result = Utf8.readUtf8StringFromBytes(this._view, this._position, Number.MAX_SAFE_INTEGER, bytesToRead);
@@ -508,7 +508,7 @@ export class BinaryReader
508508
* @returns {string} A string read from the stream.
509509
* @throws `InvalidArgumentError` Thrown when `charactersToRead` is not a number nor numeric string or when it is less than 1.
510510
* @throws `EncodingError` Thrown when an unknown encoding is provided as the argument.
511-
* @throws `OutOfBoundsError` Thrown when there are not enough bytes left in the stream or when the lenfth prefix is longer than 5 bytes.
511+
* @throws `EndOfStreamError` Thrown when there are not enough bytes left in the stream or when the lenfth prefix is longer than 5 bytes.
512512
* @throws `InvalidUtf8CharacterError` Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.
513513
* @link [C# `BinaryReader.ReadString` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readstring?view=netframework-4.7.2)
514514
*/
@@ -519,14 +519,14 @@ export class BinaryReader
519519
}
520520

521521
if (this.remainingBytes === 0) {
522-
throw new OutOfBoundsError(OutOfBoundsMessageFactory.readStringZeroBytesLeft());
522+
throw new EndOfStreamError(EndOfStreamMessageFactory.readStringZeroBytesLeft());
523523
}
524524

525525
const [stringLength, newPosition] = Int7.read7BitEncodedInt(this._position, this._view);
526526

527527
const remainingBytes = this.length - newPosition;
528528
if (remainingBytes < stringLength) {
529-
throw new OutOfBoundsError(OutOfBoundsMessageFactory.readStringTooLongLeft(stringLength, remainingBytes));
529+
throw new EndOfStreamError(EndOfStreamMessageFactory.readStringTooLongLeft(stringLength, remainingBytes));
530530
}
531531

532532
const data = Utf8.readUtf8StringFromBytes(this._view, newPosition, Number.MAX_SAFE_INTEGER, stringLength);
@@ -541,8 +541,8 @@ export class BinaryReader
541541
const bytesRemaining = this.remainingBytes;
542542

543543
if (bytesRemaining < bytesExpected) {
544-
throw new OutOfBoundsError(
545-
OutOfBoundsMessageFactory.notEnoughBytesInBuffer(bytesExpected, bytesRemaining, operationName),
544+
throw new EndOfStreamError(
545+
EndOfStreamMessageFactory.notEnoughBytesInBuffer(bytesExpected, bytesRemaining, operationName),
546546
);
547547
}
548548
}

0 commit comments

Comments
 (0)