Skip to content

Instantly share code, notes, and snippets.

@tmd45
Created June 1, 2012 13:33
Show Gist options
  • Save tmd45/2852172 to your computer and use it in GitHub Desktop.
Save tmd45/2852172 to your computer and use it in GitHub Desktop.
Cutting by byte, MS932 character. After, insert this String to Oracle DB by jdbc, whereupon get ORA-12899
/** 分割開始インデックス */
static final int DIVISION_BYTE_START_INDEX = 0;
/** 分割後文字長 */
static final int DIVISION_BYTE_LENGTH_ADDR = 48;
/**
* 引数の文字列を[分割後文字長(byte)]以下にカットする。<br>
* 扱う文字コードは MS932 とする。
*
* @param str 文字列(MS932)
* @return String [分割後文字長(byte)]以下の文字列
*/
String getDivideStringByByte(String str) {
if(str == null) {
return null;
}
String result = "";
try {
// バイト配列化
byte[] byteAddr = address.getBytes("MS932");
if(byteAddr.length > DIVISION_BYTE_START_INDEX) {
// byte文字列が分割開始インデックス以上の長さである
if(byteAddr.length > DIVISION_BYTE_START_INDEX + DIVISION_BYTE_LENGTH_ADDR) {
// byte文字列が分割したい文字長以上の長さである
result = new String(byteAddr,
DIVISION_BYTE_START_INDEX,
DIVISION_BYTE_LENGTH_ADDR,
"MS932").trim();
} else {
// 必要文字長に満たない場合
result = new String(byteAddr,
DIVISION_BYTE_START_INDEX,
byteAddr.length - DIVISION_BYTE_START_INDEX, // 長さ:文字長 - 分割(開始)位置
"MS932").trim();
}
// いわゆる“おまじない”コード(恥)
// result = new String(result.getBytes("MS932"));
} catch(UnsupportedEncodingException ue) {
throw new IllegalArgumentException(ue);
}
return result;
}
}
@tmd45
Copy link
Author

tmd45 commented Jun 1, 2012

JDK 1.5.0_12

@tmd45
Copy link
Author

tmd45 commented Jun 2, 2012

Qiita に突っ込んできた。 http://qiita.com/items/fa7e4fdbd4f7ad9a29ae

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment