file-reader-writer
Version:
File reader/writer
203 lines (202 loc) • 7.51 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 fsFstat = bluebird_1.default.promisify(fs_1.default.fstat);
const fsWrite = bluebird_1.default.promisify(fs_1.default.write);
class FileWriter extends FileBase_1.FileBase {
constructor(file, flags) {
super(file, flags || 'w');
this.fileSize = 0;
}
get length() {
return this.fileSize;
}
refreshStats() {
return __awaiter(this, void 0, void 0, function* () {
this.stats = yield fsFstat(this.fd);
this.fileSize = this.stats.size;
});
}
bufferAlloc(size) {
return Buffer.allocUnsafe ? Buffer.allocUnsafe(size) : new Buffer(size);
}
write(data) {
return __awaiter(this, void 0, void 0, function* () {
const bytesWritten = yield fsWrite(this.fd, data, 0, data.length, this.pointer);
if (data.length !== bytesWritten) {
throw new Error('"data.length" and "bytesWritten" do not match!');
}
this.pointer += data.length;
if (this.pointer > this.fileSize) {
this.fileSize = this.pointer;
}
});
}
writeString(string, encoding) {
return __awaiter(this, void 0, void 0, function* () {
return this.write(Buffer.from ? Buffer.from(string, encoding) : new Buffer(string, encoding));
});
}
writeIntBE(size, value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(size);
buffer.writeIntBE(value, 0, size);
return this.write(buffer);
});
}
writeIntLE(size, value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(size);
buffer.writeIntLE(value, 0, size);
return this.write(buffer);
});
}
writeUIntBE(size, value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(size);
buffer.writeUIntBE(value, 0, size);
return this.write(buffer);
});
}
writeUIntLE(size, value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(size);
buffer.writeUIntLE(value, 0, size);
return this.write(buffer);
});
}
writeInt8(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeIntBE(1, value);
});
}
writeUInt8(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeUIntBE(1, value);
});
}
writeInt16BE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeIntBE(2, value);
});
}
writeInt16LE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeIntLE(2, value);
});
}
writeUInt16BE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeUIntBE(2, value);
});
}
writeUInt16LE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeUIntLE(2, value);
});
}
writeInt32BE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeIntBE(4, value);
});
}
writeInt32LE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeIntLE(4, value);
});
}
writeUInt32BE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeUIntBE(4, value);
});
}
writeUInt32LE(value) {
return __awaiter(this, void 0, void 0, function* () {
return this.writeUIntLE(4, value);
});
}
writeInt64BE(value) {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.writeBigInt64BE) {
throw new errors_1.NotSupportedError('writeBigInt64BE not supported!');
}
const buffer = this.bufferAlloc(8);
buffer.writeBigInt64BE(value, 0);
return this.write(buffer);
});
}
writeInt64LE(value) {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.writeBigInt64LE) {
throw new errors_1.NotSupportedError('writeBigInt64LE not supported!');
}
const buffer = this.bufferAlloc(8);
buffer.writeBigInt64LE(value, 0);
return this.write(buffer);
});
}
writeUInt64BE(value) {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.writeBigUInt64BE) {
throw new errors_1.NotSupportedError('writeBigUInt64BE not supported!');
}
const buffer = this.bufferAlloc(8);
buffer.writeBigUInt64BE(value, 0);
return this.write(buffer);
});
}
writeUInt64LE(value) {
return __awaiter(this, void 0, void 0, function* () {
if (!Buffer.prototype.writeBigUInt64LE) {
throw new errors_1.NotSupportedError('writeBigUInt64LE not supported!');
}
const buffer = this.bufferAlloc(8);
buffer.writeBigUInt64LE(value, 0);
return this.write(buffer);
});
}
writeFloatBE(value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(4);
buffer.writeFloatBE(value, 0);
return this.write(buffer);
});
}
writeFloatLE(value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(4);
buffer.writeFloatLE(value, 0);
return this.write(buffer);
});
}
writeDoubleBE(value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(8);
buffer.writeDoubleBE(value, 0);
return this.write(buffer);
});
}
writeDoubleLE(value) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = this.bufferAlloc(8);
buffer.writeDoubleLE(value, 0);
return this.write(buffer);
});
}
}
exports.FileWriter = FileWriter;