bytes.buffer详解

bytes.buffer定义如下:

// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.

type Buffer struct {
	buf      []byte // contents are the bytes buf[off : len(buf)]
	off      int    // read at &buf[off], write at &buf[len(buf)]
	lastRead readOp // last read operation, so that Unread* can work correctly.
}

Buffer结构体,内部维护一个[]byte类型的slice,用off标记读到的位置,
slice的长度就是写的位置,lastRead用于标记上次是否是读操作,用于读操作的回退。

主要方法:

  • Bytes() []byte
    返回全部未读的内容,格式为[]byte,即是内部buf切片切出来的切片,
    因此有内存泄漏的风险,并且如果后续又操作了Read()、Write()或者Reset(),
    这个返回的结果会被影响到

  • String() string
    返回未读内容的字符串形式,不会有内存泄漏的风险

  • empty() bool
    判断是否有未读的元素

  • Len() int
    未读的长度

  • Cap() int
    底层slice的容量

  • Truncate(n int)
    对未读部分进行截取,只保留前n个字节

  • Reset()
    重置slice,容量不变,长度变为0

  • tryGrowByReslice(n int) (int, bool)
    判断写入n个字节时是否需要扩容,返回写入的index,并且可能修改buf的长度

  • grow(n int) int
    扩充buf的空间以之能容纳新增的n个字节, 其内部会将未读的部分移动到buf开头
    因此返回写入的index,就是扩容前内部存储的有效字节个数

  • Grow(n int)
    供外部调用的扩容方法,内部会修改buf的长度

  • Write(p []byte) (n int, err error)
    将p这个slice的内容写入到buf中
    返回成功写入的数量

  • WriteString(s string)
    将s这个string写入buf中,返回成功写入的数量

  • ReadFrom(r io.Reader) (n int64, err error)
    从r中读取字节,一直到到EOF,写入到buf中

  • makeSlice(n int) []byte
    新生成一个n长度的slice

  • WriteTo(w io.Writer) (n int64, err error)
    将buf内容读取到w中

  • WriteByte(c byte) error
    将c写入buf中

  • WriteRune(c rune) (n int, err error)
    将rune写入buf中

  • Read(p []byte) (n int, err error)
    读取len§的长度到p中

  • Next(n int) []byte
    从buf中读取n个字节

  • ReadByte() (byte, error)
    从buf中读取一个字节

  • ReadRune() (r rune, size int, err error)
    从buf中读取一个rune

  • UnreadRune() error
    从buf中回退一个rune

  • UnreadByte() error
    从buf中回退一个byte

  • ReadBytes(delim byte) (line []byte, err error)
    读取buf,直到碰到delim这个byte,返回一个新的slice用来包裹读到的内容

  • readSlice(delim) (line []byte, err error)
    同ReadBytes类似,但不是新的slice,而是原buf的切片

  • ReadString(delim byte) (line string, err error)
    同ReadBytes,但返回string

总结:
Buffer结构体有Read和Write方法,实现了io.Reader和io.Writer接口。

  • 作为一个缓存,往其中写的方法有Write、WriteString、WriteByte、WriteRune和ReadFrom方法,其数据来源分别是[]byte、string、byte、Rune和io.Reader。
  • 从其中读的方法有Bytes、String、Read、ReadByte、ReadRune和WriteTo,其中有的返回读到的数据,有的将读到的数据保存到函数传递的作为存储的参数中。
  • 还有UnreadRune和UnreadByte用于回退读到的内容, ReadBytes、readSlice、ReadString用于指定读取到某个指定的byte。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ByteBuf 是 Netty 框架中的一个重要概念,它是一个字节容器,可以方便地进行字节的读写操作。下面就是 ByteBuf 的用法详解。 1. 创建 ByteBuf 创建 ByteBuf 的方式有两种,一种是使用 Unpooled 工具类创建,另一种是使用 ByteBufAllocator 工具类创建。 使用 Unpooled 工具类创建: ```java // 创建一个容量为10个字节的 ByteBuf ByteBuf buf = Unpooled.buffer(10); ``` 使用 ByteBufAllocator 工具类创建: ```java // 获取 ByteBufAllocator 实例 ByteBufAllocator allocator = ByteBufAllocator.DEFAULT; // 创建一个容量为10个字节的 ByteBuf ByteBuf buf = allocator.buffer(10); ``` 2. 写入数据 写入数据可以使用 ByteBuf 的 write 方法,它有很多重载方法,可以写入不同类型的数据。 ```java // 写入一个字节 byte b = 1; buf.writeByte(b); // 写入一个整数 int i = 100; buf.writeInt(i); // 写入一个字符串 String str = "hello world"; buf.writeBytes(str.getBytes()); ``` 3. 读取数据 读取数据可以使用 ByteBuf 的 read 方法,它也有很多重载方法,可以读取不同类型的数据。 ```java // 读取一个字节 byte b = buf.readByte(); // 读取一个整数 int i = buf.readInt(); // 读取一个字符串 byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); String str = new String(bytes); ``` 4. 获取 ByteBuf 的信息 可以使用 ByteBuf 的一些方法获取它的一些信息,比如容量、读索引、写索引等。 ```java // 获取容量 int capacity = buf.capacity(); // 获取可读字节数 int readableBytes = buf.readableBytes(); // 获取可写字节数 int writableBytes = buf.writableBytes(); // 获取读索引 int readerIndex = buf.readerIndex(); // 获取写索引 int writerIndex = buf.writerIndex(); ``` 5. 派生 ByteBuf 可以使用 ByteBuf 的 slice、duplicate、readSlice 等方法来派生一个新的 ByteBuf,这个新的 ByteBuf 与原来的 ByteBuf 共享底层的字节数组,但是它们的读写索引是独立的。 ```java // 派生一个新的 ByteBuf,共享底层的字节数组 ByteBuf slice = buf.slice(); // 派生一个新的 ByteBuf,共享底层的字节数组 ByteBuf duplicate = buf.duplicate(); // 派生一个新的 ByteBuf,共享底层的字节数组,但是它的读写索引是独立的 ByteBuf slice = buf.readSlice(5); ``` 6. 释放 ByteBuf 在使用完 ByteBuf 后,需要将它释放,否则会出现内存泄漏问题。可以使用 ByteBuf 的 release 方法来释放它。 ```java buf.release(); ``` 以上就是 ByteBuf 的用法详解,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值