바이트를 직접 ByteBuffer로 읽은 다음 원래 버퍼를 byte [] 배열로 다시 래핑하지 않고 디코드하여 메모리 할당을 최소화하려고합니다.Java direct ByteBuffer - 문자 디코드
따라서 힙에 새 배열을 할당 할 때 StandardCharsets.UTF_8.decode()
을 사용하지 마십시오.
바이트를 디코딩하는 방법에 붙어 있습니다. 문자열을 버퍼에 쓴 다음 id를 다시 읽는 다음 코드를 생각해보십시오.
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2 << 16);
byteBuffer.put("Hello Dávid".getBytes(StandardCharsets.UTF_8));
byteBuffer.flip();
CharBuffer charBuffer = byteBuffer.asCharBuffer();
for (int i = charBuffer.position(); i < charBuffer.length(); i++) {
System.out.println(charBuffer.get());
}
코드 출력 :
䡥汬漠
가 어떻게 버퍼를 디코딩 할 수 있습니까?
'ByteBuffer'가 아닌 'CharBuffer'를 뒤집어 야합니다. 'CharBuffer'는 반전 된 상태를 상속받습니다. – EJP