]> granicus.if.org Git - icu/commitdiff
ICU-8515 Add getMaxBytesForString to CharsetEncoderICU
authorMichael Ow <mow@svn.icu-project.org>
Mon, 27 Jun 2011 17:03:06 +0000 (17:03 +0000)
committerMichael Ow <mow@svn.icu-project.org>
Mon, 27 Jun 2011 17:03:06 +0000 (17:03 +0000)
X-SVN-Rev: 30237

icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetEncoderICU.java
icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java

index 5bd69a70ab140f518e9791f13a40382328ada090..24efe73587773d0d966e9cc1ef3fb5acc3b44ef4 100644 (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);
+    }
+
 }
index 6bbef736610ca8930d2df864a9412d1db7e63c71..a0388b79bea4e9ce6dc39c0c34fb2c60ceb4a7be 100644 (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);
+            }
+        }
+    }
 }