Skip to content

Commit ff16522

Browse files
committed
MessageBufferPacker.toByteArray flushes packer's internal buffer automatically
1 parent 979ffc1 commit ff16522

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,37 @@ public void clear()
5959

6060
public byte[] toByteArray()
6161
{
62+
try {
63+
flush();
64+
}
65+
catch (IOException ex) {
66+
// IOException must not happen because underlaying ArrayBufferOutput never throws IOException
67+
throw new RuntimeException(ex);
68+
}
6269
return getArrayBufferOut().toByteArray();
6370
}
6471

6572
public MessageBuffer toMessageBuffer()
6673
{
74+
try {
75+
flush();
76+
}
77+
catch (IOException ex) {
78+
// IOException must not happen because underlaying ArrayBufferOutput never throws IOException
79+
throw new RuntimeException(ex);
80+
}
6781
return getArrayBufferOut().toMessageBuffer();
6882
}
6983

7084
public List<MessageBuffer> toBufferList()
7185
{
86+
try {
87+
flush();
88+
}
89+
catch (IOException ex) {
90+
// IOException must not happen because underlaying ArrayBufferOutput never throws IOException
91+
throw new RuntimeException(ex);
92+
}
7293
return getArrayBufferOut().toBufferList();
7394
}
7495
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.core
17+
18+
import java.io.ByteArrayOutputStream
19+
import java.util.Arrays
20+
import org.msgpack.core.MessagePack.{UnpackerConfig, PackerConfig}
21+
import org.msgpack.core.buffer.OutputStreamBufferOutput
22+
import org.msgpack.value.Value
23+
import org.msgpack.value.ValueFactory._
24+
25+
/**
26+
*
27+
*/
28+
class MessageBufferPackerTest extends MessagePackSpec {
29+
"MessageBufferPacker" should {
30+
"be equivalent to ByteArrayOutputStream" in {
31+
val packer1 = MessagePack.newDefaultBufferPacker()
32+
packer1.packValue(newMap(Array[Value](
33+
newString("a"), newInteger(1),
34+
newString("b"), newString("s"))))
35+
36+
val stream = new ByteArrayOutputStream()
37+
val packer2 = MessagePack.newDefaultPacker(stream)
38+
packer2.packValue(newMap(Array[Value](
39+
newString("a"), newInteger(1),
40+
newString("b"), newString("s"))))
41+
packer2.flush()
42+
43+
Arrays.equals(packer1.toByteArray(), stream.toByteArray()) shouldBe true
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)