diff options
| author | Elliott Hughes <enh@google.com> | 2012-07-30 13:10:08 -0700 |
|---|---|---|
| committer | Android Git Automerger <android-git-automerger@android.com> | 2012-07-30 13:10:08 -0700 |
| commit | 1a45a21fa89c42f499bb765c29dea7a85ba157f5 (patch) | |
| tree | fe6c0dd70a2100db0838feaf2c7152d562ef353c | |
| parent | 8962582befb42ab2cfe642ab107d0694f844f84c (diff) | |
| parent | 0b8dc2e15ca1a6d6c0b541476ae270a9a9d4e7ba (diff) | |
am 0b8dc2e1: Merge "De-pessimize the String(byte[], int, int) constructor."
* commit '0b8dc2e15ca1a6d6c0b541476ae270a9a9d4e7ba':
De-pessimize the String(byte[], int, int) constructor.
| -rw-r--r-- | luni/src/main/java/java/lang/String.java | 12 | ||||
| -rw-r--r-- | luni/src/test/java/libcore/java/lang/StringTest.java | 22 |
2 files changed, 19 insertions, 15 deletions
diff --git a/luni/src/main/java/java/lang/String.java b/luni/src/main/java/java/lang/String.java index efd4210087..224a268c97 100644 --- a/luni/src/main/java/java/lang/String.java +++ b/luni/src/main/java/java/lang/String.java @@ -168,17 +168,7 @@ public final class String implements Serializable, Comparable<String>, CharSeque * if {@code byteCount < 0 || offset < 0 || offset + byteCount > data.length}. */ public String(byte[] data, int offset, int byteCount) { - if ((offset | byteCount) < 0 || byteCount > data.length - offset) { - throw failedBoundsCheck(data.length, offset, byteCount); - } - CharBuffer cb = Charset.defaultCharset().decode(ByteBuffer.wrap(data, offset, byteCount)); - this.count = cb.length(); - this.offset = 0; - if (count > 0) { - value = cb.array(); - } else { - value = EmptyArray.CHAR; - } + this(data, offset, byteCount, Charset.defaultCharset()); } /** diff --git a/luni/src/test/java/libcore/java/lang/StringTest.java b/luni/src/test/java/libcore/java/lang/StringTest.java index 42a7aada76..99dba49f2d 100644 --- a/luni/src/test/java/libcore/java/lang/StringTest.java +++ b/luni/src/test/java/libcore/java/lang/StringTest.java @@ -91,10 +91,24 @@ public class StringTest extends TestCase { } } - public void testStringFromCharset() { - Charset cs = Charset.forName("UTF-8"); - byte[] bytes = new byte[] {(byte) 'h', (byte) 'i'}; - assertEquals("hi", new String(bytes, cs)); + public void testString_BII() throws Exception { + byte[] bytes = "xa\u0666bx".getBytes("UTF-8"); + assertEquals("a\u0666b", new String(bytes, 1, bytes.length - 2)); + } + + public void testString_BIIString() throws Exception { + byte[] bytes = "xa\u0666bx".getBytes("UTF-8"); + assertEquals("a\u0666b", new String(bytes, 1, bytes.length - 2, "UTF-8")); + } + + public void testString_BIICharset() throws Exception { + byte[] bytes = "xa\u0666bx".getBytes("UTF-8"); + assertEquals("a\u0666b", new String(bytes, 1, bytes.length - 2, Charset.forName("UTF-8"))); + } + + public void testString_BCharset() throws Exception { + byte[] bytes = "a\u0666b".getBytes("UTF-8"); + assertEquals("a\u0666b", new String(bytes, Charset.forName("UTF-8"))); } public void testStringFromCharset_MaliciousCharset() { |
