Skip to content

Instantly share code, notes, and snippets.

@GloryAlex
Created June 26, 2021 14:17
Show Gist options
  • Save GloryAlex/a30e02e116d5ebfff230a293d95cffdb to your computer and use it in GitHub Desktop.
Save GloryAlex/a30e02e116d5ebfff230a293d95cffdb to your computer and use it in GitHub Desktop.
Bytebuf有几种不同的操作模式
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