Skip to content

Commit 04fc4f4

Browse files
committed
Merge pull request msgpack#329 from msgpack/buffer-packer-flush
MessageBufferPacker.toByteArray flushes packer's internal buffer automatically
2 parents 175b7b7 + da348ab commit 04fc4f4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
20+
import org.msgpack.value.Value
21+
import org.msgpack.value.ValueFactory._
22+
23+
class MessageBufferPackerTest extends MessagePackSpec {
24+
"MessageBufferPacker" should {
25+
"be equivalent to ByteArrayOutputStream" in {
26+
val packer1 = MessagePack.newDefaultBufferPacker
27+
packer1.packValue(newMap(Array[Value](
28+
newString("a"), newInteger(1),
29+
newString("b"), newString("s"))))
30+
31+
val stream = new ByteArrayOutputStream
32+
val packer2 = MessagePack.newDefaultPacker(stream)
33+
packer2.packValue(newMap(Array[Value](
34+
newString("a"), newInteger(1),
35+
newString("b"), newString("s"))))
36+
packer2.flush
37+
38+
packer1.toByteArray shouldBe stream.toByteArray
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)