Skip to content

Commit cb918ab

Browse files
committed
Merge pull request msgpack#146 from msgpack/v07-add-jackson-dataformat
V07 add jackson dataformat
2 parents d59d731 + fcb9ae3 commit cb918ab

22 files changed

+2096
-179
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ script: sbt ++$TRAVIS_SCALA_VERSION test
1717

1818
notifications:
1919
email:
20-
- muga.nishizawa@gmail.com
2120
- ozawa.tsuyoshi@gmail.com
2221
- leo@xerial.org
22+
- komamitsu@gmail.com

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ For sbt users:
2323
libraryDependencies += "org.msgpack" % "msgpack-core" % "0.7.0-p1"
2424
```
2525

26+
If you want to use MessagePack through [jackson-databind](https://github.com/FasterXML/jackson-databind), you can use *jackson-dataformat-msgpack*. Please read [README](msgpack-jackson/README.md) to know how to use it.
2627
- [Usage examples](msgpack-core/src/main/java/org/msgpack/core/example/MessagePackExample.java)
2728
- TODO: jackson-databind-msgpack usage
2829

@@ -74,4 +75,5 @@ credentials += Credentials("Sonatype Nexus Repository Manager",
7475

7576
```
7677
msgpack-core # Contains packer/unpacker implementation that never uses third-party libraries
78+
msgpack-jackson # Contains jackson-dataformat-java implementation
7779
```

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

Lines changed: 156 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
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+
//
116
package org.msgpack.core;
217

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

20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
import java.nio.channels.ReadableByteChannel;
23+
import java.nio.channels.WritableByteChannel;
524
import java.nio.charset.Charset;
625
import java.nio.charset.CodingErrorAction;
726

827
import static org.msgpack.core.Preconditions.checkArgument;
928

1029
/**
11-
* Includes MessagePack codes
30+
* This class has MessagePack prefix code definitions and packer/unpacker factory methods.
1231
*
1332
*/
1433
public class MessagePack {
@@ -93,8 +112,8 @@ public static class ConfigBuilder {
93112
private boolean readStringAsBinary = true;
94113
private boolean readBinaryAsString = true;
95114

96-
private CodingErrorAction onMalFormedInput = CodingErrorAction.REPORT;
97-
private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPORT;
115+
private CodingErrorAction onMalFormedInput = CodingErrorAction.REPLACE;
116+
private CodingErrorAction onUnmappableCharacter = CodingErrorAction.REPLACE;
98117

99118
private int maxUnpackStringSize = Integer.MAX_VALUE;
100119
private int stringEncoderBufferSize = 8192;
@@ -239,4 +258,137 @@ public static final boolean isFixedRaw(byte b) {
239258
public static final byte NEGFIXINT_PREFIX = (byte) 0xe0;
240259
}
241260

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+
242394
}

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

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

0 commit comments

Comments
 (0)