Skip to content

Instantly share code, notes, and snippets.

@hongjiang
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongjiang/287b3afe408d2bcf1d15 to your computer and use it in GitHub Desktop.
Save hongjiang/287b3afe408d2bcf1d15 to your computer and use it in GitHub Desktop.
test_msgpack
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import org.msgpack.MessagePack;
public class MessagePackReader {
public static void test1() throws Exception {
MessagePack msgpack = new MessagePack();
// Serialize
byte[] bytes = msgpack.write("中文测试");
// Deserialize
System.out.println(msgpack.read(bytes, String.class));
}
public static byte[] readHttp() throws Exception {
String urlStr = "http://192.168.2.4:8080/..."; //FIXME
URL url = new URL(urlStr);
BufferedInputStream input = null;
BufferedOutputStream output = null;
byte[] buf = null;
try {
buf = new byte[128];
input = new BufferedInputStream(url.openStream());
ByteArrayOutputStream outBuf = new ByteArrayOutputStream(128);
output = new BufferedOutputStream(outBuf);
int len = 0;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
}
output.flush();
buf = outBuf.toByteArray();
System.out.println(buf.length);
} finally {
input.close();
output.close();
}
return buf;
}
public static void testByString() throws Exception {
byte[] buf = readHttp();
System.out.println(new String(buf, "utf-8"));
}
public static void testByMsgpack() throws Exception {
byte[] buf = readHttp();
MessagePack msgpack = new MessagePack();
System.out.println(msgpack.read(buf, MyStringMsg.class).msg);
}
public static void main(String[] args) throws Exception {
testByMsgpack();
}
}
@hongjiang
Copy link
Author

@message
public class MyStringMsg {
public String msg;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment