diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java index 576959afd..4d443b258 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java @@ -15,12 +15,10 @@ */ package org.teavm.classlib.java.lang; -import org.teavm.classlib.impl.charset.ByteBuffer; -import org.teavm.classlib.impl.charset.CharBuffer; -import org.teavm.classlib.impl.charset.Charset; -import org.teavm.classlib.impl.charset.UTF16Helper; +import org.teavm.classlib.impl.charset.*; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TUnsupportedEncodingException; +import org.teavm.classlib.java.util.TArrays; import org.teavm.javascript.ni.GeneratedBy; import org.teavm.javascript.ni.Rename; @@ -63,7 +61,7 @@ public class TString extends TObject implements TSerializable, TComparable result.length) { + result = TArrays.copyOf(result, result.length * 2); + } + for (int i = 0; i < dest.position(); ++i) { + result[resultLength++] = destArray[i]; + } + dest.rewind(0); + } + return TArrays.copyOf(result, resultLength); + } + @Override public int hashCode() { if (hashCode == 0) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java index 5b6f1f56f..5a074a5a6 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.java.util; +import org.teavm.classlib.java.lang.TMath; import org.teavm.classlib.java.lang.TObject; /** @@ -24,7 +25,16 @@ import org.teavm.classlib.java.lang.TObject; public class TArrays extends TObject { public static char[] copyOf(char[] array, int length) { char[] result = new char[length]; - int sz = Math.min(length, array.length); + int sz = TMath.min(length, array.length); + for (int i = 0; i < sz; ++i) { + result[i] = array[i]; + } + return result; + } + + public static byte[] copyOf(byte[] array, int length) { + byte[] result = new byte[length]; + int sz = TMath.min(length, array.length); for (int i = 0; i < sz; ++i) { result[i] = array[i]; } diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringTest.java index 351883f6a..1a32e15c7 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/StringTest.java @@ -218,4 +218,56 @@ public class StringTest { byte[] bytes = { 65, -62, -69, -32, -82, -69, -16, -66, -78, -69 }; assertEquals("A\u00BB\u0BBB\uD8BB\uDCBB", new String(bytes, "UTF-8")); } + + @Test + public void createFromLongUTF8ByteArray() throws UnsupportedEncodingException { + byte[] bytes = new byte[16384]; + for (int i = 0; i < bytes.length;) { + bytes[i++] = -16; + bytes[i++] = -66; + bytes[i++] = -78; + bytes[i++] = -69; + } + String str = new String(bytes, "UTF-8"); + assertEquals('\uD8BB', str.charAt(8190)); + assertEquals('\uDCBB', str.charAt(8191)); + } + + @Test + public void getByteArray() throws UnsupportedEncodingException { + byte[] bytes = "123".getBytes("UTF-8"); + assertEquals(49, bytes[0]); + assertEquals(50, bytes[1]); + assertEquals(51, bytes[2]); + assertEquals(3, bytes.length); + } + + @Test + public void getUTF8ByteArray() throws UnsupportedEncodingException { + byte[] bytes = "A\u00BB\u0BBB\uD8BB\uDCBB".getBytes("UTF-8"); + assertEquals(65, bytes[0]); + assertEquals(-62, bytes[1]); + assertEquals(-69, bytes[2]); + assertEquals(-32, bytes[3]); + assertEquals(-82, bytes[4]); + assertEquals(-69, bytes[5]); + assertEquals(-16, bytes[6]); + assertEquals(-66, bytes[7]); + assertEquals(-78, bytes[8]); + assertEquals(-69, bytes[9]); + } + + @Test + public void getUTF8ByteArrayOfLongString() throws UnsupportedEncodingException { + char[] chars = new char[8192]; + for (int i = 0; i < chars.length;) { + chars[i++] = '\uD8BB'; + chars[i++] = '\uDCBB'; + } + byte[] bytes = new String(chars).getBytes("UTF-8"); + assertEquals(-16, bytes[16380]); + assertEquals(-66, bytes[16381]); + assertEquals(-78, bytes[16382]); + assertEquals(-69, bytes[16383]); + } }