Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace MessagePackFactory with mutable Packer/UnpackerConfig-based code
  • Loading branch information
xerial committed Jan 4, 2016
commit d9dfb062f1aaa0a6d33b343b91c23e66048910cf
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@
import java.io.IOException;
import java.util.List;

/**
* MessagePacker that is useful to produce byte array output
*/
public class MessageBufferPacker
extends MessagePacker
{
public MessageBufferPacker()
public MessageBufferPacker(MessagePack.PackerConfig config)
{
this(new ArrayBufferOutput());
this(new ArrayBufferOutput(), config);
}

public MessageBufferPacker(ArrayBufferOutput out)
public MessageBufferPacker(ArrayBufferOutput out, MessagePack.PackerConfig config)
{
super(out);
}

@Override
public MessageBufferPacker setSmallStringOptimizationThreshold(int bytes)
{
super.setSmallStringOptimizationThreshold(bytes);
return this;
super(out, config);
}

public MessageBufferOutput reset(MessageBufferOutput out)
Expand All @@ -51,23 +47,27 @@ public MessageBufferOutput reset(MessageBufferOutput out)
return super.reset(out);
}

private ArrayBufferOutput getArrayBufferOut() {
return (ArrayBufferOutput) out;
}

public void clear()
{
((ArrayBufferOutput) out).clear();
getArrayBufferOut().clear();
}

public byte[] toByteArray()
{
return ((ArrayBufferOutput) out).toByteArray();
return getArrayBufferOut().toByteArray();
}

public MessageBuffer toMessageBuffer()
{
return ((ArrayBufferOutput) out).toMessageBuffer();
return getArrayBufferOut().toMessageBuffer();
}

public List<MessageBuffer> toBufferList()
{
return ((ArrayBufferOutput) out).toBufferList();
return getArrayBufferOut().toBufferList();
}
}
173 changes: 157 additions & 16 deletions msgpack-core/src/main/java/org/msgpack/core/MessagePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@
//
package org.msgpack.core;

import org.msgpack.core.buffer.ArrayBufferInput;
import org.msgpack.core.buffer.ChannelBufferInput;
import org.msgpack.core.buffer.ChannelBufferOutput;
import org.msgpack.core.buffer.InputStreamBufferInput;
import org.msgpack.core.buffer.MessageBufferInput;
import org.msgpack.core.buffer.MessageBufferOutput;
import org.msgpack.core.buffer.OutputStreamBufferOutput;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;

/**
* This class has MessagePack prefix code definitions and packer/unpacker factory methods.
Expand All @@ -28,79 +37,100 @@ public class MessagePack
{
public static final Charset UTF8 = Charset.forName("UTF-8");

private final static MessagePackFactory defaultFactory = new MessagePackFactory();

private MessagePack()
{
// Prohibit instantiation of this class
}

/**
* Create a packer that outputs the packed data to the specified output
*
* @param out
* @return
*/
public static MessagePacker newDefaultPacker(MessageBufferOutput out)
{
return new PackerConfig().newPacker(out);
}

/**
* Equivalent to defaultFactory().newPacker(out).
* Create a packer that outputs the packed data to a target output stream
*
* @param out
* @return
*/
public static MessagePacker newDefaultPacker(OutputStream out)
{
return defaultFactory.newPacker(out);
return new PackerConfig().newPacker(out);
}

/**
* Equivalent to defaultFactory().newPacker(channel).
* Create a packer that outputs the packed data to a channel
*
* @param channel
* @return
*/
public static MessagePacker newDefaultPacker(WritableByteChannel channel)
{
return defaultFactory.newPacker(channel);
return new PackerConfig().newPacker(channel);
}

/**
* Equivalent to defaultFactory().newBufferPacker()
* Create a packer for storing packed data into a byte array
*
* @return
*/
public static MessageBufferPacker newDefaultBufferPacker()
{
return defaultFactory.newBufferPacker();
return new PackerConfig().newBufferPacker();
}

