ICU-8515 Add getMaxBytesForString to CharsetEncoderICU

X-SVN-Rev: 30237
This commit is contained in:
Michael Ow 2011-06-27 17:03:06 +00:00
parent dc38f079b5
commit cfe479cd47
2 changed files with 55 additions and 0 deletions

View file

@ -927,4 +927,26 @@ public abstract class CharsetEncoderICU extends CharsetEncoder {
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);
}
}

View file

@ -5613,6 +5613,7 @@ public class TestCharset extends TestFmwk {
}
}
}
public void TestIsFixedWidth(){
String[] fixedWidth = {
"US-ASCII",
@ -5645,4 +5646,36 @@ public class TestCharset extends TestFmwk {
}
}
}
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);
}
}
}
}