Last active
August 29, 2015 14:07
-
-
Save hongjiang/287b3afe408d2bcf1d15 to your computer and use it in GitHub Desktop.
test_msgpack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@message
public class MyStringMsg {
public String msg;
}