avoid constructing 0 byte long buffer and async stream._read() function

This commit is contained in:
Nicolas Raynaud
2016-08-18 21:39:23 +02:00
parent df33772eba
commit ef7a563d97
+22 -21
View File
@@ -222,29 +222,30 @@ export class ReadableRawVHDStream extends stream.Readable {
this.vmdkParser = vmdkParser
}
async _read () {
const next = await this.vmdkParser.next()
if (next === null) {
const paddingBuffer = new Buffer(this.size - this.position)
paddingBuffer.fill(0)
this.push(paddingBuffer)
this.push(this.footer)
this.push(null)
} else {
const offset = next.lbaBytes
const buffer = next.grain
var paddingLength = offset - this.position
if (paddingLength < 0) {
process.nextTick(() => this.emit('error', 'This VMDK file does not have its blocks in the correct order'))
}
const paddingBuffer = new Buffer(paddingLength)
paddingBuffer.fill(0)
if (paddingBuffer.length !== 0) {
_read () {
this.vmdkParser.next().then((next) => {
if (next === null) {
const paddingBuffer = new Buffer(this.size - this.position)
paddingBuffer.fill(0)
this.push(paddingBuffer)
this.push(this.footer)
this.push(null)
} else {
const offset = next.lbaBytes
const buffer = next.grain
var paddingLength = offset - this.position
if (paddingLength < 0) {
process.nextTick(() => this.emit('error', 'This VMDK file does not have its blocks in the correct order'))
}
if (paddingLength !== 0) {
const paddingBuffer = new Buffer(paddingLength)
paddingBuffer.fill(0)
this.push(paddingBuffer)
}
this.push(buffer)
this.position = offset + buffer.length
}
this.push(buffer)
this.position = offset + buffer.length
}
})
}
}