file-reader-writer
Version:
File reader/writer
179 lines (178 loc) • 6.33 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const bluebird_1 = __importDefault(require("bluebird"));
const FileBase_1 = require("./FileBase");
const errors_1 = require("./errors");
const fsRead = bluebird_1.default.promisify(fs_1.default.read);
const fsFstat = bluebird_1.default.promisify(fs_1.default.fstat);
class FileReader extends FileBase_1.FileBase {
constructor(file, flags) {
super(file, flags || 'r');
}
get length() {
return this.stats.size;
}
refreshStats() {
return __awaiter(this, void 0, void 0, function* () {
this.stats = yield fsFstat(this.fd);
});
}
isReadable(size) {
return this.stats.size - this.pointer >= size;
}
read(size) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.isReadable(size)) {
throw new Error('Not enough data to read!');
}
const result = Buffer.allocUnsafe ? Buffer.allocUnsafe(size) : new Buffer(size);
const bytesRead = yield fsRead(this.fd, result, 0, size, this.pointer);
if (size !== bytesRead) {
throw new Error('"size" and "bytesRead" do not match!');
}
this.pointer += size;
return result;
});
}
readString(size, encoding) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(size)).toString(encoding);
});
}
readIntBE(size) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(size)).readIntBE(0, size);
});
}
readIntLE(size) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(size)).readIntLE(0, size);
});
}
readUIntBE(size) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(size)).readUIntBE(0, size);
});
}
readUIntLE(size) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(size)).readUIntLE(0, size);
});
}
readInt8() {
return __awaiter(this, void 0, void 0, function* () {
return this.readIntBE(1);
});
}
readUInt8() {
return __awaiter(this, void 0, void 0, function* () {
return this.readUIntBE(1);
});
}
readInt16BE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readIntBE(2);
});
}
readInt16LE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readIntLE(2);
});
}
readUInt16BE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readUIntBE(2);
});
}
readUInt16LE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readUIntLE(2);
});
}
readInt32BE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readIntBE(4);
});
}
readInt32LE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readIntLE(4);
});
}
readUInt32BE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readUIntBE(4);
});
}
readUInt32LE() {
return __awaiter(this, void 0, void 0, function* () {
return this.readUIntLE(4);
});
}
readInt64BE() {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.readBigInt64BE) {
throw new errors_1.NotSupportedError('readInt64BE not supported!');
}
return (yield this.read(8)).readBigInt64BE(0);
});
}
readInt64LE() {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.readBigInt64LE) {
throw new errors_1.NotSupportedError('readInt64LE not supported!');
}
return (yield this.read(8)).readBigInt64LE(0);
});
}
readUInt64BE() {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.readBigUInt64BE) {
throw new errors_1.NotSupportedError('readUInt64BE not supported!');
}
return (yield this.read(8)).readBigUInt64BE(0);
});
}
readUInt64LE() {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.readBigUInt64LE) {
throw new errors_1.NotSupportedError('readUInt64LE not supported!');
}
return (yield this.read(8)).readBigUInt64LE(0);
});
}
readFloatBE() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(4)).readFloatBE(0);
});
}
readFloatLE() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(4)).readFloatLE(0);
});
}
readDoubleBE() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(8)).readDoubleBE(0);
});
}
readDoubleLE() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.read(8)).readDoubleLE(0);
});
}
}
exports.FileReader = FileReader;