当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Buffer.alloc方法代码示例

本文整理汇总了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);
}
开发者ID:whxaxes,项目名称:slither,代码行数:57,代码来源:utils.ts

示例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);
                });
            });
开发者ID:Half-Shot,项目名称:matrix-appservice-discord,代码行数:14,代码来源:util.ts

示例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;
  };
开发者ID:whxaxes,项目名称:slither,代码行数:21,代码来源:utils.ts

示例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);
                    });
                });
开发者ID:Half-Shot,项目名称:matrix-appservice-discord,代码行数:23,代码来源:util.ts


注:本文中的buffer.Buffer.alloc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。