Skip to content

Commit 499dd52

Browse files
committed
BufferRefer: accepts ByteBuffer instead of byte array
1 parent 49e1354 commit 499dd52

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

Diff for: src/main/java/org/msgpack/io/BufferReferer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
package org.msgpack.io;
1919

2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2122

2223

2324
public interface BufferReferer {
24-
public void refer(byte[] b, int off, int len, boolean gift) throws IOException;
25+
public void refer(ByteBuffer bb, boolean gift) throws IOException;
2526
}
2627

Diff for: src/main/java/org/msgpack/io/LinkedBufferInput.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,22 @@ public boolean tryRefer(BufferReferer ref, int len) throws IOException {
6969
throw new EndOfBufferException();
7070
} else if(bb.remaining() < len) {
7171
return false;
72-
} else if(!bb.hasArray()) {
73-
return false;
7472
}
75-
ref.refer(bb.array(), bb.arrayOffset()+bb.position(), len, true);
76-
bb.position(bb.position()+len);
73+
boolean success = false;
74+
int pos = bb.position();
75+
int lim = bb.limit();
76+
try {
77+
bb.limit(pos+len);
78+
ref.refer(bb, true);
79+
success = true;
80+
} finally {
81+
bb.limit(lim);
82+
if(success) {
83+
bb.position(pos+len);
84+
} else {
85+
bb.position(pos);
86+
}
87+
}
7788
return true;
7889
}
7990

Diff for: src/main/java/org/msgpack/unpacker/Accept.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.msgpack.unpacker;
1919

2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2122
import org.msgpack.io.BufferReferer;
2223
import org.msgpack.MessageTypeException;
2324

@@ -99,7 +100,7 @@ void acceptDouble(double v) {
99100
throw new MessageTypeException("Unexpected float value");
100101
}
101102

102-
public void refer(byte[] b, int off, int size, boolean gift) throws IOException {
103+
public void refer(ByteBuffer bb, boolean gift) throws IOException {
103104
throw new MessageTypeException("Unexpected raw value");
104105
}
105106
}

Diff for: src/main/java/org/msgpack/unpacker/ByteArrayAccept.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.msgpack.unpacker;
1919

2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2122

2223

2324
final class ByteArrayAccept extends Accept {
@@ -34,10 +35,10 @@ void acceptEmptyRaw() {
3435
}
3536

3637
@Override
37-
public void refer(byte[] b, int off, int len, boolean gift) throws IOException {
38+
public void refer(ByteBuffer bb, boolean gift) throws IOException {
3839
// TODO gift
39-
this.value = new byte[len];
40-
System.arraycopy(b, off, value, 0, len);
40+
this.value = new byte[bb.remaining()];
41+
bb.get(value);
4142
}
4243
}
4344

Diff for: src/main/java/org/msgpack/unpacker/StringAccept.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@
1818
package org.msgpack.unpacker;
1919

2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
22+
import java.nio.charset.Charset;
23+
import java.nio.charset.CharacterCodingException;
24+
import java.nio.charset.CharsetDecoder;
25+
import java.nio.charset.CodingErrorAction;
26+
import java.nio.charset.MalformedInputException;
2127

2228

2329
final class StringAccept extends Accept {
2430
String value;
31+
private CharsetDecoder decoder;
32+
33+
public StringAccept() {
34+
this.decoder = Charset.forName("UTF-8").newDecoder().
35+
onMalformedInput(CodingErrorAction.REPORT).
36+
onUnmappableCharacter(CodingErrorAction.REPORT);
37+
}
2538

2639
@Override
2740
void acceptRaw(byte[] raw) {
@@ -35,8 +48,9 @@ void acceptEmptyRaw() {
3548
}
3649

3750
@Override
38-
public void refer(byte[] b, int off, int len, boolean gift) throws IOException {
39-
this.value = new String(b, off, len);
51+
public void refer(ByteBuffer bb, boolean gift) throws IOException {
52+
// TODO encoding error
53+
this.value = decoder.decode(bb).toString();
4054
}
4155
}
4256

Diff for: src/main/java/org/msgpack/unpacker/ValueAccept.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.msgpack.unpacker;
1919

2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2122
import java.math.BigInteger;
2223
import org.msgpack.type.Value;
2324
import org.msgpack.type.ValueFactory;
@@ -96,9 +97,11 @@ void acceptEmptyRaw() {
9697
}
9798

9899
@Override
99-
public void refer(byte[] b, int off, int len, boolean gift) throws IOException {
100+
public void refer(ByteBuffer bb, boolean gift) throws IOException {
100101
// TODO gift
101-
uc.write(ValueFactory.rawValue(b, off, len));
102+
byte[] raw = new byte[bb.remaining()];
103+
bb.get(raw);
104+
uc.write(ValueFactory.rawValue(raw, true));
102105
}
103106

104107
@Override

0 commit comments

Comments
 (0)