Skip to content

Commit f8f9615

Browse files
committed
msgpack#114: Enhance the constructor comments so that the user can create appropriate MessageBufferInput.
1 parent a74d163 commit f8f9615

4 files changed

Lines changed: 28 additions & 40 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,23 @@ public class MessagePacker implements Closeable {
7171
*/
7272
private CharsetEncoder encoder;
7373

74+
75+
/**
76+
* Create an MessagePacker that outputs the packed data to the specified stream
77+
* @param out
78+
*/
7479
public MessagePacker(OutputStream out) {
80+
// This factory method does not have significant performance overhead.
7581
this(new OutputStreamBufferOutput(out));
7682
}
7783

78-
84+
/**
85+
* Create an MessagePacker that outputs the packed data to the given {@link org.msgpack.core.buffer.MessageBufferOutput}
86+
*
87+
* @param out MessageBufferOutput. Use {@link org.msgpack.core.buffer.OutputStreamBufferOutput}, {@link org.msgpack.core.buffer.ChannelBufferOutput} or
88+
* your own implementation of {@link org.msgpack.core.buffer.MessageBufferOutput} interface.
89+
*
90+
*/
7991
public MessagePacker(MessageBufferOutput out) {
8092
this(out, MessagePack.DEFAULT_CONFIG);
8193
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,20 @@ public MessageUnpacker(byte[] arr) {
134134
this(new ArrayBufferInput(arr));
135135
}
136136

137+
/**
138+
* Create an MessageUnpacker that reads data from the given byte array [offset, offset+length)
139+
* @param arr
140+
* @param offset
141+
* @param length
142+
*/
143+
public MessageUnpacker(byte[] arr, int offset, int length) {
144+
this(new ArrayBufferInput(arr, offset, length));
145+
}
146+
137147
/**
138148
* Create an MessageUnpacker that reads data from the given InputStream.
149+
* For reading data efficiently from byte[], use {@link #MessageUnpacker(byte[])} or {@link #MessageUnpacker(byte[], int, int)} instead of this constructor.
150+
*
139151
* @param in
140152
*/
141153
public MessageUnpacker(InputStream in) {

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,15 @@
1313
*/
1414
public class InputStreamBufferInput implements MessageBufferInput {
1515

16-
private static Field bufField;
17-
private static Field bufPosField;
18-
private static Field bufCountField;
19-
20-
private static Field getField(String name) {
21-
Field f = null;
22-
try {
23-
f = ByteArrayInputStream.class.getDeclaredField(name);
24-
f.setAccessible(true);
25-
}
26-
catch(Exception e) {
27-
e.printStackTrace();
28-
}
29-
return f;
30-
}
31-
32-
static {
33-
bufField = getField("buf");
34-
bufPosField = getField("pos");
35-
bufCountField = getField("count");
36-
}
37-
3816
private final InputStream in;
3917
private final int bufferSize;
4018
private boolean reachedEOF = false;
4119

4220
public static MessageBufferInput newBufferInput(InputStream in) {
4321
checkNotNull(in, "InputStream is null");
44-
if(in.getClass() == ByteArrayInputStream.class) {
45-
ByteArrayInputStream b = (ByteArrayInputStream) in;
46-
try {
47-
// Extract a raw byte array from the ByteArrayInputStream
48-
byte[] buf = (byte[]) bufField.get(b);
49-
int pos = (Integer) bufPosField.get(b);
50-
int length = (Integer) bufCountField.get(b);
51-
return new ArrayBufferInput(buf, pos, length);
52-
}
53-
catch(Exception e) {
54-
// Failed to retrieve the raw byte array
55-
}
56-
} else if (in instanceof FileInputStream) {
22+
if (in instanceof FileInputStream) {
5723
return new ChannelBufferInput(((FileInputStream) in).getChannel());
5824
}
59-
6025
return new InputStreamBufferInput(in);
6126
}
6227

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ class MessageUnpackerTest extends MessagePackSpec {
439439
}
440440

441441
block("v7") {
442-
//val unpacker = new MessageUnpacker(b)
443-
val unpacker = new MessageUnpacker(new ByteArrayInputStream(b))
442+
val unpacker = new MessageUnpacker(b)
444443
var i = 0
445444
while(i < R) {
446445
val len = unpacker.unpackBinaryHeader()
@@ -452,7 +451,7 @@ class MessageUnpackerTest extends MessagePackSpec {
452451
}
453452

454453
block("v7-ref") {
455-
val unpacker = new MessageUnpacker(new ByteArrayInputStream(b))
454+
val unpacker = new MessageUnpacker(b)
456455
var i = 0
457456
while(i < R) {
458457
val len = unpacker.unpackBinaryHeader()

0 commit comments

Comments
 (0)