# HG changeset patch # User igerasim # Date 1411416094 -14400 # Node ID a99f747c8197a6de7157b763c331c621ac521334 # Parent 71c9aa685da57a3a700a067163d9817a35143494 8058875: CharsetEncoder.maxBytesPerChar() should return 4 for UTF-8 diff --git a/src/java.base/share/classes/sun/nio/cs/UTF_8.java b/src/java.base/share/classes/sun/nio/cs/UTF_8.java --- a/src/java.base/share/classes/sun/nio/cs/UTF_8.java +++ b/src/java.base/share/classes/sun/nio/cs/UTF_8.java @@ -555,7 +555,7 @@ implements ArrayEncoder { private Encoder(Charset cs) { - super(cs, 1.1f, 3.0f); + super(cs, 1.1f, 4.0f); } public boolean canEncode(char c) { diff --git a/test/java/nio/charset/Charset/UTF8Has4ByteChars.java b/test/java/nio/charset/Charset/UTF8Has4ByteChars.java new file mode 100644 --- /dev/null +++ b/test/java/nio/charset/Charset/UTF8Has4ByteChars.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8058875 + * @summary CharsetEncoder.maxBytesPerChar() should return 4 for UTF-8 + */ + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; +import java.nio.charset.CodingErrorAction; + +public class UTF8Has4ByteChars { + private static CharsetEncoder encoder; + private static int bufferSize; + + public static void main(String[] args) { + Charset cs = Charset.forName("UTF-8"); + encoder = cs.newEncoder() + .onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE); + + bufferSize = (int)encoder.maxBytesPerChar(); + + for (Integer i : new Integer[] {0x10000, 0x24B62, 0x10FFFF}) { + test(new String(new int[] {i.intValue()}, 0, 1)); + } + } + + private static void test(String s) { + CharBuffer cb = CharBuffer.wrap(s.toCharArray()); + encoder.encode(cb, ByteBuffer.wrap(new byte[bufferSize]), true); + if (cb.hasRemaining()) { + throw new RuntimeException("Buffer of size " + bufferSize + + " isn't enough to hold a char"); + } + } +}