Skip to content

Commit 1605bf4

Browse files
committed
Simplify string handling for Java 9+ compact strings
msgpack-jackson3 requires Java 17+, so char[]-backed strings (Java 8) are not a concern. Java 9+ compact strings make the old AsciiCharString optimization redundant for the char[] path. - Rename AsciiCharString -> RawUtf8String; extend its use to all raw UTF-8 byte[] inputs (not just ASCII), eliminating the areAllAsciiBytes scan and the wasteful decode+re-encode for non-ASCII content. - Simplify writeCharArrayTextValue: drop getBytesIfAscii scan and just use new String(char[], offset, len); packString handles encoding efficiently via compact strings. - Simplify writeRaw(String, int, int): drop getChars -> char[] detour, use substring directly. - Simplify writeString(Reader, int) len<0 path: drop StringBuilder -> char[] copy, use sb.toString() directly. - Remove unused imports and fields (Nullable, Charset, StandardCharsets, DEFAULT_CHARSET).
1 parent 2e90092 commit 1605bf4

1 file changed

Lines changed: 9 additions & 50 deletions

File tree

msgpack-jackson3/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import tools.jackson.core.io.IOContext;
3030
import org.msgpack.core.MessagePack;
3131
import org.msgpack.core.MessagePacker;
32-
import org.msgpack.core.annotations.Nullable;
3332
import org.msgpack.core.buffer.MessageBufferOutput;
3433
import org.msgpack.core.buffer.OutputStreamBufferOutput;
3534

@@ -40,15 +39,12 @@
4039
import java.math.BigDecimal;
4140
import java.math.BigInteger;
4241
import java.nio.ByteBuffer;
43-
import java.nio.charset.Charset;
44-
import java.nio.charset.StandardCharsets;
4542
import java.util.ArrayList;
4643
import java.util.List;
4744

4845
public class MessagePackGenerator
4946
extends GeneratorBase
5047
{
51-
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
5248
private static final int IN_ROOT = 0;
5349
private static final int IN_OBJECT = 1;
5450
private static final int IN_ARRAY = 2;
@@ -64,11 +60,11 @@ public class MessagePackGenerator
6460
private boolean isElementsClosed = false;
6561
private SimpleStreamWriteContext writeContext;
6662

67-
private static final class AsciiCharString
63+
private static final class RawUtf8String
6864
{
6965
public final byte[] bytes;
7066

71-
public AsciiCharString(byte[] bytes)
67+
public RawUtf8String(byte[] bytes)
7268
{
7369
this.bytes = bytes;
7470
}
@@ -364,8 +360,8 @@ private void packNonContainer(Object v)
364360
if (v instanceof String) {
365361
messagePacker.packString((String) v);
366362
}
367-
else if (v instanceof AsciiCharString) {
368-
byte[] bytes = ((AsciiCharString) v).bytes;
363+
else if (v instanceof RawUtf8String) {
364+
byte[] bytes = ((RawUtf8String) v).bytes;
369365
messagePacker.packRawStringHeader(bytes.length);
370366
messagePacker.writePayload(bytes);
371367
}
@@ -494,49 +490,16 @@ private void addValueNode(Object value) throws IOException
494490
}
495491
}
496492

497-
@Nullable
498-
private byte[] getBytesIfAscii(char[] chars, int offset, int len)
499-
{
500-
byte[] bytes = new byte[len];
501-
for (int i = offset; i < offset + len; i++) {
502-
char c = chars[i];
503-
if (c >= 0x80) {
504-
return null;
505-
}
506-
bytes[i - offset] = (byte) c;
507-
}
508-
return bytes;
509-
}
510-
511-
private boolean areAllAsciiBytes(byte[] bytes, int offset, int len)
512-
{
513-
for (int i = offset; i < offset + len; i++) {
514-
if ((bytes[i] & 0x80) != 0) {
515-
return false;
516-
}
517-
}
518-
return true;
519-
}
520-
521493
private void writeCharArrayTextValue(char[] text, int offset, int len) throws IOException
522494
{
523-
byte[] bytes = getBytesIfAscii(text, offset, len);
524-
if (bytes != null) {
525-
addValueNode(new AsciiCharString(bytes));
526-
return;
527-
}
528495
addValueNode(new String(text, offset, len));
529496
}
530497

531498
private void writeByteArrayTextValue(byte[] text, int offset, int len) throws IOException
532499
{
533-
if (areAllAsciiBytes(text, offset, len)) {
534-
byte[] slice = new byte[len];
535-
System.arraycopy(text, offset, slice, 0, len);
536-
addValueNode(new AsciiCharString(slice));
537-
return;
538-
}
539-
addValueNode(new String(text, offset, len, DEFAULT_CHARSET));
500+
byte[] slice = new byte[len];
501+
System.arraycopy(text, offset, slice, 0, len);
502+
addValueNode(new RawUtf8String(slice));
540503
}
541504

542505
@Override
@@ -613,9 +576,7 @@ public JsonGenerator writeString(Reader reader, int len) throws JacksonException
613576
while ((read = reader.read(tmpBuf)) >= 0) {
614577
sb.append(tmpBuf, 0, read);
615578
}
616-
char[] chars = new char[sb.length()];
617-
sb.getChars(0, chars.length, chars, 0);
618-
writeCharArrayTextValue(chars, 0, chars.length);
579+
addValueNode(sb.toString());
619580
}
620581
else {
621582
char[] buf = new char[len];
@@ -682,9 +643,7 @@ public JsonGenerator writeRaw(String text) throws JacksonException
682643
public JsonGenerator writeRaw(String text, int offset, int len) throws JacksonException
683644
{
684645
try {
685-
char[] chars = new char[len];
686-
text.getChars(offset, offset + len, chars, 0);
687-
writeCharArrayTextValue(chars, 0, len);
646+
addValueNode(text.substring(offset, offset + len));
688647
}
689648
catch (IOException e) {
690649
throw _wrapIOFailure(e);

0 commit comments

Comments
 (0)