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