0基础学习网站建设,企业网站有哪些内容,自己的品牌怎么推广加盟,wordpress log文件大小介绍
Netty的ByteBuf数据位置索引是0开始的。 可以用ByteBuf的getByte(int index)方法从指定位置读出一字节#xff0c;这个操作不会改变ByteBuf的readerIndex 或者 writerIndex 的位置。如果index小于0#xff0c;或者index 1大于ByteBuf的容量#xff0c;就会抛出IndexO…介绍
Netty的ByteBuf数据位置索引是0开始的。 可以用ByteBuf的getByte(int index)方法从指定位置读出一字节这个操作不会改变ByteBuf的readerIndex 或者 writerIndex 的位置。如果index小于0或者index 1大于ByteBuf的容量就会抛出IndexOutOfBoundsException异常。 可以用ByteBuf的readByte()方法从当前readerIndex 读出一字节并且将readerIndex 的值增加1。如果ByteBuf的readableBytes的值小于1就会抛出IndexOutOfBoundsException异常。 可以使用ByteBuf的isReadable()方法判断是否有可读的数据。当(this.writerIndex - this.readerIndex) 的值大于0isReadable()返回true。
writeShort(int value)函数在ByteBuf的当前writerIndex位置开始写入一个16位的整数并且将writerIndex增加2。因为输入参数是int型占4个字节高位的16位被丢弃。 getShort(int index)函数从ByteBuf的绝对位置index开始读取1个16位的整数。这个方法不改变ByteBuf的readerIndex 和 writerIndex。
代码举例
用getByte(int index)方法正常读取数据
代码
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;public class Test {public static void main(String[] args) { ByteBuf buf Unpooled.buffer(1); for (int i 0; i 1; i) {buf.writeByte(0x68);}for (int i 0; i buf.capacity(); i) {System.out.println(buf.getByte(i));}}}运行输出
用getByte(int index)方法不正常读取数据抛出异常
代码
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;public class Test {public static void main(String[] args) { ByteBuf buf Unpooled.buffer(1); for (int i 0; i 1; i) {buf.writeByte(0x68);}for (int i 0; i buf.capacity() 1; i) {System.out.println(buf.getByte(i));}}}运行输出
用readByte()方法正常读取数据–用readableBytes()判断可读的数据字节数
代码
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;public class Test {public static void main(String[] args) { ByteBuf buf Unpooled.buffer(2); for (int i 0; i 2; i) {buf.writeByte(0x68);} while (buf.readableBytes() 0) {System.out.println(buf.readByte());}}}运行输出
用readByte()方法正常读取数据–用isReadable()判断是否有可读的数据
代码
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;public class Test {public static void main(String[] args) { ByteBuf buf Unpooled.buffer(2); for (int i 0; i 2; i) {buf.writeByte(0x68);} while (buf.isReadable()) {System.out.println(buf.readByte());}}}运行输出
用getBytes(int index, byte[] dst)读取部分内容到数组中
getBytes(int index, byte[] dst)表示从ByteBuf的绝对位置index开始拷贝部分内容到目的字节数组中。这个操作不改变ByteBuf的readerIndex 和 writerIndex。
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;public class Test {public static void main(String[] args) { ByteBuf buf Unpooled.buffer(4);for (int i 0; i buf.capacity(); i) {buf.writeByte(0x68);}byte[] data new byte[2];// 从buf的第二个字节开始拷贝2个字节的内容到data数组中buf.getBytes(1, data);System.out.println(数组data的内容);for (int i 0; i data.length; i) {System.out.println(data[i]);}System.out.println(buf的内容);for (int i 0; i buf.capacity(); i) {System.out.println(buf.getByte(i));}}}运行结果
用writeShort(int value)写入一个short型整数用getShort(int index)读取一个short型整数
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;public class Test {public static void main(String[] args) { ByteBuf buf Unpooled.buffer(4);buf.writeShort(2048);// 从buf中读取一个short型整数并打印出来看是否和传入的相同System.out.println(从ByteBuf中读取的short型整数为 buf.getShort(0));}}运行结果