Want to truncate the error string so that it exactly matches the column in the Oracle VARCHAR2 table (2000 BYTE)
Constructive forces:
The main goal is to fit the table column.
90-95% of the string text is an exception and stacktraces message. But it may contain some kind of client name with French, Turkish characters, which I am ready to ignore and see how? or something else.
I want the code to be dead simple. database coding may change. You can enter hieroglyphs, but I want the code to work anyway.
It should be "dead simple", but it made me think for a while.
What are the suggestions?
Probably the best options are to convert to ascii. But I came up with an option that is not very pleasant, 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)
{
int worstCaseScenarioBytesPerCharacter = 4;
bytes = truncatedString.substring(0, truncatedString.length() / worstCaseScenarioBytesPerCharacter).getBytes();
}
return new String(bytes, 0, bytes.length > maximumBytes ? maximumBytes : bytes.length);
}
user193689
source
share