org.msgpack.MessagePack Java Examples

The following examples show how to use org.msgpack.MessagePack. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WriterFactory.java    From transit-java with Apache License 2.0 6 votes vote down vote up
public static <T> Writer<T> getMsgpackInstance(final OutputStream out, Map<Class, WriteHandler<?,?>> customHandlers, WriteHandler<?, ?> defaultWriteHandler, Function<Object,Object> transform) throws IOException {

        Packer packer = new MessagePack().createPacker(out);

        final MsgpackEmitter emitter = new MsgpackEmitter(packer, handlerMap(customHandlers), defaultWriteHandler, transform);

        final WriteCache writeCache = new WriteCache(true);

        return new Writer<T>() {
            @Override
            public void write(T o) {
                try {
                    emitter.emit(o, false, writeCache.init());
                    out.flush();
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }
 
Example #2
Source File: LaserOfflineTopNDriver.java    From laser with Apache License 2.0 6 votes vote down vote up
public static int serializeClusteringInfo(Path path, String urlList,
		Integer port, Configuration conf) throws IOException {
	FileSystem fs = path.getFileSystem(conf);
	if (fs.exists(path)) {
		return 0;
	}
	
	MsgpackClient client = new MsgpackClient(urlList, port, conf.get("com.b5m.msgpack.collection"));
	client.setTimeout(1000);
	Value res = client.read(new Object[0], "getClusteringInfos");
	AdClusteringsInfo response = new org.msgpack.unpacker.Converter(
			new MessagePack(), res).read(AdClusteringsInfo.class);

	DataOutputStream out = fs.create(path);
	response.write(out);
	out.close();
	return 0;
}
 
Example #3
Source File: ParamUnpacker.java    From CNNdroid with MIT License 6 votes vote down vote up
private Unpacker read_parameters_MessagePack_Init(String name) {
    MessagePack msgpack = new MessagePack();
    File f = new File(name);
    byte[] nbytes = null;

    try {
        FileInputStream fin = new FileInputStream(f);
        nbytes = new byte[(int)f.length()];
        fin.read(nbytes);
        fin.close();

    } catch (Exception e) {
        Log.d("MessagePack_Init:", e.getMessage());
        return null;
    }

    ByteArrayInputStream in = new ByteArrayInputStream(nbytes);
    org.msgpack.unpacker.Unpacker unpack = msgpack.createUnpacker(in);
    return unpack;
}
 
Example #4
Source File: Metasploit.java    From TrackRay with GNU General Public License v3.0 5 votes vote down vote up
public Value unpack(byte[] obj) {
    try {
        return MessagePack.unpack(obj);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #5
Source File: EventLoop.java    From msgpack-rpc-java with Apache License 2.0 5 votes vote down vote up
public EventLoop(ExecutorService workerExecutor, ExecutorService ioExecutor,
        ScheduledExecutorService scheduledExecutor, MessagePack messagePack) {
    this.workerExecutor = workerExecutor;
    this.scheduledExecutor = scheduledExecutor;
    this.ioExecutor = ioExecutor;
    this.messagePack = messagePack;
}
 
Example #6
Source File: MemcachedClientBuilder.java    From ob1k with Apache License 2.0 5 votes vote down vote up
/**
 * Create a client builder for MessagePack values.
 * @return The builder
 */
public static <T> MemcachedClientBuilder<T> newMessagePackClient(final MessagePack messagePack, final Class<T> valueType) {
  if (!ClassUtils.isPrimitiveOrWrapper(valueType)) {
    messagePack.register(valueType);
  }
  return newClient(new MessagePackTranscoder<>(messagePack, valueType));
}
 
Example #7
Source File: TransitMPTest.java    From transit-java with Apache License 2.0 5 votes vote down vote up
public Reader readerOf(Object... things) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MessagePack msgpack = new MessagePack();
    Packer packer = msgpack.createPacker(out);

    for (Object o : things) {
        packer.write(o);
    }

    InputStream in = new ByteArrayInputStream(out.toByteArray());
    return TransitFactory.reader(TransitFactory.Format.MSGPACK, in);

}
 
Example #8
Source File: TestMessagePack.java    From springJredisCache with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
    public void test() throws IOException {

        // Create serialize objects.
        List<String> src = new ArrayList<String>();
        src.add("msgpack");
        src.add("kumofs");
        src.add("viver");

        MessagePack msgpack = new MessagePack();
// Serialize
        byte[] raw = msgpack.write(src);

// Deserialize directly using a template
        List<String> dst1 = msgpack.read(raw, Templates.tList(Templates.TString));
        System.out.println(dst1.get(0));
        System.out.println(dst1.get(1));
        System.out.println(dst1.get(2));

// Or, Deserialze to Value then convert type.
        Value dynamic = msgpack.read(raw);
        List<String> dst2 = new Converter(dynamic)
                .read(Templates.tList(Templates.TString));
        System.out.println(dst2.get(0));
        System.out.println(dst2.get(1));
        System.out.println(dst2.get(2));
    }
 
Example #9
Source File: NettyEventLoop.java    From msgpack-rpc-java with Apache License 2.0 5 votes vote down vote up
public NettyEventLoop(ExecutorService workerExecutor,
        ExecutorService ioExecutor,
        ScheduledExecutorService scheduledExecutor, MessagePack messagePack) {
    super(workerExecutor, ioExecutor, scheduledExecutor, messagePack);
}
 
Example #10
Source File: MsgpackExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {

        TestUser user = new TestUser();
        MessagePack messagePack = new MessagePack();

        //序列化
        byte[] bs = messagePack.write(user);

        //反序列化
        user = messagePack.read(bs, TestUser.class);
    }
 
Example #11
Source File: Client.java    From push with Apache License 2.0 5 votes vote down vote up
public void sendMessage(Message message) {
    Packet packet = new Packet(message.getCmd(), ClientInfo.getSessionId());
    try {
        packet.setBody(new MessagePack().write(message));
        channel.writeAndFlush(packet);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: AbstractHandler.java    From push with Apache License 2.0 5 votes vote down vote up
public T decode(Packet packet) {
    try {
        return new MessagePack().read(packet.getBody(), getType());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #13
Source File: Metasploit.java    From TrackRay with GNU General Public License v3.0 5 votes vote down vote up
public byte[] pack(Object obj) {
    try {
        return MessagePack.pack( obj );
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #14
Source File: MsgPackDecode.java    From push with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    final int length = msg.readableBytes();
    final byte[] array = new byte[length];
    msg.getBytes(msg.readerIndex(), array, 0, length);
    out.add(new MessagePack().read(array, Packet.class));
}
 
Example #15
Source File: EventLoop.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
static public EventLoop start(ExecutorService workerExecutor, ExecutorService ioExecutor) {
    return start(workerExecutor, ioExecutor,
            Executors.newScheduledThreadPool(2), new MessagePack());
}
 
Example #16
Source File: DefaultDispatcherBuilder.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public Dispatcher build(Object handler, MessagePack messagePack) {
    return new MethodDispatcher(
            new Reflect(messagePack), handler);
}
 
Example #17
Source File: EventLoop.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
static public EventLoop start(
        ExecutorService workerExecutor, ExecutorService ioExecutor,
        ScheduledExecutorService scheduledExecutor, MessagePack messagePack) {
    return getFactory().make(workerExecutor, ioExecutor, scheduledExecutor, messagePack);
}
 
Example #18
Source File: EventLoop.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public MessagePack getMessagePack() {
    return messagePack;
}
 
Example #19
Source File: EventLoop.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public void setMessagePack(MessagePack messagePack) {
    this.messagePack = messagePack;
}
 
Example #20
Source File: EventLoop.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
static public EventLoop start(MessagePack messagePack) {
    return start(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(),
            Executors.newScheduledThreadPool(2), messagePack);
}
 
Example #21
Source File: EventLoopFactory.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public EventLoop make(ExecutorService workerExecutor, ExecutorService ioExecutor,
ScheduledExecutorService scheduledExecutor, MessagePack messagePack);
 
Example #22
Source File: Future.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
Future(MessagePack messagePack, FutureImpl impl) {
    this(messagePack, impl, null);
}
 
Example #23
Source File: Future.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
Future(MessagePack messagePack, FutureImpl impl, Template resultTemplate) {
    this.impl = impl;
    this.resultTemplate = resultTemplate;
    this.messagePack = messagePack;
}
 
Example #24
Source File: Future.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public Future(MessagePack messagePack, Future<Value> future, Class<V> resultClass) {
    this(messagePack, future.impl, null);
    if (resultClass != void.class) {
        this.resultTemplate = messagePack.lookup(resultClass);
    }
}
 
Example #25
Source File: Future.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public Future(MessagePack messagePack, Future<Value> future, Template resultTemplate) {
    this(messagePack, future.impl, resultTemplate);
}
 
Example #26
Source File: Reflect.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public Reflect(MessagePack messagePack) {
    invokerBuilder = new ReflectionInvokerBuilder(messagePack);
    proxyBuilder = new ReflectionProxyBuilder(messagePack);
}
 
Example #27
Source File: JavassistInvokerBuilder.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public JavassistInvokerBuilder(MessagePack messagePack) {
    pool = ClassPool.getDefault();
    this.messagePack = messagePack;
}
 
Example #28
Source File: StopWatchDispatcherBuilder.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public Dispatcher build(Object handler, MessagePack messagePack) {
    return decorate(baseBuilder.build(handler,messagePack));
}
 
Example #29
Source File: MessagePackEncoder.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public MessagePackEncoder(int estimatedLength, MessagePack messagePack) {
    this.estimatedLength = estimatedLength;
    this.messagePack = messagePack;
}
 
Example #30
Source File: MessagePackEncoder.java    From msgpack-rpc-java with Apache License 2.0 4 votes vote down vote up
public MessagePackEncoder(MessagePack messagePack) {
    this(1024, messagePack);
}