node缓冲区

什么是缓冲液? (What are Buffers?)

Binary is simply a set or a collection of 1 and 0. Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. For example, the following are five different binaries:

Binary只是10的集合或集合。 二进制中的每个数字,一组中的每个1和0称为bit 。 计算机将数据转换为该二进制格式以存储和执行操作。 例如,以下是五个不同的二进制文件:

10, 01, 001, 1110, 00101011

10, 01, 001, 1110, 00101011

JavaScript does not have a byte type data in its core API. To handle binary data Node.js includes a binary buffer implementation with a global module called Buffer.

JavaScript的核心API中没有字节类型数据。 为了处理二进制数据,Node.js包括一个二进制缓冲区实现,该实现带有一个名为Buffer的全局模块。

创建一个缓冲区 (Creating a Buffer)

There are different ways you can create a buffer in Node.js. You can create an empty buffer by with a size of 10 bytes.

您可以使用多种方法在Node.js中创建缓冲区。 您可以创建一个10字节大小的空缓冲区。

const buf1 = Buffer.alloc(10);

From UTF-8-encoded strings, the creation is like this:

通过UTF-8编码的字符串,创建过程如下所示:

const buf2 = Buffer.from('Hello World!');

There are different accepted encoding when creating a Buffer:

创建缓冲区时,可以接受不同的编码:

  • asciiASCII
  • utf-8utf-8
  • base64:base64:
  • latin1拉丁语1
  • binary二元
  • hex十六进制

There are three separate functions allocated in the Buffer API to use and create new buffers. In above examples we have seen alloc() and from(). The third one is allocUnsafe().

在缓冲区API中分配了三个单独的函数,以使用和创建新缓冲区。 在上面的示例中,我们看到了alloc()from() 。 第三个是allocUnsafe()

const buf3 = Buffer.allocUnsafe(10);

When returned, this function might contain old data that needs to be overwritten.

返回时,此函数可能包含需要覆盖的旧数据。

与缓冲液的相互作用 (Interactions with Buffer)

There are different interactions that can be made with the Buffer API. We are going to cover most of them here. Let us start with converting a buffer to JSON.

Buffer API可以进行不同的交互。 我们将在这里介绍其中的大多数内容。 让我们从将缓冲区转换为JSON开始。

let bufferOne = Buffer.from('This is a buffer example.');
console.log(bufferOne);// Output: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>let json = JSON.stringify(bufferOne);
console.log(json);// Output: {"type": "Buffer", "data": [84,104,105,115,32,105,115,32,97,32,98,117,102,102,101,114,32,101,120,97,109,112,108,101,46]}

The JSON specifies that the type of object being transformed is a Buffer, and its data. Converting an empty buffer to JSON will show us that it contains nothing but zeros.

JSON指定要转换的对象的类型是Buffer及其数据。 将空缓冲区转换为JSON将向我们显示,它只包含零。

const emptyBuf = Buffer.alloc(10);emptyBuf.toJSON();// Output: { "type": "Buffer", "data": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] }

Do notice that, Buffer API also provides a direct function toJSON() to convert a buffer into a JSON object. To examine the size of a buffer, we can use length method.

请注意,Buffer API还提供了toJSON()的直接函数,可将缓冲区转换为JSON对象。 要检查缓冲区的大小,我们可以使用length方法。

emptyBuf.length;
// Output: 10

Now let us convert buffer to a readable string, in our case, the utf-8 encoded.

现在,让我们将缓冲区转换为可读字符串,在本例中为utf-8编码。

console.log(bufferOne.toString('utf8'));// Output: This is a buffer example.

.toString() by default converts a buffer to a utf-8 format string. This is how you decode a buffer. If you specify an encoding you can convert the buffer to another encoding

默认情况下, .toString()将缓冲区转换为utf-8格式的字符串。 这就是解码缓冲区的方式。 如果指定一种编码,则可以将缓冲区转换为另一种编码

console.log(bufferOne.toString('base64'));

有关缓冲区的更多信息: (More info on Buffers:)

  • Need a better understanding of buffers in Node.js? Check this out.

    是否需要更好地了解Node.js中的缓冲区? 看一下这个。

翻译自: https://www.freecodecamp.org/news/node-js-buffer-explained/

node缓冲区

