writer: better failsafes. optimize.
This commit is contained in:
parent
fcc3f52f72
commit
ca3dea3470
@ -700,8 +700,6 @@ CoinEntry.prototype.getSize = function getSize() {
|
||||
*/
|
||||
|
||||
CoinEntry.prototype.toWriter = function toWriter(bw) {
|
||||
var raw;
|
||||
|
||||
if (!this.raw) {
|
||||
assert(this.output);
|
||||
compress.output(this.output, bw);
|
||||
@ -712,9 +710,7 @@ CoinEntry.prototype.toWriter = function toWriter(bw) {
|
||||
// didn't use it, it's still in its
|
||||
// compressed form. Just write it back
|
||||
// as a buffer for speed.
|
||||
raw = this.raw.slice(this.offset, this.offset + this.size);
|
||||
|
||||
bw.writeBytes(raw);
|
||||
bw.copy(this.raw, this.offset, this.offset + this.size);
|
||||
|
||||
return bw;
|
||||
};
|
||||
|
||||
@ -323,58 +323,80 @@ StaticWriter.prototype.writeBytes = function writeBytes(value) {
|
||||
|
||||
StaticWriter.prototype.writeVarBytes = function writeVarBytes(value) {
|
||||
this.writeVarint(value.length);
|
||||
|
||||
if (value.length === 0)
|
||||
return;
|
||||
|
||||
this.writeBytes(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy bytes.
|
||||
* @param {Buffer} value
|
||||
* @param {Number} start
|
||||
* @param {Number} end
|
||||
*/
|
||||
|
||||
StaticWriter.prototype.copy = function copy(value, start, end) {
|
||||
var len = end - start;
|
||||
|
||||
if (len === 0)
|
||||
return;
|
||||
|
||||
value.copy(this.data, this.written, start, end);
|
||||
this.written += len;
|
||||
};
|
||||
|
||||
/**
|
||||
* Write string to buffer.
|
||||
* @param {String|Buffer} value
|
||||
* @param {String} value
|
||||
* @param {String?} enc - Any buffer-supported encoding.
|
||||
*/
|
||||
|
||||
StaticWriter.prototype.writeString = function writeString(value, enc) {
|
||||
if (typeof value !== 'string')
|
||||
return this.writeBytes(value);
|
||||
var size;
|
||||
|
||||
if (value.length === 0)
|
||||
return;
|
||||
|
||||
this.written += this.data.write(value, this.written, enc);
|
||||
size = Buffer.byteLength(value, enc);
|
||||
|
||||
this.data.write(value, this.written, enc);
|
||||
|
||||
this.written += size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a hash/hex-string.
|
||||
* @param {Hash|Buffer}
|
||||
* Write a 32 byte hash.
|
||||
* @param {Hash} value
|
||||
*/
|
||||
|
||||
StaticWriter.prototype.writeHash = function writeHash(value) {
|
||||
this.writeString(value, 'hex');
|
||||
if (typeof value !== 'string') {
|
||||
assert(value.length === 32);
|
||||
return this.writeBytes(value);
|
||||
}
|
||||
assert(value.length === 64);
|
||||
this.data.write(value, this.written, 'hex');
|
||||
this.written += 32;
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a string with a varint length before it.
|
||||
* @param {String|Buffer}
|
||||
* @param {String}
|
||||
* @param {String?} enc - Any buffer-supported encoding.
|
||||
*/
|
||||
|
||||
StaticWriter.prototype.writeVarString = function writeVarString(value, enc) {
|
||||
var size;
|
||||
|
||||
if (typeof value !== 'string')
|
||||
return this.writeVarBytes(value);
|
||||
if (value.length === 0) {
|
||||
this.writeVarint(0);
|
||||
return;
|
||||
}
|
||||
|
||||
size = Buffer.byteLength(value, enc);
|
||||
|
||||
this.writeVarint(size);
|
||||
this.data.write(value, this.written, enc);
|
||||
|
||||
if (value.length === 0)
|
||||
return;
|
||||
|
||||
this.writeString(value, enc);
|
||||
this.written += size;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -433,44 +433,60 @@ BufferWriter.prototype.writeVarBytes = function writeVarBytes(value) {
|
||||
this.ops.push(new WriteOp(BYTES, value));
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy bytes.
|
||||
* @param {Buffer} value
|
||||
* @param {Number} start
|
||||
* @param {Number} end
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.copy = function copy(value, start, end) {
|
||||
assert(end >= start);
|
||||
value = value.slice(start, end);
|
||||
this.writeBytes(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Write string to buffer.
|
||||
* @param {String|Buffer} value
|
||||
* @param {String} value
|
||||
* @param {String?} enc - Any buffer-supported encoding.
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeString = function writeString(value, enc) {
|
||||
if (typeof value !== 'string')
|
||||
return this.writeBytes(value);
|
||||
|
||||
this.written += Buffer.byteLength(value, enc);
|
||||
|
||||
if (value.length === 0)
|
||||
return;
|
||||
|
||||
this.written += Buffer.byteLength(value, enc);
|
||||
this.ops.push(new WriteOp(STR, value, enc));
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a hash/hex-string.
|
||||
* @param {Hash|Buffer}
|
||||
* Write a 32 byte hash.
|
||||
* @param {Hash} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeHash = function writeHash(value) {
|
||||
if (typeof value !== 'string') {
|
||||
assert(value.length === 32);
|
||||
return this.writeBytes(value);
|
||||
}
|
||||
assert(value.length === 64);
|
||||
this.writeString(value, 'hex');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a string with a varint length before it.
|
||||
* @param {String|Buffer}
|
||||
* @param {String}
|
||||
* @param {String?} enc - Any buffer-supported encoding.
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeVarString = function writeVarString(value, enc) {
|
||||
var size;
|
||||
|
||||
if (typeof value !== 'string')
|
||||
return this.writeVarBytes(value);
|
||||
if (value.length === 0) {
|
||||
this.ops.push(new WriteOp(VARINT, 0));
|
||||
return;
|
||||
}
|
||||
|
||||
size = Buffer.byteLength(value, enc);
|
||||
|
||||
@ -479,9 +495,6 @@ BufferWriter.prototype.writeVarString = function writeVarString(value, enc) {
|
||||
|
||||
this.ops.push(new WriteOp(VARINT, size));
|
||||
|
||||
if (value.length === 0)
|
||||
return;
|
||||
|
||||
this.ops.push(new WriteOp(STR, value, enc));
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user