|
1 | 1 | package org.msgpack.core; |
2 | 2 |
|
3 | | -import org.msgpack.core.buffer.ArrayBufferInput; |
| 3 | +import org.msgpack.core.buffer.*; |
4 | 4 |
|
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.nio.channels.ReadableByteChannel; |
| 8 | +import java.nio.channels.WritableByteChannel; |
5 | 9 | import java.nio.charset.Charset; |
6 | 10 | import java.nio.charset.CodingErrorAction; |
7 | 11 |
|
8 | 12 | import static org.msgpack.core.Preconditions.checkArgument; |
9 | 13 |
|
10 | 14 | /** |
11 | | - * Includes MessagePack codes |
| 15 | + * This class has MessagePack prefix code definitions and packer/unpacker factory methods. |
12 | 16 | * |
13 | 17 | */ |
14 | 18 | public class MessagePack { |
@@ -93,8 +97,8 @@ public static class ConfigBuilder { |
93 | 97 | private boolean readStringAsBinary = true; |
94 | 98 | private boolean readBinaryAsString = true; |
95 | 99 |
|
96 | | - private CodingErrorAction onMalFormedInput = CodingErrorAction.REPORT; |
97 | | - private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPORT; |
| 100 | + private CodingErrorAction onMalFormedInput = CodingErrorAction.REPLACE; |
| 101 | + private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPLACE; |
98 | 102 |
|
99 | 103 | private int maxUnpackStringSize = Integer.MAX_VALUE; |
100 | 104 | private int stringEncoderBufferSize = 8192; |
@@ -239,4 +243,137 @@ public static final boolean isFixedRaw(byte b) { |
239 | 243 | public static final byte NEGFIXINT_PREFIX = (byte) 0xe0; |
240 | 244 | } |
241 | 245 |
|
| 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 | + |
242 | 379 | } |
0 commit comments