Want to truncate error string so it for sure fits into Oracle table column VARCHAR2(2000 BYTE)
Design forces:
The main goal is to fit to the table column.
90-95% of string text is exception message and stacktraces. But it could contain some customer name with french, turkish characters which I am willing to disregard and see as ? or whatever.
I want code to be dead simple. database encoding can change. Chinese characters can be introduced but I want code to work anyway.
Should be "dead simple" but it got me pondering for a while.
What are suggestions?
Probably the best options is to convert to ascii. But I came up variant which is not nice but probably works.
public static String trimStringToBytes(StringBuilder builder, int maximumBytes)
{
String truncatedString = builder.length() > maximumBytes ? builder.substring(0, maximumBytes) : builder.toString();
byte[] bytes;
String asciiCharsetName = "US-ASCII";
try
{
bytes = truncatedString.getBytes(asciiCharsetName);
}
catch (UnsupportedEncodingException e)
{
//not really possible as JVM should support always US-ASCII but anyway
int worstCaseScenarioBytesPerCharacter = 4;
bytes = truncatedString.substring(0, truncatedString.length() / worstCaseScenarioBytesPerCharacter).getBytes();
}
return new String(bytes, 0, bytes.length > maximumBytes ? maximumBytes : bytes.length);
}