Skip to content

Commit d26aaca

Browse files
committed
Merge pull request #367 from msgpack/use-replace-mode-in-packer
Use REPLACE mode when converting java string into UTF8.
2 parents c9d07aa + 3e0a7c2 commit d26aaca

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.charset.CharacterCodingException;
2828
import java.nio.charset.CharsetEncoder;
2929
import java.nio.charset.CoderResult;
30+
import java.nio.charset.CodingErrorAction;
3031

3132
import static org.msgpack.core.MessagePack.Code.ARRAY16;
3233
import static org.msgpack.core.MessagePack.Code.ARRAY32;
@@ -447,7 +448,20 @@ private void packStringWithGetBytes(String s)
447448
private void prepareEncoder()
448449
{
449450
if (encoder == null) {
450-
this.encoder = MessagePack.UTF8.newEncoder();
451+
/**
452+
* Even if String object contains invalid UTF-8 characters, we should not throw any exception.
453+
*
454+
* The following exception has happened before:
455+
*
456+
* org.msgpack.core.MessageStringCodingException: java.nio.charset.MalformedInputException: Input length = 1
457+
* at org.msgpack.core.MessagePacker.encodeStringToBufferAt(MessagePacker.java:467) ~[msgpack-core-0.8.6.jar:na]
458+
* at org.msgpack.core.MessagePacker.packString(MessagePacker.java:535) ~[msgpack-core-0.8.6.jar:na]
459+
*
460+
* This happened on JVM 7. But no ideas how to reproduce.
461+
*/
462+
this.encoder = MessagePack.UTF8.newEncoder()
463+
.onMalformedInput(CodingErrorAction.REPLACE)
464+
.onUnmappableCharacter(CodingErrorAction.REPLACE);
451465
}
452466
encoder.reset();
453467
}

0 commit comments

Comments
 (0)