/**
* Equivalent to defaultFactory().newUnpacker(in).
* Create an unpacker that reads the data from a given input
*
* @param in
* @return
*/
public static MessageUnpacker newDefaultUnpacker(MessageBufferInput in)
{
return new UnpackerConfig().newUnpacker(in);
}

/**
* Create an unpacker that reads the data from a given input stream
*
* @param in
* @return
*/
public static MessageUnpacker newDefaultUnpacker(InputStream in)
{
return defaultFactory.newUnpacker(in);
return new UnpackerConfig().newUnpacker(in);
}

/**
* Equivalent to defaultFactory().newUnpacker(channel).
* Create an unpacker that reads the data from a given channel
*
* @param channel
* @return
*/
public static MessageUnpacker newDefaultUnpacker(ReadableByteChannel channel)
{
return defaultFactory.newUnpacker(channel);
return new UnpackerConfig().newUnpacker(channel);
}

/**
* Equivalent to defaultFactory().newUnpacker(contents).
* Create an unpacker that reads the data from a given byte array
*
* @param contents
* @return
*/
public static MessageUnpacker newDefaultUnpacker(byte[] contents)
{
return defaultFactory.newUnpacker(contents);
return new UnpackerConfig().newUnpacker(contents);
}

/**
* Equivalent to defaultFactory().newUnpacker(contents, offset, length).
* Create an unpacker that reads the data from a given byte array [offset, offset+length)
*
* @param contents
* @param offset
Expand All @@ -109,6 +139,117 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents)
*/
public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, int length)
{
return defaultFactory.newUnpacker(contents, offset, length);
return new UnpackerConfig().newUnpacker(contents, offset, length);
}

/**
* MessagePacker configuration.
*/
public static class PackerConfig
{
public int smallStringOptimizationThreshold = 512;

/**
* Create a packer that outputs the packed data to a given output
*
* @param out
* @return
*/
public MessagePacker newPacker(MessageBufferOutput out) {
return new MessagePacker(out, this);
}

/**
* Create a packer that outputs the packed data to a given output stream
*
* @param out
* @return
*/
public MessagePacker newPacker(OutputStream out) {
return newPacker(new OutputStreamBufferOutput(out));
}

/**
* Create a packer that outputs the packed data to a given output channel
*
* @param channel
* @return
*/
public MessagePacker newPacker(WritableByteChannel channel) {
return newPacker(new ChannelBufferOutput(channel));
}

/**
* Create a packer for storing packed data into a byte array
*
* @return
*/
public MessageBufferPacker newBufferPacker() {
return new MessageBufferPacker(this);
}
}

/**
* MessageUnpacker configuration.
*/
public static class UnpackerConfig
{
public boolean allowStringAsBinary = true;
public boolean allowBinaryAsString = true;
public CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE;
public CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE;
public int stringSizeLimit = Integer.MAX_VALUE;
public int stringDecoderBufferSize = 8192;

/**
* Create an unpacker that reads the data from a given input
*
* @param in
* @return
*/
public MessageUnpacker newUnpacker(MessageBufferInput in) {
return new MessageUnpacker(in, this);
}

/**
* Create an unpacker that reads the data from a given input stream
*
* @param in
* @return
*/
public MessageUnpacker newUnpacker(InputStream in) {
return newUnpacker(new InputStreamBufferInput(in));
}

/**
* Create an unpacker that reads the data from a given channel
*
* @param channel
* @return
*/
public MessageUnpacker newUnpacker(ReadableByteChannel channel) {
return newUnpacker(new ChannelBufferInput(channel));
}

/**
* Create an unpacker that reads the data from a given byte array
*
* @param contents
* @return
*/
public MessageUnpacker newUnpacker(byte[] contents) {
return newUnpacker(new ArrayBufferInput(contents));
}

/**
* Create an unpacker that reads the data from a given byte array [offset, offset+size)
*
* @param contents
* @return
*/
public MessageUnpacker newUnpacker(byte[] contents, int offset, int length) {
return newUnpacker(new ArrayBufferInput(contents, offset, length));
}

}
}
Loading