안드로이드 개발 질문/답변
(글 수 45,052)
아직도 ByteBuffer에서 좀 해매고 있는데요.
- 1번문장 -
buff = ByteBuffer.allocateDirect(1024);
int realBuffLen = sCh.read(buff);
buff.flip();
System.out.println("len:"+realBuffLen);
byte[] msg = buff.array();
String result = new String(msg, 0, realBuffLen);
buff.clear();
- 2번문장 -
buff = ByteBuffer.allocate(1024);
int realBuffLen = sCh.read(buff);
buff.flip();
System.out.println("len:"+realBuffLen);
byte[] msg = buff.array();
String result = new String(msg, 0, realBuffLen);
buff.clear();
1번 문장과 2번 문장의 차이는 allocate와 allocateDirect의 차이인데요.
allocateDirect 사용시 buff.array()부분에서 에러가 나던데요..
array()함수나 get()을 이용해서 byteBuffer인 buff변수에서 byte배열을 통체로 가지고 오고 싶은데.
방법을 못찾겠어서 이렇게 질문을 올립니다.




자답입니다.
allocateDirect의 경우 커널단 버퍼생성이기에 전체 Array를 가져오는 매소드인 get(byte[] src), array()함수를 지원하지 않는듯합니다.
가져올려면 get(byte[] src, offset, length)함수 정도로 가져와야되는데 이것도 커널단 접근이여서 속도는 빠르지만 아주 극악의 확률로
에러가 존재하였습니다.
연관 검색어 : SocketChannel, NIO, Channel, read(), write(), array(), get(), UnsupportedOperationException
참고 : http://kldp.org/node/43477