Skip to content

Commit 85bb15a

Browse files
committed
feat: add more read methods.
1 parent f508c25 commit 85bb15a

File tree

1 file changed

+386
-0
lines changed

1 file changed

+386
-0
lines changed

src/dynamicBuffer.ts

Lines changed: 386 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,392 @@ export class DynamicBuffer {
765765
return this.buffer[offset];
766766
}
767767

768+
/**
769+
* Reads a signed, big-dian 64-bit integer from `buf` at the specified `offset`.
770+
*
771+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
772+
* `buf.length - 8`, default `0`.
773+
* @returns Integer read from the buffer at the specified offset.
774+
*/
775+
readBigInt64BE(offset: number = 0): BigInt {
776+
if (!this.buffer || this.length < 8) {
777+
throw new RangeError('Attempt to access memory outside buffer bounds');
778+
}
779+
780+
checkRange('offset', offset, 0, this.length - 8);
781+
782+
return this.buffer.readBigInt64BE(offset);
783+
}
784+
785+
/**
786+
* Reads a signed, little-dian 64-bit integer from `buf` at the specified `offset`.
787+
*
788+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
789+
* `buf.length - 8`, default `0`.
790+
* @returns Integer read from the buffer at the specified offset.
791+
*/
792+
readBigInt64LE(offset: number = 0): BigInt {
793+
if (!this.buffer || this.length < 8) {
794+
throw new RangeError('Attempt to access memory outside buffer bounds');
795+
}
796+
797+
checkRange('offset', offset, 0, this.length - 8);
798+
799+
return this.buffer.readBigInt64LE(offset);
800+
}
801+
802+
/**
803+
* Reads an unsigned, big-dian 64-bit integer from `buf` at the specified `offset`.
804+
*
805+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
806+
* `buf.length - 8`, default `0`.
807+
* @returns Integer read from the buffer at the specified offset.
808+
*/
809+
readBigUInt64BE(offset: number = 0): BigInt {
810+
if (!this.buffer || this.length < 8) {
811+
throw new RangeError('Attempt to access memory outside buffer bounds');
812+
}
813+
814+
checkRange('offset', offset, 0, this.length - 8);
815+
816+
return this.buffer.readBigUInt64BE(offset);
817+
}
818+
819+
/**
820+
* Reads an unsigned, little-dian 64-bit integer from `buf` at the specified `offset`.
821+
*
822+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
823+
* `buf.length - 8`, default `0`.
824+
* @returns Integer read from the buffer at the specified offset.
825+
*/
826+
readBigUInt64LE(offset: number = 0): BigInt {
827+
if (!this.buffer || this.length < 8) {
828+
throw new RangeError('Attempt to access memory outside buffer bounds');
829+
}
830+
831+
checkRange('offset', offset, 0, this.length - 8);
832+
833+
return this.buffer.readBigUInt64LE(offset);
834+
}
835+
836+
/**
837+
* Reads a 64-bit, big-endian double from `buf` at the specified `offset`.
838+
*
839+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
840+
* `buf.length - 8`, default `0`.
841+
* @returns Double-precision floating-point number read from the buffer at the specified offset.
842+
*/
843+
readDoubleBE(offset: number = 0): number {
844+
if (!this.buffer || this.length < 8) {
845+
throw new RangeError('Attempt to access memory outside buffer bounds');
846+
}
847+
848+
checkRange('offset', offset, 0, this.length - 8);
849+
850+
return this.buffer.readDoubleBE(offset);
851+
}
852+
853+
/**
854+
* Reads a 64-bit, little-endian double from `buf` at the specified `offset`.
855+
*
856+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
857+
* `buf.length - 8`, default `0`.
858+
* @returns Double-precision floating-point number read from the buffer at the specified offset.
859+
*/
860+
readDoubleLE(offset: number = 0): number {
861+
if (!this.buffer || this.length < 8) {
862+
throw new RangeError('Attempt to access memory outside buffer bounds');
863+
}
864+
865+
checkRange('offset', offset, 0, this.length - 8);
866+
867+
return this.buffer.readDoubleLE(offset);
868+
}
869+
870+
/**
871+
* Reads a 32-bit, big-endian double from `buf` at the specified `offset`.
872+
*
873+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
874+
* `buf.length - 4`, default `0`.
875+
* @returns Single-precision floating-point number read from the buffer at the specified offset.
876+
*/
877+
readFloatBE(offset: number = 0): number {
878+
if (!this.buffer || this.length < 4) {
879+
throw new RangeError('Attempt to access memory outside buffer bounds');
880+
}
881+
882+
checkRange('offset', offset, 0, this.length - 4);
883+
884+
return this.buffer.readFloatBE(offset);
885+
}
886+
887+
/**
888+
* Reads a 32-bit, little-endian double from `buf` at the specified `offset`.
889+
*
890+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
891+
* `buf.length - 4`, default `0`.
892+
* @returns Single-precision floating-point number read from the buffer at the specified offset.
893+
*/
894+
readFloatLE(offset: number = 0): number {
895+
if (!this.buffer || this.length < 4) {
896+
throw new RangeError('Attempt to access memory outside buffer bounds');
897+
}
898+
899+
checkRange('offset', offset, 0, this.length - 4);
900+
901+
return this.buffer.readFloatLE(offset);
902+
}
903+
904+
/**
905+
* Reads a signed 8-bit integer from `buf` at the specified `offset`.
906+
*
907+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
908+
* `buf.length - 1`, default `0`.
909+
* @returns Integer read from the buffer at the specified offset.
910+
*/
911+
readInt8(offset: number = 0): number {
912+
if (!this.buffer || this.length < 1) {
913+
throw new RangeError('Attempt to access memory outside buffer bounds');
914+
}
915+
916+
checkRange('offset', offset, 0, this.length - 1);
917+
918+
return this.buffer.readInt8(offset);
919+
}
920+
921+
/**
922+
* Reads a signed, big-endian 16-bit integer from `buf` at the specified `offset`.
923+
*
924+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
925+
* `buf.length - 2`, default `0`.
926+
* @returns Integer read from the buffer at the specified offset.
927+
*/
928+
readInt16BE(offset: number = 0): number {
929+
if (!this.buffer || this.length < 2) {
930+
throw new RangeError('Attempt to access memory outside buffer bounds');
931+
}
932+
933+
checkRange('offset', offset, 0, this.length - 2);
934+
935+
return this.buffer.readInt16BE(offset);
936+
}
937+
938+
/**
939+
* Reads a signed, little-endian 16-bit integer from `buf` at the specified `offset`.
940+
*
941+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
942+
* `buf.length - 2`, default `0`.
943+
* @returns Integer read from the buffer at the specified offset.
944+
*/
945+
readInt16LE(offset: number = 0): number {
946+
if (!this.buffer || this.length < 2) {
947+
throw new RangeError('Attempt to access memory outside buffer bounds');
948+
}
949+
950+
checkRange('offset', offset, 0, this.length - 2);
951+
952+
return this.buffer.readInt16LE(offset);
953+
}
954+
955+
/**
956+
* Reads a signed, big-endian 32-bit integer from `buf` at the specified `offset`.
957+
*
958+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
959+
* `buf.length - 4`, default `0`.
960+
* @returns Integer read from the buffer at the specified offset.
961+
*/
962+
readInt32BE(offset: number = 0): number {
963+
if (!this.buffer || this.length < 4) {
964+
throw new RangeError('Attempt to access memory outside buffer bounds');
965+
}
966+
967+
checkRange('offset', offset, 0, this.length - 4);
968+
969+
return this.buffer.readInt32BE(offset);
970+
}
971+
972+
/**
973+
* Reads a signed, little-endian 32-bit integer from `buf` at the specified `offset`.
974+
*
975+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
976+
* `buf.length - 4`, default `0`.
977+
* @returns Integer read from the buffer at the specified offset.
978+
*/
979+
readInt32LE(offset: number = 0): number {
980+
if (!this.buffer || this.length < 4) {
981+
throw new RangeError('Attempt to access memory outside buffer bounds');
982+
}
983+
984+
checkRange('offset', offset, 0, this.length - 4);
985+
986+
return this.buffer.readInt32LE(offset);
987+
}
988+
989+
/**
990+
* Reads `byteLength` number of bytes from the `buf` at the specified `offset` and interprets the
991+
* result as a big-endian, two's complement signed value supporting up to 48 bits of accuracy.
992+
*
993+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
994+
* `buf.length - byteLength`.
995+
* @param byteLength Number of bytes to read, and it must satisfy `0 < byteLength <= 6`.
996+
* @returns Integer read from the buffer at the specified offset.
997+
*/
998+
readIntBE(offset: number, byteLength: number): number {
999+
if (!this.buffer || this.length < byteLength) {
1000+
throw new RangeError('Attempt to access memory outside buffer bounds');
1001+
}
1002+
1003+
checkRange('byteLength', byteLength, 1, 6);
1004+
checkRange('offset', offset, 0, this.length - byteLength);
1005+
1006+
return this.buffer.readIntBE(offset, byteLength);
1007+
}
1008+
1009+
/**
1010+
* Reads `byteLength` number of bytes from the `buf` at the specified `offset` and interprets the
1011+
* result as a little-endian, two's complement signed value supporting up to 48 bits of accuracy.
1012+
*
1013+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1014+
* `buf.length - byteLength`.
1015+
* @param byteLength Number of bytes to read, and it must satisfy `0 < byteLength <= 6`.
1016+
* @returns Integer read from the buffer at the specified offset.
1017+
*/
1018+
readIntLE(offset: number, byteLength: number) {
1019+
if (!this.buffer || this.length < byteLength) {
1020+
throw new RangeError('Attempt to access memory outside buffer bounds');
1021+
}
1022+
1023+
checkRange('byteLength', byteLength, 1, 6);
1024+
checkRange('offset', offset, 0, this.length - byteLength);
1025+
1026+
return this.buffer.readIntLE(offset, byteLength);
1027+
}
1028+
1029+
/**
1030+
* Reads an unsigned 8-bit integer from `buf` at the specified `offset`.
1031+
*
1032+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1033+
* `buf.length - 1`, default `0`.
1034+
* @returns Integer read from the buffer at the specified offset.
1035+
*/
1036+
readUInt8(offset: number = 0): number {
1037+
if (!this.buffer || this.length < 1) {
1038+
throw new RangeError('Attempt to access memory outside buffer bounds');
1039+
}
1040+
1041+
checkRange('offset', offset, 0, this.length - 1);
1042+
1043+
return this.buffer.readUInt8(offset);
1044+
}
1045+
1046+
/**
1047+
* Reads an unsigned, big-endian 16-bit integer from `buf` at the specified `offset`.
1048+
*
1049+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1050+
* `buf.length - 2`, default `0`.
1051+
* @returns Integer read from the buffer at the specified offset.
1052+
*/
1053+
readUInt16BE(offset: number = 0): number {
1054+
if (!this.buffer || this.length < 2) {
1055+
throw new RangeError('Attempt to access memory outside buffer bounds');
1056+
}
1057+
1058+
checkRange('offset', offset, 0, this.length - 2);
1059+
1060+
return this.buffer.readUInt16BE(offset);
1061+
}
1062+
1063+
/**
1064+
* Reads an unsigned, little-endian 16-bit integer from `buf` at the specified `offset`.
1065+
*
1066+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1067+
* `buf.length - 2`, default `0`.
1068+
* @returns Integer read from the buffer at the specified offset.
1069+
*/
1070+
readUInt16LE(offset: number = 0): number {
1071+
if (!this.buffer || this.length < 2) {
1072+
throw new RangeError('Attempt to access memory outside buffer bounds');
1073+
}
1074+
1075+
checkRange('offset', offset, 0, this.length - 2);
1076+
1077+
return this.buffer.readUInt16LE(offset);
1078+
}
1079+
1080+
/**
1081+
* Reads an unsigned, big-endian 32-bit integer from `buf` at the specified `offset`.
1082+
*
1083+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1084+
* `buf.length - 4`, default `0`.
1085+
* @returns Integer read from the buffer at the specified offset.
1086+
*/
1087+
readUInt32BE(offset: number = 0): number {
1088+
if (!this.buffer || this.length < 4) {
1089+
throw new RangeError('Attempt to access memory outside buffer bounds');
1090+
}
1091+
1092+
checkRange('offset', offset, 0, this.length - 4);
1093+
1094+
return this.buffer.readUInt32BE(offset);
1095+
}
1096+
1097+
/**
1098+
* Reads an unsigned, little-endian 32-bit integer from `buf` at the specified `offset`.
1099+
*
1100+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1101+
* `buf.length - 4`, default `0`.
1102+
* @returns Integer read from the buffer at the specified offset.
1103+
*/
1104+
readUInt32LE(offset: number = 0): number {
1105+
if (!this.buffer || this.length < 4) {
1106+
throw new RangeError('Attempt to access memory outside buffer bounds');
1107+
}
1108+
1109+
checkRange('offset', offset, 0, this.length - 4);
1110+
1111+
return this.buffer.readUInt32LE(offset);
1112+
}
1113+
1114+
/**
1115+
* Reads `byteLength` number of bytes from the `buf` at the specified `offset` and interprets the
1116+
* result as an unsigned big-endian integer supporting up to 48 bits of accuracy.
1117+
*
1118+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1119+
* `buf.length - byteLength`.
1120+
* @param byteLength Number of bytes to read, and it must satisfy `0 < byteLength <= 6`.
1121+
* @returns Integer read from the buffer at the specified offset.
1122+
*/
1123+
readUIntBE(offset: number, byteLength: number): number {
1124+
if (!this.buffer || this.length < byteLength) {
1125+
throw new RangeError('Attempt to access memory outside buffer bounds');
1126+
}
1127+
1128+
checkRange('byteLength', byteLength, 1, 6);
1129+
checkRange('offset', offset, 0, this.length - byteLength);
1130+
1131+
return this.buffer.readUIntBE(offset, byteLength);
1132+
}
1133+
1134+
/**
1135+
* Reads `byteLength` number of bytes from the `buf` at the specified `offset` and interprets the
1136+
* result as an unsigned little-endian integer supporting up to 48 bits of accuracy.
1137+
*
1138+
* @param offset Number of bytes to skip before starting to read, and it must between `0` and
1139+
* `buf.length - byteLength`.
1140+
* @param byteLength Number of bytes to read, and it must satisfy `0 < byteLength <= 6`.
1141+
* @returns Integer read from the buffer at the specified offset.
1142+
*/
1143+
readUIntLE(offset: number, byteLength: number): number {
1144+
if (!this.buffer || this.length < byteLength) {
1145+
throw new RangeError('Attempt to access memory outside buffer bounds');
1146+
}
1147+
1148+
checkRange('byteLength', byteLength, 1, 6);
1149+
checkRange('offset', offset, 0, this.length - byteLength);
1150+
1151+
return this.buffer.readUIntLE(offset, byteLength);
1152+
}
1153+
7681154
/**
7691155
* Calls the specified callback function for all the bytes in an buffer. The return value of
7701156
* the callback function is the accumulated result, and is provided as an argument in the next

0 commit comments

Comments
 (0)