Created
June 26, 2021 14:17
-
-
Save GloryAlex/a30e02e116d5ebfff230a293d95cffdb to your computer and use it in GitHub Desktop.
Bytebuf有几种不同的操作模式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ByteBuf buf = ...// get ByteBuf | |
int length = buf.readableBytes();//get readable length | |
if(buf.hasArray()){ | |
//ByteBuf in heap | |
byte[] array = buf.array(); | |
int offset = buf.arrayOffset()+buf.readerIndex(); | |
handleArray(array,offset,length);//your method | |
}else{ | |
//ByteBuf in direct buffer | |
byte[] array=new byte[length]; | |
buf.getBytes(buf.readerIndex(),array);//copy it to new bytes array | |
handleArray(array,0,length);//your method | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment