1515//
1616package org .msgpack .core ;
1717
18+ import org .msgpack .core .buffer .ArrayBufferInput ;
19+ import org .msgpack .core .buffer .ChannelBufferInput ;
20+ import org .msgpack .core .buffer .ChannelBufferOutput ;
21+ import org .msgpack .core .buffer .InputStreamBufferInput ;
22+ import org .msgpack .core .buffer .MessageBufferInput ;
23+ import org .msgpack .core .buffer .MessageBufferOutput ;
24+ import org .msgpack .core .buffer .OutputStreamBufferOutput ;
25+
1826import java .io .InputStream ;
1927import java .io .OutputStream ;
2028import java .nio .channels .ReadableByteChannel ;
2129import java .nio .channels .WritableByteChannel ;
2230import java .nio .charset .Charset ;
31+ import java .nio .charset .CodingErrorAction ;
2332
2433/**
2534 * This class has MessagePack prefix code definitions and packer/unpacker factory methods.
@@ -28,79 +37,100 @@ public class MessagePack
2837{
2938 public static final Charset UTF8 = Charset .forName ("UTF-8" );
3039
31- private final static MessagePackFactory defaultFactory = new MessagePackFactory ();
32-
3340 private MessagePack ()
3441 {
42+ // Prohibit instantiation of this class
43+ }
44+
45+ /**
46+ * Create a packer that outputs the packed data to the specified output
47+ *
48+ * @param out
49+ * @return
50+ */
51+ public static MessagePacker newDefaultPacker (MessageBufferOutput out )
52+ {
53+ return new PackerConfig ().newPacker (out );
3554 }
3655
3756 /**
38- * Equivalent to defaultFactory().newPacker(out).
57+ * Create a packer that outputs the packed data to a target output stream
3958 *
4059 * @param out
4160 * @return
4261 */
4362 public static MessagePacker newDefaultPacker (OutputStream out )
4463 {
45- return defaultFactory .newPacker (out );
64+ return new PackerConfig () .newPacker (out );
4665 }
4766
4867 /**
49- * Equivalent to defaultFactory().newPacker( channel).
68+ * Create a packer that outputs the packed data to a channel
5069 *
5170 * @param channel
5271 * @return
5372 */
5473 public static MessagePacker newDefaultPacker (WritableByteChannel channel )
5574 {
56- return defaultFactory .newPacker (channel );
75+ return new PackerConfig () .newPacker (channel );
5776 }
5877
5978 /**
60- * Equivalent to defaultFactory().newBufferPacker()
79+ * Create a packer for storing packed data into a byte array
6180 *
6281 * @return
6382 */
6483 public static MessageBufferPacker newDefaultBufferPacker ()
6584 {
66- return defaultFactory .newBufferPacker ();
85+ return new PackerConfig () .newBufferPacker ();
6786 }
6887
6988 /**
70- * Equivalent to defaultFactory().newUnpacker(in).
89+ * Create an unpacker that reads the data from a given input
90+ *
91+ * @param in
92+ * @return
93+ */
94+ public static MessageUnpacker newDefaultUnpacker (MessageBufferInput in )
95+ {
96+ return new UnpackerConfig ().newUnpacker (in );
97+ }
98+
99+ /**
100+ * Create an unpacker that reads the data from a given input stream
71101 *
72102 * @param in
73103 * @return
74104 */
75105 public static MessageUnpacker newDefaultUnpacker (InputStream in )
76106 {
77- return defaultFactory .newUnpacker (in );
107+ return new UnpackerConfig () .newUnpacker (in );
78108 }
79109
80110 /**
81- * Equivalent to defaultFactory().newUnpacker( channel).
111+ * Create an unpacker that reads the data from a given channel
82112 *
83113 * @param channel
84114 * @return
85115 */
86116 public static MessageUnpacker newDefaultUnpacker (ReadableByteChannel channel )
87117 {
88- return defaultFactory .newUnpacker (channel );
118+ return new UnpackerConfig () .newUnpacker (channel );
89119 }
90120
91121 /**
92- * Equivalent to defaultFactory().newUnpacker(contents).
122+ * Create an unpacker that reads the data from a given byte array
93123 *
94124 * @param contents
95125 * @return
96126 */
97127 public static MessageUnpacker newDefaultUnpacker (byte [] contents )
98128 {
99- return defaultFactory .newUnpacker (contents );
129+ return new UnpackerConfig () .newUnpacker (contents );
100130 }
101131
102132 /**
103- * Equivalent to defaultFactory().newUnpacker(contents, offset, length).
133+ * Create an unpacker that reads the data from a given byte array [ offset, offset+ length)
104134 *
105135 * @param contents
106136 * @param offset
@@ -109,6 +139,117 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents)
109139 */
110140 public static MessageUnpacker newDefaultUnpacker (byte [] contents , int offset , int length )
111141 {
112- return defaultFactory .newUnpacker (contents , offset , length );
142+ return new UnpackerConfig ().newUnpacker (contents , offset , length );
143+ }
144+
145+ /**
146+ * MessagePacker configuration.
147+ */
148+ public static class PackerConfig
149+ {
150+ public int smallStringOptimizationThreshold = 512 ;
151+
152+ /**
153+ * Create a packer that outputs the packed data to a given output
154+ *
155+ * @param out
156+ * @return
157+ */
158+ public MessagePacker newPacker (MessageBufferOutput out ) {
159+ return new MessagePacker (out , this );
160+ }
161+
162+ /**
163+ * Create a packer that outputs the packed data to a given output stream
164+ *
165+ * @param out
166+ * @return
167+ */
168+ public MessagePacker newPacker (OutputStream out ) {
169+ return newPacker (new OutputStreamBufferOutput (out ));
170+ }
171+
172+ /**
173+ * Create a packer that outputs the packed data to a given output channel
174+ *
175+ * @param channel
176+ * @return
177+ */
178+ public MessagePacker newPacker (WritableByteChannel channel ) {
179+ return newPacker (new ChannelBufferOutput (channel ));
180+ }
181+
182+ /**
183+ * Create a packer for storing packed data into a byte array
184+ *
185+ * @return
186+ */
187+ public MessageBufferPacker newBufferPacker () {
188+ return new MessageBufferPacker (this );
189+ }
190+ }
191+
192+ /**
193+ * MessageUnpacker configuration.
194+ */
195+ public static class UnpackerConfig
196+ {
197+ public boolean allowStringAsBinary = true ;
198+ public boolean allowBinaryAsString = true ;
199+ public CodingErrorAction actionOnMalformedString = CodingErrorAction .REPLACE ;
200+ public CodingErrorAction actionOnUnmappableString = CodingErrorAction .REPLACE ;
201+ public int stringSizeLimit = Integer .MAX_VALUE ;
202+ public int stringDecoderBufferSize = 8192 ;
203+
204+ /**
205+ * Create an unpacker that reads the data from a given input
206+ *
207+ * @param in
208+ * @return
209+ */
210+ public MessageUnpacker newUnpacker (MessageBufferInput in ) {
211+ return new MessageUnpacker (in , this );
212+ }
213+
214+ /**
215+ * Create an unpacker that reads the data from a given input stream
216+ *
217+ * @param in
218+ * @return
219+ */
220+ public MessageUnpacker newUnpacker (InputStream in ) {
221+ return newUnpacker (new InputStreamBufferInput (in ));
222+ }
223+
224+ /**
225+ * Create an unpacker that reads the data from a given channel
226+ *
227+ * @param channel
228+ * @return
229+ */
230+ public MessageUnpacker newUnpacker (ReadableByteChannel channel ) {
231+ return newUnpacker (new ChannelBufferInput (channel ));
232+ }
233+
234+ /**
235+ * Create an unpacker that reads the data from a given byte array
236+ *
237+ * @param contents
238+ * @return
239+ */
240+ public MessageUnpacker newUnpacker (byte [] contents ) {
241+ return newUnpacker (new ArrayBufferInput (contents ));
242+ }
243+
244+ /**
245+ * Create an unpacker that reads the data from a given byte array [offset, offset+size)
246+ *
247+ * @param contents
248+ * @return
249+ */
250+ public MessageUnpacker newUnpacker (byte [] contents , int offset , int length ) {
251+ return newUnpacker (new ArrayBufferInput (contents , offset , length ));
252+ }
253+
113254 }
114255}
0 commit comments