diff options
-rw-r--r-- | luni/src/main/java/java/nio/charset/CharsetEncoder.java | 31 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/nio/charset/CharsetEncoderTest.java | 15 |
2 files changed, 34 insertions, 12 deletions
diff --git a/luni/src/main/java/java/nio/charset/CharsetEncoder.java b/luni/src/main/java/java/nio/charset/CharsetEncoder.java index 4415498..9d53328 100644 --- a/luni/src/main/java/java/nio/charset/CharsetEncoder.java +++ b/luni/src/main/java/java/nio/charset/CharsetEncoder.java @@ -442,26 +442,33 @@ public abstract class CharsetEncoder { * <p> * This method will call {@link #implFlush(ByteBuffer) implFlush}. Some * encoders may need to write some bytes to the output buffer when they have - * read all input characters, subclasses can overridden - * {@link #implFlush(ByteBuffer) implFlush} to perform writing action. + * read all input characters. Subclasses can override + * {@link #implFlush(ByteBuffer) implFlush} to perform any writes that are + * required at the end of the output sequence, such as footers and other + * metadata. * <p> - * The maximum number of written bytes won't larger than - * {@link ByteBuffer#remaining() out.remaining()}. If some encoder wants to + * The maximum number of written bytes won't be larger than + * {@link ByteBuffer#remaining() out.remaining()}. If the encoder wants to * write more bytes than the output buffer's available remaining space, then - * <code>CoderResult.OVERFLOW</code> will be returned, and this method - * must be called again with a byte buffer that has free space. Otherwise - * this method will return <code>CoderResult.UNDERFLOW</code>, which - * means one encoding process has been completed successfully. + * it will return {@code CoderResult.OVERFLOW}. This method must then be + * called again with a byte buffer that has free space. + * <p> + * If the encoder was asked to flush its output when its input is incomplete, + * (because it ends with an unpaired surrogate, say) it may return + * {@code CodeResult.MALFORMED}. + * <p> + * In all other cases the encoder will return {@code CoderResult.UNDERFLOW}, + * which signifies that all the input so far has been successfully encoded. * <p> * During the flush, the output buffer's position will be changed * accordingly, while its mark and limit will be intact. * <p> * This method is a no-op if the encoder has already been flushed. * - * @param out - * the given output buffer. - * @return <code>CoderResult.UNDERFLOW</code> or - * <code>CoderResult.OVERFLOW</code>. + * @param out the given output buffer. + * @return {@code CoderResult.UNDERFLOW} or + * {@code CoderResult.OVERFLOW} or + * {@code CoderResult.MALFORMED} * @throws IllegalStateException * if this encoder isn't already flushed or at end of input. */ diff --git a/luni/src/test/java/libcore/java/nio/charset/CharsetEncoderTest.java b/luni/src/test/java/libcore/java/nio/charset/CharsetEncoderTest.java index 13a897a..e9ab8ae 100644 --- a/luni/src/test/java/libcore/java/nio/charset/CharsetEncoderTest.java +++ b/luni/src/test/java/libcore/java/nio/charset/CharsetEncoderTest.java @@ -23,6 +23,7 @@ import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetDecoder; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; +import java.nio.charset.StandardCharsets; import java.util.Arrays; public class CharsetEncoderTest extends junit.framework.TestCase { @@ -215,4 +216,18 @@ public class CharsetEncoderTest extends junit.framework.TestCase { assertEquals(0x00, bb.get(2)); assertEquals(0x00, bb.get(3)); } + + // http://b/19185235 + public void testFlushWithIncompleteInput() { + CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder(); + ByteBuffer output = ByteBuffer.allocate(10); + CoderResult result = encoder.encode(CharBuffer.wrap("\ud800"), output, + true /* endOfInput */); + assertTrue(result.isUnderflow()); + + result = encoder.flush(output); + assertTrue(result.isMalformed()); + assertEquals(1, result.length()); + assertEquals(0, output.position()); + } } |