From 8f5547222ce6ce88af79fe8f07155dccf4552fff Mon Sep 17 00:00:00 2001 From: C-3PO Date: Sat, 23 Jun 2018 22:51:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Move=20ByteReader=20class=20into=20?= =?UTF-8?q?separate=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/extractFile.ts | 37 +------------------------------- src/ssn/extractFileByteReader.ts | 35 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 src/ssn/extractFileByteReader.ts diff --git a/src/ssn/extractFile.ts b/src/ssn/extractFile.ts index b52382f..6d56f48 100644 --- a/src/ssn/extractFile.ts +++ b/src/ssn/extractFile.ts @@ -1,42 +1,7 @@ import * as zlib from 'zlib'; import { ISsnFileEntry } from '../interfaces/ISsnFileEntry'; import decryptFile from './decryption/decryptFile'; - -class ByteReader { - private dvArray: DataView[]; - private dvIndex = 0; - private pos = 0; - constructor(dvArray: DataView[], startDvIndex: number, offset: number) { - this.dvArray = dvArray; - this.dvIndex = startDvIndex; - this.pos = offset; - } - public readByte() { - const curByte = this.dvArray[this.dvIndex].getUint8(this.pos); - this.pos += 1; - if (this.pos >= this.dvArray[this.dvIndex].byteLength) { - this.pos = 0; - this.dvIndex += 1; - if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); } - } - return curByte; - } - public seek(num: number) { - this.pos += num; - if (this.pos >= this.dvArray[this.dvIndex].byteLength) { - this.pos -= this.dvArray[this.dvIndex].byteLength; - this.dvIndex += 1; - if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); } - } - } - public extractDv(length: number) { - const dv = new DataView(new ArrayBuffer(length)); - for (let i = 0; i < length; i += 1) { - dv.setUint8(i, this.readByte()); //TODO: refactor this so it is more optimized - } - return dv; - } -} +import ByteReader from './extractFileByteReader'; /** Extracts the given file from the given DataView array and returns it as an ArrayBuffer. * Will throw an error when end of final DataView is reached. diff --git a/src/ssn/extractFileByteReader.ts b/src/ssn/extractFileByteReader.ts new file mode 100644 index 0000000..a653970 --- /dev/null +++ b/src/ssn/extractFileByteReader.ts @@ -0,0 +1,35 @@ +export default class ByteReader { + private dvArray: DataView[]; + private dvIndex = 0; + private pos = 0; + constructor(dvArray: DataView[], startDvIndex: number, offset: number) { + this.dvArray = dvArray; + this.dvIndex = startDvIndex; + this.pos = offset; + } + public readByte() { + const curByte = this.dvArray[this.dvIndex].getUint8(this.pos); + this.pos += 1; + if (this.pos >= this.dvArray[this.dvIndex].byteLength) { + this.pos = 0; + this.dvIndex += 1; + if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); } + } + return curByte; + } + public seek(num: number) { + this.pos += num; + if (this.pos >= this.dvArray[this.dvIndex].byteLength) { + this.pos -= this.dvArray[this.dvIndex].byteLength; + this.dvIndex += 1; + if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); } + } + } + public extractDv(length: number) { + const dv = new DataView(new ArrayBuffer(length)); + for (let i = 0; i < length; i += 1) { + dv.setUint8(i, this.readByte()); //TODO: refactor this so it is more optimized + } + return dv; + } +}