|
| 1 | +// |
| 2 | +// MessagePack for Java |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
1 | 16 | package org.msgpack.core; |
2 | 17 |
|
3 | | -import org.msgpack.core.buffer.ArrayBufferInput; |
| 18 | +import org.msgpack.core.buffer.*; |
4 | 19 |
|
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | +import java.nio.channels.ReadableByteChannel; |
| 23 | +import java.nio.channels.WritableByteChannel; |
5 | 24 | import java.nio.charset.Charset; |
6 | 25 | import java.nio.charset.CodingErrorAction; |
7 | 26 |
|
8 | 27 | import static org.msgpack.core.Preconditions.checkArgument; |
9 | 28 |
|
10 | 29 | /** |
11 | | - * Includes MessagePack codes |
| 30 | + * This class has MessagePack prefix code definitions and packer/unpacker factory methods. |
12 | 31 | * |
13 | 32 | */ |
14 | 33 | public class MessagePack { |
@@ -93,8 +112,8 @@ public static class ConfigBuilder { |
93 | 112 | private boolean readStringAsBinary = true; |
94 | 113 | private boolean readBinaryAsString = true; |
95 | 114 |
|
96 | | - private CodingErrorAction onMalFormedInput = CodingErrorAction.REPORT; |
97 | | - private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPORT; |
| 115 | + private CodingErrorAction onMalFormedInput = CodingErrorAction.REPLACE; |
| 116 | + private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPLACE; |
98 | 117 |
|
99 | 118 | private int maxUnpackStringSize = Integer.MAX_VALUE; |
100 | 119 | private int stringEncoderBufferSize = 8192; |
@@ -239,4 +258,137 @@ public static final boolean isFixedRaw(byte b) { |
239 | 258 | public static final byte NEGFIXINT_PREFIX = (byte) 0xe0; |
240 | 259 | } |
241 | 260 |
|
| 261 | + // Packer/Unpacker factory methods |
| 262 | + |
| 263 | + private final MessagePack.Config config; |
| 264 | + |
| 265 | + public MessagePack() { |
| 266 | + this(MessagePack.DEFAULT_CONFIG); |
| 267 | + } |
| 268 | + |
| 269 | + public MessagePack(MessagePack.Config config) { |
| 270 | + this.config = config; |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * Default MessagePack packer/unpacker factory |
| 275 | + */ |
| 276 | + public static final MessagePack DEFAULT = new MessagePack(MessagePack.DEFAULT_CONFIG); |
| 277 | + |
| 278 | + |
| 279 | + /** |
| 280 | + * Create a MessagePacker that outputs the packed data to the specified stream, using the default configuration |
| 281 | + * @param out |
| 282 | + * @return |
| 283 | + */ |
| 284 | + public static MessagePacker newDefaultPacker(OutputStream out) { |
| 285 | + return DEFAULT.newPacker(out); |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Create a MessagePacker that outputs the packed data to the specified channel, using the default configuration |
| 290 | + * @param channel |
| 291 | + * @return |
| 292 | + */ |
| 293 | + public static MessagePacker newDefaultPacker(WritableByteChannel channel) { |
| 294 | + return DEFAULT.newPacker(channel); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * Create a MessageUnpacker that reads data from then given InputStream, using the default configuration |
| 299 | + * @param in |
| 300 | + * @return |
| 301 | + */ |
| 302 | + public static MessageUnpacker newDefaultUnpacker(InputStream in) { |
| 303 | + return DEFAULT.newUnpacker(in); |
| 304 | + } |
| 305 | + |
| 306 | + /** |
| 307 | + * Create a MessageUnpacker that reads data from the given channel, using the default configuration |
| 308 | + * @param channel |
| 309 | + * @return |
| 310 | + */ |
| 311 | + public static MessageUnpacker newDefaultUnpacker(ReadableByteChannel channel) { |
| 312 | + return DEFAULT.newUnpacker(channel); |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * Create a MessageUnpacker that reads data from the given byte array, using the default configuration |
| 317 | + * @param arr |
| 318 | + * @return |
| 319 | + */ |
| 320 | + public static MessageUnpacker newDefaultUnpacker(byte[] arr) { |
| 321 | + return DEFAULT.newUnpacker(arr); |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * Create a MessageUnpacker that reads data form the given byte array [offset, .. offset+length), using the default |
| 326 | + * configuration. |
| 327 | + * @param arr |
| 328 | + * @param offset |
| 329 | + * @param length |
| 330 | + * @return |
| 331 | + */ |
| 332 | + public static MessageUnpacker newDefaultUnpacker(byte[] arr, int offset, int length) { |
| 333 | + return DEFAULT.newUnpacker(arr, offset, length); |
| 334 | + } |
| 335 | + |
| 336 | + |
| 337 | + /** |
| 338 | + * Create a MessagePacker that outputs the packed data to the specified stream |
| 339 | + * @param out |
| 340 | + */ |
| 341 | + public MessagePacker newPacker(OutputStream out) { |
| 342 | + return new MessagePacker(new OutputStreamBufferOutput(out), config); |
| 343 | + } |
| 344 | + |
| 345 | + /** |
| 346 | + * Create a MessagePacker that outputs the packed data to the specified channel |
| 347 | + * @param channel |
| 348 | + */ |
| 349 | + public MessagePacker newPacker(WritableByteChannel channel) { |
| 350 | + return new MessagePacker(new ChannelBufferOutput(channel), config); |
| 351 | + } |
| 352 | + |
| 353 | + /** |
| 354 | + * Create a MessageUnpacker that reads data from the given InputStream. |
| 355 | + * For reading data efficiently from byte[], use {@link MessageUnpacker(byte[])} or {@link MessageUnpacker(byte[], int, int)} instead of this constructor. |
| 356 | + * |
| 357 | + * @param in |
| 358 | + */ |
| 359 | + public MessageUnpacker newUnpacker(InputStream in) { |
| 360 | + return new MessageUnpacker(InputStreamBufferInput.newBufferInput(in), config); |
| 361 | + } |
| 362 | + |
| 363 | + /** |
| 364 | + * Create a MessageUnpacker that reads data from the given ReadableByteChannel. |
| 365 | + * @param in |
| 366 | + */ |
| 367 | + public MessageUnpacker newUnpacker(ReadableByteChannel in) { |
| 368 | + return new MessageUnpacker(new ChannelBufferInput(in), config); |
| 369 | + } |
| 370 | + |
| 371 | + |
| 372 | + /** |
| 373 | + * Create a MessageUnpacker that reads data from the given byte array. |
| 374 | + * |
| 375 | + * @param arr |
| 376 | + */ |
| 377 | + public MessageUnpacker newUnpacker(byte[] arr) { |
| 378 | + return new MessageUnpacker(new ArrayBufferInput(arr), config); |
| 379 | + } |
| 380 | + |
| 381 | + /** |
| 382 | + * Create a MessageUnpacker that reads data from the given byte array [offset, offset+length) |
| 383 | + * @param arr |
| 384 | + * @param offset |
| 385 | + * @param length |
| 386 | + */ |
| 387 | + public MessageUnpacker newUnpacker(byte[] arr, int offset, int length) { |
| 388 | + return new MessageUnpacker(new ArrayBufferInput(arr, offset, length), config); |
| 389 | + } |
| 390 | + |
| 391 | + |
| 392 | + |
| 393 | + |
242 | 394 | } |
0 commit comments