Netty 바이트 버퍼
Pure Java에서 Buffer와 그 하위 클래스들을 제공함에도 불구하고 네티에서는 자체적인 바이트 버퍼 API를 사용
네티 바이트 버퍼
특징
별도의 읽기 인덱스와 쓰기 인덱스
flip 메서드 없이 읽기 쓰기 가능
가변 바이트 버퍼
프레임워크 레벨의 바이트 버퍼 풀 제공
복합 버퍼
Java ByteBuffer와 호환됨
자료형에 따른 클래스를 제공하지 않고 별도의 method로 제공
생성
생성을 위해 ByteBufAllocator 인터페이스 사용 (구현체는 PooledByteBufAllocator)
Pooled의 경우
ByteBufAllocator.DEFAULT.heapBuffer()
,ByteBufAllocator.DEFAULT.directBuffer()
호출로 생성Unpooled의 경우
Unpooled.buffer()
,Unpooled.directBuffer()
호출로 생성
종류
Pooled
Unpooled
Direct Buffer
PooledDirectByteBuf
UnpooledDirectByteBuf
Heap Buffer
PooledHeapByteBuf
UnpooledHeapByteBuf
사용
읽기 쓰기
write{Type}
read{Type}
크기 조절
capacity
: 현재 저장된 바이트 크기보다 작게 조절하면 뒷부분이 잘림
부호 없는 값 읽기
getUnsigned{Type}
Endian 바꾸기
order
: 패러미터가 없으면 현재 Endian 반환. ByteOrder 클래스의 상수값 사용
Java ByteBuffer 바꾸기
nioBuffer
: 내부 buffer 배열이 공유됨
풀링
각 ByteBuf는 각자의 reference counting을 가지고 있어 객체의 재활용이 가능하며 GC 횟수 감소
보통 Direct Allocating, Deallocating 비용이 비싸기 때문에 사용됨
retain
: 자체적인 reference counting 증가release
: 자체적인 reference counting 감소. 만약에 count가 0이라면 객체를 할당 해제하거나 pool로 되돌려줌. 이 작업은 객체에 마지막으로 접근한 지점에서 수행ChannelPipeline 내부에서 사용되는 ByteBuf는 몇가지 특징을 더 가지고 있음
inbound handler에서 사용되는 ByteBuf는 자동으로 release 하지 않음. 직접 수행해야함
outbound handler에서 사용되는 ByteBuf는 write 한 이후에 자동적으로 release 수행함.
Last updated