@@ -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
0 commit comments