배운 것

byte[] 를 String으로 읽어들일 때

썬,더 호글 2016. 2. 1. 22:23
protected String getMessageBody(Message message) {
        // String messageBody = new String(message.getBody());
        // charset 이 지정 안되어 버그 가능성이 있는 코드.
        // getBody() 의 return type 이 무엇이냐에 따라 다르지만, 만약 byte[] 형태이고 내용이 json 형식이라면 아래처럼 권장
        return new String(message.getBody(), StandardCharsets.UTF_8);
    }



byte[] 를  String으로 읽어들이고 싶을 때, charset이 설정되지 않으면 버그 가능성이 있다고 한다. -by dex.