node缓冲区_Node.js缓冲区介绍相关推荐

  1. node 更新_Node.js 15 正式发布,14 将支持到 2023 年

    来源 | https://www.oschina.net/news/119346/node-js-15-releasedJavaScript 运行时 Node.js 已经更新到了 15 版本.Node ...

  2. mac怎么查node版本_Node.js 微服务实践:基于容器的一站式命令行工具链

    作者:个推Node.js 开发工程师 之诺 背景与摘要 由于工程数量的快速增长,个推在实践基于 Node.js 的微服务开发的过程中,遇到了如下问题: 1. 每次新建项目都需要安装一次依赖,这些依赖之 ...

  3. node 升级_Node.js 版本知多少?又该如何选择?

    习惯成自然是个魔术师.它对美丽的东西是残酷的,但是对丑陋的东西却是仁慈的.--威达 Node.js 曾出现过与 io.js 的分裂,自合并成立 Node.js 基金会以来,就开始使用 Long Ter ...

  4. node.js的开发流程_Node.js子流程:您需要了解的一切

    node.js的开发流程 by Samer Buna 通过Samer Buna Node.js子流程:您需要了解的一切 (Node.js Child Processes: Everything you ...

  5. (8)Node.js 模块介绍

    一.Node.js模块介绍 模块(包)是 Node.js 中具有特定功能的对象. 二.web浏览器端和Node端的对比图 我们通过如上图可以看到,再web浏览器端的基本语法,再Node端也能使用,但是 ...

  6. js 多个定时器_Node.js系列深入浅出Node模块化开发——CommonJS规范

    前言 本文将为大家透彻的介绍关于Node的模块化--CommonJS的一切. 看完本文可以掌握,以下几个方面: 什么是模块化,以及没有模块化会带来哪些问题,是如何解决的: JavaScript的设计缺 ...

  7. 行缓冲、全缓冲、无缓冲以及用户缓冲区、内核缓冲区介绍

    文章目录 1- 缓冲区介绍 (1)缓冲区以及作用 (2)缓冲区的类型 [1]行缓冲(验证) [2]全缓冲(验证) [3]无缓冲 2- 内核缓冲区与用户缓冲区 (1)用户进程和操作系统的关系 (2)用户 ...

  8. java 缓冲区溢出_缓冲区溢出详解

    1 缓冲区溢出原理 缓冲区是一块连续的计算机内存区域,可保存相同数据类型的多个实例.缓冲区可以是堆栈(自动变量).堆(动态内存)和静态数据区(全局或静态).在C/C++语言中,通常使用字符数组和mal ...

  9. 58. 缓冲区溢出篇——缓冲区溢出原理简介

    缓冲区溢出(Buffer Overflow)是计算机安全领域内既经典而又古老的话题.随着计算机系统安全性的加强,传统的缓冲区溢出攻击方式可能变得不再奏效,相应的介绍缓冲区溢出原理的资料也变得" ...

最新文章

  1. [Design] Strategy Pattern
  2. Swing中事件的三种处理方法
  3. yolov3yolov4yolov5比较
  4. 亿级 ELK 日志平台构建实践
  5. Python 生成器(yield)
  6. linux查看redis内存,Linux查看redis占用内存的方法
  7. python 动态编译代码,Python:在运行时动态创建函数
  8. python分布爬虫_13天搞定Python分布爬虫(第七天)(Scrapy)
  9. 日期和时间的正则表达式
  10. 怎么把PDF转换成Word格式?
  11. 【图像分割】基于马尔可夫随机场实现图像分割附matlab代码
  12. 暗原色先验单一输入图像去雾
  13. Anaconda版本与Python版本对应关系
  14. 龙芯2f笔记本安装debian错误--bzcat
  15. 梯度散度旋度常用基本关系
  16. 宝塔面板网站解决跨域问题
  17. 中国自动浓咖啡机市场趋势报告、技术动态创新及市场预测
  18. java玫瑰花代码_教小白用Html代码做玫瑰花
  19. 搜狐云景openapi初探
  20. 数学建模速成! 两小时零基础入门 MATLAB 教程(一)—— Matlab常用操作和基本语法

热门文章

  1. 03-树1 树的同构 (25 分)
  2. Java集合(六):专用集合和遗留类
  3. Java开发环境!java基础知识点总结
  4. go语言调用c 的头文件 so,Golang生成共享库(shared library)以及Golang生成C可调用的动态库.so和静态库.a...
  5. [New Portal]Windows Azure Web Site (4) Web Site Gallery
  6. CODE[VS] 1621 混合牛奶 USACO
  7. Codeforces Round #444 (Div. 2) C.Solution for Cube 模拟
  8. webpack基础使用Loader(三)
  9. 30.Android之百度地图简单学习
  10. git学习相关的博客地址