Skip to content

Commit 6ca1176

Browse files
committed
Moved the factory methods from core.MessagePackFactory to MessagePack to avoid the name conflicts with jackson.MessagePackFactory
1 parent 8b75906 commit 6ca1176

File tree

7 files changed

+185
-162
lines changed

7 files changed

+185
-162
lines changed

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

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package org.msgpack.core;
22

3-
import org.msgpack.core.buffer.ArrayBufferInput;
3+
import org.msgpack.core.buffer.*;
44

5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
import java.nio.channels.ReadableByteChannel;
8+
import java.nio.channels.WritableByteChannel;
59
import java.nio.charset.Charset;
610
import java.nio.charset.CodingErrorAction;
711

812
import static org.msgpack.core.Preconditions.checkArgument;
913

1014
/**
11-
* Includes MessagePack codes
15+
* This class has MessagePack prefix code definitions and packer/unpacker factory methods.
1216
*
1317
*/
1418
public class MessagePack {
@@ -93,8 +97,8 @@ public static class ConfigBuilder {
9397
private boolean readStringAsBinary = true;
9498
private boolean readBinaryAsString = true;
9599

96-
private CodingErrorAction onMalFormedInput = CodingErrorAction.REPORT;
97-
private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPORT;
100+
private CodingErrorAction onMalFormedInput = CodingErrorAction.REPLACE;
101+
private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPLACE;
98102

99103
private int maxUnpackStringSize = Integer.MAX_VALUE;
100104
private int stringEncoderBufferSize = 8192;
@@ -239,4 +243,137 @@ public static final boolean isFixedRaw(byte b) {
239243
public static final byte NEGFIXINT_PREFIX = (byte) 0xe0;
240244
}
241245

246+
// Packer/Unpacker factory methods
247+
248+
private final MessagePack.Config config;
249+
250+
public MessagePack() {
251+
this(MessagePack.DEFAULT_CONFIG);
252+
}
253+
254+
public MessagePack(MessagePack.Config config) {
255+
this.config = config;
256+
}
257+
258+
/**
259+
* Default MessagePack packer/unpacker factory
260+
*/
261+
public static final MessagePack DEFAULT = new MessagePack(MessagePack.DEFAULT_CONFIG);
262+
263+
264+
/**
265+
* Create a MessagePacker that outputs the packed data to the specified stream, using the default configuration
266+
* @param out
267+
* @return
268+
*/
269+
public static MessagePacker newDefaultPacker(OutputStream out) {
270+
return DEFAULT.newPacker(out);
271+
}
272+
273+
/**
274+
* Create a MessagePacker that outputs the packed data to the specified channel, using the default configuration
275+
* @param channel
276+
* @return
277+
*/
278+
public static MessagePacker newDefaultPacker(WritableByteChannel channel) {
279+
return DEFAULT.newPacker(channel);
280+
}
281+
282+
/**
283+
* Create a MessageUnpacker that reads data from then given InputStream, using the default configuration
284+
* @param in
285+
* @return
286+
*/
287+
public static MessageUnpacker newDefaultUnpacker(InputStream in) {
288+
return DEFAULT.newUnpacker(in);
289+
}
290+
291+
/**
292+
* Create a MessageUnpacker that reads data from the given channel, using the default configuration
293+
* @param channel
294+
* @return
295+
*/
296+
public static MessageUnpacker newDefaultUnpacker(ReadableByteChannel channel) {
297+
return DEFAULT.newUnpacker(channel);
298+
}
299+
300+
/**
301+
* Create a MessageUnpacker that reads data from the given byte array, using the default configuration
302+
* @param arr
303+
* @return
304+
*/
305+
public static MessageUnpacker newDefaultUnpacker(byte[] arr) {
306+
return DEFAULT.newUnpacker(arr);
307+
}
308+
309+
/**
310+
* Create a MessageUnpacker that reads data form the given byte array [offset, .. offset+length), using the default
311+
* configuration.
312+
* @param arr
313+
* @param offset
314+
* @param length
315+
* @return
316+
*/
317+
public static MessageUnpacker newDefaultUnpacker(byte[] arr, int offset, int length) {
318+
return DEFAULT.newUnpacker(arr, offset, length);
319+
}
320+
321+
322+
/**
323+
* Create a MessagePacker that outputs the packed data to the specified stream
324+
* @param out
325+
*/
326+
public MessagePacker newPacker(OutputStream out) {
327+
return new MessagePacker(new OutputStreamBufferOutput(out), config);
328+
}
329+
330+
/**
331+
* Create a MessagePacker that outputs the packed data to the specified channel
332+
* @param channel
333+
*/
334+
public MessagePacker newPacker(WritableByteChannel channel) {
335+
return new MessagePacker(new ChannelBufferOutput(channel), config);
336+
}
337+
338+
/**
339+
* Create a MessageUnpacker that reads data from the given InputStream.
340+
* For reading data efficiently from byte[], use {@link MessageUnpacker(byte[])} or {@link MessageUnpacker(byte[], int, int)} instead of this constructor.
341+
*
342+
* @param in
343+
*/
344+
public MessageUnpacker newUnpacker(InputStream in) {
345+
return new MessageUnpacker(InputStreamBufferInput.newBufferInput(in), config);
346+
}
347+
348+
/**
349+
* Create a MessageUnpacker that reads data from the given ReadableByteChannel.
350+
* @param in
351+
*/
352+
public MessageUnpacker newUnpacker(ReadableByteChannel in) {
353+
return new MessageUnpacker(new ChannelBufferInput(in), config);
354+
}
355+
356+
357+
/**
358+
* Create a MessageUnpacker that reads data from the given byte array.
359+
*
360+
* @param arr
361+
*/
362+
public MessageUnpacker newUnpacker(byte[] arr) {
363+
return new MessageUnpacker(new ArrayBufferInput(arr), config);
364+
}
365+
366+
/**
367+
* Create a MessageUnpacker that reads data from the given byte array [offset, offset+length)
368+
* @param arr
369+
* @param offset
370+
* @param length
371+
*/
372+
public MessageUnpacker newUnpacker(byte[] arr, int offset, int length) {
373+
return new MessageUnpacker(new ArrayBufferInput(arr, offset, length), config);
374+
}
375+
376+
377+
378+
242379
}

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

Lines changed: 0 additions & 112 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait MessagePackSpec
2424

2525
def createMessagePackData(f: MessagePacker => Unit) : Array[Byte] = {
2626
val b = new ByteArrayOutputStream()
27-
val packer = MessagePackFactory.DEFAULT.newPacker(b)
27+
val packer = MessagePack.newDefaultPacker(b)
2828
f(packer)
2929
packer.close()
3030
b.toByteArray

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package org.msgpack.core
22

33
import scala.util.Random
44
import MessagePack.Code
5-
import org.scalatest.prop.PropertyChecks
65
import java.io.ByteArrayOutputStream
76
import java.math.BigInteger
8-
import xerial.core.io.{Resource, IOUtil}
9-
import java.nio.{ByteBuffer, CharBuffer}
10-
import java.nio.charset.{Charset, UnmappableCharacterException, CodingErrorAction}
7+
import java.nio.CharBuffer
8+
import java.nio.charset.{UnmappableCharacterException, CodingErrorAction}
119

1210
/**
1311
* Created on 2014/05/07.
@@ -24,13 +22,13 @@ class MessagePackTest extends MessagePackSpec {
2422
false
2523
}
2624
catch {
27-
case e:UnmappableCharacterException =>
25+
case e: UnmappableCharacterException =>
2826
true
29-
case _ : Exception => false
27+
case _: Exception => false
3028
}
31-
3229
}
3330

31+
3432
"MessagePack" should {
3533
"detect fixint values" in {
3634

@@ -100,19 +98,19 @@ class MessagePackTest extends MessagePackSpec {
10098

10199
}
102100

103-
val mf = MessagePackFactory.DEFAULT;
101+
val msgpack = MessagePack.DEFAULT;
104102

105103
def check[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A) {
106104
var b: Array[Byte] = null
107105
try {
108106
val bs = new ByteArrayOutputStream()
109-
val packer = mf.newPacker(bs)
107+
val packer = msgpack.newPacker(bs)
110108
pack(packer)
111109
packer.close()
112110

113111
b = bs.toByteArray
114112

115-
val unpacker = mf.newUnpacker(b)
113+
val unpacker = msgpack.newUnpacker(b)
116114
val ret = unpack(unpacker)
117115
ret shouldBe v
118116
}
@@ -128,13 +126,13 @@ class MessagePackTest extends MessagePackSpec {
128126
def checkException[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A) {
129127
var b: Array[Byte] = null
130128
val bs = new ByteArrayOutputStream()
131-
val packer = mf.newPacker(bs)
129+
val packer = msgpack.newPacker(bs)
132130
pack(packer)
133131
packer.close()
134132

135133
b = bs.toByteArray
136134

137-
val unpacker = mf.newUnpacker(b)
135+
val unpacker = msgpack.newUnpacker(b)
138136
val ret = unpack(unpacker)
139137

140138
fail("cannot not reach here")

0 commit comments

Comments
 (0)