Skip to content

Commit 202ca62

Browse files
committed
Merge pull request msgpack#359 from msgpack/test-str8-disable
Fix packString
2 parents 032fc2e + e65e7b7 commit 202ca62

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Release Notes
22

3+
## 0.8.5
4+
* Add PackerConfig.withStr8FormatSupport (default: true) for backward compatibility with earier versions of msgpack v0.6, which doesn't have STR8 type.
5+
36
## 0.8.4
47
* Embed bundle paramters for OSGi
58

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ private void prepareEncoder()
449449
if (encoder == null) {
450450
this.encoder = MessagePack.UTF8.newEncoder();
451451
}
452+
encoder.reset();
452453
}
453454

454455
private int encodeStringToBufferAt(int pos, String s)
@@ -466,7 +467,13 @@ private int encodeStringToBufferAt(int pos, String s)
466467
throw new MessageStringCodingException(e);
467468
}
468469
}
469-
if (cr.isUnderflow() || cr.isOverflow()) {
470+
if (!cr.isUnderflow() || cr.isOverflow()) {
471+
// Underflow should be on to ensure all of the input string is encoded
472+
return -1;
473+
}
474+
// NOTE: This flush method does nothing if we use UTF8 encoder, but other general encoders require this
475+
cr = encoder.flush(bb);
476+
if (!cr.isUnderflow()) {
470477
return -1;
471478
}
472479
return bb.position() - startPosition;
@@ -499,7 +506,7 @@ else if (s.length() < (1 << 8)) {
499506
// keep 2-byte header region and write raw string
500507
int written = encodeStringToBufferAt(position + 2, s);
501508
if (written >= 0) {
502-
if (written < (1 << 8)) {
509+
if (str8FormatSupport && written < (1 << 8)) {
503510
buffer.putByte(position++, STR8);
504511
buffer.putByte(position++, (byte) written);
505512
position += written;

msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,39 @@ class MessagePackerTest extends MessagePackSpec {
282282
.writePayload(payload)
283283
.close()
284284
}
285+
286+
"pack small string with STR8" in {
287+
val packer = new PackerConfig().newBufferPacker()
288+
packer.packString("Hello. This is a string longer than 32 characters!")
289+
val b = packer.toByteArray
290+
291+
val unpacker = MessagePack.newDefaultUnpacker(b)
292+
val f = unpacker.getNextFormat
293+
f shouldBe MessageFormat.STR8
294+
}
295+
296+
"be able to disable STR8 for backward compatibility" in {
297+
val config = new PackerConfig()
298+
.withStr8FormatSupport(false)
299+
300+
val packer = config.newBufferPacker()
301+
packer.packString("Hello. This is a string longer than 32 characters!")
302+
val unpacker = MessagePack.newDefaultUnpacker(packer.toByteArray)
303+
val f = unpacker.getNextFormat
304+
f shouldBe MessageFormat.STR16
305+
}
306+
307+
"be able to disable STR8 when using CharsetEncoder" in {
308+
val config = new PackerConfig()
309+
.withStr8FormatSupport(false)
310+
.withSmallStringOptimizationThreshold(0) // Disable small string optimization
311+
312+
val packer = config.newBufferPacker()
313+
packer.packString("small string")
314+
val unpacker = MessagePack.newDefaultUnpacker(packer.toByteArray)
315+
val f = unpacker.getNextFormat
316+
f shouldNot be (MessageFormat.STR8)
317+
val s = unpacker.unpackString()
318+
s shouldBe "small string"
319+
}
285320
}

0 commit comments

Comments
 (0)