Skip to content

Commit 0d30434

Browse files
author
Stephane Landelle
committed
Add MessagePacker.totalWrittenBytes
1 parent 8cdc9a9 commit 0d30434

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public class MessagePacker implements Closeable {
6060

6161
private int position;
6262

63+
/**
64+
* Total read byte size
65+
*/
66+
private long totalWrittenBytes;
67+
6368
/**
6469
* String encoder
6570
*/
@@ -96,9 +101,13 @@ public MessageBufferOutput reset(MessageBufferOutput out) throws IOException {
96101
MessageBufferOutput old = this.out;
97102
this.out = newOut;
98103
this.position = 0;
104+
this.totalWrittenBytes = 0;
99105
return old;
100106
}
101107

108+
public long getTotalWritternBytes() {
109+
return totalWrittenBytes;
110+
}
102111

103112
private void prepareEncoder() {
104113
if(encoder == null) {
@@ -138,6 +147,7 @@ public void close() throws IOException {
138147
}
139148

140149
private void ensureCapacity(int numBytesToWrite) throws IOException {
150+
totalWrittenBytes += numBytesToWrite;
141151
if(buffer == null || position + numBytesToWrite >= buffer.size()) {
142152
flush();
143153
buffer = out.next(Math.max(config.getPackerBufferSize(), numBytesToWrite));
@@ -355,10 +365,12 @@ public MessagePacker packString(String s) throws IOException {
355365
encoder.reset();
356366
while(in.hasRemaining()) {
357367
try {
368+
int originalEncodeBuffer = encodeBuffer.position();
358369
CoderResult cr = encoder.encode(in, encodeBuffer, true);
359370

360371
if(cr.isUnderflow()) {
361372
cr = encoder.flush(encodeBuffer);
373+
totalWrittenBytes += encodeBuffer.position() - originalEncodeBuffer;
362374
}
363375

364376
if(cr.isOverflow()) {
@@ -499,6 +511,7 @@ public MessagePacker packRawStringHeader(int len) throws IOException {
499511

500512

501513
public MessagePacker writePayload(ByteBuffer src) throws IOException {
514+
totalWrittenBytes += src.remaining();
502515
if(src.remaining() >= config.getPackerRawDataCopyingThreshold()) {
503516
// Use the source ByteBuffer directly to avoid memory copy
504517

@@ -523,6 +536,7 @@ public MessagePacker writePayload(ByteBuffer src) throws IOException {
523536
position += writeLen;
524537
}
525538
}
539+
526540
return this;
527541
}
528542

@@ -555,6 +569,7 @@ public MessagePacker writePayload(byte[] src, int off, int len) throws IOExcepti
555569
cursor += writeLen;
556570
}
557571
}
572+
totalWrittenBytes += len;
558573
return this;
559574
}
560575

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,23 @@ class MessagePackerTest extends MessagePackSpec {
199199
up1.hasNext shouldBe false
200200
up1.close
201201
}
202-
203202
}
204203

204+
"compute totalWrittenBytes" in {
205+
val out = new ByteArrayOutputStream
206+
val packerTotalWrittenBytes = IOUtil.withResource(msgpack.newPacker(out)) { packer =>
207+
208+
packer.packByte(0)
209+
.packBoolean(true)
210+
.packShort(12)
211+
.packInt(1024)
212+
.packLong(Long.MaxValue)
213+
.packString("foobar")
214+
.flush()
215+
216+
packer.getTotalWritternBytes
217+
}
218+
219+
out.toByteArray.length shouldBe packerTotalWrittenBytes
220+
}
205221
}

0 commit comments

Comments
 (0)