Skip to content

Commit 59bc83b

Browse files
committed
Merge pull request msgpack#169 from msgpack/fix_msgbufoutput_reset
MessageBufferOutput#reset shouldn't close an output passed as a paramete...
2 parents 669b000 + 51e83af commit 59bc83b

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ChannelBufferOutput(WritableByteChannel channel) {
1919
}
2020

2121
public void reset(WritableByteChannel channel) throws IOException {
22-
channel.close();
22+
this.channel.close();
2323
this.channel = channel;
2424
}
2525

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public OutputStreamBufferOutput(OutputStream out) {
1919
}
2020

2121
public void reset(OutputStream out) throws IOException {
22-
out.close();
22+
this.out.close();
2323
this.out = out;
2424
}
2525

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.msgpack.core.buffer
2+
3+
import org.msgpack.core.MessagePackSpec
4+
import java.io._
5+
6+
class MessageBufferOutputTest extends MessagePackSpec {
7+
8+
def createTempFile = {
9+
val f = File.createTempFile("msgpackTest", "msgpack")
10+
f.deleteOnExit
11+
f
12+
}
13+
14+
def createTempFileWithOutputStream = {
15+
val f = createTempFile
16+
val out = new FileOutputStream(f)
17+
(f, out)
18+
}
19+
20+
def createTempFileWithChannel = {
21+
val (f, out) = createTempFileWithOutputStream
22+
val ch = out.getChannel
23+
(f, ch)
24+
}
25+
26+
def writeIntToBuf(buf:MessageBufferOutput) = {
27+
val mb0 = buf.next(8)
28+
mb0.putInt(0, 42)
29+
buf.flush(mb0)
30+
buf.close
31+
}
32+
33+
"OutputStreamBufferOutput" should {
34+
"reset buffer" in {
35+
val (f0, out0) = createTempFileWithOutputStream
36+
val buf = new OutputStreamBufferOutput(out0)
37+
writeIntToBuf(buf)
38+
f0.length.toInt should be > 0
39+
40+
val (f1, out1) = createTempFileWithOutputStream
41+
buf.reset(out1)
42+
writeIntToBuf(buf)
43+
f1.length.toInt should be > 0
44+
}
45+
}
46+
47+
"ChannelBufferOutput" should {
48+
"reset buffer" in {
49+
val (f0, ch0) = createTempFileWithChannel
50+
val buf = new ChannelBufferOutput(ch0)
51+
writeIntToBuf(buf)
52+
f0.length.toInt should be > 0
53+
54+
val (f1, ch1) = createTempFileWithChannel
55+
buf.reset(ch1)
56+
writeIntToBuf(buf)
57+
f1.length.toInt should be > 0
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)