Skip to content

Commit 651a2a0

Browse files
authored
Add low-level APIs for unpacking Timestamp values (#580)
1 parent c5f98a2 commit 651a2a0

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public byte getType()
5959
return type;
6060
}
6161

62+
public boolean isTimestampType()
63+
{
64+
return type == MessagePack.Code.EXT_TIMESTAMP;
65+
}
66+
6267
public int getLength()
6368
{
6469
return length;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,9 @@ public Instant unpackTimestamp()
12841284
}
12851285

12861286
/**
1287-
* Internal method that can be used only when the extension type header is already read.
1287+
* Unpack timestamp that can be used after reading the extension type header with unpackExtensionTypeHeader.
12881288
*/
1289-
private Instant unpackTimestamp(ExtensionTypeHeader ext) throws IOException
1289+
public Instant unpackTimestamp(ExtensionTypeHeader ext) throws IOException
12901290
{
12911291
if (ext.getType() != EXT_TIMESTAMP) {
12921292
throw unexpectedExtension("Timestamp", EXT_TIMESTAMP, ext.getType());

msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
import org.msgpack.value.ExtensionValue;
2727
import org.msgpack.value.FloatValue;
2828
import org.msgpack.value.IntegerValue;
29+
import org.msgpack.value.TimestampValue;
2930
import org.msgpack.value.Value;
3031

3132
import java.io.File;
3233
import java.io.FileInputStream;
3334
import java.io.FileOutputStream;
3435
import java.io.IOException;
3536
import java.math.BigInteger;
37+
import java.time.Instant;
3638

3739
/**
3840
* This class describes the usage of MessagePack
@@ -145,6 +147,9 @@ public static void packer()
145147
packer.packExtensionTypeHeader((byte) 1, 10); // type number [0, 127], data byte length
146148
packer.writePayload(extData);
147149

150+
// Pack timestamp
151+
packer.packTimestamp(Instant.now());
152+
148153
// Succinct syntax for packing
149154
packer
150155
.packInt(1)
@@ -228,8 +233,15 @@ else if (iv.isInLongRange()) {
228233
break;
229234
case EXTENSION:
230235
ExtensionValue ev = v.asExtensionValue();
231-
byte extType = ev.getType();
232-
byte[] extValue = ev.getData();
236+
if (ev.isTimestampValue()) {
237+
// Reading the value as a timestamp
238+
TimestampValue ts = ev.asTimestampValue();
239+
Instant tsValue = ts.toInstant();
240+
}
241+
else {
242+
byte extType = ev.getType();
243+
byte[] extValue = ev.getData();
244+
}
233245
break;
234246
}
235247
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,24 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
662662
}
663663
}
664664

665+
test("pack/unpack timestamp through ExtValue") {
666+
val posLong = Gen.chooseNum[Long](-31557014167219200L, 31556889864403199L)
667+
forAll(posLong) { (millis: Long) =>
668+
val v = Instant.ofEpochMilli(millis)
669+
check(v, { _.packTimestamp(millis) },
670+
{ u =>
671+
val extHeader = u.unpackExtensionTypeHeader()
672+
if(extHeader.isTimestampType) {
673+
u.unpackTimestamp(extHeader)
674+
}
675+
else {
676+
fail("Cannot reach here")
677+
}
678+
}
679+
)
680+
}
681+
}
682+
665683
test("MessagePack.PackerConfig") {
666684
test("should be immutable") {
667685
val a = new MessagePack.PackerConfig()

0 commit comments

Comments
 (0)