Skip to content

Commit b09d044

Browse files
committed
Add reset method to MessageBufferInput/Output implementations
1 parent 2d1cf0b commit b09d044

6 files changed

Lines changed: 42 additions & 5 deletions

File tree

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

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

23+
public void reset(byte[] arr) {
24+
this.buffer = MessageBuffer.wrap(checkNotNull(arr, "input array is null"));
25+
this.isRead = false;
26+
}
27+
28+
public void reset(byte[] arr, int offset, int len) {
29+
this.buffer = MessageBuffer.wrap(checkNotNull(arr, "input array is null")).slice(offset, len);
30+
this.isRead = false;
31+
}
32+
2333
@Override
2434
public MessageBuffer next() throws IOException {
2535
if(isRead)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
*/
1010
public class ByteBufferInput implements MessageBufferInput {
1111

12-
private final ByteBuffer input;
12+
private ByteBuffer input;
1313
private boolean isRead = false;
1414

1515
public ByteBufferInput(ByteBuffer input) {
1616
this.input = checkNotNull(input, "input ByteBuffer is null");
1717
}
1818

19+
public void reset(ByteBuffer input) {
20+
this.input = input;
21+
isRead = false;
22+
}
1923

2024
@Override
2125
public MessageBuffer next() throws IOException {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public class ChannelBufferInput implements MessageBufferInput {
1313

14-
private final ReadableByteChannel channel;
14+
private ReadableByteChannel channel;
1515
private boolean reachedEOF = false;
1616
private final int bufferSize;
1717

@@ -25,6 +25,11 @@ public ChannelBufferInput(ReadableByteChannel channel, int bufferSize) {
2525
this.bufferSize = bufferSize;
2626
}
2727

28+
public void reset(ReadableByteChannel channel) throws IOException {
29+
channel.close();
30+
this.channel = channel;
31+
this.reachedEOF = false;
32+
}
2833
@Override
2934
public MessageBuffer next() throws IOException {
3035

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111
*/
1212
public class ChannelBufferOutput implements MessageBufferOutput {
1313

14-
private final WritableByteChannel channel;
14+
private WritableByteChannel channel;
1515
private MessageBuffer buffer;
1616

1717
public ChannelBufferOutput(WritableByteChannel channel) {
1818
this.channel = checkNotNull(channel, "output channel is null");
1919
}
2020

21+
public void reset(WritableByteChannel channel) throws IOException {
22+
channel.close();
23+
this.channel = channel;
24+
}
25+
26+
2127
@Override
2228
public MessageBuffer next(int bufferSize) throws IOException {
2329
if(buffer == null || buffer.size() != bufferSize) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class InputStreamBufferInput implements MessageBufferInput {
1515

16-
private final InputStream in;
16+
private InputStream in;
1717
private final int bufferSize;
1818
private boolean reachedEOF = false;
1919

@@ -34,6 +34,13 @@ public InputStreamBufferInput(InputStream in, int bufferSize) {
3434
this.bufferSize = bufferSize;
3535
}
3636

37+
public void reset(InputStream in) throws IOException {
38+
in.close();
39+
this.in = in;
40+
reachedEOF = false;
41+
}
42+
43+
3744
@Override
3845
public MessageBuffer next() throws IOException {
3946
if(reachedEOF)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
*/
1111
public class OutputStreamBufferOutput implements MessageBufferOutput {
1212

13-
private final OutputStream out;
13+
private OutputStream out;
1414
private MessageBuffer buffer;
1515
private byte[] tmpBuf;
1616

1717
public OutputStreamBufferOutput(OutputStream out) {
1818
this.out = checkNotNull(out, "output is null");
1919
}
2020

21+
public void reset(OutputStream out) throws IOException {
22+
out.close();
23+
this.out = out;
24+
}
25+
2126
@Override
2227
public MessageBuffer next(int bufferSize) throws IOException {
2328
if(buffer == null || buffer.size != bufferSize) {

0 commit comments

Comments
 (0)