Skip to content

Commit a719283

Browse files
authored
Merge pull request msgpack#459 from isopov/clear-position
add MessagePacker.clear() method to clear position
2 parents 33ce348 + 41e7fe7 commit a719283

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ private ArrayBufferOutput getArrayBufferOut()
5656
return (ArrayBufferOutput) out;
5757
}
5858

59-
/**
60-
* Clears the written data.
61-
*/
59+
@Override
6260
public void clear()
6361
{
62+
super.clear();
6463
getArrayBufferOut().clear();
6564
}
6665

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ public long getTotalWrittenBytes()
260260
return totalFlushBytes + position;
261261
}
262262

263+
/**
264+
* Clears the written data.
265+
*/
266+
public void clear()
267+
{
268+
position = 0;
269+
}
270+
263271
/**
264272
* Flushes internal buffer to the underlying output.
265273
* <p>

msgpack-core/src/main/java/org/msgpack/core/buffer/ArrayBufferOutput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
public class ArrayBufferOutput
2929
implements MessageBufferOutput
3030
{
31-
private List<MessageBuffer> list;
31+
private final List<MessageBuffer> list;
32+
private final int bufferSize;
3233
private MessageBuffer lastBuffer;
33-
private int bufferSize;
3434

3535
public ArrayBufferOutput()
3636
{

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.msgpack.core
1717

1818
import java.io.ByteArrayOutputStream
19+
import java.util.Arrays
1920

2021
import org.msgpack.value.ValueFactory._
2122

@@ -32,5 +33,19 @@ class MessageBufferPackerTest extends MessagePackSpec {
3233

3334
packer1.toByteArray shouldBe stream.toByteArray
3435
}
36+
37+
"clear unflushed" in {
38+
val packer = MessagePack.newDefaultBufferPacker
39+
packer.packInt(1);
40+
packer.clear();
41+
packer.packInt(2);
42+
43+
packer.toByteArray shouldBe Array(2)
44+
val buffer = packer.toBufferList().get(0)
45+
buffer.toByteArray() shouldBe Array(2)
46+
val array = Arrays.copyOf(buffer.sliceAsByteBuffer().array(), buffer.size())
47+
array shouldBe Array(2)
48+
}
49+
3550
}
3651
}

0 commit comments

Comments
 (0)