Skip to content

Commit ebb2c18

Browse files
committed
Remove calling of the old resource in reset()
1 parent 10f7878 commit ebb2c18

File tree

10 files changed

+157
-33
lines changed

10 files changed

+157
-33
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,17 @@ public MessagePacker(MessageBufferOutput out, MessagePack.Config config) {
8383
this.position = 0;
8484
}
8585

86+
/**
87+
* Reset output. This method doesn't close the old resource.
88+
* @param out new output
89+
*/
8690
public void reset(MessageBufferOutput out) throws IOException {
8791
// Validate the argument
8892
MessageBufferOutput newOut = checkNotNull(out, "MessageBufferOutput is null");
8993

90-
try {
91-
if(this.out != newOut) {
92-
this.out.close();
93-
}
94-
}
95-
finally {
96-
// Reset the internal states here for the exception safety
97-
this.out = newOut;
98-
this.position = 0;
99-
}
94+
// Reset the internal states
95+
this.out = newOut;
96+
this.position = 0;
10097
}
10198

10299

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,21 @@ public MessageUnpacker(MessageBufferInput in, MessagePack.Config config) {
146146
this.config = checkNotNull(config, "Config");
147147
}
148148

149+
/**
150+
* Reset input. This method doesn't close the old resource.
151+
* @param in new input
152+
*/
149153
public void reset(MessageBufferInput in) throws IOException {
150154
MessageBufferInput newIn = checkNotNull(in, "MessageBufferInput is null");
151155

152-
try {
153-
if(in != newIn) {
154-
close();
155-
}
156-
}
157-
finally {
158-
// Reset the internal states here for the exception safety
159-
this.in = newIn;
160-
this.buffer = EMPTY_BUFFER;
161-
this.position = 0;
162-
this.totalReadBytes = 0;
163-
this.secondaryBuffer = null;
164-
this.reachedEOF = false;
165-
// No need to initialize the already allocated string decoder here since we can reuse it.
166-
}
156+
// Reset the internal states
157+
this.in = newIn;
158+
this.buffer = EMPTY_BUFFER;
159+
this.position = 0;
160+
this.totalReadBytes = 0;
161+
this.secondaryBuffer = null;
162+
this.reachedEOF = false;
163+
// No need to initialize the already allocated string decoder here since we can reuse it.
167164
}
168165

