Skip to content

Instantly share code, notes, and snippets.

@il-kyun
Last active October 6, 2021 19:35
Show Gist options
  • Save il-kyun/724b999c7495bec893445f79275f1b65 to your computer and use it in GitHub Desktop.
Save il-kyun/724b999c7495bec893445f79275f1b65 to your computer and use it in GitHub Desktop.
Java ByteBuffer vs Netty ByteBuf
import java.nio.ByteBuffer;
public class ByteBufferTest {
public static void main(String[] args) {
//byteBuffer 할당
ByteBuffer byteBuffer = ByteBuffer.allocate(11);
System.out.println("byteBuffer is direct buffer : " + byteBuffer.isDirect());
System.out.println("byteBuffer capacity is : " + byteBuffer.capacity());
System.out.println("byteBuffer initial value : " + byteBuffer);
//값 할당
byte[] bytes = "Netty".getBytes();
for (byte b : bytes) {
byteBuffer.put(b);
System.out.println("#byteBuffer value : " + byteBuffer);
}
//java.nio.BufferUnderflowException
//byteBuffer.get();
// position to zero
byteBuffer.rewind();
System.out.println((char) byteBuffer.get());
System.out.println("after get, byteBuffer value 0 : " + byteBuffer);
System.out.println((char) byteBuffer.get());
System.out.println("after get, byteBuffer value 1 : " + byteBuffer);
// limit -> position & position -> 0
byteBuffer.flip();
System.out.println(byteBuffer);
System.out.println((char) byteBuffer.get(1));
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class ByteBufTest {
public static void main(String[] args) {
// 4 types of buffers
ByteBuf heapBufferInPool = ByteBufAllocator.DEFAULT.heapBuffer(11);
ByteBuf directBufferInPool = ByteBufAllocator.DEFAULT.directBuffer(11);
ByteBuf heapBufferNotInPool = Unpooled.buffer(11);
ByteBuf directBufferNotInPool = Unpooled.directBuffer(11);
// how to manipulate buffer
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(11);
System.out.println(byteBuf.capacity());
System.out.println(byteBuf.isDirect());
byteBuf.writeInt(65537);
System.out.println("readable bytes : " + byteBuf.readableBytes());
System.out.println("writable bytes : " + byteBuf.writableBytes());
System.out.println("is readable : " + byteBuf.isReadable());
byteBuf.clear();
System.out.println("readable bytes : " + byteBuf.readableBytes());
System.out.println("writable bytes : " + byteBuf.writableBytes());
String sourceString = "hello netty";
byteBuf.writeBytes(sourceString.getBytes());
System.out.println("readable bytes : " + byteBuf.readableBytes());
System.out.println("writable bytes : " + byteBuf.writableBytes());
System.out.println(byteBuf.toString(Charset.defaultCharset()));
//change capacity
byteBuf.capacity(6);
System.out.println(byteBuf.toString(Charset.defaultCharset()));
byteBuf.capacity(13);
System.out.println(byteBuf.toString(Charset.defaultCharset()));
byteBuf.writeBytes("world".getBytes());
System.out.println(byteBuf.toString(Charset.defaultCharset()));
//convert byteBuffer to byteBuf
ByteBuffer byteBuffer = ByteBuffer.wrap("hi".getBytes());
ByteBuf buf = Unpooled.wrappedBuffer(byteBuffer);
System.out.println(buf.toString(Charset.defaultCharset()));
}
}
@xiaozhiliaoo
Copy link

what is different?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment