本文整理汇总了TypeScript中buffer.Buffer.alloc方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Buffer.alloc方法的具体用法?TypeScript Buffer.alloc怎么用?TypeScript Buffer.alloc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.alloc方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: encode
export function encode({ opt, data }) {
const bufList: Buffer[] = [];
let byteLen = 0;
let offset = 0;
data = Array.isArray(data) ? data : [data];
let buf = Buffer.alloc(allocLen);
bufList.push(buf);
const writeUInt = (value, byteLength) => {
byteLen += byteLength;
const less = allocLen - offset;
if (less < byteLength) {
if (less) {
// split buffer
byteLength -= less;
const i = Math.pow(2, byteLength * 8);
const l = (value / i) >>> 0;
value -= l * i;
buf.writeUIntBE(l, offset, less);
}
buf = Buffer.alloc(allocLen);
bufList.push(buf);
offset = 0;
}
buf.writeUIntBE(value, offset, byteLength);
offset += byteLength;
};
writeUInt(opt, OPT_LEN);
// set buffer
data.forEach((item) => {
const packetType = packetTypes[item.type];
writeUInt(item.type, PACKET_TYPE_LEN);
for (const key in packetType) {
if (packetType.hasOwnProperty(key)) {
let value = +item.packet[key] || 0;
const packetItem = packetType[key];
const byteLength = packetItem.byteLen;
if (packetItem.encode) {
value = packetItem.encode(value);
}
writeUInt(value, byteLength);
}
}
});
if (bufList.length > 1) {
buf = Buffer.concat(bufList, bufList.length * allocLen);
}
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + byteLen);
}
示例2: reject
const req = ht.get((url), (res) => {
let buffer = Buffer.alloc(0);
if (res.statusCode !== HTTP_OK) {
reject(`Non 200 status code (${res.statusCode})`);
}
res.on("data", (d) => {
buffer = Buffer.concat([buffer, d]);
});
res.on("end", () => {
resolve(buffer);
});
});
示例3:
const writeUInt = (value, byteLength) => {
byteLen += byteLength;
const less = allocLen - offset;
if (less < byteLength) {
if (less) {
// split buffer
byteLength -= less;
const i = Math.pow(2, byteLength * 8);
const l = (value / i) >>> 0;
value -= l * i;
buf.writeUIntBE(l, offset, less);
}
buf = Buffer.alloc(allocLen);
bufList.push(buf);
offset = 0;
}
buf.writeUIntBE(value, offset, byteLength);
offset += byteLength;
};
示例4: resolve
const req = ht.get( url, (res) => {
let buffer = Buffer.alloc(0);
if (res.headers.hasOwnProperty("content-type")) {
contenttype = res.headers["content-type"];
} else {
log.verbose("No content-type given by server, guessing based on file name.");
contenttype = mime.lookup(url);
}
if (name === null) {
const names = url.split("/");
name = names[names.length - 1];
}
res.on("data", (d) => {
buffer = Buffer.concat([buffer, d]);
});
res.on("end", () => {
resolve(buffer);
});
});