Java/Netty

[Netwok/Netty]02. ByteBuffer - ByteBuffer

양승길 2017. 10. 9. 18:54

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

(출처 : http://aoruqjfu.fun25.co.kr/index.php/post/561)

  1. Buffer
    사전적 의미는 완충을 말한다.
    컴퓨터 시스템의 어떤 장치에서 다른 장치로 전송을 할 때에, 
    일시적으로 그 데이터를 보관하는 일종의 메모리 영역이다.
    buffering이라 함은 buffer를 채우는 동작을 일컷는 말로서 유사어로 Queue가 있다.

  2. ByteBuffer
    Java NIO에서 제공되는 Buffer다.
    NIO의 Buffer는 다양한 자료형들을 관리하고있으며 내부의 배열상태를 관리한다.(ByteBuffer, CharBuffer, IntBuffer...)

  3. Buffer의 field
    • capacity
      Buffer에 저장할 수 있는 최대 크기. 한 번 지정하면 변경이 불가능하다.

    • position
      Buffer를 읽고 있는 현재 위치, Buffer를 쓰고 있는 현재 위치를 말한다.
      초기값은 0이고 읽고 쓸 때마다 증가된다. limit와 capacity보다 작거나 같다.

    • limit
      읽고 쓸 수 있는 공간의 최대치를 말한다. limit는 변경이가능하며 capacity보다는 크게 지정될 수 없다.

  4. Buffer의 method
    • public static ByteBuffer allocate(int capacity)
      capacity를 사용자가 직접 지정하고 position은 초기값으로 지정한다.
      limit는 capacity와 동일하게 입력된다. ByteBuffer를 새로 생성할 때 사용되며 JVM Heap에 생성한다.
      ByteBuffer의 하위 빈 HeapByteBuffer를 생성하는 셈이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Allocate {
 
    public static void main(String[] args) {
        
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
 
        // java.nio.HeapByteBuffer[pos=0 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
        // 11 Bytes Put
        byte[] source = "Hello world".getBytes(); 
        byteBuffer.put(source);
 
        // java.nio.HeapByteBuffer[pos=1 lim=11 cap=11]
        System.out.println(byteBuffer);
    }
 
}
cs



    • public static ByteBuffer allocateDireact(int capacity)
      capacity를 사용자가 직접 지정하고 position은 초기값으로 지정한다.
      limit는 capacity와 동일하게 입력된다. ByteBuffer를 새로 생성할 때 사용되며 JVM Heap에 생성한다. 
      ByteBuffer의 하위 빈 MappedByteBuffer의 하위 빈 DirectByteBuffer를 생성하는 셈이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class AllocateDirect {
 
    public static void main(String[] args) {
        
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(11);
        
        // java.nio.DirectByteBuffer[pos=0 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        // true
        System.out.println(byteBuffer.isDirect());
 
    }
 
}
cs



    • public static ByteBuffer wrap(byte[] array)
      배열을 이용해서 객체를 생성한다. 이는 HeapByteBuffer를 이용한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Wrap {
 
    public static void main(String[] args) {
        
        byte[] array = {123456789100};
        ByteBuffer byteBuffer = ByteBuffer.wrap(array);
 
        // java.nio.HeapByteBuffer[pos=0 lim=11 cap=11]
        System.out.print(byteBuffer);
        
        // false
        System.out.println(byteBuffer.isDirect());
        
    }
 
}
cs



    • public ByteBuffer put(byte b)
      현재 position을 기준으로 하여 byte를 write한다. 이후 position은 1 증가.
    • public byte get()
      현재 position을 기준으로 하여 byte를 read한다. 이후 position은 1 증가.
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
public class ReadWrite {
 
    public static void main(String[] args) {
        
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
 
        // java.nio.HeapByteBuffer[pos=0 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
        
        byteBuffer.put((byte1);
        System.out.println("write 1 Byte");
        
        // java.nio.HeapByteBuffer[pos=1 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
        
        byteBuffer.get();            
        System.out.println("read 1 Byte");
        
        // java.nio.HeapByteBuffer[pos=2 lim=11 cap=11]
        System.out.println(byteBuffer);
        
    }
 
}
cs


    • public final Buffer rewind()
      position을 0으로 이동
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
public class Rewind {
 
    public static void main(String[] args) {
        
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
 
        // java.nio.HeapByteBuffer[pos=0 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
        byteBuffer.put((byte10);
        System.out.println("write 1 Byte");
        // java.nio.HeapByteBuffer[pos=1 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
        byteBuffer.put((byte20);                            
        System.out.println("write 1 Byte");
        // java.nio.HeapByteBuffer[pos=2 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
        byteBuffer.rewind();
        System.out.println("rewind");
        // java.nio.HeapByteBuffer[pos=0 lim=11 cap=11]
        System.out.println(byteBuffer);
 
        
        byteBuffer.get();
        System.out.println("read 1 Byte");
        // java.nio.HeapByteBuffer[pos=1 lim=11 cap=11]
        System.out.println(byteBuffer);
        
        
    }
 
}
cs


    • public final Buffer flip()
      limit를 현재 position으로 두고, 현재 position을 0으로 변경
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Flip {
 
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(11);
 
        System.out.println(byteBuffer);
 
        byteBuffer.put((byte10);
        byteBuffer.put((byte20);
        byteBuffer.put((byte30);
        byteBuffer.put((byte40);
        System.out.println("write 4 Bytes");
        
        System.out.println(byteBuffer);
 
        System.out.println("flip");
        byteBuffer.flip();
 
        // 11 : 0 : 4
        System.out.println(byteBuffer);
    }
 
}
 
cs