Java/Netty

[Netwok/Netty]03. ByteBuffer VS ByteBuf - ByteBuf

양승길 2017. 10. 14. 20:21

[Netwok/Netty]03. ByteBuffer VS ByteBuf - ByteBuf

(출처 : https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html)


1. 차이점

ByteBuffer는 capacity, limit, potion로 데이터를 읽고 썼다.
읽든 쓰든 position이 변경되고, flip을 통해 읽기와 쓰기를 하는데 있어 계산을 하면서 개발해야 한다. 
또한 capacity는 한번 지정하면 변경이 불가하기에 새로 객체를 생성해야된다.
그러니까 setCapacity(int capacity)같은 method가 없다는 의미.
게다가 capacity를 넘는 데이터를 입력하면 BufferOverflowException발생한다.

Bytebuf는 capacity, read index, write index만을 두고 있기 때문에
따로 flip이라는 동작을 하지 않아도 읽고 쓰는 작업에 편의성이 있다.
capacity를 변경하는 method를 지원한다. capacity를 넘는 데이터를 입력하면 2 제곱 단위로 증가후에 입력한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class DiffernceBoth {
 
    public static void main(String[] args) {
        
        ByteBuffer byteBuffer = ByteBuffer.allocate(13);
        // java.nio.HeapByteBuffer[pos=0 lim=13 cap=13]
        System.out.println(byteBuffer);
        
        byteBuffer = ByteBuffer.allocate(14);
        // java.nio.HeapByteBuffer[pos=0 lim=13 cap=14]
        System.out.println(byteBuffer);
        
//        BufferOverflowException
//        byteBuffer.put("012345678912341".getBytes());
        
        
        ByteBuf byteBuf = Unpooled.buffer(13);
        // (ridx: 0, widx: 0, cap: 13)
        System.out.println(byteBuf);
        
        byteBuf.capacity(14);
        // (ridx: 0, widx: 0, cap: 14)
        System.out.println(byteBuf);
        
        // write 59 bytes
        byteBuf.writeBytes("This is byte array longer than previous capacity of byteBuf".getBytes());
        // (ridx: 0, widx: 59, cap: 64)
        System.out.println(byteBuf);
        
        byteBuf.capacity(14);
        // This is byte a
        System.out.println(byteBuf.toString(Charset.defaultCharset()));
        // (ridx: 0, widx: 14, cap: 14)
        System.out.println(byteBuf);
        
        
    }
 
}
cs


2. 객체생성
    
Unpooled.buffer(int initialCapacity)라는 method를 통해 생성하길 권장한다.
    실제로 생성자는 있지만 override해야 되는 method들이 많기 때문에 비효율적이다.
    (예제 소스를 쓰자니 overrided method가 워낙 많아서 생략)


3. read, write, clear
    ByteBuffer와 다른점은 없으나 읽을수 있는 byte와 쓸 수 있는 byte를 명확하게 보여주기 때문에 편의성이 있다.
    그리고 안에 내용들을 모두 초기화 하는 clear가 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class ReadWrite {
 
    public static void main(String[] args) {
        
        ByteBuf byteBuf = Unpooled.buffer(12);
        // read : 0, write : 12
        System.out.print("read : " + byteBuf.readableBytes());
        System.out.println(", write : " + byteBuf.writableBytes());
 
        System.out.println("write 11 bytes");
        byteBuf.writeBytes("Hello world".getBytes());
        // read : 11, write : 1
        System.out.print("read : " + byteBuf.readableBytes());
        System.out.println(", write : " + byteBuf.writableBytes());
        
        System.out.println("read 4 bytes");
        byteBuf.readInt();
        // read : 7, write : 1
        System.out.print("read : " + byteBuf.readableBytes());
        System.out.println(", write : " +byteBuf.writableBytes());
        
        System.out.println("clear");
        byteBuf.clear();
        // read : 0, write : 12
        System.out.print("read : " + byteBuf.readableBytes());
        System.out.println(", write : " +byteBuf.writableBytes());        
        
        
    }
 
}
cs