169166
public long getTotalReadBytes() {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public ArrayBufferInput(byte[] arr, int offset, int length) {
2424
this.buffer = MessageBuffer.wrap(checkNotNull(arr, "input array is null")).slice(offset, length);
2525
}
2626

27+
/**
28+
* Reset buffer. This method doesn't close the old resource.
29+
* @param buf new buffer
30+
*/
2731
public void reset(MessageBuffer buf) {
2832
this.buffer = buf;
2933
this.isRead = false;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public ByteBufferInput(ByteBuffer input) {
1616
this.input = checkNotNull(input, "input ByteBuffer is null");
1717
}
1818

19+
/**
20+
* Reset buffer. This method doesn't close the old resource.
21+
* @param input new buffer
22+
*/
1923
public void reset(ByteBuffer input) {
2024
this.input = input;
2125
isRead = false;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ public ChannelBufferInput(ReadableByteChannel channel, int bufferSize) {
2525
this.bufferSize = bufferSize;
2626
}
2727

28+
/**
29+
* Reset channel. This method doesn't close the old resource.
30+
* @param channel new channel
31+
*/
2832
public void reset(ReadableByteChannel channel) throws IOException {
29-
this.channel.close();
3033
this.channel = channel;
3134
this.reachedEOF = false;
3235
}
36+
3337
@Override
3438
public MessageBuffer next() throws IOException {
3539

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ public ChannelBufferOutput(WritableByteChannel channel) {
1818
this.channel = checkNotNull(channel, "output channel is null");
1919
}
2020

21+
/**
22+
* Reset channel. This method doesn't close the old resource.
23+
* @param channel new channel
24+
*/
2125
public void reset(WritableByteChannel channel) throws IOException {
22-
this.channel.close();
2326
this.channel = channel;
2427
}
2528

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ public InputStreamBufferInput(InputStream in, int bufferSize) {
3232
this.bufferSize = bufferSize;
3333
}
3434

35+
/**
36+
* Reset Stream. This method doesn't close the old resource.
37+
* @param in new stream
38+
*/
3539
public void reset(InputStream in) throws IOException {
36-
this.in.close();
3740
this.in = in;
3841
reachedEOF = false;
3942
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ public OutputStreamBufferOutput(OutputStream out) {
1818
this.out = checkNotNull(out, "output is null");
1919
}
2020

21+
/**
22+
* Reset Stream. This method doesn't close the old resource.
23+
* @param out new stream
24+
*/
2125
public void reset(OutputStream out) throws IOException {
22-
this.out.close();
2326
this.out = out;
2427
}
2528

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

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//
1616
package org.msgpack.core
1717

18-
import java.io.ByteArrayOutputStream
18+
import java.io.{FileInputStream, FileOutputStream, File, ByteArrayOutputStream}
1919

20-
import org.msgpack.core.buffer.OutputStreamBufferOutput
20+
import org.msgpack.core.buffer.{ChannelBufferOutput, MessageBufferOutput, OutputStreamBufferOutput}
2121
import xerial.core.io.IOUtil
2222

2323
import scala.util.Random
@@ -41,6 +41,24 @@ class MessagePackerTest extends MessagePackSpec {
4141
result shouldBe answer
4242
}
4343

44+
def createTempFile = {
45+
val f = File.createTempFile("msgpackTest", "msgpack")
46+
f.deleteOnExit
47+
f
48+
}
49+
50+
def createTempFileWithOutputStream = {
51+
val f = createTempFile
52+
val out = new FileOutputStream(f)
53+
(f, out)
54+
}
55+
56+
def createTempFileWithChannel = {
57+
val (f, out) = createTempFileWithOutputStream
58+
val ch = out.getChannel
59+
(f, ch)
60+
}
61+
4462
"MessagePacker" should {
4563

4664
"reset the internal states" in {
@@ -130,5 +148,59 @@ class MessagePackerTest extends MessagePackSpec {
130148
case (bufferSize, stringSize) => test(bufferSize, stringSize)
131149
}
132150
}
151+
152+
"reset OutputStreamBufferOutput" in {
153+
val (f0, out0) = createTempFileWithOutputStream
154+
val packer = MessagePack.newDefaultPacker(out0)
155+
packer.packInt(99)
156+
packer.close
157+
158+
val up0 = MessagePack.newDefaultUnpacker(new FileInputStream(f0))
159+
up0.unpackInt shouldBe 99
160+
up0.hasNext shouldBe false
161+
up0.close
162+
163+
val (f1, out1) = createTempFileWithOutputStream
164+
packer.reset(new OutputStreamBufferOutput(out1))
165+
packer.packInt(99)
166+
packer.flush
167+
packer.reset(new OutputStreamBufferOutput(out1))
168+
packer.packString("hello")
169+
packer.close
170+
171+
val up1 = MessagePack.newDefaultUnpacker(new FileInputStream(f1))
172+
up1.unpackInt shouldBe 99
173+
up1.unpackString shouldBe "hello"
174+
up1.hasNext shouldBe false
175+
up1.close
176+
}
177+
178+
"reset ChannelBufferOutput" in {
179+
val (f0, out0) = createTempFileWithChannel
180+
val packer = MessagePack.newDefaultPacker(out0)
181+
packer.packInt(99)
182+
packer.close
183+
184+
val up0 = MessagePack.newDefaultUnpacker(new FileInputStream(f0))
185+
up0.unpackInt shouldBe 99
186+
up0.hasNext shouldBe false
187+
up0.close
188+
189+
val (f1, out1) = createTempFileWithChannel
190+
packer.reset(new ChannelBufferOutput(out1))
191+
packer.packInt(99)
192+
packer.flush
193+
packer.reset(new ChannelBufferOutput(out1))
194+
packer.packString("hello")
195+
packer.close
196+
197+
val up1 = MessagePack.newDefaultUnpacker(new FileInputStream(f1))
198+
up1.unpackInt shouldBe 99
199+
up1.unpackString shouldBe "hello"
200+
up1.hasNext shouldBe false
201+
up1.close
202+
}
203+
133204
}
205+
134206
}

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.msgpack.core
22

3-
import java.io.{EOFException, ByteArrayInputStream, ByteArrayOutputStream}
3+
import java.io._
44
import scala.util.Random
5-
import org.msgpack.core.buffer.{MessageBuffer, MessageBufferInput, OutputStreamBufferOutput, ArrayBufferInput}
5+
import org.msgpack.core.buffer._
66
import org.msgpack.value.ValueType
77
import xerial.core.io.IOUtil
88

@@ -143,6 +143,19 @@ class MessageUnpackerTest extends MessagePackSpec {
143143
}
144144
}
145145

146+
def createTempFile = {
147+
val f = File.createTempFile("msgpackTest", "msgpack")
148+
f.deleteOnExit
149+
val p = MessagePack.newDefaultPacker(new FileOutputStream(f))
150+
p.packInt(99)
151+
p.close
152+
f
153+
}
154+
155+
def checkFile(u:MessageUnpacker) = {
156+
u.unpackInt shouldBe 99
157+
u.hasNext shouldBe false
158+
}
146159

147160
"MessageUnpacker" should {
148161

@@ -581,5 +594,29 @@ class MessageUnpackerTest extends MessagePackSpec {
581594
// This performance comparition is too close, so we disabled it
582595
// t("reuse-array-input").averageWithoutMinMax should be <= t("no-buffer-reset").averageWithoutMinMax
583596
}
597+
598+
"reset ChannelBufferInput" in {
599+
val f0 = createTempFile
600+
val u = MessagePack.newDefaultUnpacker(new FileInputStream(f0).getChannel)
601+
checkFile(u)
602+
603+
val f1 = createTempFile
604+
val ch = new FileInputStream(f1).getChannel
605+
u.reset(new ChannelBufferInput(ch))
606+
checkFile(u)
607+
u.close
608+
}
609+
610+
"reset InputStreamBufferInput" in {
611+
val f0 = createTempFile
612+
val u = MessagePack.newDefaultUnpacker(new FileInputStream(f0))
613+
checkFile(u)
614+
615+
val f1 = createTempFile
616+
val in = new FileInputStream(f1)
617+
u.reset(new InputStreamBufferInput(in))
618+
checkFile(u)
619+
u.close
620+
}
584621
}
585622
}

0 commit comments

Comments
 (0)