public final float maxCharsPerByte() {
return ((CharsetICU)(this.charset())).maxCharsPerByte;
}
+
+ /**
+ * Calculates the size of a buffer for conversion from Unicode to a charset.
+ * The calculated size is guaranteed to be sufficient for this conversion.
+ *
+ * It takes into account initial and final non-character bytes that are output
+ * by some converters.
+ * It does not take into account callbacks which output more than one charset
+ * character sequence per call, like escape callbacks.
+ * The default (substitution) callback only outputs one charset character sequence.
+ *
+ * @param length Number of chars to be converted.
+ * @param maxCharSize Return value from maxBytesPerChar for the converter
+ * that will be used.
+ * @return Size of a buffer that will be large enough to hold the output of bytes
+ *
+ * @draft ICU 49
+ */
+ public static int getMaxBytesForString(int length, int maxCharSize) {
+ return ((length + 10) * maxCharSize);
+ }
+
}
}
}
}
+
public void TestIsFixedWidth(){
String[] fixedWidth = {
"US-ASCII",
}
}
}
+
+ public void TestBytesLengthForString() {
+ CharsetProviderICU provider = new CharsetProviderICU();
+ String[] charsets = {
+ "windows-949-2000",
+ "ibm-1047_P100-1995,swaplfnl",
+ "ibm-930_P120-1999",
+ "ISCII,version=0",
+ "ISO_2022,locale=ko,version=0"
+ };
+
+ int[] expected = {
+ 40,
+ 20,
+ 60,
+ 80,
+ 60
+ };
+
+ int stringLength = 10;
+ int length;
+ int maxCharSize;
+
+ for (int i = 0; i < charsets.length; i++) {
+ maxCharSize = (int)provider.charsetForName(charsets[i]).newEncoder().maxBytesPerChar();
+ length = CharsetEncoderICU.getMaxBytesForString(stringLength, maxCharSize);
+
+ if (length != expected[i]) {
+ errln("For charset " + charsets[i] + " with string length " + stringLength + ", expected max byte length is " + expected[i] + " but got " + length);
+ }
+ }
+ }
}