Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Commit f9a4f7f

Browse files
committed
Close the previous assigned input in MessageBufferInput#reset
1 parent 669b000 commit f9a4f7f

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ChannelBufferInput(ReadableByteChannel channel, int bufferSize) {
2626
}
2727

2828
public void reset(ReadableByteChannel channel) throws IOException {
29-
channel.close();
29+
this.channel.close();
3030
this.channel = channel;
3131
this.reachedEOF = false;
3232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public InputStreamBufferInput(InputStream in, int bufferSize) {
3333
}
3434

3535
public void reset(InputStream in) throws IOException {
36-
in.close();
36+
this.in.close();
3737
this.in = in;
3838
reachedEOF = false;
3939
}

msgpack-core/src/test/scala/org/msgpack/core/buffer/MessageBufferInputTest.scala

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package org.msgpack.core.buffer
22

3-
import org.msgpack.core.MessagePackSpec
3+
import org.msgpack.core.{MessageUnpacker, MessagePack, MessagePackSpec}
44
import java.io._
55
import xerial.core.io.IOUtil
66
import scala.util.Random
77
import java.util.zip.{GZIPOutputStream, GZIPInputStream}
88
import java.nio.ByteBuffer
9+
import org.msgpack.unpacker.MessagePackUnpacker
910

1011
/**
1112
* Created on 5/30/14.
@@ -98,4 +99,54 @@ class MessageBufferInputTest extends MessagePackSpec {
9899
}
99100

100101
}
102+
103+
def createTempFile = {
104+
val f = File.createTempFile("msgpackTest", "msgpack")
105+
f.deleteOnExit
106+
f
107+
}
108+
109+
def createTempFileWithInputStream = {
110+
val f = createTempFile
111+
val out = new FileOutputStream(f)
112+
new MessagePack().newPacker(out).packInt(42).close
113+
val in = new FileInputStream(f)
114+
(f, in)
115+
}
116+
117+
def createTempFileWithChannel = {
118+
val (f, in) = createTempFileWithInputStream
119+
val ch = in.getChannel
120+
(f, ch)
121+
}
122+
123+
def readInt(buf:MessageBufferInput) : Int = {
124+
val unpacker = new MessageUnpacker(buf)
125+
unpacker.unpackInt
126+
}
127+
128+
"InputStreamBufferInput" should {
129+
"reset buffer" in {
130+
val (f0, in0) = createTempFileWithInputStream
131+
val buf = new InputStreamBufferInput(in0)
132+
readInt(buf) shouldBe 42
133+
134+
val (f1, in1) = createTempFileWithInputStream
135+
buf.reset(in1)
136+
readInt(buf) shouldBe 42
137+
}
138+
}
139+
140+
"ChannelBufferInput" should {
141+
"reset buffer" in {
142+
val (f0, in0) = createTempFileWithChannel
143+
val buf = new ChannelBufferInput(in0)
144+
readInt(buf) shouldBe 42
145+
146+
val (f1, in1) = createTempFileWithChannel
147+
buf.reset(in1)
148+
readInt(buf) shouldBe 42
149+
}
150+
}
151+
101152
}

0 commit comments

Comments
 (0)