buffer缓冲区

buffer缓冲区

  • buffer是固定大小的内存区域,可以将buffer类比坐数组,每个元素代表一个数据字节

buffer的数据形式

  • 保存在buffer中的数据是以十六进制的形式展示,但是在计算机底层的处理还是二进制

为什么需要buffer

  • 帮助开发者处理二进制数据,传统上数组只处理字符串而不是二进制数据
  • 当对文件的IO操作时,输出的速度大于写入的速度,则会将数据存入buffer中

buffer的使用

// 1、将字符串转换成buffer
const buf1 = Buffer.from('哈哈哈')
console.log(buf1)


// 2、创建一个指定大小的buffer,无法调整大小
const buf2 = Buffer.alloc(10)
console.log(buf2)


// 在buffer池中使用原有的buffer,可能含有敏感数据,但是性能会更好
const buf3 = Buffer.allocUnsafe(10)
console.log(buf3)

// 将buffer转换成字符串
const str = buf1.toString()
console.log(str)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请帮我为以下代码添加注释,并排版package T14; //Buffer.java public class Buffer { private int buffer = -1; // buffer缓冲区被producer 和 consumer 线程共享 private int occupiedBufferCount = 0; // 控制缓冲区buffers读写的条件边量 public synchronized void set( int value ) //place value into buffer { String name = Thread.currentThread().getName();//get name of thread that called this method // while there are no empty locations, place thread in waiting state while ( occupiedBufferCount == 1 ) { // output thread information and buffer information, then wait try { //System.err.println("Buffer full. "+ name + " waits." ); wait(); } // if waiting thread interrupted, print stack trace catch ( InterruptedException exception ) { exception.printStackTrace(); } } // end while buffer = value; // set new buffer value ++occupiedBufferCount; System.err.println(name + " writes " + buffer); notify(); // tell waiting thread to enter ready state } // end method set; releases lock on SynchronizedBuffer public synchronized int get() // return value from buffer { // for output purposes, get name of thread that called this method String name = Thread.currentThread().getName(); // while no data to read, place thread in waiting state while ( occupiedBufferCount == 0 ) { // output thread information and buffer information, then wait try { //System.err.println("Buffer empty. "+name + " waits." ); wait(); } catch ( InterruptedException exception ) { exception.printStackTrace(); } } // end while // indicate that producer can store another value , because consumer just retrieved buffer value --occupiedBufferCount; System.err.println( name + " reads " + buffer ); notify(); // tell waiting thread to become ready to execute return buffer; } // end method get; releases lock on SynchronizedBuffer }
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值