encoding 별로 JAVA 문자열 byte단위로 자르기
public String[] parseStringByBytes(String raw, int len, String encoding) {
if (raw == null)
return null;
String[] ary = null;
try {
// raw 의 byte
byte[] rawBytes = raw.getBytes(encoding);
int rawLength = rawBytes.length;
int index = 0;
int minus_byte_num = 0;
int offset = 0;
int hangul_byte_num = encoding.equals("UTF-8") ? 3 : 2;
if (rawLength > len) {
int aryLength = (rawLength / len) + (rawLength % len != 0 ? 1 : 0);
ary = new String[aryLength];
for (int i = 0; i < aryLength; i++) {
minus_byte_num = 0;
offset = len;
if (index + offset > rawBytes.length) {
offset = rawBytes.length - index;
}
for (int j = 0; j < offset; j++) {
if (((int) rawBytes[index + j] & 0x80) != 0) {
minus_byte_num++;
}
}
if (minus_byte_num % hangul_byte_num != 0) {
offset -= minus_byte_num % hangul_byte_num;
}
ary[i] = new String(rawBytes, index, offset, encoding);
index += offset;
}
} else {
ary = new String[] { raw };
}
} catch (Exception e) {
}
return ary;
}
출처 : http://blog.naver.com/PostView.nhn?blogId=nackhwa7&logNo=140103352320
'Java' 카테고리의 다른 글
HttpsURLConnection 으로 Http통신할 때 오류 메세지 잡아내기 (0) | 2018.05.30 |
---|---|
클래스로더 오류 관련 (0) | 2018.02.19 |