mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-21 07:45:02 +02:00
Compare commits
516 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
076bd07f39 | ||
![]() |
00e897503b | ||
![]() |
2f3c47f385 | ||
![]() |
2b28211701 | ||
![]() |
46c794fc2e | ||
![]() |
0408738b48 | ||
![]() |
eb8a338a0e | ||
![]() |
23480bfe8a | ||
![]() |
3384dbc92d | ||
![]() |
8446549ed8 | ||
![]() |
6a9f3ae71d | ||
![]() |
c58ce1a975 | ||
![]() |
c6d9bbd7b0 | ||
![]() |
65ddd1a455 | ||
![]() |
58c0fe0f91 | ||
![]() |
b96b62f2ac | ||
![]() |
43fc0a52a6 | ||
![]() |
4f3d9a1ded | ||
![]() |
b50ff920f0 | ||
![]() |
983ae0c5a2 | ||
![]() |
9d4db84987 | ||
![]() |
dfaa281476 | ||
![]() |
c1c25c455c | ||
![]() |
e65197f386 | ||
![]() |
ecbb8f8711 | ||
![]() |
03aa736199 | ||
![]() |
821b1f0384 | ||
![]() |
233ad3aaff | ||
![]() |
9c4812f4d4 | ||
![]() |
337cc9fa78 | ||
![]() |
dfe29a0329 | ||
![]() |
85e90aa81e | ||
![]() |
86d30fe571 | ||
![]() |
173302267f | ||
![]() |
8e3ea8d26c | ||
![]() |
cb065563c2 | ||
![]() |
27b89b237b | ||
![]() |
21f0d0bfc4 | ||
![]() |
d5e583b09e | ||
![]() |
15fb9bbcb2 | ||
![]() |
a03418ab12 | ||
![]() |
035513844d | ||
![]() |
193906b8ce | ||
![]() |
d354f2e8c2 | ||
![]() |
24a8ee436f | ||
![]() |
6e5f9404b7 | ||
![]() |
64fe90aabb | ||
![]() |
7e31d487e0 | ||
![]() |
ee16e2eb30 | ||
![]() |
003fb3392f | ||
![]() |
0e82278cd5 | ||
![]() |
5b544248bc | ||
![]() |
d4d7495c0a | ||
![]() |
8325cce44c | ||
![]() |
ee0debc5b3 | ||
![]() |
8f1bd12a17 | ||
![]() |
fccf1016e4 | ||
![]() |
1b010a082b | ||
![]() |
324f215316 | ||
![]() |
ace4f1e7f0 | ||
![]() |
5c5f16f148 | ||
![]() |
f50855d5c0 | ||
![]() |
86917b0ba7 | ||
![]() |
7201fcbe0f | ||
![]() |
83f1735fbb | ||
![]() |
80fd8e70f0 | ||
![]() |
698c1c2b7a | ||
![]() |
5d3287f5aa | ||
![]() |
6990fe6f51 | ||
![]() |
0b81068368 | ||
![]() |
5b1851ae22 | ||
![]() |
0d5708a01d | ||
![]() |
67ab510b5d | ||
![]() |
d1264a1289 | ||
![]() |
3b973021a1 | ||
![]() |
182624895f | ||
![]() |
548de3739c | ||
![]() |
718a3efd64 | ||
![]() |
76b8c3250b | ||
![]() |
dafaa8bd8c | ||
![]() |
9c9b2c25ea | ||
![]() |
e89cd81022 | ||
![]() |
5debbd2be8 | ||
![]() |
fe26df5355 | ||
![]() |
85778494e4 | ||
![]() |
a94ce3c715 | ||
![]() |
a440ff1117 | ||
![]() |
8195137bc5 | ||
![]() |
3e2bd25e4e | ||
![]() |
ffd0c2f624 | ||
![]() |
23333c98d7 | ||
![]() |
5a12ab98c4 | ||
![]() |
0b327a63fc | ||
![]() |
15b28a245c | ||
![]() |
6ef5684797 | ||
![]() |
d439b1495b | ||
![]() |
9e096a3f0e | ||
![]() |
0acf6ec150 | ||
![]() |
4b36340474 | ||
![]() |
910e642a8b | ||
![]() |
9f571146fb | ||
![]() |
339725f73d | ||
![]() |
aff964c58b | ||
![]() |
0c07e745f8 | ||
![]() |
419d2e9564 | ||
![]() |
f936a307c6 | ||
![]() |
4e4678edfa | ||
![]() |
cb7a4b3116 | ||
![]() |
5ca4c42a74 | ||
![]() |
1238a7ca07 | ||
![]() |
a9db60a73d | ||
![]() |
bd32ac19d4 | ||
![]() |
eaf9944e43 | ||
![]() |
703fdbc01d | ||
![]() |
2daa08b0e7 | ||
![]() |
05d9d22d9e | ||
![]() |
8b7894f9bd | ||
![]() |
a71439607f | ||
![]() |
aca0d7f969 | ||
![]() |
310a8e4342 | ||
![]() |
24fbe1ef5b | ||
![]() |
33d8faa35d | ||
![]() |
44be714f65 | ||
![]() |
0df3da6b10 | ||
![]() |
79197b6ec7 | ||
![]() |
353b6b51cb | ||
![]() |
bd9a2c0d3a | ||
![]() |
53b0ee6536 | ||
![]() |
78daac0f1b | ||
![]() |
b970b9b9a8 | ||
![]() |
0339db57f1 | ||
![]() |
461b147897 | ||
![]() |
a3cd13b399 | ||
![]() |
eb5d326a1e | ||
![]() |
cce6eb94e2 | ||
![]() |
8311c72c69 | ||
![]() |
9a059285d9 | ||
![]() |
469ac7891d | ||
![]() |
16264a5693 | ||
![]() |
6eedb50f56 | ||
![]() |
5f07215662 | ||
![]() |
d3a02fb2ca | ||
![]() |
2c823f1aaa | ||
![]() |
706293aadc | ||
![]() |
dfdceb4258 | ||
![]() |
0014b7fdb9 | ||
![]() |
3b28f1f8af | ||
![]() |
c689d476ca | ||
![]() |
cc534fd21f | ||
![]() |
124efcf247 | ||
![]() |
dd452b05e3 | ||
![]() |
b5617f7df9 | ||
![]() |
b73ca1ba3a | ||
![]() |
39ad071c4f | ||
![]() |
fa0b576a45 | ||
![]() |
c2c7591987 | ||
![]() |
3e939e3775 | ||
![]() |
f6384e10bc | ||
![]() |
c283842563 | ||
![]() |
1135976225 | ||
![]() |
76679d33df | ||
![]() |
e9d44b90bc | ||
![]() |
8a7a391166 | ||
![]() |
d6c5a9eece | ||
![]() |
56ad6915d0 | ||
![]() |
466c260782 | ||
![]() |
d8e2d1725a | ||
![]() |
33b43d03ac | ||
![]() |
517509db6e | ||
![]() |
95f01a5976 | ||
![]() |
d08d3bf56d | ||
![]() |
587fd669e8 | ||
![]() |
e3553b87fe | ||
![]() |
a078d2360c | ||
![]() |
2d3abf8e6e | ||
![]() |
cacae0fb7d | ||
![]() |
d7fc215c16 | ||
![]() |
bb69aa18f2 | ||
![]() |
e4f515166a | ||
![]() |
f325acb0ea | ||
![]() |
d1b6e65dd8 | ||
![]() |
4688252bd4 | ||
![]() |
b1df5d3ad7 | ||
![]() |
09bad2938e | ||
![]() |
bad69fd397 | ||
![]() |
b4ae6bf82c | ||
![]() |
eac0f83864 | ||
![]() |
3f16f080ac | ||
![]() |
5de2b974fb | ||
![]() |
6a9cb51828 | ||
![]() |
f1c294ca50 | ||
![]() |
c320e44a23 | ||
![]() |
ea36ef3107 | ||
![]() |
fedc37d079 | ||
![]() |
86ccfcc03c | ||
![]() |
bc8d8ab65a | ||
![]() |
a7a23d3bc8 | ||
![]() |
82d33944e6 | ||
![]() |
8a629ad6fb | ||
![]() |
e1711ffcf2 | ||
![]() |
063d51c662 | ||
![]() |
d8e3575a46 | ||
![]() |
6fa609be3f | ||
![]() |
70982e204c | ||
![]() |
2af7df5865 | ||
![]() |
59610e81de | ||
![]() |
cdfac703ef | ||
![]() |
ad5ebd007e | ||
![]() |
1bd347d997 | ||
![]() |
147056073d | ||
![]() |
1b3231e617 | ||
![]() |
7ac4ad3e38 | ||
![]() |
0a345cb12b | ||
![]() |
19ff0dd17f | ||
![]() |
e3bf8a404b | ||
![]() |
dbb28d9a8f | ||
![]() |
19fd4e755c | ||
![]() |
77698cd924 | ||
![]() |
945d279f28 | ||
![]() |
645d296841 | ||
![]() |
57446de875 | ||
![]() |
3473800ab6 | ||
![]() |
7e5c5153a8 | ||
![]() |
1b8979f285 | ||
![]() |
2aac51dd20 | ||
![]() |
69e32d264c | ||
![]() |
71ae75a5bf | ||
![]() |
86043fd87e | ||
![]() |
d8aaef4f04 | ||
![]() |
7fc34b6369 | ||
![]() |
92b346efa7 | ||
![]() |
c790735b9f | ||
![]() |
64711e615e | ||
![]() |
2065affd45 | ||
![]() |
4067c56b5d | ||
![]() |
65515638aa | ||
![]() |
371ba3ffe3 | ||
![]() |
5395b62f2f | ||
![]() |
3e164eab26 | ||
![]() |
ad85533975 | ||
![]() |
d6c2a97931 | ||
![]() |
17def94ba4 | ||
![]() |
cf254ea240 | ||
![]() |
2d05110239 | ||
![]() |
0017f4fce8 | ||
![]() |
02f3dd947a | ||
![]() |
ef0874feba | ||
![]() |
14aa1420f0 | ||
![]() |
c506cd97e0 | ||
![]() |
3761aacb1d | ||
![]() |
233f13aac5 | ||
![]() |
0ced3ec2d2 | ||
![]() |
5b786f65a4 | ||
![]() |
770542c8c7 | ||
![]() |
4f1207a38c | ||
![]() |
2b65f81e23 | ||
![]() |
c10eb2c17b | ||
![]() |
3f5ac54bf5 | ||
![]() |
790f9409b1 | ||
![]() |
a3d831b723 | ||
![]() |
b9483deea3 | ||
![]() |
c211d2ac45 | ||
![]() |
119a03dd5f | ||
![]() |
f3fbb7e1f6 | ||
![]() |
2be98e8467 | ||
![]() |
91b1806e9d | ||
![]() |
8548b3645a | ||
![]() |
4bbfb3f9a3 | ||
![]() |
98eec72522 | ||
![]() |
562d50df4d | ||
![]() |
f3ee5ab372 | ||
![]() |
aa2a3e5b07 | ||
![]() |
8467307239 | ||
![]() |
90da951a6f | ||
![]() |
59f81b331c | ||
![]() |
77a7d3d26a | ||
![]() |
4321b80999 | ||
![]() |
4b93b06323 | ||
![]() |
96fb2e4b7c | ||
![]() |
3dc1048dfb | ||
![]() |
28f4bd5a67 | ||
![]() |
b01c270889 | ||
![]() |
7c92f8a90b | ||
![]() |
a4a04872a3 | ||
![]() |
f2d13cd647 | ||
![]() |
9346908485 | ||
![]() |
b3a7ba14f7 | ||
![]() |
524ef9553c | ||
![]() |
a85f6e72fe | ||
![]() |
1ae6a41336 | ||
![]() |
3ec55791c6 | ||
![]() |
a3accd28ea | ||
![]() |
fc5bc84207 | ||
![]() |
d8b4051d6d | ||
![]() |
804a1cc22d | ||
![]() |
cdd60e5f9c | ||
![]() |
7c76f07384 | ||
![]() |
a3b1ef9527 | ||
![]() |
0bd4150a80 | ||
![]() |
7f7f5253f2 | ||
![]() |
2a0a847634 | ||
![]() |
92ddb37ed3 | ||
![]() |
01f944e6bd | ||
![]() |
732c8d7350 | ||
![]() |
21678aeef5 | ||
![]() |
cda1ca35a4 | ||
![]() |
923580d2cd | ||
![]() |
190af1d32b | ||
![]() |
bffe0443f9 | ||
![]() |
29e99e229b | ||
![]() |
2736b88dd5 | ||
![]() |
e739c60e9f | ||
![]() |
1c0afbc5c5 | ||
![]() |
ee1ba5c0f2 | ||
![]() |
02342ba540 | ||
![]() |
12a130e9bc | ||
![]() |
dfb97e7961 | ||
![]() |
54e03a62bd | ||
![]() |
ec9659ff25 | ||
![]() |
0a41b253f3 | ||
![]() |
002b86198c | ||
![]() |
7161a235f1 | ||
![]() |
391034a785 | ||
![]() |
446a7fbd67 | ||
![]() |
2ccb09434f | ||
![]() |
7974060a40 | ||
![]() |
e8abcc1765 | ||
![]() |
1be1927a1f | ||
![]() |
894ff71664 | ||
![]() |
93bed9c5df | ||
![]() |
34c008adce | ||
![]() |
22ddd91b1f | ||
![]() |
e121f34407 | ||
![]() |
df8a3e870a | ||
![]() |
13b6708a09 | ||
![]() |
6aa196cf55 | ||
![]() |
142493076a | ||
![]() |
3d905a7a4f | ||
![]() |
0a8a6ed168 | ||
![]() |
68b6fa46e6 | ||
![]() |
80f7c54e4d | ||
![]() |
c0e2041006 | ||
![]() |
f6d2cd7704 | ||
![]() |
6d9a629b15 | ||
![]() |
ead8edc7cd | ||
![]() |
664eefdddb | ||
![]() |
220d76c974 | ||
![]() |
d973192b5e | ||
![]() |
978bb5059f | ||
![]() |
f59178bc33 | ||
![]() |
53899cc492 | ||
![]() |
6379d0fe0f | ||
![]() |
a1c4d8696a | ||
![]() |
7f42ed86f2 | ||
![]() |
6bb8b4c994 | ||
![]() |
afefbe4e56 | ||
![]() |
5cd37e5505 | ||
![]() |
d6a825981d | ||
![]() |
a1c01c6722 | ||
![]() |
8d182f1d79 | ||
![]() |
29707bd2ea | ||
![]() |
c2bf2a8174 | ||
![]() |
b402849557 | ||
![]() |
cb85dcfcb8 | ||
![]() |
49379140c7 | ||
![]() |
63f6c86b46 | ||
![]() |
1865898cd4 | ||
![]() |
8935ecfdb8 | ||
![]() |
4902bed409 | ||
![]() |
e6f6aba207 | ||
![]() |
bab622de25 | ||
![]() |
a86c1624a7 | ||
![]() |
c707392a5a | ||
![]() |
1f07721ec4 | ||
![]() |
4767e45035 | ||
![]() |
f2a64ed685 | ||
![]() |
2c9966a0a3 | ||
![]() |
953aa95c64 | ||
![]() |
446266776e | ||
![]() |
845af014dc | ||
![]() |
7c8f8703a1 | ||
![]() |
a0c18e4380 | ||
![]() |
e8d8099563 | ||
![]() |
2c9d90d463 | ||
![]() |
130d2064d5 | ||
![]() |
a10eb2a0d7 | ||
![]() |
b71cc5d7ee | ||
![]() |
8512f9eda1 | ||
![]() |
80058083b8 | ||
![]() |
d5a17a3c25 | ||
![]() |
eab7c87781 | ||
![]() |
d2962d8676 | ||
![]() |
5e602fb575 | ||
![]() |
599964ea5f | ||
![]() |
562de7926b | ||
![]() |
8eaed95e02 | ||
![]() |
3cffd46008 | ||
![]() |
e239bfda8a | ||
![]() |
bd887b660d | ||
![]() |
7c1e0ea95d | ||
![]() |
afbddbfcda | ||
![]() |
9eeb702ca5 | ||
![]() |
4cb6d6995f | ||
![]() |
fe7e7a8d07 | ||
![]() |
f8ee79ab72 | ||
![]() |
a11165830b | ||
![]() |
11cde61eab | ||
![]() |
cd862409cc | ||
![]() |
7644555d6b | ||
![]() |
07e68aa694 | ||
![]() |
0ae206b1bb | ||
![]() |
5bdac96375 | ||
![]() |
f0e044ecd8 | ||
![]() |
f32234291e | ||
![]() |
83acd6529f | ||
![]() |
6a60cb4dc0 | ||
![]() |
1de03fbe18 | ||
![]() |
0e0a2aa981 | ||
![]() |
af73b9d11b | ||
![]() |
859969241a | ||
![]() |
6852a8ca9d | ||
![]() |
c694f1a4a9 | ||
![]() |
d36543b204 | ||
![]() |
6981234736 | ||
![]() |
10bf3ee9de | ||
![]() |
9953218de1 | ||
![]() |
50c74103aa | ||
![]() |
c5e15123fd | ||
![]() |
4adcdb5ba8 | ||
![]() |
60b36ffaa3 | ||
![]() |
d86104ed5d | ||
![]() |
0f02ef20a9 | ||
![]() |
0768cf17b6 | ||
![]() |
197205853f | ||
![]() |
bebcc24ab8 | ||
![]() |
987248ccbb | ||
![]() |
56ece4db0f | ||
![]() |
c7f8b94ccd | ||
![]() |
95b820305a | ||
![]() |
599b200ca5 | ||
![]() |
a9566b31be | ||
![]() |
f30837d726 | ||
![]() |
1242ffa4c6 | ||
![]() |
37c0347c47 | ||
![]() |
65befb84a0 | ||
![]() |
a41f7ce3bd | ||
![]() |
81ced6bf2a | ||
![]() |
fa8033f998 | ||
![]() |
19e3178d0c | ||
![]() |
1e6262f24f | ||
![]() |
beb2284440 | ||
![]() |
0c4f0de13d | ||
![]() |
0cd31a4b96 | ||
![]() |
ef0a86e7cc | ||
![]() |
b79c1345b9 | ||
![]() |
5bb8b6f16c | ||
![]() |
f6f675d1e1 | ||
![]() |
9f684e7670 | ||
![]() |
a998706452 | ||
![]() |
5e19bc6f84 | ||
![]() |
169f287970 | ||
![]() |
c56926428c | ||
![]() |
43eab5c4e5 | ||
![]() |
c6424c2ce7 | ||
![]() |
dfe19d308c | ||
![]() |
b75db110dc | ||
![]() |
9e50ba6ec6 | ||
![]() |
aca2ba13c2 | ||
![]() |
c5afe7a573 | ||
![]() |
8b90968cb1 | ||
![]() |
9281dba896 | ||
![]() |
c7555f1c3c | ||
![]() |
e781831032 | ||
![]() |
8025895168 | ||
![]() |
799935e44c | ||
![]() |
209d8d058c | ||
![]() |
0368a70dd7 | ||
![]() |
c3603426de | ||
![]() |
80db9971b5 | ||
![]() |
e3e771708e | ||
![]() |
b9bca2a19f | ||
![]() |
ffae70a99a | ||
![]() |
10ec1e48b0 | ||
![]() |
84123f5445 | ||
![]() |
25531d8393 | ||
![]() |
adfadc542a | ||
![]() |
7682e1cb57 | ||
![]() |
1fe4109a42 | ||
![]() |
b97baf4d47 | ||
![]() |
f91728561f | ||
![]() |
2b75d54ce1 | ||
![]() |
cdc09a7d30 | ||
![]() |
4cc6c3e535 | ||
![]() |
8f43e033a4 | ||
![]() |
918dbd1926 | ||
![]() |
8fc86ce7fa | ||
![]() |
bf0cb40586 | ||
![]() |
a62aefe74b | ||
![]() |
138d232149 | ||
![]() |
8fa64e3ab2 | ||
![]() |
8d0d2bd3fc | ||
![]() |
2146f5f623 | ||
![]() |
623df23570 | ||
![]() |
4a15d8b6d2 | ||
![]() |
af83a62474 | ||
![]() |
a0705a6c67 | ||
![]() |
712b8eec3d | ||
![]() |
558e9c21ed | ||
![]() |
23a7137e6a | ||
![]() |
71a1cb0184 | ||
![]() |
09b47cc536 | ||
![]() |
b5c78de2dd | ||
![]() |
a1bd14e516 | ||
![]() |
ff0e1bbbc0 | ||
![]() |
18c712cd99 | ||
![]() |
78f542f6c0 |
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
*.o
|
||||
*.so
|
||||
ruby/Makefile
|
||||
*.5
|
||||
*.8
|
||||
*.6
|
||||
_obj
|
||||
_test
|
@@ -1,4 +1,14 @@
|
||||
|
||||
2011-04-24 version 0.5.6:
|
||||
|
||||
* #42 fixes double-free problem on msgpack_unpacker_release_zone
|
||||
|
||||
2011-02-24 version 0.5.5:
|
||||
|
||||
* eliminates dependency of winsock2.h header
|
||||
* fixes msgpack_vc.postbuild.bat file
|
||||
* fixes some implicit cast warnings
|
||||
|
||||
2010-08-29 version 0.5.4:
|
||||
|
||||
* includes msgpack_vc2008.vcproj file in source package
|
||||
|
@@ -38,7 +38,8 @@ test -f ChangeLog || touch ChangeLog
|
||||
test -f NEWS || touch NEWS
|
||||
test -f README || cp -f README.md README
|
||||
|
||||
if ! ./preprocess; then
|
||||
./preprocess
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
AC_INIT(src/object.cpp)
|
||||
AC_CONFIG_AUX_DIR(ac)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.5.4)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.5.6)
|
||||
AC_CONFIG_HEADER(config.h)
|
||||
|
||||
AC_SUBST(CFLAGS)
|
||||
@@ -55,7 +55,7 @@ if test "$msgpack_cv_atomic_ops" != "yes"; then
|
||||
Note that gcc < 4.1 is not supported.
|
||||
|
||||
If you are using gcc >= 4.1 and the default target CPU architecture is "i386", try to
|
||||
add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as follows:
|
||||
add CFLAGS="-march=i686" and CXXFLAGS="-march=i686" options to ./configure as follows:
|
||||
|
||||
$ ./configure CFLAGS="-march=i686" CXXFLAGS="-march=i686"
|
||||
])
|
||||
|
@@ -24,7 +24,7 @@ copy src\msgpack\pack.hpp include\msgpack\
|
||||
copy src\msgpack\unpack.hpp include\msgpack\
|
||||
copy src\msgpack\object.hpp include\msgpack\
|
||||
copy src\msgpack\zone.hpp include\msgpack\
|
||||
copy src\msgpack\type.hpp include\msgpack\type\
|
||||
copy src\msgpack\type.hpp include\msgpack\
|
||||
copy src\msgpack\type\bool.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\deque.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\fixint.hpp include\msgpack\type\
|
||||
@@ -40,6 +40,6 @@ copy src\msgpack\type\string.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\vector.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tuple.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\define.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tr1\unordered_map.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tr1\unordered_set.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tr1\unordered_map.hpp include\msgpack\type\tr1\
|
||||
copy src\msgpack\type\tr1\unordered_set.hpp include\msgpack\type\tr1\
|
||||
|
||||
|
@@ -12,7 +12,7 @@ preprocess() {
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "$1" == "clean" ];then
|
||||
if [ "$1" = "clean" ];then
|
||||
rm -f src/msgpack/type/tuple.hpp
|
||||
rm -f src/msgpack/type/define.hpp
|
||||
rm -f src/msgpack/zone.hpp
|
||||
|
@@ -105,9 +105,6 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
|
||||
#define msgpack_pack_inline_func_cint(name) \
|
||||
inline int msgpack_pack ## name
|
||||
|
||||
#define msgpack_pack_inline_func_cint(name) \
|
||||
inline int msgpack_pack ## name
|
||||
|
||||
#define msgpack_pack_inline_func_fixint(name) \
|
||||
inline int msgpack_pack_fix ## name
|
||||
|
||||
|
@@ -28,9 +28,13 @@ class sbuffer : public msgpack_sbuffer {
|
||||
public:
|
||||
sbuffer(size_t initsz = MSGPACK_SBUFFER_INIT_SIZE)
|
||||
{
|
||||
base::data = (char*)::malloc(initsz);
|
||||
if(!base::data) {
|
||||
throw std::bad_alloc();
|
||||
if(initsz == 0) {
|
||||
base::data = NULL;
|
||||
} else {
|
||||
base::data = (char*)::malloc(initsz);
|
||||
if(!base::data) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
base::size = 0;
|
||||
@@ -80,7 +84,7 @@ public:
|
||||
private:
|
||||
void expand_buffer(size_t len)
|
||||
{
|
||||
size_t nsize = (base::alloc) ?
|
||||
size_t nsize = (base::alloc > 0) ?
|
||||
base::alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
|
||||
|
||||
while(nsize < base::size + len) { nsize *= 2; }
|
||||
|
@@ -30,7 +30,7 @@ namespace msgpack {
|
||||
inline float& operator>> (object o, float& v)
|
||||
{
|
||||
if(o.type != type::DOUBLE) { throw type_error(); }
|
||||
v = o.via.dec;
|
||||
v = (float)o.via.dec;
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const double& v)
|
||||
inline void operator<< (object& o, float v)
|
||||
{
|
||||
o.type = type::DOUBLE;
|
||||
o.via.dec = v;
|
||||
o.via.dec = (double)v;
|
||||
}
|
||||
|
||||
inline void operator<< (object& o, double v)
|
||||
|
@@ -35,11 +35,11 @@ namespace detail {
|
||||
if(o.type == type::POSITIVE_INTEGER) {
|
||||
if(o.via.u64 > (uint64_t)std::numeric_limits<T>::max())
|
||||
{ throw type_error(); }
|
||||
return o.via.u64;
|
||||
return (T)o.via.u64;
|
||||
} else if(o.type == type::NEGATIVE_INTEGER) {
|
||||
if(o.via.i64 < (int64_t)std::numeric_limits<T>::min())
|
||||
{ throw type_error(); }
|
||||
return o.via.i64;
|
||||
return (T)o.via.i64;
|
||||
}
|
||||
throw type_error();
|
||||
}
|
||||
@@ -51,7 +51,7 @@ namespace detail {
|
||||
if(o.type == type::POSITIVE_INTEGER) {
|
||||
if(o.via.u64 > (uint64_t)std::numeric_limits<T>::max())
|
||||
{ throw type_error(); }
|
||||
return o.via.u64;
|
||||
return (T)o.via.u64;
|
||||
}
|
||||
throw type_error();
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ inline void operator<< (object::with_zone& o, const std::string& v)
|
||||
o.type = type::RAW;
|
||||
char* ptr = (char*)o.zone->malloc(v.size());
|
||||
o.via.raw.ptr = ptr;
|
||||
o.via.raw.size = v.size();
|
||||
o.via.raw.size = (uint32_t)v.size();
|
||||
memcpy(ptr, v.data(), v.size());
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ inline void operator<< (object& o, const std::string& v)
|
||||
{
|
||||
o.type = type::RAW;
|
||||
o.via.raw.ptr = v.data();
|
||||
o.via.raw.size = v.size();
|
||||
o.via.raw.size = (uint32_t)v.size();
|
||||
}
|
||||
|
||||
|
||||
|
@@ -258,17 +258,7 @@ inline object unpacker::data()
|
||||
|
||||
inline zone* unpacker::release_zone()
|
||||
{
|
||||
if(!msgpack_unpacker_flush_zone(this)) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
|
||||
zone* r = new zone();
|
||||
|
||||
msgpack_zone old = *base::z;
|
||||
*base::z = *r;
|
||||
*static_cast<msgpack_zone*>(r) = old;
|
||||
|
||||
return r;
|
||||
return static_cast<msgpack::zone*>(msgpack_unpacker_release_zone(static_cast<msgpack_unpacker*>(this)));
|
||||
}
|
||||
|
||||
inline void unpacker::reset_zone()
|
||||
|
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* MessagePack for C memory pool implementation
|
||||
*
|
||||
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
* Copyright (C) 2008-2010 FURUHASHI Sadayuki
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -73,6 +73,8 @@ static inline void* msgpack_zone_malloc_no_align(msgpack_zone* zone, size_t size
|
||||
static inline bool msgpack_zone_push_finalizer(msgpack_zone* zone,
|
||||
void (*func)(void* data), void* data);
|
||||
|
||||
static inline void msgpack_zone_swap(msgpack_zone* a, msgpack_zone* b);
|
||||
|
||||
bool msgpack_zone_is_empty(msgpack_zone* zone);
|
||||
|
||||
void msgpack_zone_clear(msgpack_zone* zone);
|
||||
@@ -129,6 +131,13 @@ bool msgpack_zone_push_finalizer(msgpack_zone* zone,
|
||||
return true;
|
||||
}
|
||||
|
||||
void msgpack_zone_swap(msgpack_zone* a, msgpack_zone* b)
|
||||
{
|
||||
msgpack_zone tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for C++ memory pool
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2008-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -43,6 +43,8 @@ public:
|
||||
|
||||
void clear();
|
||||
|
||||
void swap(zone& o);
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template <typename T<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
T* allocate(<%=(1..i).map{|j|"A#{j} a#{j}"}.join(', ')%>);
|
||||
@@ -111,6 +113,11 @@ inline void zone::clear()
|
||||
msgpack_zone_clear(this);
|
||||
}
|
||||
|
||||
inline void zone::swap(zone& o)
|
||||
{
|
||||
msgpack_zone_swap(this, &o);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void zone::object_destructor(void* obj)
|
||||
{
|
||||
|
@@ -164,7 +164,7 @@ void msgpack_object_print(FILE* out, msgpack_object o)
|
||||
|
||||
default:
|
||||
// FIXME
|
||||
fprintf(out, "#<UNKNOWN %hu %"PRIu64">", o.type, o.via.u64);
|
||||
fprintf(out, "#<UNKNOWN %i %"PRIu64">", o.type, o.via.u64);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -19,6 +19,10 @@
|
||||
#include "msgpack/unpack_define.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _msgpack_atomic_counter_header
|
||||
#include _msgpack_atomic_counter_header
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
msgpack_zone* z;
|
||||
@@ -333,6 +337,7 @@ msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
|
||||
|
||||
msgpack_zone* old = mpac->z;
|
||||
mpac->z = r;
|
||||
CTX_CAST(mpac->ctx)->user.z = mpac->z;
|
||||
|
||||
return old;
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
|
||||
AM_CPPFLAGS = -I../src
|
||||
AM_C_CPPFLAGS = -I../src
|
||||
AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main
|
||||
AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main -pthread
|
||||
|
||||
check_PROGRAMS = \
|
||||
zone \
|
||||
|
@@ -7,9 +7,21 @@ TEST(streaming, basic)
|
||||
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
|
||||
|
||||
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
|
||||
|
||||
// 1, 2, 3, "raw", ["data"], {0.3: 0.4}
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
EXPECT_EQ(0, msgpack_pack_raw(pk, 3));
|
||||
EXPECT_EQ(0, msgpack_pack_raw_body(pk, "raw", 3));
|
||||
EXPECT_EQ(0, msgpack_pack_array(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_raw(pk, 4));
|
||||
EXPECT_EQ(0, msgpack_pack_raw_body(pk, "data", 4));
|
||||
EXPECT_EQ(0, msgpack_pack_map(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_float(pk, 0.4));
|
||||
EXPECT_EQ(0, msgpack_pack_double(pk, 0.8));
|
||||
int max_count = 6;
|
||||
|
||||
msgpack_packer_free(pk);
|
||||
|
||||
const char* input = buffer->data;
|
||||
@@ -22,36 +34,65 @@ TEST(streaming, basic)
|
||||
msgpack_unpacked_init(&result);
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
while(count < max_count) {
|
||||
bool unpacked = false;
|
||||
|
||||
msgpack_unpacker_reserve_buffer(&pac, 32*1024);
|
||||
|
||||
/* read buffer into msgpack_unapcker_buffer(&pac) upto
|
||||
* msgpack_unpacker_buffer_capacity(&pac) bytes. */
|
||||
size_t len = 1;
|
||||
memcpy(msgpack_unpacker_buffer(&pac), input, len);
|
||||
input += len;
|
||||
while(!unpacked) {
|
||||
/* read buffer into msgpack_unapcker_buffer(&pac) upto
|
||||
* msgpack_unpacker_buffer_capacity(&pac) bytes. */
|
||||
memcpy(msgpack_unpacker_buffer(&pac), input, 1);
|
||||
input += 1;
|
||||
|
||||
msgpack_unpacker_buffer_consumed(&pac, len);
|
||||
EXPECT_TRUE(input <= eof);
|
||||
|
||||
while(msgpack_unpacker_next(&pac, &result)) {
|
||||
msgpack_object obj = result.data;
|
||||
switch(count++) {
|
||||
case 0:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(1, result.data.via.u64);
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(2, result.data.via.u64);
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(3, result.data.via.u64);
|
||||
return;
|
||||
msgpack_unpacker_buffer_consumed(&pac, 1);
|
||||
|
||||
while(msgpack_unpacker_next(&pac, &result)) {
|
||||
unpacked = 1;
|
||||
msgpack_object obj = result.data;
|
||||
msgpack_object e;
|
||||
switch(count++) {
|
||||
case 0:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
|
||||
EXPECT_EQ(1, obj.via.u64);
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
|
||||
EXPECT_EQ(2, obj.via.u64);
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
|
||||
EXPECT_EQ(3, obj.via.u64);
|
||||
break;
|
||||
case 3:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_RAW, obj.type);
|
||||
EXPECT_EQ(std::string("raw",3), std::string(obj.via.raw.ptr, obj.via.raw.size));
|
||||
break;
|
||||
case 4:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type);
|
||||
EXPECT_EQ(1, obj.via.array.size);
|
||||
e = obj.via.array.ptr[0];
|
||||
EXPECT_EQ(MSGPACK_OBJECT_RAW, e.type);
|
||||
EXPECT_EQ(std::string("data",4), std::string(e.via.raw.ptr, e.via.raw.size));
|
||||
break;
|
||||
case 5:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_MAP, obj.type);
|
||||
EXPECT_EQ(1, obj.via.map.size);
|
||||
e = obj.via.map.ptr[0].key;
|
||||
EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, e.type);
|
||||
ASSERT_FLOAT_EQ(0.4, (float)e.via.dec);
|
||||
e = obj.via.map.ptr[0].val;
|
||||
EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, e.type);
|
||||
ASSERT_DOUBLE_EQ(0.8, e.via.dec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(input < eof);
|
||||
}
|
||||
|
||||
msgpack_unpacker_destroy(&pac);
|
||||
msgpack_unpacked_destroy(&result);
|
||||
}
|
||||
|
||||
|
11
go/Makefile
Normal file
11
go/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=msgpack
|
||||
|
||||
GOFILES=pack.go unpack.go
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
||||
|
||||
%: install %.go
|
||||
$(GC) $*.go
|
||||
$(LD) -o $@ $*.$O
|
218
go/msgpack_test.go
Normal file
218
go/msgpack_test.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package msgpack_test
|
||||
|
||||
import (
|
||||
. "msgpack"
|
||||
"testing"
|
||||
"bytes"
|
||||
"reflect"
|
||||
"math"
|
||||
);
|
||||
|
||||
func equal(lhs reflect.Value, rhs reflect.Value) bool {
|
||||
{
|
||||
_rhs, ok := rhs.(*reflect.InterfaceValue)
|
||||
if ok { return equal(lhs, _rhs.Elem()) }
|
||||
}
|
||||
switch _lhs := lhs.(type) {
|
||||
case *reflect.InterfaceValue:
|
||||
return equal(_lhs.Elem(), rhs)
|
||||
case *reflect.BoolValue:
|
||||
_rhs, ok := rhs.(*reflect.BoolValue)
|
||||
return ok && _lhs.Get() == _rhs.Get()
|
||||
case *reflect.UintValue:
|
||||
_rhs, ok := rhs.(*reflect.UintValue)
|
||||
return ok && _lhs.Get() == _rhs.Get()
|
||||
case *reflect.IntValue:
|
||||
_rhs, ok := rhs.(*reflect.IntValue)
|
||||
return ok && _lhs.Get() == _rhs.Get()
|
||||
case *reflect.FloatValue:
|
||||
_rhs, ok := rhs.(*reflect.FloatValue)
|
||||
return ok && _lhs.Get() == _rhs.Get()
|
||||
case reflect.ArrayOrSliceValue:
|
||||
_rhs := rhs.(reflect.ArrayOrSliceValue)
|
||||
if _lhs.Len() != _rhs.Len() { return false; }
|
||||
for i := 0; i < _lhs.Len(); i++ {
|
||||
if !equal(_lhs.Elem(i), _rhs.Elem(i)) { return false; }
|
||||
}
|
||||
return true;
|
||||
case *reflect.MapValue:
|
||||
_rhs := rhs.(*reflect.MapValue)
|
||||
if _lhs.Len() != _rhs.Len() { return false; }
|
||||
for _, k := range _lhs.Keys() {
|
||||
lv, rv := _lhs.Elem(k), _rhs.Elem(k)
|
||||
if lv == nil || rv == nil || !equal(lv, rv) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
func TestPackUint8(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []uint8 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255 } {
|
||||
_, err := PackUint8(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackUint16(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []uint16 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535 } {
|
||||
_, err := PackUint16(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackUint32(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []uint32 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535, 65536, 4294967293, 4294967294, 4294967295 } {
|
||||
_, err := PackUint32(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff, 0xce, 0x00, 0x01, 0x00, 0x00, 0xce, 0xff, 0xff, 0xff, 0xfd, 0xce, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xff, 0xff, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackUint64(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []uint64 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535, 65536, 4294967293, 4294967294, 4294967295, 4294967296, 18446744073709551613, 18446744073709551614, 18446744073709551615 } {
|
||||
_, err := PackUint64(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff, 0xce, 0x00, 0x01, 0x00, 0x00, 0xce, 0xff, 0xff, 0xff, 0xfd, 0xce, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackInt8(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []int8 { -128, -127, -34, -33, -32, -31, 0, 1, 126, 127 } {
|
||||
_, err := PackInt8(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackInt16(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []int16 { -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767 } {
|
||||
_, err := PackInt16(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackInt32(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []int32 { -2147483648, -2147483647, -2147483646, -32771, -32770, -32769, -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767, 32768, 32769, 32770, 2147483645, 2147483646, 2147483647 } {
|
||||
_, err := PackInt32(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xd2, 0x80, 0x00, 0x00, 0x00, 0xd2, 0x80, 0x00, 0x00, 0x01, 0xd2, 0x80, 0x00, 0x00, 0x02, 0xd2, 0xff, 0xff, 0x7f, 0xfd, 0xd2, 0xff, 0xff, 0x7f, 0xfe, 0xd2, 0xff, 0xff, 0x7f, 0xff, 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff, 0xd2, 0x00, 0x00, 0x80, 0x00, 0xd2, 0x00, 0x00, 0x80, 0x01, 0xd2, 0x00, 0x00, 0x80, 0x02, 0xd2, 0x7f, 0xff, 0xff, 0xfd, 0xd2, 0x7f, 0xff, 0xff, 0xfe, 0xd2, 0x7f, 0xff, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackInt64(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []int64 { -9223372036854775808, -9223372036854775807, -9223372036854775806, -2147483651, -2147483650, -2147483649, -2147483648, -2147483647, -2147483646, -32771, -32770, -32769, -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767, 32768, 32769, 32770, 2147483645, 2147483646, 2147483647, 2147483648, 2147483649, 2147483650, 4294967296, 4294967297, 4294967298 } {
|
||||
_, err := PackInt64(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfd, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xd2, 0x80, 0x00, 0x00, 0x00, 0xd2, 0x80, 0x00, 0x00, 0x01, 0xd2, 0x80, 0x00, 0x00, 0x02, 0xd2, 0xff, 0xff, 0x7f, 0xfd, 0xd2, 0xff, 0xff, 0x7f, 0xfe, 0xd2, 0xff, 0xff, 0x7f, 0xff, 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff, 0xd2, 0x00, 0x00, 0x80, 0x00, 0xd2, 0x00, 0x00, 0x80, 0x01, 0xd2, 0x00, 0x00, 0x80, 0x02, 0xd2, 0x7f, 0xff, 0xff, 0xfd, 0xd2, 0x7f, 0xff, 0xff, 0xfe, 0xd2, 0x7f, 0xff, 0xff, 0xff, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x02, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02 }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackNil(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
nbyteswrite, err := PackNil(b)
|
||||
if nbyteswrite != 1 { t.Error("nbyteswrite != 1") }
|
||||
if err != nil { t.Error("err != nil") }
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xc0 }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackBool(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range []bool { false, true } {
|
||||
nbyteswrite, err := PackBool(b, i)
|
||||
if nbyteswrite != 1 { t.Error("nbyteswrite != 1") }
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xc2, 0xc3 }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackInt32Array(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
_, err := PackInt32Array(b, []int32 {})
|
||||
if err != nil { t.Error("err != nil") }
|
||||
_, err = PackInt32Array(b, []int32 { 0 })
|
||||
if err != nil { t.Error("err != nil") }
|
||||
_, err = PackInt32Array(b, []int32 { 0, 1 })
|
||||
if err != nil { t.Error("err != nil") }
|
||||
_, err = PackInt32Array(b, []int32 { 0, 1, 2 })
|
||||
if err != nil { t.Error("err != nil") }
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0x00, 0x01, 0x02 }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackArray(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
_, err := PackArray(b, reflect.NewValue([]int32 {}).(reflect.ArrayOrSliceValue))
|
||||
if err != nil { t.Error("err != nil") }
|
||||
_, err = PackArray(b, reflect.NewValue([]int32 { 0 }).(reflect.ArrayOrSliceValue))
|
||||
if err != nil { t.Error("err != nil") }
|
||||
_, err = PackArray(b, reflect.NewValue([]int32 { 0, 1 }).(reflect.ArrayOrSliceValue))
|
||||
if err != nil { t.Error("err != nil") }
|
||||
_, err = PackArray(b, reflect.NewValue([]int32 { 0, 1, 2 }).(reflect.ArrayOrSliceValue))
|
||||
if err != nil { t.Error("err != nil") }
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0x00, 0x01, 0x02 }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPackMap(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
_, err := PackMap(b, reflect.NewValue(map[int] int { 0: 1, 2: 3, 4: 5 }).(*reflect.MapValue))
|
||||
if err != nil { t.Error("err != nil") }
|
||||
if bytes.Compare(b.Bytes(), []byte { 0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03 }) != 0 { t.Error("wrong output", b.Bytes()) }
|
||||
}
|
||||
|
||||
func TestPack(t *testing.T) {
|
||||
b := &bytes.Buffer{}
|
||||
for _, i := range [](interface{}) { nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128 } {
|
||||
_, err := Pack(b, i)
|
||||
if err != nil { t.Error("err != nil") }
|
||||
}
|
||||
if bytes.Compare(b.Bytes(), []byte { 0xc0, 0xc2, 0xc3, 0x00, 0x01, 0x02, 0x03, 0x7f, 0xf0, 0xff, 0xd0, 0xef, 0xd1, 0x00, 0x80 }) == 0 { t.Error("wrong output") }
|
||||
}
|
||||
|
||||
func TestUnpackArray(t *testing.T) {
|
||||
b := bytes.NewBuffer([]byte { 0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0xd1, 0x00, 0x00, 0xd2, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xdc, 0x00, 0x02, 0x00, 0x01, 0xdd, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03 })
|
||||
for _, v := range [](interface{}) { [](interface{}) {}, [](interface{}) { int8(0) }, [](interface{}) { int8(0), int8(1) }, [](interface{}) { int16(0), int32(1), int64(2) }, [](interface{}){ int8(0), int8(1) }, [](interface{}) { int8(0), int8(1), int8(2), int8(3) } } {
|
||||
retval, _, e := Unpack(b)
|
||||
if e != nil { t.Error("err != nil") }
|
||||
if !equal(reflect.NewValue(retval.Interface()), reflect.NewValue(v)) { t.Errorf("%s != %s", retval.Interface(), v) }
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnpackInt(t *testing.T) {
|
||||
b := bytes.NewBuffer([]byte { 0xff, 0xe0, 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xd0, 0x01, 0xd0, 0x80, 0xd0, 0xff, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xd1, 0x00, 0x00, 0xd1, 0x7f, 0xff, 0xd1, 0xff, 0xff, 0xcd, 0x80, 0x00, 0xcd, 0xff, 0xff, 0xd2, 0x7f, 0xff, 0xff, 0xff, 0xce, 0x7f, 0xff, 0xff, 0xff, 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff })
|
||||
for _, v := range [](interface{}) {
|
||||
int8(-1), int8(-32), int8(0), int8(1), int8(2), int8(125), int8(126), int8(127), int8(1), int8(-128), int8(-1), uint8(128), uint8(253), uint8(254), uint8(255), int16(0), int16(32767), int16(-1), uint16(32768), uint16(65535), int32(2147483647), uint32(2147483647), int64(9223372036854775807), uint64(18446744073709551615) } {
|
||||
retval, _, e := Unpack(b)
|
||||
if e != nil { t.Error("err != nil") }
|
||||
if retval.Interface() != v { t.Errorf("%u != %u", retval.Interface(), v) }
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnpackFloat(t *testing.T) {
|
||||
b := bytes.NewBuffer([]byte { 0xca, 0x3d, 0xcc, 0xcc, 0xcd, 0xca, 0x3e, 0x4c, 0xcc, 0xcd, 0xca, 0xbd, 0xcc, 0xcc, 0xcd, 0xca, 0xbe, 0x4c, 0xcc, 0xcd, 0xca, 0x7f, 0x80, 0x00, 0x00, 0xca, 0xff, 0x80, 0x00, 0x00, 0xca, 0xff, 0xc0, 0x00, 0x0, 0xcb, 0x3f, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x3f, 0xc9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0xbf, 0xc9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0xcb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })
|
||||
for _, v := range [](interface{}) { float32(.1), float32(.2), float32(-.1), float32(-.2), float32(math.Inf(1)), float32(math.Inf(-1)), float32(math.NaN()), float64(.1), float64(.2), float64(-.1), float64(-.2) } {
|
||||
retval, _, e := Unpack(b)
|
||||
if e != nil { t.Error("err != nil") }
|
||||
isnan := false
|
||||
if _v, ok := v.(float64); ok {
|
||||
isnan = math.IsNaN(_v)
|
||||
} else if _v, ok := v.(float32); ok {
|
||||
isnan = math.IsNaN(float64(_v))
|
||||
}
|
||||
if isnan {
|
||||
if retval.Interface() == v { t.Errorf("[NaN] %u == %u", retval.Interface(), v) }
|
||||
} else {
|
||||
if retval.Interface() != v { t.Errorf("%u != %u", retval.Interface(), v) }
|
||||
}
|
||||
}
|
||||
}
|
591
go/pack.go
Normal file
591
go/pack.go
Normal file
@@ -0,0 +1,591 @@
|
||||
package msgpack
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"unsafe"
|
||||
"reflect"
|
||||
);
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint8(writer io.Writer, value uint8) (n int, err os.Error) {
|
||||
if value < 128 {
|
||||
return writer.Write([]byte { byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { 0xcc, byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint16(writer io.Writer, value uint16) (n int, err os.Error) {
|
||||
if value < 128 {
|
||||
return writer.Write([]byte { byte(value) })
|
||||
} else if value < 256 {
|
||||
return writer.Write([]byte { 0xcc, byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { 0xcd, byte(value >> 8), byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint32(writer io.Writer, value uint32) (n int, err os.Error) {
|
||||
if value < 128 {
|
||||
return writer.Write([]byte { byte(value) })
|
||||
} else if value < 256 {
|
||||
return writer.Write([]byte { 0xcc, byte(value) })
|
||||
} else if value < 65536 {
|
||||
return writer.Write([]byte { 0xcd, byte(value >> 8), byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { 0xce, byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint64(writer io.Writer, value uint64) (n int, err os.Error) {
|
||||
if value < 128 {
|
||||
return writer.Write([]byte { byte(value) })
|
||||
} else if value < 256 {
|
||||
return writer.Write([]byte { 0xcc, byte(value) })
|
||||
} else if value < 65536 {
|
||||
return writer.Write([]byte { 0xcd, byte(value >> 8), byte(value) })
|
||||
} else if value < 4294967296 {
|
||||
return writer.Write([]byte { 0xce, byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { 0xcf, byte(value >> 56), byte(value >> 48), byte(value >> 40), byte(value >> 32), byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value) })
|
||||
}
|
||||
|
||||
func PackUint(writer io.Writer, value uint) (n int, err os.Error) {
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
switch unsafe.Sizeof(value) {
|
||||
case 4:
|
||||
return PackUint32(writer, *(*uint32)(unsafe.Pointer(&value)))
|
||||
case 8:
|
||||
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, os.ENOENT // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt8(writer io.Writer, value int8) (n int, err os.Error) {
|
||||
if value < -32 {
|
||||
return writer.Write([]byte { 0xd0, byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt16(writer io.Writer, value int16) (n int, err os.Error) {
|
||||
if value < -128 || value >= 128 {
|
||||
return writer.Write([]byte { 0xd1, byte(uint16(value) >> 8), byte(value) })
|
||||
} else if value < -32 {
|
||||
return writer.Write([]byte { 0xd0, byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt32(writer io.Writer, value int32) (n int, err os.Error) {
|
||||
if value < -32768 || value >= 32768 {
|
||||
return writer.Write([]byte { 0xd2, byte(uint32(value) >> 24), byte(uint32(value) >> 16), byte(uint32(value) >> 8), byte(value) })
|
||||
} else if value < -128 {
|
||||
return writer.Write([]byte { 0xd1, byte(uint32(value) >> 8), byte(value) })
|
||||
} else if value < -32 {
|
||||
return writer.Write([]byte { 0xd0, byte(value) })
|
||||
} else if value >= 128 {
|
||||
return writer.Write([]byte { 0xd1, byte(uint32(value) >> 8), byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt64(writer io.Writer, value int64) (n int, err os.Error) {
|
||||
if value < -2147483648 || value >= 2147483648 {
|
||||
return writer.Write([]byte { 0xd3, byte(uint64(value) >> 56), byte(uint64(value) >> 48), byte(uint64(value) >> 40), byte(uint64(value) >> 32), byte(uint64(value) >> 24), byte(uint64(value) >> 16), byte(uint64(value) >> 8), byte(value) })
|
||||
} else if value < -32768 || value >= 32768 {
|
||||
return writer.Write([]byte { 0xd2, byte(uint64(value) >> 24), byte(uint64(value) >> 16), byte(uint64(value) >> 8), byte(value) })
|
||||
} else if value < -128 || value >= 128 {
|
||||
return writer.Write([]byte { 0xd1, byte(uint64(value) >> 8), byte(value) })
|
||||
} else if value < -32 {
|
||||
return writer.Write([]byte { 0xd0, byte(value) })
|
||||
}
|
||||
return writer.Write([]byte { byte(value) })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt(writer io.Writer, value int) (n int, err os.Error) {
|
||||
switch unsafe.Sizeof(value) {
|
||||
case 4:
|
||||
return PackInt32(writer, *(*int32)(unsafe.Pointer(&value)))
|
||||
case 8:
|
||||
return PackInt64(writer, *(*int64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, os.ENOENT // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackNil(writer io.Writer) (n int, err os.Error) {
|
||||
return writer.Write([]byte{ 0xc0 })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackBool(writer io.Writer, value bool) (n int, err os.Error) {
|
||||
var code byte;
|
||||
if value {
|
||||
code = 0xc3
|
||||
} else {
|
||||
code = 0xc2
|
||||
}
|
||||
return writer.Write([]byte{ code })
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackFloat32(writer io.Writer, value float32) (n int, err os.Error) {
|
||||
return PackUint32(writer, *(*uint32)(unsafe.Pointer(&value)))
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackFloat64(writer io.Writer, value float64) (n int, err os.Error) {
|
||||
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackBytes(writer io.Writer, value []byte) (n int, err os.Error) {
|
||||
if len(value) < 32 {
|
||||
n1, err := writer.Write([]byte { 0xa0 | uint8(len(value)) })
|
||||
if err != nil { return n1, err }
|
||||
n2, err := writer.Write(value)
|
||||
return n1 + n2, err
|
||||
} else if len(value) < 65536 {
|
||||
n1, err := writer.Write([]byte { 0xda, byte(len(value) >> 16), byte(len(value)) })
|
||||
if err != nil { return n1, err }
|
||||
n2, err := writer.Write(value)
|
||||
return n1 + n2, err
|
||||
}
|
||||
n1, err := writer.Write([]byte { 0xdb, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n1, err }
|
||||
n2, err := writer.Write(value)
|
||||
return n1 + n2, err
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint16Array(writer io.Writer, value []uint16) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint16(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint16(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint16(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint32Array(writer io.Writer, value []uint32) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUint64Array(writer io.Writer, value []uint64) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackUint64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackUintArray(writer io.Writer, value []uint) (n int, err os.Error) {
|
||||
switch unsafe.Sizeof(0) {
|
||||
case 4:
|
||||
return PackUint32Array(writer, *(*[]uint32)(unsafe.Pointer(&value)))
|
||||
case 8:
|
||||
return PackUint64Array(writer, *(*[]uint64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, os.ENOENT // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt8Array(writer io.Writer, value []int8) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt8(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt8(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt8(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt16Array(writer io.Writer, value []int16) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt16(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt16(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt16(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt32Array(writer io.Writer, value []int32) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackInt64Array(writer io.Writer, value []int64) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackInt64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackIntArray(writer io.Writer, value []int) (n int, err os.Error) {
|
||||
switch unsafe.Sizeof(0) {
|
||||
case 4:
|
||||
return PackInt32Array(writer, *(*[]int32)(unsafe.Pointer(&value)))
|
||||
case 8:
|
||||
return PackInt64Array(writer, *(*[]int64)(unsafe.Pointer(&value)))
|
||||
}
|
||||
return 0, os.ENOENT // never get here
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackFloat32Array(writer io.Writer, value []float32) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackFloat32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackFloat32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackFloat32(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackFloat64Array(writer io.Writer, value []float64) (n int, err os.Error) {
|
||||
if len(value) < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackFloat64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if len(value) < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackFloat64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(len(value) >> 24), byte(len(value) >> 16), byte(len(value) >> 8), byte(len(value)) })
|
||||
if err != nil { return n, err }
|
||||
for _, i := range value {
|
||||
_n, err := PackFloat64(writer, i)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackArray(writer io.Writer, value reflect.ArrayOrSliceValue) (n int, err os.Error) {
|
||||
{
|
||||
elemType, ok := value.Type().(reflect.ArrayOrSliceType).Elem().(*reflect.UintType)
|
||||
if ok && elemType.Kind() == reflect.Uint8 {
|
||||
return PackBytes(writer, value.Interface().([]byte))
|
||||
}
|
||||
}
|
||||
|
||||
l := value.Len()
|
||||
if l < 16 {
|
||||
n, err := writer.Write([]byte { 0x90 | byte(l) })
|
||||
if err != nil { return n, err }
|
||||
for i := 0; i < l; i++ {
|
||||
_n, err := PackValue(writer, value.Elem(i))
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if l < 65536 {
|
||||
n, err := writer.Write([]byte { 0xdc, byte(l >> 8), byte(l) })
|
||||
if err != nil { return n, err }
|
||||
for i := 0; i < l; i++ {
|
||||
_n, err := PackValue(writer, value.Elem(i))
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdd, byte(l >> 24), byte(l >> 16), byte(l >> 8), byte(l) })
|
||||
if err != nil { return n, err }
|
||||
for i := 0; i < l; i++ {
|
||||
_n, err := PackValue(writer, value.Elem(i))
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackMap(writer io.Writer, value *reflect.MapValue) (n int, err os.Error) {
|
||||
keys := value.Keys()
|
||||
if value.Len() < 16 {
|
||||
n, err := writer.Write([]byte { 0x80 | byte(len(keys)) })
|
||||
if err != nil { return n, err }
|
||||
for _, k := range keys {
|
||||
_n, err := PackValue(writer, k)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
_n, err = PackValue(writer, value.Elem(k))
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else if value.Len() < 65536 {
|
||||
n, err := writer.Write([]byte { 0xde, byte(len(keys) >> 8), byte(len(keys)) })
|
||||
if err != nil { return n, err }
|
||||
for _, k := range keys {
|
||||
_n, err := PackValue(writer, k)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
_n, err = PackValue(writer, value.Elem(k))
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
} else {
|
||||
n, err := writer.Write([]byte { 0xdf, byte(len(keys) >> 24), byte(len(keys) >> 16), byte(len(keys) >> 8), byte(len(keys)) })
|
||||
if err != nil { return n, err }
|
||||
for _, k := range keys {
|
||||
_n, err := PackValue(writer, k)
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
_n, err = PackValue(writer, value.Elem(k))
|
||||
if err != nil { return n, err }
|
||||
n += _n
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func PackValue(writer io.Writer, value reflect.Value) (n int, err os.Error) {
|
||||
if value == nil || value.Type() == nil { return PackNil(writer) }
|
||||
switch _value := value.(type) {
|
||||
case *reflect.BoolValue: return PackBool(writer, _value.Get())
|
||||
case *reflect.UintValue: return PackUint64(writer, _value.Get())
|
||||
case *reflect.IntValue: return PackInt64(writer, _value.Get())
|
||||
case *reflect.FloatValue: return PackFloat64(writer, _value.Get())
|
||||
case *reflect.ArrayValue: return PackArray(writer, _value)
|
||||
case *reflect.SliceValue: return PackArray(writer, _value)
|
||||
case *reflect.MapValue: return PackMap(writer, _value)
|
||||
case *reflect.InterfaceValue:
|
||||
__value := reflect.NewValue(_value.Interface())
|
||||
_, ok := __value.(*reflect.InterfaceValue)
|
||||
if !ok {
|
||||
return PackValue(writer, __value)
|
||||
}
|
||||
}
|
||||
panic("unsupported type: " + value.Type().String())
|
||||
}
|
||||
|
||||
// Packs a given value and writes it into the specified writer.
|
||||
func Pack(writer io.Writer, value interface{}) (n int, err os.Error) {
|
||||
if value == nil { return PackNil(writer) }
|
||||
switch _value := value.(type) {
|
||||
case bool: return PackBool(writer, _value)
|
||||
case uint8: return PackUint8(writer, _value)
|
||||
case uint16: return PackUint16(writer, _value)
|
||||
case uint32: return PackUint32(writer, _value)
|
||||
case uint64: return PackUint64(writer, _value)
|
||||
case uint: return PackUint(writer, _value)
|
||||
case int8: return PackInt8(writer, _value)
|
||||
case int16: return PackInt16(writer, _value)
|
||||
case int32: return PackInt32(writer, _value)
|
||||
case int64: return PackInt64(writer, _value)
|
||||
case int: return PackInt(writer, _value)
|
||||
case float32: return PackFloat32(writer, _value)
|
||||
case float64: return PackFloat64(writer, _value)
|
||||
case []byte: return PackBytes(writer, _value)
|
||||
case []uint16: return PackUint16Array(writer, _value)
|
||||
case []uint32: return PackUint32Array(writer, _value)
|
||||
case []uint64: return PackUint64Array(writer, _value)
|
||||
case []uint: return PackUintArray(writer, _value)
|
||||
case []int8: return PackInt8Array(writer, _value)
|
||||
case []int16: return PackInt16Array(writer, _value)
|
||||
case []int32: return PackInt32Array(writer, _value)
|
||||
case []int64: return PackInt64Array(writer, _value)
|
||||
case []int: return PackIntArray(writer, _value)
|
||||
case []float32: return PackFloat32Array(writer, _value)
|
||||
case []float64: return PackFloat64Array(writer, _value)
|
||||
default:
|
||||
return PackValue(writer, reflect.NewValue(value))
|
||||
}
|
||||
return 0, nil // never get here
|
||||
}
|
288
go/unpack.go
Normal file
288
go/unpack.go
Normal file
@@ -0,0 +1,288 @@
|
||||
package msgpack
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"unsafe"
|
||||
"strconv"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func readByte(reader io.Reader) (v uint8, err os.Error) {
|
||||
data := [1]byte{}
|
||||
_, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, e }
|
||||
return data[0], nil
|
||||
}
|
||||
|
||||
func readUint16(reader io.Reader) (v uint16, n int, err os.Error) {
|
||||
data := [2]byte{}
|
||||
n, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, n, e }
|
||||
return (uint16(data[0]) << 8) | uint16(data[1]), n, nil
|
||||
}
|
||||
|
||||
func readUint32(reader io.Reader) (v uint32, n int, err os.Error) {
|
||||
data := [4]byte{}
|
||||
n, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, n, e }
|
||||
return (uint32(data[0]) << 24) | (uint32(data[1]) << 16) | (uint32(data[2]) << 8) | uint32(data[3]), n, nil
|
||||
}
|
||||
|
||||
func readUint64(reader io.Reader) (v uint64, n int, err os.Error) {
|
||||
data := [8]byte{}
|
||||
n, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, n, e }
|
||||
return (uint64(data[0]) << 56) | (uint64(data[1]) << 48) | (uint64(data[2]) << 40) | (uint64(data[3]) << 32) | (uint64(data[4]) << 24) | (uint64(data[5]) << 16) | (uint64(data[6]) << 8) | uint64(data[7]), n, nil
|
||||
}
|
||||
|
||||
func readInt16(reader io.Reader) (v int16, n int, err os.Error) {
|
||||
data := [2]byte{}
|
||||
n, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, n, e }
|
||||
return (int16(data[0]) << 8) | int16(data[1]), n, nil
|
||||
}
|
||||
|
||||
func readInt32(reader io.Reader) (v int32, n int, err os.Error) {
|
||||
data := [4]byte{}
|
||||
n, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, n, e }
|
||||
return (int32(data[0]) << 24) | (int32(data[1]) << 16) | (int32(data[2]) << 8) | int32(data[3]), n, nil
|
||||
}
|
||||
|
||||
func readInt64(reader io.Reader) (v int64, n int, err os.Error) {
|
||||
data := [8]byte{}
|
||||
n, e := reader.Read(data[0:])
|
||||
if e != nil { return 0, n, e }
|
||||
return (int64(data[0]) << 56) | (int64(data[1]) << 48) | (int64(data[2]) << 40) | (int64(data[3]) << 32) | (int64(data[4]) << 24) | (int64(data[5]) << 16) | (int64(data[6]) << 8) | int64(data[7]), n, nil
|
||||
}
|
||||
|
||||
func unpackArray(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
|
||||
retval := make([]interface{}, nelems)
|
||||
nbytesread := 0
|
||||
var i uint
|
||||
for i = 0; i < nelems; i++ {
|
||||
v, n, e := Unpack(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval[i] = v.Interface()
|
||||
}
|
||||
return reflect.NewValue(retval), nbytesread, nil
|
||||
}
|
||||
|
||||
func unpackArrayReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
|
||||
retval := make([]reflect.Value, nelems)
|
||||
nbytesread := 0
|
||||
var i uint
|
||||
for i = 0; i < nelems; i++ {
|
||||
v, n, e := UnpackReflected(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval[i] = v
|
||||
}
|
||||
return reflect.NewValue(retval), nbytesread, nil
|
||||
}
|
||||
|
||||
func unpackMap(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
|
||||
retval := make(map [interface{}] interface{})
|
||||
nbytesread := 0
|
||||
var i uint
|
||||
for i = 0; i < nelems; i++ {
|
||||
k, n, e := Unpack(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
v, n, e := Unpack(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval[k.Interface()] = v.Interface()
|
||||
}
|
||||
return reflect.NewValue(retval), nbytesread, nil
|
||||
}
|
||||
|
||||
func unpackMapReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
|
||||
retval := make(map [reflect.Value] reflect.Value)
|
||||
nbytesread := 0
|
||||
var i uint
|
||||
for i = 0; i < nelems; i++ {
|
||||
k, n, e := UnpackReflected(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
v, n, e := UnpackReflected(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval[k] = v
|
||||
}
|
||||
return reflect.NewValue(retval), nbytesread, nil
|
||||
}
|
||||
|
||||
func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err os.Error) {
|
||||
var retval reflect.Value
|
||||
var nbytesread int = 0
|
||||
|
||||
c, e := readByte(reader)
|
||||
if e != nil { return nil, 0, e }
|
||||
nbytesread += 1
|
||||
if c < 0x80 || c >= 0xe0 {
|
||||
retval = reflect.NewValue(int8(c))
|
||||
} else if c >= 0x80 && c <= 0x8f {
|
||||
if reflected {
|
||||
retval, n, e = unpackMapReflected(reader, uint(c & 0xf))
|
||||
} else {
|
||||
retval, n, e = unpackMap(reader, uint(c & 0xf))
|
||||
}
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
nbytesread += n
|
||||
} else if c >= 0x90 && c <= 0x9f {
|
||||
if reflected {
|
||||
retval, n, e = unpackArrayReflected(reader, uint(c & 0xf))
|
||||
} else {
|
||||
retval, n, e = unpackArray(reader, uint(c & 0xf))
|
||||
}
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
nbytesread += n
|
||||
} else if c >= 0xa0 && c <= 0xbf {
|
||||
data := make([]byte, c & 0xf);
|
||||
n, e := reader.Read(data)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
} else {
|
||||
switch c {
|
||||
case 0xc0: retval = reflect.NewValue(nil)
|
||||
case 0xc2: retval = reflect.NewValue(false)
|
||||
case 0xc3: retval = reflect.NewValue(true)
|
||||
case 0xca:
|
||||
data, n, e := readUint32(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(*(*float32)(unsafe.Pointer(&data)))
|
||||
case 0xcb:
|
||||
data, n, e := readUint64(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(*(*float64)(unsafe.Pointer(&data)))
|
||||
case 0xcc:
|
||||
data, e := readByte(reader)
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(uint8(data))
|
||||
nbytesread += 1
|
||||
case 0xcd:
|
||||
data, n, e := readUint16(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xce:
|
||||
data, n, e := readUint32(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xcf:
|
||||
data, n, e := readUint64(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xd0:
|
||||
data, e := readByte(reader)
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(int8(data))
|
||||
nbytesread += 1
|
||||
case 0xd1:
|
||||
data, n, e := readInt16(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xd2:
|
||||
data, n, e := readInt32(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xd3:
|
||||
data, n, e := readInt64(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xda:
|
||||
nbytestoread, n, e := readUint16(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
data := make([]byte, nbytestoread)
|
||||
n, e = reader.Read(data)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xdb:
|
||||
nbytestoread, n, e := readUint32(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
data := make([]byte, nbytestoread)
|
||||
n, e = reader.Read(data)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
retval = reflect.NewValue(data)
|
||||
case 0xdc:
|
||||
nelemstoread, n, e := readUint16(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
if reflected {
|
||||
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
|
||||
} else {
|
||||
retval, n, e = unpackArray(reader, uint(nelemstoread))
|
||||
}
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
case 0xdd:
|
||||
nelemstoread, n, e := readUint32(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
if reflected {
|
||||
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
|
||||
} else {
|
||||
retval, n, e = unpackArray(reader, uint(nelemstoread))
|
||||
}
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
case 0xde:
|
||||
nelemstoread, n, e := readUint16(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
if reflected {
|
||||
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
|
||||
} else {
|
||||
retval, n, e = unpackMap(reader, uint(nelemstoread))
|
||||
}
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
case 0xdf:
|
||||
nelemstoread, n, e := readUint32(reader)
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
if reflected {
|
||||
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
|
||||
} else {
|
||||
retval, n, e = unpackMap(reader, uint(nelemstoread))
|
||||
}
|
||||
nbytesread += n
|
||||
if e != nil { return nil, nbytesread, e }
|
||||
default:
|
||||
panic("unsupported code: " + strconv.Itoa(int(c)))
|
||||
}
|
||||
}
|
||||
return retval, nbytesread, nil
|
||||
}
|
||||
|
||||
// Reads a value from the reader, unpack and returns it.
|
||||
func Unpack(reader io.Reader) (v reflect.Value, n int, err os.Error) {
|
||||
return unpack(reader, false)
|
||||
}
|
||||
|
||||
// Reads unpack a value from the reader, unpack and returns it. When the
|
||||
// value is an array or map, leaves the elements wrapped by corresponding
|
||||
// wrapper objects defined in reflect package.
|
||||
func UnpackReflected(reader io.Reader) (v reflect.Value, n int, err os.Error) {
|
||||
return unpack(reader, true)
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2009, Hideyuki Tanaka
|
||||
Copyright (c) 2009-2010, Hideyuki Tanaka
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@@ -1,137 +0,0 @@
|
||||
#include <msgpack.h>
|
||||
|
||||
void msgpack_sbuffer_init_wrap(msgpack_sbuffer* sbuf)
|
||||
{
|
||||
msgpack_sbuffer_init(sbuf);
|
||||
}
|
||||
|
||||
void msgpack_sbuffer_destroy_wrap(msgpack_sbuffer* sbuf)
|
||||
{
|
||||
msgpack_sbuffer_destroy(sbuf);
|
||||
}
|
||||
|
||||
int msgpack_sbuffer_write_wrap(void* data, const char* buf, unsigned int len)
|
||||
{
|
||||
return msgpack_sbuffer_write(data, buf, len);
|
||||
}
|
||||
|
||||
msgpack_packer* msgpack_packer_new_wrap(void *data, msgpack_packer_write callback)
|
||||
{
|
||||
return msgpack_packer_new(data, callback);
|
||||
}
|
||||
|
||||
void msgpack_packer_free_wrap(msgpack_packer* pk)
|
||||
{
|
||||
msgpack_packer_free(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint8_wrap(msgpack_packer* pk, uint8_t d)
|
||||
{
|
||||
return msgpack_pack_uint8(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint16_wrap(msgpack_packer* pk, uint16_t d)
|
||||
{
|
||||
return msgpack_pack_uint16(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint32_wrap(msgpack_packer* pk, uint32_t d)
|
||||
{
|
||||
return msgpack_pack_uint32(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_uint64_wrap(msgpack_packer* pk, uint64_t d)
|
||||
{
|
||||
return msgpack_pack_uint64(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int8_wrap(msgpack_packer* pk, int8_t d)
|
||||
{
|
||||
return msgpack_pack_int8(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int16_wrap(msgpack_packer* pk, int16_t d)
|
||||
{
|
||||
return msgpack_pack_int16(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int32_wrap(msgpack_packer* pk, int32_t d)
|
||||
{
|
||||
return msgpack_pack_int32(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_int64_wrap(msgpack_packer* pk, int64_t d)
|
||||
{
|
||||
return msgpack_pack_int64(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_double_wrap(msgpack_packer* pk, double d)
|
||||
{
|
||||
return msgpack_pack_double(pk, d);
|
||||
}
|
||||
|
||||
int msgpack_pack_nil_wrap(msgpack_packer* pk)
|
||||
{
|
||||
return msgpack_pack_nil(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_true_wrap(msgpack_packer* pk)
|
||||
{
|
||||
return msgpack_pack_true(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_false_wrap(msgpack_packer* pk)
|
||||
{
|
||||
return msgpack_pack_false(pk);
|
||||
}
|
||||
|
||||
int msgpack_pack_array_wrap(msgpack_packer* pk, unsigned int n)
|
||||
{
|
||||
return msgpack_pack_array(pk, n);
|
||||
}
|
||||
|
||||
int msgpack_pack_map_wrap(msgpack_packer* pk, unsigned int n)
|
||||
{
|
||||
return msgpack_pack_map(pk, n);
|
||||
}
|
||||
|
||||
int msgpack_pack_raw_wrap(msgpack_packer* pk, size_t l)
|
||||
{
|
||||
return msgpack_pack_raw(pk, l);
|
||||
}
|
||||
|
||||
int msgpack_pack_raw_body_wrap(msgpack_packer* pk, const void *b, size_t l)
|
||||
{
|
||||
return msgpack_pack_raw_body(pk, b, l);
|
||||
}
|
||||
|
||||
bool msgpack_unpacker_reserve_buffer_wrap(msgpack_unpacker *mpac, size_t size)
|
||||
{
|
||||
return msgpack_unpacker_reserve_buffer(mpac, size);
|
||||
}
|
||||
|
||||
char *msgpack_unpacker_buffer_wrap(msgpack_unpacker *mpac)
|
||||
{
|
||||
return msgpack_unpacker_buffer(mpac);
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_buffer_capacity_wrap(const msgpack_unpacker *mpac)
|
||||
{
|
||||
return msgpack_unpacker_buffer_capacity(mpac);
|
||||
}
|
||||
|
||||
void msgpack_unpacker_buffer_consumed_wrap(msgpack_unpacker *mpac, size_t size)
|
||||
{
|
||||
msgpack_unpacker_buffer_consumed(mpac, size);
|
||||
}
|
||||
|
||||
void msgpack_unpacker_data_wrap(msgpack_unpacker *mpac, msgpack_object *obj)
|
||||
{
|
||||
*obj=msgpack_unpacker_data(mpac);
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_message_size_wrap(const msgpack_unpacker *mpac)
|
||||
{
|
||||
return msgpack_unpacker_message_size(mpac);
|
||||
}
|
||||
|
@@ -1,32 +1,50 @@
|
||||
Name: msgpack
|
||||
Version: 0.2.2
|
||||
License: BSD3
|
||||
License-File: LICENSE
|
||||
Author: Hideyuki Tanaka
|
||||
Maintainer: Hideyuki Tanaka <tanaka.hideyuki@gmail.com>
|
||||
Category: Data
|
||||
Synopsis: A Haskell binding to MessagePack
|
||||
Name: msgpack
|
||||
Version: 0.6.1.1
|
||||
Synopsis: A Haskell implementation of MessagePack
|
||||
Description:
|
||||
A Haskell binding to MessagePack <http://msgpack.sourceforge.jp/>
|
||||
Homepage: http://github.com/tanakh/hsmsgpack
|
||||
Stability: Experimental
|
||||
Tested-with: GHC==6.10.4
|
||||
Cabal-Version: >=1.2
|
||||
Build-Type: Simple
|
||||
A Haskell implementation of MessagePack <http://msgpack.org/>
|
||||
|
||||
library
|
||||
build-depends: base>=4 && <5, mtl, bytestring
|
||||
ghc-options: -O2 -Wall
|
||||
hs-source-dirs: src
|
||||
extra-libraries: msgpackc
|
||||
License: BSD3
|
||||
License-File: LICENSE
|
||||
Copyright: Copyright (c) 2009-2011, Hideyuki Tanaka
|
||||
Category: Data
|
||||
Author: Hideyuki Tanaka
|
||||
Maintainer: Hideyuki Tanaka <tanaka.hideyuki@gmail.com>
|
||||
Homepage: http://github.com/msgpack/msgpack
|
||||
Stability: Experimental
|
||||
Cabal-Version: >= 1.6
|
||||
Build-Type: Simple
|
||||
|
||||
Extra-source-files:
|
||||
test/Test.hs
|
||||
test/UserData.hs
|
||||
|
||||
Library
|
||||
Build-depends: base >=4 && <5,
|
||||
transformers >= 0.2 && < 0.3,
|
||||
bytestring >= 0.9 && < 0.10,
|
||||
text >= 0.11 && < 0.12,
|
||||
vector >= 0.7 && < 0.8,
|
||||
attoparsec >= 0.8 && < 0.9,
|
||||
binary >= 0.5.0 && < 0.5.1,
|
||||
data-binary-ieee754 >= 0.4 && < 0.5,
|
||||
deepseq >= 1.1 && <1.2,
|
||||
template-haskell >= 2.4 && < 2.6
|
||||
|
||||
Ghc-options: -Wall
|
||||
Hs-source-dirs: src
|
||||
|
||||
Exposed-modules:
|
||||
Data.MessagePack
|
||||
Data.MessagePack.Base
|
||||
Data.MessagePack.Class
|
||||
Data.MessagePack.Feed
|
||||
Data.MessagePack.Monad
|
||||
Data.MessagePack.Stream
|
||||
Data.MessagePack.Assoc
|
||||
Data.MessagePack.Pack
|
||||
Data.MessagePack.Unpack
|
||||
Data.MessagePack.Object
|
||||
Data.MessagePack.Derive
|
||||
|
||||
C-Sources:
|
||||
cbits/msgpack.c
|
||||
Other-modules:
|
||||
Data.MessagePack.Internal.Utf8
|
||||
|
||||
Source-repository head
|
||||
Type: git
|
||||
Location: git://github.com/msgpack/msgpack.git
|
||||
|
@@ -1,7 +1,7 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009-2010
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
@@ -13,51 +13,15 @@
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack(
|
||||
module Data.MessagePack.Base,
|
||||
module Data.MessagePack.Class,
|
||||
module Data.MessagePack.Feed,
|
||||
module Data.MessagePack.Monad,
|
||||
module Data.MessagePack.Stream,
|
||||
|
||||
-- * Pack and Unpack
|
||||
packb,
|
||||
unpackb,
|
||||
|
||||
-- * Pure version of Pack and Unpack
|
||||
packb',
|
||||
unpackb',
|
||||
module Data.MessagePack.Assoc,
|
||||
module Data.MessagePack.Pack,
|
||||
module Data.MessagePack.Unpack,
|
||||
module Data.MessagePack.Object,
|
||||
module Data.MessagePack.Derive,
|
||||
) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import System.IO.Unsafe
|
||||
|
||||
import Data.MessagePack.Base
|
||||
import Data.MessagePack.Class
|
||||
import Data.MessagePack.Feed
|
||||
import Data.MessagePack.Monad
|
||||
import Data.MessagePack.Stream
|
||||
|
||||
-- | Pack Haskell data to MessagePack string.
|
||||
packb :: OBJECT a => a -> IO ByteString
|
||||
packb dat = do
|
||||
sb <- newSimpleBuffer
|
||||
pc <- newPacker sb
|
||||
pack pc dat
|
||||
simpleBufferData sb
|
||||
|
||||
-- | Unpack MessagePack string to Haskell data.
|
||||
unpackb :: OBJECT a => ByteString -> IO (Result a)
|
||||
unpackb bs = do
|
||||
withZone $ \z -> do
|
||||
r <- unpackObject z bs
|
||||
return $ case r of
|
||||
Left err -> Left (show err)
|
||||
Right (_, dat) -> fromObject dat
|
||||
|
||||
-- | Pure version of 'packb'.
|
||||
packb' :: OBJECT a => a -> ByteString
|
||||
packb' dat = unsafePerformIO $ packb dat
|
||||
|
||||
-- | Pure version of 'unpackb'.
|
||||
unpackb' :: OBJECT a => ByteString -> Result a
|
||||
unpackb' bs = unsafePerformIO $ unpackb bs
|
||||
import Data.MessagePack.Assoc
|
||||
import Data.MessagePack.Pack
|
||||
import Data.MessagePack.Unpack
|
||||
import Data.MessagePack.Object
|
||||
import Data.MessagePack.Derive
|
||||
|
28
haskell/src/Data/MessagePack/Assoc.hs
Normal file
28
haskell/src/Data/MessagePack/Assoc.hs
Normal file
@@ -0,0 +1,28 @@
|
||||
{-# Language DeriveDataTypeable #-}
|
||||
{-# Language GeneralizedNewtypeDeriving #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Assoc
|
||||
-- Copyright : (c) Daiki Handa, 2010
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- MessagePack map labeling type
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Assoc (
|
||||
Assoc(..)
|
||||
) where
|
||||
|
||||
import Control.DeepSeq
|
||||
import Data.Typeable
|
||||
|
||||
-- not defined for general Functor for performance reason.
|
||||
-- (ie. you would want to write custom instances for each type using specialized mapM-like functions)
|
||||
newtype Assoc a=Assoc{unAssoc :: a} deriving(Show,Eq,Ord,Typeable,NFData)
|
||||
|
@@ -1,584 +0,0 @@
|
||||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Base
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Low Level Interface to MessagePack C API
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Base(
|
||||
-- * Simple Buffer
|
||||
SimpleBuffer,
|
||||
newSimpleBuffer,
|
||||
simpleBufferData,
|
||||
|
||||
-- * Serializer
|
||||
Packer,
|
||||
newPacker,
|
||||
|
||||
packU8,
|
||||
packU16,
|
||||
packU32,
|
||||
packU64,
|
||||
packS8,
|
||||
packS16,
|
||||
packS32,
|
||||
packS64,
|
||||
|
||||
packTrue,
|
||||
packFalse,
|
||||
|
||||
packInt,
|
||||
packDouble,
|
||||
packNil,
|
||||
packBool,
|
||||
|
||||
packArray,
|
||||
packMap,
|
||||
packRAW,
|
||||
packRAWBody,
|
||||
packRAW',
|
||||
|
||||
-- * Stream Deserializer
|
||||
Unpacker,
|
||||
defaultInitialBufferSize,
|
||||
newUnpacker,
|
||||
unpackerReserveBuffer,
|
||||
unpackerBuffer,
|
||||
unpackerBufferCapacity,
|
||||
unpackerBufferConsumed,
|
||||
unpackerFeed,
|
||||
unpackerExecute,
|
||||
unpackerData,
|
||||
unpackerReleaseZone,
|
||||
unpackerResetZone,
|
||||
unpackerReset,
|
||||
unpackerMessageSize,
|
||||
|
||||
-- * MessagePack Object
|
||||
Object(..),
|
||||
packObject,
|
||||
|
||||
UnpackReturn(..),
|
||||
unpackObject,
|
||||
|
||||
-- * Memory Zone
|
||||
Zone,
|
||||
newZone,
|
||||
freeZone,
|
||||
withZone,
|
||||
) where
|
||||
|
||||
import Control.Exception
|
||||
import Control.Monad
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS hiding (pack, unpack)
|
||||
import Data.Int
|
||||
import Data.Word
|
||||
import Foreign.C
|
||||
import Foreign.Concurrent
|
||||
import Foreign.ForeignPtr hiding (newForeignPtr)
|
||||
import Foreign.Marshal.Alloc
|
||||
import Foreign.Marshal.Array
|
||||
import Foreign.Ptr
|
||||
import Foreign.Storable
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
type SimpleBuffer = ForeignPtr ()
|
||||
|
||||
type WriteCallback = Ptr () -> CString -> CUInt -> IO CInt
|
||||
|
||||
-- | Create a new Simple Buffer. It will be deleted automatically.
|
||||
newSimpleBuffer :: IO SimpleBuffer
|
||||
newSimpleBuffer = do
|
||||
ptr <- mallocBytes (#size msgpack_sbuffer)
|
||||
fptr <- newForeignPtr ptr $ do
|
||||
msgpack_sbuffer_destroy ptr
|
||||
free ptr
|
||||
withForeignPtr fptr $ \p ->
|
||||
msgpack_sbuffer_init p
|
||||
return fptr
|
||||
|
||||
-- | Get data of Simple Buffer.
|
||||
simpleBufferData :: SimpleBuffer -> IO ByteString
|
||||
simpleBufferData sb =
|
||||
withForeignPtr sb $ \ptr -> do
|
||||
size <- (#peek msgpack_sbuffer, size) ptr
|
||||
dat <- (#peek msgpack_sbuffer, data) ptr
|
||||
BS.packCStringLen (dat, fromIntegral (size :: CSize))
|
||||
|
||||
foreign import ccall "msgpack_sbuffer_init_wrap" msgpack_sbuffer_init ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
foreign import ccall "msgpack_sbuffer_destroy_wrap" msgpack_sbuffer_destroy ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
foreign import ccall "msgpack_sbuffer_write_wrap" msgpack_sbuffer_write ::
|
||||
WriteCallback
|
||||
|
||||
type Packer = ForeignPtr ()
|
||||
|
||||
-- | Create new Packer. It will be deleted automatically.
|
||||
newPacker :: SimpleBuffer -> IO Packer
|
||||
newPacker sbuf = do
|
||||
cb <- wrap_callback msgpack_sbuffer_write
|
||||
ptr <- withForeignPtr sbuf $ \ptr ->
|
||||
msgpack_packer_new ptr cb
|
||||
fptr <- newForeignPtr ptr $ do
|
||||
msgpack_packer_free ptr
|
||||
return fptr
|
||||
|
||||
foreign import ccall "msgpack_packer_new_wrap" msgpack_packer_new ::
|
||||
Ptr () -> FunPtr WriteCallback -> IO (Ptr ())
|
||||
|
||||
foreign import ccall "msgpack_packer_free_wrap" msgpack_packer_free ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
foreign import ccall "wrapper" wrap_callback ::
|
||||
WriteCallback -> IO (FunPtr WriteCallback)
|
||||
|
||||
packU8 :: Packer -> Word8 -> IO Int
|
||||
packU8 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint8 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint8_wrap" msgpack_pack_uint8 ::
|
||||
Ptr () -> Word8 -> IO CInt
|
||||
|
||||
packU16 :: Packer -> Word16 -> IO Int
|
||||
packU16 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint16 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint16_wrap" msgpack_pack_uint16 ::
|
||||
Ptr () -> Word16 -> IO CInt
|
||||
|
||||
packU32 :: Packer -> Word32 -> IO Int
|
||||
packU32 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint32 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint32_wrap" msgpack_pack_uint32 ::
|
||||
Ptr () -> Word32 -> IO CInt
|
||||
|
||||
packU64 :: Packer -> Word64 -> IO Int
|
||||
packU64 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_uint64 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_uint64_wrap" msgpack_pack_uint64 ::
|
||||
Ptr () -> Word64 -> IO CInt
|
||||
|
||||
packS8 :: Packer -> Int8 -> IO Int
|
||||
packS8 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int8 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int8_wrap" msgpack_pack_int8 ::
|
||||
Ptr () -> Int8 -> IO CInt
|
||||
|
||||
packS16 :: Packer -> Int16 -> IO Int
|
||||
packS16 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int16 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int16_wrap" msgpack_pack_int16 ::
|
||||
Ptr () -> Int16 -> IO CInt
|
||||
|
||||
packS32 :: Packer -> Int32 -> IO Int
|
||||
packS32 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int32 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int32_wrap" msgpack_pack_int32 ::
|
||||
Ptr () -> Int32 -> IO CInt
|
||||
|
||||
packS64 :: Packer -> Int64 -> IO Int
|
||||
packS64 pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_int64 ptr n
|
||||
|
||||
foreign import ccall "msgpack_pack_int64_wrap" msgpack_pack_int64 ::
|
||||
Ptr () -> Int64 -> IO CInt
|
||||
|
||||
-- | Pack an integral data.
|
||||
packInt :: Integral a => Packer -> a -> IO Int
|
||||
packInt pc n = packS64 pc $ fromIntegral n
|
||||
|
||||
-- | Pack a double data.
|
||||
packDouble :: Packer -> Double -> IO Int
|
||||
packDouble pc d =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_double ptr (realToFrac d)
|
||||
|
||||
foreign import ccall "msgpack_pack_double_wrap" msgpack_pack_double ::
|
||||
Ptr () -> CDouble -> IO CInt
|
||||
|
||||
-- | Pack a nil.
|
||||
packNil :: Packer -> IO Int
|
||||
packNil pc =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_nil ptr
|
||||
|
||||
foreign import ccall "msgpack_pack_nil_wrap" msgpack_pack_nil ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
packTrue :: Packer -> IO Int
|
||||
packTrue pc =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_true ptr
|
||||
|
||||
foreign import ccall "msgpack_pack_true_wrap" msgpack_pack_true ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
packFalse :: Packer -> IO Int
|
||||
packFalse pc =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_false ptr
|
||||
|
||||
foreign import ccall "msgpack_pack_false_wrap" msgpack_pack_false ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
-- | Pack a bool data.
|
||||
packBool :: Packer -> Bool -> IO Int
|
||||
packBool pc True = packTrue pc
|
||||
packBool pc False = packFalse pc
|
||||
|
||||
-- | 'packArray' @p n@ starts packing an array.
|
||||
-- Next @n@ data will consist this array.
|
||||
packArray :: Packer -> Int -> IO Int
|
||||
packArray pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_array ptr (fromIntegral n)
|
||||
|
||||
foreign import ccall "msgpack_pack_array_wrap" msgpack_pack_array ::
|
||||
Ptr () -> CUInt -> IO CInt
|
||||
|
||||
-- | 'packMap' @p n@ starts packing a map.
|
||||
-- Next @n@ pairs of data (2*n data) will consist this map.
|
||||
packMap :: Packer -> Int -> IO Int
|
||||
packMap pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_map ptr (fromIntegral n)
|
||||
|
||||
foreign import ccall "msgpack_pack_map_wrap" msgpack_pack_map ::
|
||||
Ptr () -> CUInt -> IO CInt
|
||||
|
||||
-- | 'packRAW' @p n@ starts packing a byte sequence.
|
||||
-- Next total @n@ bytes of 'packRAWBody' call will consist this sequence.
|
||||
packRAW :: Packer -> Int -> IO Int
|
||||
packRAW pc n =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
msgpack_pack_raw ptr (fromIntegral n)
|
||||
|
||||
foreign import ccall "msgpack_pack_raw_wrap" msgpack_pack_raw ::
|
||||
Ptr () -> CSize -> IO CInt
|
||||
|
||||
-- | Pack a byte sequence.
|
||||
packRAWBody :: Packer -> ByteString -> IO Int
|
||||
packRAWBody pc bs =
|
||||
liftM fromIntegral $ withForeignPtr pc $ \ptr ->
|
||||
BS.useAsCStringLen bs $ \(str, len) ->
|
||||
msgpack_pack_raw_body ptr (castPtr str) (fromIntegral len)
|
||||
|
||||
foreign import ccall "msgpack_pack_raw_body_wrap" msgpack_pack_raw_body ::
|
||||
Ptr () -> Ptr () -> CSize -> IO CInt
|
||||
|
||||
-- | Pack a single byte stream. It calls 'packRAW' and 'packRAWBody'.
|
||||
packRAW' :: Packer -> ByteString -> IO Int
|
||||
packRAW' pc bs = do
|
||||
_ <- packRAW pc (BS.length bs)
|
||||
packRAWBody pc bs
|
||||
|
||||
type Unpacker = ForeignPtr ()
|
||||
|
||||
defaultInitialBufferSize :: Int
|
||||
defaultInitialBufferSize = 32 * 1024 -- #const MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE
|
||||
|
||||
-- | 'newUnpacker' @initialBufferSize@ creates a new Unpacker. It will be deleted automatically.
|
||||
newUnpacker :: Int -> IO Unpacker
|
||||
newUnpacker initialBufferSize = do
|
||||
ptr <- msgpack_unpacker_new (fromIntegral initialBufferSize)
|
||||
fptr <- newForeignPtr ptr $ do
|
||||
msgpack_unpacker_free ptr
|
||||
return fptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_new" msgpack_unpacker_new ::
|
||||
CSize -> IO (Ptr ())
|
||||
|
||||
foreign import ccall "msgpack_unpacker_free" msgpack_unpacker_free ::
|
||||
Ptr() -> IO ()
|
||||
|
||||
-- | 'unpackerReserveBuffer' @up size@ reserves at least @size@ bytes of buffer.
|
||||
unpackerReserveBuffer :: Unpacker -> Int -> IO Bool
|
||||
unpackerReserveBuffer up size =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM (/=0) $ msgpack_unpacker_reserve_buffer ptr (fromIntegral size)
|
||||
|
||||
foreign import ccall "msgpack_unpacker_reserve_buffer_wrap" msgpack_unpacker_reserve_buffer ::
|
||||
Ptr () -> CSize -> IO CChar
|
||||
|
||||
-- | Get a pointer of unpacker buffer.
|
||||
unpackerBuffer :: Unpacker -> IO (Ptr CChar)
|
||||
unpackerBuffer up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_buffer ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_buffer_wrap" msgpack_unpacker_buffer ::
|
||||
Ptr () -> IO (Ptr CChar)
|
||||
|
||||
-- | Get size of allocated buffer.
|
||||
unpackerBufferCapacity :: Unpacker -> IO Int
|
||||
unpackerBufferCapacity up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM fromIntegral $ msgpack_unpacker_buffer_capacity ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_buffer_capacity_wrap" msgpack_unpacker_buffer_capacity ::
|
||||
Ptr () -> IO CSize
|
||||
|
||||
-- | 'unpackerBufferConsumed' @up size@ notices that writed @size@ bytes to buffer.
|
||||
unpackerBufferConsumed :: Unpacker -> Int -> IO ()
|
||||
unpackerBufferConsumed up size =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_buffer_consumed ptr (fromIntegral size)
|
||||
|
||||
foreign import ccall "msgpack_unpacker_buffer_consumed_wrap" msgpack_unpacker_buffer_consumed ::
|
||||
Ptr () -> CSize -> IO ()
|
||||
|
||||
-- | Write byte sequence to Unpacker. It is utility funciton, calls 'unpackerReserveBuffer', 'unpackerBuffer' and 'unpackerBufferConsumed'.
|
||||
unpackerFeed :: Unpacker -> ByteString -> IO ()
|
||||
unpackerFeed up bs =
|
||||
BS.useAsCStringLen bs $ \(str, len) -> do
|
||||
True <- unpackerReserveBuffer up len
|
||||
ptr <- unpackerBuffer up
|
||||
copyArray ptr str len
|
||||
unpackerBufferConsumed up len
|
||||
|
||||
-- | Execute deserializing. It returns 0 when buffer contains not enough bytes, returns 1 when succeeded, returns negative value when it failed.
|
||||
unpackerExecute :: Unpacker -> IO Int
|
||||
unpackerExecute up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM fromIntegral $ msgpack_unpacker_execute ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_execute" msgpack_unpacker_execute ::
|
||||
Ptr () -> IO CInt
|
||||
|
||||
-- | Returns a deserialized object when 'unpackerExecute' returned 1.
|
||||
unpackerData :: Unpacker -> IO Object
|
||||
unpackerData up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
allocaBytes (#size msgpack_object) $ \pobj -> do
|
||||
msgpack_unpacker_data ptr pobj
|
||||
peekObject pobj
|
||||
|
||||
foreign import ccall "msgpack_unpacker_data_wrap" msgpack_unpacker_data ::
|
||||
Ptr () -> Ptr () -> IO ()
|
||||
|
||||
-- | Release memory zone. The returned zone must be freed by calling 'freeZone'.
|
||||
unpackerReleaseZone :: Unpacker -> IO Zone
|
||||
unpackerReleaseZone up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_release_zone ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_release_zone" msgpack_unpacker_release_zone ::
|
||||
Ptr () -> IO (Ptr ())
|
||||
|
||||
-- | Free memory zone used by Unapcker.
|
||||
unpackerResetZone :: Unpacker -> IO ()
|
||||
unpackerResetZone up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_reset_zone ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_reset_zone" msgpack_unpacker_reset_zone ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
-- | Reset Unpacker state except memory zone.
|
||||
unpackerReset :: Unpacker -> IO ()
|
||||
unpackerReset up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
msgpack_unpacker_reset ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_reset" msgpack_unpacker_reset ::
|
||||
Ptr () -> IO ()
|
||||
|
||||
-- | Returns number of bytes of sequence of deserializing object.
|
||||
unpackerMessageSize :: Unpacker -> IO Int
|
||||
unpackerMessageSize up =
|
||||
withForeignPtr up $ \ptr ->
|
||||
liftM fromIntegral $ msgpack_unpacker_message_size ptr
|
||||
|
||||
foreign import ccall "msgpack_unpacker_message_size_wrap" msgpack_unpacker_message_size ::
|
||||
Ptr () -> IO CSize
|
||||
|
||||
type Zone = Ptr ()
|
||||
|
||||
-- | Create a new memory zone. It must be freed manually.
|
||||
newZone :: IO Zone
|
||||
newZone =
|
||||
msgpack_zone_new (#const MSGPACK_ZONE_CHUNK_SIZE)
|
||||
|
||||
-- | Free a memory zone.
|
||||
freeZone :: Zone -> IO ()
|
||||
freeZone z =
|
||||
msgpack_zone_free z
|
||||
|
||||
-- | Create a memory zone, then execute argument, then free memory zone.
|
||||
withZone :: (Zone -> IO a) -> IO a
|
||||
withZone z =
|
||||
bracket newZone freeZone z
|
||||
|
||||
foreign import ccall "msgpack_zone_new" msgpack_zone_new ::
|
||||
CSize -> IO Zone
|
||||
|
||||
foreign import ccall "msgpack_zone_free" msgpack_zone_free ::
|
||||
Zone -> IO ()
|
||||
|
||||
-- | Object Representation of MessagePack data.
|
||||
data Object =
|
||||
ObjectNil
|
||||
| ObjectBool Bool
|
||||
| ObjectInteger Int
|
||||
| ObjectDouble Double
|
||||
| ObjectRAW ByteString
|
||||
| ObjectArray [Object]
|
||||
| ObjectMap [(Object, Object)]
|
||||
deriving (Show)
|
||||
|
||||
peekObject :: Ptr a -> IO Object
|
||||
peekObject ptr = do
|
||||
typ <- (#peek msgpack_object, type) ptr
|
||||
case (typ :: CInt) of
|
||||
(#const MSGPACK_OBJECT_NIL) ->
|
||||
return ObjectNil
|
||||
(#const MSGPACK_OBJECT_BOOLEAN) ->
|
||||
peekObjectBool ptr
|
||||
(#const MSGPACK_OBJECT_POSITIVE_INTEGER) ->
|
||||
peekObjectPositiveInteger ptr
|
||||
(#const MSGPACK_OBJECT_NEGATIVE_INTEGER) ->
|
||||
peekObjectNegativeInteger ptr
|
||||
(#const MSGPACK_OBJECT_DOUBLE) ->
|
||||
peekObjectDouble ptr
|
||||
(#const MSGPACK_OBJECT_RAW) ->
|
||||
peekObjectRAW ptr
|
||||
(#const MSGPACK_OBJECT_ARRAY) ->
|
||||
peekObjectArray ptr
|
||||
(#const MSGPACK_OBJECT_MAP) ->
|
||||
peekObjectMap ptr
|
||||
_ ->
|
||||
fail $ "peekObject: unknown object type (" ++ show typ ++ ")"
|
||||
|
||||
peekObjectBool :: Ptr a -> IO Object
|
||||
peekObjectBool ptr = do
|
||||
b <- (#peek msgpack_object, via.boolean) ptr
|
||||
return $ ObjectBool $ (b :: CUChar) /= 0
|
||||
|
||||
peekObjectPositiveInteger :: Ptr a -> IO Object
|
||||
peekObjectPositiveInteger ptr = do
|
||||
n <- (#peek msgpack_object, via.u64) ptr
|
||||
return $ ObjectInteger $ fromIntegral (n :: Word64)
|
||||
|
||||
peekObjectNegativeInteger :: Ptr a -> IO Object
|
||||
peekObjectNegativeInteger ptr = do
|
||||
n <- (#peek msgpack_object, via.i64) ptr
|
||||
return $ ObjectInteger $ fromIntegral (n :: Int64)
|
||||
|
||||
peekObjectDouble :: Ptr a -> IO Object
|
||||
peekObjectDouble ptr = do
|
||||
d <- (#peek msgpack_object, via.dec) ptr
|
||||
return $ ObjectDouble $ realToFrac (d :: CDouble)
|
||||
|
||||
peekObjectRAW :: Ptr a -> IO Object
|
||||
peekObjectRAW ptr = do
|
||||
size <- (#peek msgpack_object, via.raw.size) ptr
|
||||
p <- (#peek msgpack_object, via.raw.ptr) ptr
|
||||
bs <- BS.packCStringLen (p, fromIntegral (size :: Word32))
|
||||
return $ ObjectRAW bs
|
||||
|
||||
peekObjectArray :: Ptr a -> IO Object
|
||||
peekObjectArray ptr = do
|
||||
csize <- (#peek msgpack_object, via.array.size) ptr
|
||||
let size = fromIntegral (csize :: Word32)
|
||||
p <- (#peek msgpack_object, via.array.ptr) ptr
|
||||
objs <- mapM (\i -> peekObject $ p `plusPtr`
|
||||
((#size msgpack_object) * i))
|
||||
[0..size-1]
|
||||
return $ ObjectArray objs
|
||||
|
||||
peekObjectMap :: Ptr a -> IO Object
|
||||
peekObjectMap ptr = do
|
||||
csize <- (#peek msgpack_object, via.map.size) ptr
|
||||
let size = fromIntegral (csize :: Word32)
|
||||
p <- (#peek msgpack_object, via.map.ptr) ptr
|
||||
dat <- mapM (\i -> peekObjectKV $ p `plusPtr`
|
||||
((#size msgpack_object_kv) * i))
|
||||
[0..size-1]
|
||||
return $ ObjectMap dat
|
||||
|
||||
peekObjectKV :: Ptr a -> IO (Object, Object)
|
||||
peekObjectKV ptr = do
|
||||
k <- peekObject $ ptr `plusPtr` (#offset msgpack_object_kv, key)
|
||||
v <- peekObject $ ptr `plusPtr` (#offset msgpack_object_kv, val)
|
||||
return (k, v)
|
||||
|
||||
-- | Pack a Object.
|
||||
packObject :: Packer -> Object -> IO ()
|
||||
packObject pc ObjectNil = packNil pc >> return ()
|
||||
|
||||
packObject pc (ObjectBool b) = packBool pc b >> return ()
|
||||
|
||||
packObject pc (ObjectInteger n) = packInt pc n >> return ()
|
||||
|
||||
packObject pc (ObjectDouble d) = packDouble pc d >> return ()
|
||||
|
||||
packObject pc (ObjectRAW bs) = packRAW' pc bs >> return ()
|
||||
|
||||
packObject pc (ObjectArray ls) = do
|
||||
_ <- packArray pc (length ls)
|
||||
mapM_ (packObject pc) ls
|
||||
|
||||
packObject pc (ObjectMap ls) = do
|
||||
_ <- packMap pc (length ls)
|
||||
mapM_ (\(a, b) -> packObject pc a >> packObject pc b) ls
|
||||
|
||||
data UnpackReturn =
|
||||
UnpackContinue -- ^ not enough bytes to unpack object
|
||||
| UnpackParseError -- ^ got invalid bytes
|
||||
| UnpackError -- ^ other error
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | Unpack a single MessagePack object from byte sequence.
|
||||
unpackObject :: Zone -> ByteString -> IO (Either UnpackReturn (Int, Object))
|
||||
unpackObject z dat =
|
||||
allocaBytes (#size msgpack_object) $ \ptr ->
|
||||
BS.useAsCStringLen dat $ \(str, len) ->
|
||||
alloca $ \poff -> do
|
||||
poke poff 0
|
||||
ret <- msgpack_unpack str (fromIntegral len) poff z ptr
|
||||
case ret of
|
||||
(#const MSGPACK_UNPACK_SUCCESS) -> do
|
||||
off <- peek poff
|
||||
obj <- peekObject ptr
|
||||
return $ Right (fromIntegral off, obj)
|
||||
(#const MSGPACK_UNPACK_EXTRA_BYTES) -> do
|
||||
off <- peek poff
|
||||
obj <- peekObject ptr
|
||||
return $ Right (fromIntegral off, obj)
|
||||
(#const MSGPACK_UNPACK_CONTINUE) ->
|
||||
return $ Left UnpackContinue
|
||||
(#const MSGPACK_UNPACK_PARSE_ERROR) ->
|
||||
return $ Left UnpackParseError
|
||||
_ ->
|
||||
return $ Left UnpackError
|
||||
|
||||
foreign import ccall "msgpack_unpack" msgpack_unpack ::
|
||||
Ptr CChar -> CSize -> Ptr CSize -> Zone -> Ptr () -> IO CInt
|
@@ -1,101 +0,0 @@
|
||||
{-# LANGUAGE TypeSynonymInstances #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE OverlappingInstances #-}
|
||||
{-# LANGUAGE IncoherentInstances #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Class
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Serializing Haskell values to and from MessagePack Objects.
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Class(
|
||||
-- * Serialization to and from Object
|
||||
OBJECT(..),
|
||||
Result,
|
||||
pack,
|
||||
) where
|
||||
|
||||
import Control.Monad.Error
|
||||
import Data.ByteString.Char8 (ByteString)
|
||||
import qualified Data.ByteString.Char8 as C8
|
||||
|
||||
import Data.MessagePack.Base
|
||||
|
||||
-- | The class of types serializable to and from MessagePack object
|
||||
class OBJECT a where
|
||||
toObject :: a -> Object
|
||||
fromObject :: Object -> Result a
|
||||
|
||||
-- | A type for parser results
|
||||
type Result a = Either String a
|
||||
|
||||
instance OBJECT Object where
|
||||
toObject = id
|
||||
fromObject = Right
|
||||
|
||||
fromObjectError :: String
|
||||
fromObjectError = "fromObject: cannot cast"
|
||||
|
||||
instance OBJECT () where
|
||||
toObject = const ObjectNil
|
||||
fromObject ObjectNil = Right ()
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT Int where
|
||||
toObject = ObjectInteger
|
||||
fromObject (ObjectInteger n) = Right n
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT Bool where
|
||||
toObject = ObjectBool
|
||||
fromObject (ObjectBool b) = Right b
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT Double where
|
||||
toObject = ObjectDouble
|
||||
fromObject (ObjectDouble d) = Right d
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT ByteString where
|
||||
toObject = ObjectRAW
|
||||
fromObject (ObjectRAW bs) = Right bs
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT String where
|
||||
toObject = toObject . C8.pack
|
||||
fromObject obj = liftM C8.unpack $ fromObject obj
|
||||
|
||||
instance OBJECT a => OBJECT [a] where
|
||||
toObject = ObjectArray . map toObject
|
||||
fromObject (ObjectArray arr) =
|
||||
mapM fromObject arr
|
||||
fromObject _ =
|
||||
Left fromObjectError
|
||||
|
||||
instance (OBJECT a, OBJECT b) => OBJECT [(a, b)] where
|
||||
toObject =
|
||||
ObjectMap . map (\(a, b) -> (toObject a, toObject b))
|
||||
fromObject (ObjectMap mem) = do
|
||||
mapM (\(a, b) -> liftM2 (,) (fromObject a) (fromObject b)) mem
|
||||
fromObject _ =
|
||||
Left fromObjectError
|
||||
|
||||
instance OBJECT a => OBJECT (Maybe a) where
|
||||
toObject (Just a) = toObject a
|
||||
toObject Nothing = ObjectNil
|
||||
|
||||
fromObject ObjectNil = return Nothing
|
||||
fromObject obj = liftM Just $ fromObject obj
|
||||
|
||||
-- | Pack a serializable Haskell value.
|
||||
pack :: OBJECT a => Packer -> a -> IO ()
|
||||
pack pc = packObject pc . toObject
|
106
haskell/src/Data/MessagePack/Derive.hs
Normal file
106
haskell/src/Data/MessagePack/Derive.hs
Normal file
@@ -0,0 +1,106 @@
|
||||
{-# Language TemplateHaskell #-}
|
||||
|
||||
module Data.MessagePack.Derive (
|
||||
derivePack,
|
||||
deriveUnpack,
|
||||
deriveObject,
|
||||
) where
|
||||
|
||||
import Control.Applicative
|
||||
import Control.Monad
|
||||
import Language.Haskell.TH
|
||||
|
||||
import Data.MessagePack.Pack
|
||||
import Data.MessagePack.Unpack
|
||||
import Data.MessagePack.Object
|
||||
|
||||
deriveUnpack :: Name -> Q [Dec]
|
||||
deriveUnpack typName = do
|
||||
TyConI (DataD _ name _ cons _) <- reify typName
|
||||
|
||||
return
|
||||
[ InstanceD [] (AppT (ConT ''Unpackable) (ConT name))
|
||||
[ FunD 'get [Clause [] (NormalB $ ch $ map body cons) []]
|
||||
]]
|
||||
|
||||
where
|
||||
body (NormalC conName elms) =
|
||||
DoE $
|
||||
tupOrListP (map VarP names) (VarE 'get) ++
|
||||
[ NoBindS $ AppE (VarE 'return) $ foldl AppE (ConE conName) $ map VarE names ]
|
||||
where
|
||||
names = zipWith (\ix _ -> mkName $ "a" ++ show (ix :: Int)) [1..] elms
|
||||
|
||||
body (RecC conName elms) =
|
||||
body (NormalC conName $ map (\(_, b, c) -> (b, c)) elms)
|
||||
|
||||
ch = foldl1 (\e f -> AppE (AppE (VarE '(<|>)) e) f)
|
||||
|
||||
derivePack :: Name -> Q [Dec]
|
||||
derivePack typName = do
|
||||
TyConI (DataD _ name _ cons _) <- reify typName
|
||||
|
||||
return
|
||||
[ InstanceD [] (AppT (ConT ''Packable) (ConT name))
|
||||
[ FunD 'put (map body cons)
|
||||
]]
|
||||
|
||||
where
|
||||
body (NormalC conName elms) =
|
||||
Clause
|
||||
[ ConP conName $ map VarP names ]
|
||||
(NormalB $ AppE (VarE 'put) $ tupOrListE $ map VarE names) []
|
||||
where
|
||||
names = zipWith (\ix _ -> mkName $ "a" ++ show (ix :: Int)) [1..] elms
|
||||
|
||||
body (RecC conName elms) =
|
||||
body (NormalC conName $ map (\(_, b, c) -> (b, c)) elms)
|
||||
|
||||
deriveObject :: Name -> Q [Dec]
|
||||
deriveObject typName = do
|
||||
g <- derivePack typName
|
||||
p <- deriveUnpack typName
|
||||
|
||||
TyConI (DataD _ name _ cons _) <- reify typName
|
||||
let o = InstanceD [] (AppT (ConT ''OBJECT) (ConT name))
|
||||
[ FunD 'toObject (map toObjectBody cons),
|
||||
FunD 'tryFromObject [Clause [ VarP oname ]
|
||||
(NormalB $ ch $ map tryFromObjectBody cons) []]]
|
||||
|
||||
return $ g ++ p ++ [o]
|
||||
where
|
||||
toObjectBody (NormalC conName elms) =
|
||||
Clause
|
||||
[ ConP conName $ map VarP names ]
|
||||
(NormalB $ AppE (VarE 'toObject) $ tupOrListE $ map VarE names) []
|
||||
where
|
||||
names = zipWith (\ix _ -> mkName $ "a" ++ show (ix :: Int)) [1..] elms
|
||||
toObjectBody (RecC conName elms) =
|
||||
toObjectBody (NormalC conName $ map (\(_, b, c) -> (b, c)) elms)
|
||||
|
||||
tryFromObjectBody (NormalC conName elms) =
|
||||
DoE $
|
||||
tupOrListP (map VarP names) (AppE (VarE 'tryFromObject) (VarE oname)) ++
|
||||
[ NoBindS $ AppE (VarE 'return) $ foldl AppE (ConE conName) $ map VarE names ]
|
||||
where
|
||||
names = zipWith (\ix _ -> mkName $ "a" ++ show (ix :: Int)) [1..] elms
|
||||
tryFromObjectBody (RecC conName elms) =
|
||||
tryFromObjectBody (NormalC conName $ map (\(_, b, c) -> (b, c)) elms)
|
||||
|
||||
oname = mkName "o"
|
||||
ch = foldl1 (\e f -> AppE (AppE (VarE '(<|>)) e) f)
|
||||
|
||||
tupOrListP :: [Pat] -> Exp -> [Stmt]
|
||||
tupOrListP ls e
|
||||
| length ls == 0 =
|
||||
let lsname = mkName "ls" in
|
||||
[ BindS (VarP lsname) e
|
||||
, NoBindS $ AppE (VarE 'guard) $ AppE (VarE 'null) $ SigE (VarE lsname) (AppT ListT (ConT ''())) ]
|
||||
| length ls == 1 = [ BindS (ListP ls) e ]
|
||||
| otherwise = [ BindS (TupP ls) e ]
|
||||
|
||||
tupOrListE :: [Exp] -> Exp
|
||||
tupOrListE ls
|
||||
| length ls == 0 = SigE (ListE []) (AppT ListT (ConT ''()))
|
||||
| length ls == 1 = ListE ls
|
||||
| otherwise = TupE ls
|
@@ -1,62 +0,0 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Feed
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Feeders for Stream Deserializers
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Feed(
|
||||
-- * Feeder type
|
||||
Feeder,
|
||||
-- * Feeders
|
||||
feederFromHandle,
|
||||
feederFromFile,
|
||||
feederFromString,
|
||||
) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.IORef
|
||||
import System.IO
|
||||
|
||||
-- | Feeder returns Just ByteString when bytes remains, otherwise Nothing.
|
||||
type Feeder = IO (Maybe ByteString)
|
||||
|
||||
-- | Feeder from Handle
|
||||
feederFromHandle :: Handle -> IO Feeder
|
||||
feederFromHandle h = return $ do
|
||||
bs <- BS.hGetNonBlocking h bufSize
|
||||
if BS.length bs > 0
|
||||
then do return $ Just bs
|
||||
else do
|
||||
c <- BS.hGet h 1
|
||||
if BS.length c > 0
|
||||
then do return $ Just c
|
||||
else do
|
||||
hClose h
|
||||
return Nothing
|
||||
where
|
||||
bufSize = 4096
|
||||
|
||||
-- | Feeder from File
|
||||
feederFromFile :: FilePath -> IO Feeder
|
||||
feederFromFile path =
|
||||
openFile path ReadMode >>= feederFromHandle
|
||||
|
||||
-- | Feeder from ByteString
|
||||
feederFromString :: ByteString -> IO Feeder
|
||||
feederFromString bs = do
|
||||
r <- newIORef (Just bs)
|
||||
return $ f r
|
||||
where
|
||||
f r = do
|
||||
mb <- readIORef r
|
||||
writeIORef r Nothing
|
||||
return mb
|
28
haskell/src/Data/MessagePack/Internal/Utf8.hs
Normal file
28
haskell/src/Data/MessagePack/Internal/Utf8.hs
Normal file
@@ -0,0 +1,28 @@
|
||||
module Data.MessagePack.Internal.Utf8 (
|
||||
encodeUtf8,
|
||||
decodeUtf8,
|
||||
skipChar,
|
||||
toLBS,
|
||||
fromLBS,
|
||||
) where
|
||||
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as T
|
||||
import qualified Data.Text.Encoding.Error as T
|
||||
|
||||
encodeUtf8 :: String -> B.ByteString
|
||||
encodeUtf8 = T.encodeUtf8 . T.pack
|
||||
|
||||
decodeUtf8 :: B.ByteString -> String
|
||||
decodeUtf8 = T.unpack . T.decodeUtf8With skipChar
|
||||
|
||||
skipChar :: T.OnDecodeError
|
||||
skipChar _ _ = Nothing
|
||||
|
||||
toLBS :: B.ByteString -> BL.ByteString
|
||||
toLBS bs = BL.fromChunks [bs]
|
||||
|
||||
fromLBS :: BL.ByteString -> B.ByteString
|
||||
fromLBS = B.concat . BL.toChunks
|
@@ -1,156 +0,0 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Monad
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Monadic Stream Serializers and Deserializers
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Monad(
|
||||
-- * Classes
|
||||
MonadPacker(..),
|
||||
MonadUnpacker(..),
|
||||
|
||||
-- * Packer and Unpacker type
|
||||
PackerT(..),
|
||||
UnpackerT(..),
|
||||
|
||||
-- * Packers
|
||||
packToString,
|
||||
packToHandle,
|
||||
packToFile,
|
||||
|
||||
-- * Unpackers
|
||||
unpackFrom,
|
||||
unpackFromString,
|
||||
unpackFromHandle,
|
||||
unpackFromFile,
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Control.Monad.Trans
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import System.IO
|
||||
|
||||
import Data.MessagePack.Base hiding (Unpacker)
|
||||
import qualified Data.MessagePack.Base as Base
|
||||
import Data.MessagePack.Class
|
||||
import Data.MessagePack.Feed
|
||||
|
||||
class Monad m => MonadPacker m where
|
||||
-- | Serialize a object
|
||||
put :: OBJECT a => a -> m ()
|
||||
|
||||
class Monad m => MonadUnpacker m where
|
||||
-- | Deserialize a object
|
||||
get :: OBJECT a => m a
|
||||
|
||||
-- | Serializer Type
|
||||
newtype PackerT m r = PackerT { runPackerT :: Base.Packer -> m r }
|
||||
|
||||
instance Monad m => Monad (PackerT m) where
|
||||
a >>= b =
|
||||
PackerT $ \pc -> do
|
||||
r <- runPackerT a pc
|
||||
runPackerT (b r) pc
|
||||
|
||||
return r =
|
||||
PackerT $ \_ -> return r
|
||||
|
||||
instance MonadTrans PackerT where
|
||||
lift m = PackerT $ \_ -> m
|
||||
|
||||
instance MonadIO m => MonadIO (PackerT m) where
|
||||
liftIO = lift . liftIO
|
||||
|
||||
instance MonadIO m => MonadPacker (PackerT m) where
|
||||
put v = PackerT $ \pc -> liftIO $ do
|
||||
pack pc v
|
||||
|
||||
-- | Execute given serializer and returns byte sequence.
|
||||
packToString :: MonadIO m => PackerT m r -> m ByteString
|
||||
packToString m = do
|
||||
sb <- liftIO $ newSimpleBuffer
|
||||
pc <- liftIO $ newPacker sb
|
||||
_ <- runPackerT m pc
|
||||
liftIO $ simpleBufferData sb
|
||||
|
||||
-- | Execute given serializer and write byte sequence to Handle.
|
||||
packToHandle :: MonadIO m => Handle -> PackerT m r -> m ()
|
||||
packToHandle h m = do
|
||||
sb <- packToString m
|
||||
liftIO $ BS.hPut h sb
|
||||
liftIO $ hFlush h
|
||||
|
||||
-- | Execute given serializer and write byte sequence to file.
|
||||
packToFile :: MonadIO m => FilePath -> PackerT m r -> m ()
|
||||
packToFile p m = do
|
||||
sb <- packToString m
|
||||
liftIO $ BS.writeFile p sb
|
||||
|
||||
-- | Deserializer type
|
||||
newtype UnpackerT m r = UnpackerT { runUnpackerT :: Base.Unpacker -> Feeder -> m r }
|
||||
|
||||
instance Monad m => Monad (UnpackerT m) where
|
||||
a >>= b =
|
||||
UnpackerT $ \up feed -> do
|
||||
r <- runUnpackerT a up feed
|
||||
runUnpackerT (b r) up feed
|
||||
|
||||
return r =
|
||||
UnpackerT $ \_ _ -> return r
|
||||
|
||||
instance MonadTrans UnpackerT where
|
||||
lift m = UnpackerT $ \_ _ -> m
|
||||
|
||||
instance MonadIO m => MonadIO (UnpackerT m) where
|
||||
liftIO = lift . liftIO
|
||||
|
||||
instance MonadIO m => MonadUnpacker (UnpackerT m) where
|
||||
get = UnpackerT $ \up feed -> liftIO $ do
|
||||
executeOne up feed
|
||||
obj <- unpackerData up
|
||||
freeZone =<< unpackerReleaseZone up
|
||||
unpackerReset up
|
||||
let Right r = fromObject obj
|
||||
return r
|
||||
|
||||
where
|
||||
executeOne up feed = do
|
||||
resp <- unpackerExecute up
|
||||
guard $ resp>=0
|
||||
when (resp==0) $ do
|
||||
Just bs <- feed
|
||||
unpackerFeed up bs
|
||||
executeOne up feed
|
||||
|
||||
-- | Execute deserializer using given feeder.
|
||||
unpackFrom :: MonadIO m => Feeder -> UnpackerT m r -> m r
|
||||
unpackFrom f m = do
|
||||
up <- liftIO $ newUnpacker defaultInitialBufferSize
|
||||
runUnpackerT m up f
|
||||
|
||||
-- | Execute deserializer using given handle.
|
||||
unpackFromHandle :: MonadIO m => Handle -> UnpackerT m r -> m r
|
||||
unpackFromHandle h m =
|
||||
flip unpackFrom m =<< liftIO (feederFromHandle h)
|
||||
|
||||
-- | Execute deserializer using given file content.
|
||||
unpackFromFile :: MonadIO m => FilePath -> UnpackerT m r -> m r
|
||||
unpackFromFile p m = do
|
||||
h <- liftIO $ openFile p ReadMode
|
||||
r <- flip unpackFrom m =<< liftIO (feederFromHandle h)
|
||||
liftIO $ hClose h
|
||||
return r
|
||||
|
||||
-- | Execute deserializer from given byte sequence.
|
||||
unpackFromString :: MonadIO m => ByteString -> UnpackerT m r -> m r
|
||||
unpackFromString bs m = do
|
||||
flip unpackFrom m =<< liftIO (feederFromString bs)
|
334
haskell/src/Data/MessagePack/Object.hs
Normal file
334
haskell/src/Data/MessagePack/Object.hs
Normal file
@@ -0,0 +1,334 @@
|
||||
{-# Language TypeSynonymInstances #-}
|
||||
{-# Language FlexibleInstances #-}
|
||||
{-# Language IncoherentInstances #-}
|
||||
{-# Language DeriveDataTypeable #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Object
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009-2010
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- MessagePack object definition
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Object(
|
||||
-- * MessagePack Object
|
||||
Object(..),
|
||||
|
||||
-- * Serialization to and from Object
|
||||
OBJECT(..),
|
||||
-- Result,
|
||||
) where
|
||||
|
||||
import Control.DeepSeq
|
||||
import Control.Exception
|
||||
import Control.Monad
|
||||
import Control.Monad.Trans.Error ()
|
||||
import qualified Data.Attoparsec as A
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as T
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import qualified Data.Text.Lazy.Encoding as TL
|
||||
import Data.Typeable
|
||||
|
||||
import Data.MessagePack.Assoc
|
||||
import Data.MessagePack.Pack
|
||||
import Data.MessagePack.Unpack
|
||||
import Data.MessagePack.Internal.Utf8
|
||||
|
||||
-- | Object Representation of MessagePack data.
|
||||
data Object
|
||||
= ObjectNil
|
||||
| ObjectBool Bool
|
||||
| ObjectInteger Int
|
||||
| ObjectFloat Float
|
||||
| ObjectDouble Double
|
||||
| ObjectRAW B.ByteString
|
||||
| ObjectArray [Object]
|
||||
| ObjectMap [(Object, Object)]
|
||||
deriving (Show, Eq, Ord, Typeable)
|
||||
|
||||
|
||||
instance NFData Object where
|
||||
rnf obj =
|
||||
case obj of
|
||||
ObjectNil -> ()
|
||||
ObjectBool b -> rnf b
|
||||
ObjectInteger n -> rnf n
|
||||
ObjectFloat f -> rnf f
|
||||
ObjectDouble d -> rnf d
|
||||
ObjectRAW bs -> bs `seq` ()
|
||||
ObjectArray a -> rnf a
|
||||
ObjectMap m -> rnf m
|
||||
|
||||
|
||||
instance Unpackable Object where
|
||||
get =
|
||||
A.choice
|
||||
[ liftM ObjectInteger get
|
||||
, liftM (\() -> ObjectNil) get
|
||||
, liftM ObjectBool get
|
||||
, liftM ObjectFloat get
|
||||
, liftM ObjectDouble get
|
||||
, liftM ObjectRAW get
|
||||
, liftM ObjectArray get
|
||||
, liftM (ObjectMap . unAssoc) get
|
||||
]
|
||||
|
||||
instance Packable Object where
|
||||
put obj =
|
||||
case obj of
|
||||
ObjectInteger n ->
|
||||
put n
|
||||
ObjectNil ->
|
||||
put ()
|
||||
ObjectBool b ->
|
||||
put b
|
||||
ObjectFloat f ->
|
||||
put f
|
||||
ObjectDouble d ->
|
||||
put d
|
||||
ObjectRAW raw ->
|
||||
put raw
|
||||
ObjectArray arr ->
|
||||
put arr
|
||||
ObjectMap m ->
|
||||
put $ Assoc m
|
||||
|
||||
-- | The class of types serializable to and from MessagePack object
|
||||
class (Unpackable a, Packable a) => OBJECT a where
|
||||
-- | Encode a value to MessagePack object
|
||||
toObject :: a -> Object
|
||||
toObject = unpack . pack
|
||||
|
||||
-- | Decode a value from MessagePack object
|
||||
fromObject :: Object -> a
|
||||
fromObject a =
|
||||
case tryFromObject a of
|
||||
Left err ->
|
||||
throw $ UnpackError err
|
||||
Right ret ->
|
||||
ret
|
||||
|
||||
-- | Decode a value from MessagePack object
|
||||
tryFromObject :: Object -> Either String a
|
||||
tryFromObject = tryUnpack . pack
|
||||
|
||||
instance OBJECT Object where
|
||||
toObject = id
|
||||
tryFromObject = Right
|
||||
|
||||
tryFromObjectError :: Either String a
|
||||
tryFromObjectError = Left "tryFromObject: cannot cast"
|
||||
|
||||
instance OBJECT () where
|
||||
toObject = const ObjectNil
|
||||
tryFromObject ObjectNil = Right ()
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT Int where
|
||||
toObject = ObjectInteger
|
||||
tryFromObject (ObjectInteger n) = Right n
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT Bool where
|
||||
toObject = ObjectBool
|
||||
tryFromObject (ObjectBool b) = Right b
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT Double where
|
||||
toObject = ObjectDouble
|
||||
tryFromObject (ObjectDouble d) = Right d
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT Float where
|
||||
toObject = ObjectFloat
|
||||
tryFromObject (ObjectFloat f) = Right f
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT String where
|
||||
toObject = toObject . encodeUtf8
|
||||
tryFromObject obj = liftM decodeUtf8 $ tryFromObject obj
|
||||
|
||||
instance OBJECT B.ByteString where
|
||||
toObject = ObjectRAW
|
||||
tryFromObject (ObjectRAW bs) = Right bs
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT BL.ByteString where
|
||||
toObject = ObjectRAW . fromLBS
|
||||
tryFromObject (ObjectRAW bs) = Right $ toLBS bs
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT T.Text where
|
||||
toObject = ObjectRAW . T.encodeUtf8
|
||||
tryFromObject (ObjectRAW bs) = Right $ T.decodeUtf8With skipChar bs
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT TL.Text where
|
||||
toObject = ObjectRAW . fromLBS . TL.encodeUtf8
|
||||
tryFromObject (ObjectRAW bs) = Right $ TL.decodeUtf8With skipChar $ toLBS bs
|
||||
tryFromObject _ = tryFromObjectError
|
||||
|
||||
instance OBJECT a => OBJECT [a] where
|
||||
toObject = ObjectArray . map toObject
|
||||
tryFromObject (ObjectArray arr) =
|
||||
mapM tryFromObject arr
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2) => OBJECT (a1, a2) where
|
||||
toObject (a1, a2) = ObjectArray [toObject a1, toObject a2]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
return (v1, v2)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3) => OBJECT (a1, a2, a3) where
|
||||
toObject (a1, a2, a3) = ObjectArray [toObject a1, toObject a2, toObject a3]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
return (v1, v2, v3)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3, OBJECT a4) => OBJECT (a1, a2, a3, a4) where
|
||||
toObject (a1, a2, a3, a4) = ObjectArray [toObject a1, toObject a2, toObject a3, toObject a4]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3, o4] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
v4 <- tryFromObject o4
|
||||
return (v1, v2, v3, v4)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3, OBJECT a4, OBJECT a5) => OBJECT (a1, a2, a3, a4, a5) where
|
||||
toObject (a1, a2, a3, a4, a5) = ObjectArray [toObject a1, toObject a2, toObject a3, toObject a4, toObject a5]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3, o4, o5] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
v4 <- tryFromObject o4
|
||||
v5 <- tryFromObject o5
|
||||
return (v1, v2, v3, v4, v5)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3, OBJECT a4, OBJECT a5, OBJECT a6) => OBJECT (a1, a2, a3, a4, a5, a6) where
|
||||
toObject (a1, a2, a3, a4, a5, a6) = ObjectArray [toObject a1, toObject a2, toObject a3, toObject a4, toObject a5, toObject a6]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3, o4, o5, o6] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
v4 <- tryFromObject o4
|
||||
v5 <- tryFromObject o5
|
||||
v6 <- tryFromObject o6
|
||||
return (v1, v2, v3, v4, v5, v6)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3, OBJECT a4, OBJECT a5, OBJECT a6, OBJECT a7) => OBJECT (a1, a2, a3, a4, a5, a6, a7) where
|
||||
toObject (a1, a2, a3, a4, a5, a6, a7) = ObjectArray [toObject a1, toObject a2, toObject a3, toObject a4, toObject a5, toObject a6, toObject a7]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3, o4, o5, o6, o7] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
v4 <- tryFromObject o4
|
||||
v5 <- tryFromObject o5
|
||||
v6 <- tryFromObject o6
|
||||
v7 <- tryFromObject o7
|
||||
return (v1, v2, v3, v4, v5, v6, v7)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3, OBJECT a4, OBJECT a5, OBJECT a6, OBJECT a7, OBJECT a8) => OBJECT (a1, a2, a3, a4, a5, a6, a7, a8) where
|
||||
toObject (a1, a2, a3, a4, a5, a6, a7, a8) = ObjectArray [toObject a1, toObject a2, toObject a3, toObject a4, toObject a5, toObject a6, toObject a7, toObject a8]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3, o4, o5, o6, o7, o8] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
v4 <- tryFromObject o4
|
||||
v5 <- tryFromObject o5
|
||||
v6 <- tryFromObject o6
|
||||
v7 <- tryFromObject o7
|
||||
v8 <- tryFromObject o8
|
||||
return (v1, v2, v3, v4, v5, v6, v7, v8)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a1, OBJECT a2, OBJECT a3, OBJECT a4, OBJECT a5, OBJECT a6, OBJECT a7, OBJECT a8, OBJECT a9) => OBJECT (a1, a2, a3, a4, a5, a6, a7, a8, a9) where
|
||||
toObject (a1, a2, a3, a4, a5, a6, a7, a8, a9) = ObjectArray [toObject a1, toObject a2, toObject a3, toObject a4, toObject a5, toObject a6, toObject a7, toObject a8, toObject a9]
|
||||
tryFromObject (ObjectArray arr) =
|
||||
case arr of
|
||||
[o1, o2, o3, o4, o5, o6, o7, o8, o9] -> do
|
||||
v1 <- tryFromObject o1
|
||||
v2 <- tryFromObject o2
|
||||
v3 <- tryFromObject o3
|
||||
v4 <- tryFromObject o4
|
||||
v5 <- tryFromObject o5
|
||||
v6 <- tryFromObject o6
|
||||
v7 <- tryFromObject o7
|
||||
v8 <- tryFromObject o8
|
||||
v9 <- tryFromObject o9
|
||||
return (v1, v2, v3, v4, v5, v6, v7, v8, v9)
|
||||
_ ->
|
||||
tryFromObjectError
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance (OBJECT a, OBJECT b) => OBJECT (Assoc [(a,b)]) where
|
||||
toObject =
|
||||
ObjectMap . map (\(a, b) -> (toObject a, toObject b)) . unAssoc
|
||||
tryFromObject (ObjectMap mem) = do
|
||||
liftM Assoc $ mapM (\(a, b) -> liftM2 (,) (tryFromObject a) (tryFromObject b)) mem
|
||||
tryFromObject _ =
|
||||
tryFromObjectError
|
||||
|
||||
instance OBJECT a => OBJECT (Maybe a) where
|
||||
toObject (Just a) = toObject a
|
||||
toObject Nothing = ObjectNil
|
||||
|
||||
tryFromObject ObjectNil = return Nothing
|
||||
tryFromObject obj = liftM Just $ tryFromObject obj
|
||||
|
204
haskell/src/Data/MessagePack/Pack.hs
Normal file
204
haskell/src/Data/MessagePack/Pack.hs
Normal file
@@ -0,0 +1,204 @@
|
||||
{-# Language FlexibleInstances #-}
|
||||
{-# Language IncoherentInstances #-}
|
||||
{-# Language TypeSynonymInstances #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Pack
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009-2010
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- MessagePack Serializer using @Data.Binary.Pack@
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Pack (
|
||||
-- * Serializable class
|
||||
Packable(..),
|
||||
-- * Simple function to pack a Haskell value
|
||||
pack,
|
||||
) where
|
||||
|
||||
import Data.Binary.Put
|
||||
import Data.Binary.IEEE754
|
||||
import Data.Bits
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as T
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import qualified Data.Text.Lazy.Encoding as TL
|
||||
import qualified Data.Vector as V
|
||||
|
||||
import Data.MessagePack.Assoc
|
||||
import Data.MessagePack.Internal.Utf8
|
||||
|
||||
-- | Serializable class
|
||||
class Packable a where
|
||||
-- | Serialize a value
|
||||
put :: a -> Put
|
||||
|
||||
-- | Pack Haskell data to MessagePack string.
|
||||
pack :: Packable a => a -> BL.ByteString
|
||||
pack = runPut . put
|
||||
|
||||
instance Packable Int where
|
||||
put n =
|
||||
case n of
|
||||
_ | n >= 0 && n <= 127 ->
|
||||
putWord8 $ fromIntegral n
|
||||
_ | n >= -32 && n <= -1 ->
|
||||
putWord8 $ fromIntegral n
|
||||
_ | n >= 0 && n < 0x100 -> do
|
||||
putWord8 0xCC
|
||||
putWord8 $ fromIntegral n
|
||||
_ | n >= 0 && n < 0x10000 -> do
|
||||
putWord8 0xCD
|
||||
putWord16be $ fromIntegral n
|
||||
_ | n >= 0 && n < 0x100000000 -> do
|
||||
putWord8 0xCE
|
||||
putWord32be $ fromIntegral n
|
||||
_ | n >= 0 -> do
|
||||
putWord8 0xCF
|
||||
putWord64be $ fromIntegral n
|
||||
_ | n >= -0x80 -> do
|
||||
putWord8 0xD0
|
||||
putWord8 $ fromIntegral n
|
||||
_ | n >= -0x8000 -> do
|
||||
putWord8 0xD1
|
||||
putWord16be $ fromIntegral n
|
||||
_ | n >= -0x80000000 -> do
|
||||
putWord8 0xD2
|
||||
putWord32be $ fromIntegral n
|
||||
_ -> do
|
||||
putWord8 0xD3
|
||||
putWord64be $ fromIntegral n
|
||||
|
||||
instance Packable () where
|
||||
put _ =
|
||||
putWord8 0xC0
|
||||
|
||||
instance Packable Bool where
|
||||
put True = putWord8 0xC3
|
||||
put False = putWord8 0xC2
|
||||
|
||||
instance Packable Float where
|
||||
put f = do
|
||||
putWord8 0xCA
|
||||
putFloat32be f
|
||||
|
||||
instance Packable Double where
|
||||
put d = do
|
||||
putWord8 0xCB
|
||||
putFloat64be d
|
||||
|
||||
instance Packable String where
|
||||
put = putString encodeUtf8 B.length putByteString
|
||||
|
||||
instance Packable B.ByteString where
|
||||
put = putString id B.length putByteString
|
||||
|
||||
instance Packable BL.ByteString where
|
||||
put = putString id (fromIntegral . BL.length) putLazyByteString
|
||||
|
||||
instance Packable T.Text where
|
||||
put = putString T.encodeUtf8 B.length putByteString
|
||||
|
||||
instance Packable TL.Text where
|
||||
put = putString TL.encodeUtf8 (fromIntegral . BL.length) putLazyByteString
|
||||
|
||||
putString :: (s -> t) -> (t -> Int) -> (t -> Put) -> s -> Put
|
||||
putString cnv lf pf str = do
|
||||
let bs = cnv str
|
||||
case lf bs of
|
||||
len | len <= 31 -> do
|
||||
putWord8 $ 0xA0 .|. fromIntegral len
|
||||
len | len < 0x10000 -> do
|
||||
putWord8 0xDA
|
||||
putWord16be $ fromIntegral len
|
||||
len -> do
|
||||
putWord8 0xDB
|
||||
putWord32be $ fromIntegral len
|
||||
pf bs
|
||||
|
||||
instance Packable a => Packable [a] where
|
||||
put = putArray length (mapM_ put)
|
||||
|
||||
instance Packable a => Packable (V.Vector a) where
|
||||
put = putArray V.length (V.mapM_ put)
|
||||
|
||||
instance (Packable a1, Packable a2) => Packable (a1, a2) where
|
||||
put = putArray (const 2) f where
|
||||
f (a1, a2) = put a1 >> put a2
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3) => Packable (a1, a2, a3) where
|
||||
put = putArray (const 3) f where
|
||||
f (a1, a2, a3) = put a1 >> put a2 >> put a3
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3, Packable a4) => Packable (a1, a2, a3, a4) where
|
||||
put = putArray (const 4) f where
|
||||
f (a1, a2, a3, a4) = put a1 >> put a2 >> put a3 >> put a4
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3, Packable a4, Packable a5) => Packable (a1, a2, a3, a4, a5) where
|
||||
put = putArray (const 5) f where
|
||||
f (a1, a2, a3, a4, a5) = put a1 >> put a2 >> put a3 >> put a4 >> put a5
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3, Packable a4, Packable a5, Packable a6) => Packable (a1, a2, a3, a4, a5, a6) where
|
||||
put = putArray (const 6) f where
|
||||
f (a1, a2, a3, a4, a5, a6) = put a1 >> put a2 >> put a3 >> put a4 >> put a5 >> put a6
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3, Packable a4, Packable a5, Packable a6, Packable a7) => Packable (a1, a2, a3, a4, a5, a6, a7) where
|
||||
put = putArray (const 7) f where
|
||||
f (a1, a2, a3, a4, a5, a6, a7) = put a1 >> put a2 >> put a3 >> put a4 >> put a5 >> put a6 >> put a7
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3, Packable a4, Packable a5, Packable a6, Packable a7, Packable a8) => Packable (a1, a2, a3, a4, a5, a6, a7, a8) where
|
||||
put = putArray (const 8) f where
|
||||
f (a1, a2, a3, a4, a5, a6, a7, a8) = put a1 >> put a2 >> put a3 >> put a4 >> put a5 >> put a6 >> put a7 >> put a8
|
||||
|
||||
instance (Packable a1, Packable a2, Packable a3, Packable a4, Packable a5, Packable a6, Packable a7, Packable a8, Packable a9) => Packable (a1, a2, a3, a4, a5, a6, a7, a8, a9) where
|
||||
put = putArray (const 9) f where
|
||||
f (a1, a2, a3, a4, a5, a6, a7, a8, a9) = put a1 >> put a2 >> put a3 >> put a4 >> put a5 >> put a6 >> put a7 >> put a8 >> put a9
|
||||
|
||||
putArray :: (a -> Int) -> (a -> Put) -> a -> Put
|
||||
putArray lf pf arr = do
|
||||
case lf arr of
|
||||
len | len <= 15 ->
|
||||
putWord8 $ 0x90 .|. fromIntegral len
|
||||
len | len < 0x10000 -> do
|
||||
putWord8 0xDC
|
||||
putWord16be $ fromIntegral len
|
||||
len -> do
|
||||
putWord8 0xDD
|
||||
putWord32be $ fromIntegral len
|
||||
pf arr
|
||||
|
||||
instance (Packable k, Packable v) => Packable (Assoc [(k,v)]) where
|
||||
put = putMap length (mapM_ putPair) . unAssoc
|
||||
|
||||
instance (Packable k, Packable v) => Packable (Assoc (V.Vector (k,v))) where
|
||||
put = putMap V.length (V.mapM_ putPair) . unAssoc
|
||||
|
||||
putPair :: (Packable a, Packable b) => (a, b) -> Put
|
||||
putPair (a, b) = put a >> put b
|
||||
|
||||
putMap :: (a -> Int) -> (a -> Put) -> a -> Put
|
||||
putMap lf pf m = do
|
||||
case lf m of
|
||||
len | len <= 15 ->
|
||||
putWord8 $ 0x80 .|. fromIntegral len
|
||||
len | len < 0x10000 -> do
|
||||
putWord8 0xDE
|
||||
putWord16be $ fromIntegral len
|
||||
len -> do
|
||||
putWord8 0xDF
|
||||
putWord32be $ fromIntegral len
|
||||
pf m
|
||||
|
||||
instance Packable a => Packable (Maybe a) where
|
||||
put Nothing = put ()
|
||||
put (Just a) = put a
|
||||
|
@@ -1,82 +0,0 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Stream
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- Lazy Stream Serializers and Deserializers
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Stream(
|
||||
unpackObjects,
|
||||
unpackObjectsFromFile,
|
||||
unpackObjectsFromHandle,
|
||||
unpackObjectsFromString,
|
||||
) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import System.IO
|
||||
import System.IO.Unsafe
|
||||
|
||||
import Data.MessagePack.Base
|
||||
import Data.MessagePack.Feed
|
||||
|
||||
-- | Unpack objects using given feeder.
|
||||
unpackObjects :: Feeder -> IO [Object]
|
||||
unpackObjects feeder = do
|
||||
up <- newUnpacker defaultInitialBufferSize
|
||||
f up
|
||||
where
|
||||
f up = unsafeInterleaveIO $ do
|
||||
mbo <- unpackOnce up
|
||||
case mbo of
|
||||
Just o -> do
|
||||
os <- f up
|
||||
return $ o:os
|
||||
Nothing ->
|
||||
return []
|
||||
|
||||
unpackOnce up = do
|
||||
resp <- unpackerExecute up
|
||||
case resp of
|
||||
0 -> do
|
||||
r <- feedOnce up
|
||||
if r
|
||||
then unpackOnce up
|
||||
else return Nothing
|
||||
1 -> do
|
||||
obj <- unpackerData up
|
||||
freeZone =<< unpackerReleaseZone up
|
||||
unpackerReset up
|
||||
return $ Just obj
|
||||
_ ->
|
||||
error $ "unpackerExecute fails: " ++ show resp
|
||||
|
||||
feedOnce up = do
|
||||
dat <- feeder
|
||||
case dat of
|
||||
Nothing ->
|
||||
return False
|
||||
Just bs -> do
|
||||
unpackerFeed up bs
|
||||
return True
|
||||
|
||||
-- | Unpack objects from file.
|
||||
unpackObjectsFromFile :: FilePath -> IO [Object]
|
||||
unpackObjectsFromFile fname =
|
||||
unpackObjects =<< feederFromFile fname
|
||||
|
||||
-- | Unpack objects from handle.
|
||||
unpackObjectsFromHandle :: Handle -> IO [Object]
|
||||
unpackObjectsFromHandle h =
|
||||
unpackObjects =<< feederFromHandle h
|
||||
|
||||
-- | Unpack oobjects from given byte sequence.
|
||||
unpackObjectsFromString :: ByteString -> IO [Object]
|
||||
unpackObjectsFromString bs =
|
||||
unpackObjects =<< feederFromString bs
|
323
haskell/src/Data/MessagePack/Unpack.hs
Normal file
323
haskell/src/Data/MessagePack/Unpack.hs
Normal file
@@ -0,0 +1,323 @@
|
||||
{-# Language FlexibleInstances #-}
|
||||
{-# Language IncoherentInstances #-}
|
||||
{-# Language TypeSynonymInstances #-}
|
||||
{-# Language DeriveDataTypeable #-}
|
||||
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Unpack
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009-2010
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- MessagePack Deserializer using @Data.Attoparsec@
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Unpack(
|
||||
-- * MessagePack deserializer
|
||||
Unpackable(..),
|
||||
-- * Simple function to unpack a Haskell value
|
||||
unpack,
|
||||
tryUnpack,
|
||||
-- * Unpack exception
|
||||
UnpackError(..),
|
||||
-- * ByteString utils
|
||||
IsByteString(..),
|
||||
) where
|
||||
|
||||
import Control.Exception
|
||||
import Control.Monad
|
||||
import qualified Data.Attoparsec as A
|
||||
import Data.Binary.Get
|
||||
import Data.Binary.IEEE754
|
||||
import Data.Bits
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as T
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import qualified Data.Text.Lazy.Encoding as TL
|
||||
import Data.Int
|
||||
import Data.Typeable
|
||||
import qualified Data.Vector as V
|
||||
import Data.Word
|
||||
import Text.Printf
|
||||
|
||||
import Data.MessagePack.Assoc
|
||||
import Data.MessagePack.Internal.Utf8
|
||||
|
||||
-- | Deserializable class
|
||||
class Unpackable a where
|
||||
-- | Deserialize a value
|
||||
get :: A.Parser a
|
||||
|
||||
class IsByteString s where
|
||||
toBS :: s -> B.ByteString
|
||||
|
||||
instance IsByteString B.ByteString where
|
||||
toBS = id
|
||||
|
||||
instance IsByteString BL.ByteString where
|
||||
toBS = B.concat . BL.toChunks
|
||||
|
||||
-- | The exception of unpack
|
||||
data UnpackError =
|
||||
UnpackError String
|
||||
deriving (Show, Typeable)
|
||||
|
||||
instance Exception UnpackError
|
||||
|
||||
-- | Unpack MessagePack string to Haskell data.
|
||||
unpack :: (Unpackable a, IsByteString s) => s -> a
|
||||
unpack bs =
|
||||
case tryUnpack bs of
|
||||
Left err ->
|
||||
throw $ UnpackError err
|
||||
Right ret ->
|
||||
ret
|
||||
|
||||
-- | Unpack MessagePack string to Haskell data.
|
||||
tryUnpack :: (Unpackable a, IsByteString s) => s -> Either String a
|
||||
tryUnpack bs =
|
||||
case A.parse get (toBS bs) of
|
||||
A.Fail _ _ err ->
|
||||
Left err
|
||||
A.Partial _ ->
|
||||
Left "not enough input"
|
||||
A.Done _ ret ->
|
||||
Right ret
|
||||
|
||||
instance Unpackable Int where
|
||||
get = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
_ | c .&. 0x80 == 0x00 ->
|
||||
return $ fromIntegral c
|
||||
_ | c .&. 0xE0 == 0xE0 ->
|
||||
return $ fromIntegral (fromIntegral c :: Int8)
|
||||
0xCC ->
|
||||
return . fromIntegral =<< A.anyWord8
|
||||
0xCD ->
|
||||
return . fromIntegral =<< parseUint16
|
||||
0xCE ->
|
||||
return . fromIntegral =<< parseUint32
|
||||
0xCF ->
|
||||
return . fromIntegral =<< parseUint64
|
||||
0xD0 ->
|
||||
return . fromIntegral =<< parseInt8
|
||||
0xD1 ->
|
||||
return . fromIntegral =<< parseInt16
|
||||
0xD2 ->
|
||||
return . fromIntegral =<< parseInt32
|
||||
0xD3 ->
|
||||
return . fromIntegral =<< parseInt64
|
||||
_ ->
|
||||
fail $ printf "invlid integer tag: 0x%02X" c
|
||||
|
||||
instance Unpackable () where
|
||||
get = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
0xC0 ->
|
||||
return ()
|
||||
_ ->
|
||||
fail $ printf "invlid nil tag: 0x%02X" c
|
||||
|
||||
instance Unpackable Bool where
|
||||
get = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
0xC3 ->
|
||||
return True
|
||||
0xC2 ->
|
||||
return False
|
||||
_ ->
|
||||
fail $ printf "invlid bool tag: 0x%02X" c
|
||||
|
||||
instance Unpackable Float where
|
||||
get = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
0xCA ->
|
||||
return . runGet getFloat32be . toLBS =<< A.take 4
|
||||
_ ->
|
||||
fail $ printf "invlid float tag: 0x%02X" c
|
||||
|
||||
instance Unpackable Double where
|
||||
get = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
0xCB ->
|
||||
return . runGet getFloat64be . toLBS =<< A.take 8
|
||||
_ ->
|
||||
fail $ printf "invlid double tag: 0x%02X" c
|
||||
|
||||
instance Unpackable String where
|
||||
get = parseString (\n -> return . decodeUtf8 =<< A.take n)
|
||||
|
||||
instance Unpackable B.ByteString where
|
||||
get = parseString A.take
|
||||
|
||||
instance Unpackable BL.ByteString where
|
||||
get = parseString (\n -> return . toLBS =<< A.take n)
|
||||
|
||||
instance Unpackable T.Text where
|
||||
get = parseString (\n -> return . T.decodeUtf8With skipChar =<< A.take n)
|
||||
|
||||
instance Unpackable TL.Text where
|
||||
get = parseString (\n -> return . TL.decodeUtf8With skipChar . toLBS =<< A.take n)
|
||||
|
||||
parseString :: (Int -> A.Parser a) -> A.Parser a
|
||||
parseString aget = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
_ | c .&. 0xE0 == 0xA0 ->
|
||||
aget . fromIntegral $ c .&. 0x1F
|
||||
0xDA ->
|
||||
aget . fromIntegral =<< parseUint16
|
||||
0xDB ->
|
||||
aget . fromIntegral =<< parseUint32
|
||||
_ ->
|
||||
fail $ printf "invlid raw tag: 0x%02X" c
|
||||
|
||||
instance Unpackable a => Unpackable [a] where
|
||||
get = parseArray (flip replicateM get)
|
||||
|
||||
instance Unpackable a => Unpackable (V.Vector a) where
|
||||
get = parseArray (flip V.replicateM get)
|
||||
|
||||
instance (Unpackable a1, Unpackable a2) => Unpackable (a1, a2) where
|
||||
get = parseArray f where
|
||||
f 2 = get >>= \a1 -> get >>= \a2 -> return (a1, a2)
|
||||
f n = fail $ printf "wrong tupple size: expected 2 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3) => Unpackable (a1, a2, a3) where
|
||||
get = parseArray f where
|
||||
f 3 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> return (a1, a2, a3)
|
||||
f n = fail $ printf "wrong tupple size: expected 3 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3, Unpackable a4) => Unpackable (a1, a2, a3, a4) where
|
||||
get = parseArray f where
|
||||
f 4 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> get >>= \a4 -> return (a1, a2, a3, a4)
|
||||
f n = fail $ printf "wrong tupple size: expected 4 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3, Unpackable a4, Unpackable a5) => Unpackable (a1, a2, a3, a4, a5) where
|
||||
get = parseArray f where
|
||||
f 5 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> get >>= \a4 -> get >>= \a5 -> return (a1, a2, a3, a4, a5)
|
||||
f n = fail $ printf "wrong tupple size: expected 5 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3, Unpackable a4, Unpackable a5, Unpackable a6) => Unpackable (a1, a2, a3, a4, a5, a6) where
|
||||
get = parseArray f where
|
||||
f 6 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> get >>= \a4 -> get >>= \a5 -> get >>= \a6 -> return (a1, a2, a3, a4, a5, a6)
|
||||
f n = fail $ printf "wrong tupple size: expected 6 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3, Unpackable a4, Unpackable a5, Unpackable a6, Unpackable a7) => Unpackable (a1, a2, a3, a4, a5, a6, a7) where
|
||||
get = parseArray f where
|
||||
f 7 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> get >>= \a4 -> get >>= \a5 -> get >>= \a6 -> get >>= \a7 -> return (a1, a2, a3, a4, a5, a6, a7)
|
||||
f n = fail $ printf "wrong tupple size: expected 7 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3, Unpackable a4, Unpackable a5, Unpackable a6, Unpackable a7, Unpackable a8) => Unpackable (a1, a2, a3, a4, a5, a6, a7, a8) where
|
||||
get = parseArray f where
|
||||
f 8 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> get >>= \a4 -> get >>= \a5 -> get >>= \a6 -> get >>= \a7 -> get >>= \a8 -> return (a1, a2, a3, a4, a5, a6, a7, a8)
|
||||
f n = fail $ printf "wrong tupple size: expected 8 but got " n
|
||||
|
||||
instance (Unpackable a1, Unpackable a2, Unpackable a3, Unpackable a4, Unpackable a5, Unpackable a6, Unpackable a7, Unpackable a8, Unpackable a9) => Unpackable (a1, a2, a3, a4, a5, a6, a7, a8, a9) where
|
||||
get = parseArray f where
|
||||
f 9 = get >>= \a1 -> get >>= \a2 -> get >>= \a3 -> get >>= \a4 -> get >>= \a5 -> get >>= \a6 -> get >>= \a7 -> get >>= \a8 -> get >>= \a9 -> return (a1, a2, a3, a4, a5, a6, a7, a8, a9)
|
||||
f n = fail $ printf "wrong tupple size: expected 9 but got " n
|
||||
|
||||
parseArray :: (Int -> A.Parser a) -> A.Parser a
|
||||
parseArray aget = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
_ | c .&. 0xF0 == 0x90 ->
|
||||
aget . fromIntegral $ c .&. 0x0F
|
||||
0xDC ->
|
||||
aget . fromIntegral =<< parseUint16
|
||||
0xDD ->
|
||||
aget . fromIntegral =<< parseUint32
|
||||
_ ->
|
||||
fail $ printf "invlid array tag: 0x%02X" c
|
||||
|
||||
instance (Unpackable k, Unpackable v) => Unpackable (Assoc [(k,v)]) where
|
||||
get = liftM Assoc $ parseMap (flip replicateM parsePair)
|
||||
|
||||
instance (Unpackable k, Unpackable v) => Unpackable (Assoc (V.Vector (k, v))) where
|
||||
get = liftM Assoc $ parseMap (flip V.replicateM parsePair)
|
||||
|
||||
parsePair :: (Unpackable k, Unpackable v) => A.Parser (k, v)
|
||||
parsePair = do
|
||||
a <- get
|
||||
b <- get
|
||||
return (a, b)
|
||||
|
||||
parseMap :: (Int -> A.Parser a) -> A.Parser a
|
||||
parseMap aget = do
|
||||
c <- A.anyWord8
|
||||
case c of
|
||||
_ | c .&. 0xF0 == 0x80 ->
|
||||
aget . fromIntegral $ c .&. 0x0F
|
||||
0xDE ->
|
||||
aget . fromIntegral =<< parseUint16
|
||||
0xDF ->
|
||||
aget . fromIntegral =<< parseUint32
|
||||
_ ->
|
||||
fail $ printf "invlid map tag: 0x%02X" c
|
||||
|
||||
instance Unpackable a => Unpackable (Maybe a) where
|
||||
get =
|
||||
A.choice
|
||||
[ liftM Just get
|
||||
, liftM (\() -> Nothing) get ]
|
||||
|
||||
parseUint16 :: A.Parser Word16
|
||||
parseUint16 = do
|
||||
b0 <- A.anyWord8
|
||||
b1 <- A.anyWord8
|
||||
return $ (fromIntegral b0 `shiftL` 8) .|. fromIntegral b1
|
||||
|
||||
parseUint32 :: A.Parser Word32
|
||||
parseUint32 = do
|
||||
b0 <- A.anyWord8
|
||||
b1 <- A.anyWord8
|
||||
b2 <- A.anyWord8
|
||||
b3 <- A.anyWord8
|
||||
return $ (fromIntegral b0 `shiftL` 24) .|.
|
||||
(fromIntegral b1 `shiftL` 16) .|.
|
||||
(fromIntegral b2 `shiftL` 8) .|.
|
||||
fromIntegral b3
|
||||
|
||||
parseUint64 :: A.Parser Word64
|
||||
parseUint64 = do
|
||||
b0 <- A.anyWord8
|
||||
b1 <- A.anyWord8
|
||||
b2 <- A.anyWord8
|
||||
b3 <- A.anyWord8
|
||||
b4 <- A.anyWord8
|
||||
b5 <- A.anyWord8
|
||||
b6 <- A.anyWord8
|
||||
b7 <- A.anyWord8
|
||||
return $ (fromIntegral b0 `shiftL` 56) .|.
|
||||
(fromIntegral b1 `shiftL` 48) .|.
|
||||
(fromIntegral b2 `shiftL` 40) .|.
|
||||
(fromIntegral b3 `shiftL` 32) .|.
|
||||
(fromIntegral b4 `shiftL` 24) .|.
|
||||
(fromIntegral b5 `shiftL` 16) .|.
|
||||
(fromIntegral b6 `shiftL` 8) .|.
|
||||
fromIntegral b7
|
||||
|
||||
parseInt8 :: A.Parser Int8
|
||||
parseInt8 = return . fromIntegral =<< A.anyWord8
|
||||
|
||||
parseInt16 :: A.Parser Int16
|
||||
parseInt16 = return . fromIntegral =<< parseUint16
|
||||
|
||||
parseInt32 :: A.Parser Int32
|
||||
parseInt32 = return . fromIntegral =<< parseUint32
|
||||
|
||||
parseInt64 :: A.Parser Int64
|
||||
parseInt64 = return . fromIntegral =<< parseUint64
|
@@ -1,16 +1,21 @@
|
||||
import Control.Monad.Trans
|
||||
{-# Language OverloadedStrings #-}
|
||||
|
||||
import Control.Monad.IO.Class
|
||||
import qualified Data.ByteString as B
|
||||
import Data.MessagePack
|
||||
|
||||
main = do
|
||||
sb <- packToString $ do
|
||||
sb <- return $ packToString $ do
|
||||
put [1,2,3::Int]
|
||||
put (3.14 :: Double)
|
||||
put "Hoge"
|
||||
put ("Hoge" :: B.ByteString)
|
||||
|
||||
print sb
|
||||
|
||||
unpackFromString sb $ do
|
||||
r <- unpackFromString sb $ do
|
||||
arr <- get
|
||||
dbl <- get
|
||||
str <- get
|
||||
liftIO $ print (arr :: [Int], dbl :: Double, str :: String)
|
||||
return (arr :: [Int], dbl :: Double, str :: B.ByteString)
|
||||
|
||||
print r
|
||||
|
@@ -1,14 +0,0 @@
|
||||
import Control.Applicative
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.MessagePack
|
||||
|
||||
main = do
|
||||
sb <- newSimpleBuffer
|
||||
pc <- newPacker sb
|
||||
pack pc [1,2,3::Int]
|
||||
pack pc True
|
||||
pack pc "hoge"
|
||||
bs <- simpleBufferData sb
|
||||
|
||||
os <- unpackObjectsFromString bs
|
||||
mapM_ print os
|
@@ -1,36 +1,70 @@
|
||||
import Test.Framework
|
||||
import Test.Framework.Providers.QuickCheck2
|
||||
import Test.QuickCheck
|
||||
|
||||
import Control.Monad
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import qualified Data.ByteString.Lazy.Char8 as L
|
||||
import Data.MessagePack
|
||||
|
||||
{-
|
||||
main = do
|
||||
sb <- newSimpleBuffer
|
||||
pc <- newPacker sb
|
||||
instance Arbitrary a => Arbitrary (Assoc a) where
|
||||
arbitrary = liftM Assoc arbitrary
|
||||
|
||||
pack pc [(1,2),(2,3),(3::Int,4::Int)]
|
||||
pack pc [4,5,6::Int]
|
||||
pack pc "hoge"
|
||||
mid :: (Packable a, Unpackable a) => a -> a
|
||||
mid = unpack . pack
|
||||
|
||||
bs <- simpleBufferData sb
|
||||
print bs
|
||||
prop_mid_int a = a == mid a
|
||||
where types = a :: Int
|
||||
prop_mid_nil a = a == mid a
|
||||
where types = a :: ()
|
||||
prop_mid_bool a = a == mid a
|
||||
where types = a :: Bool
|
||||
prop_mid_double a = a == mid a
|
||||
where types = a :: Double
|
||||
prop_mid_string a = a == mid a
|
||||
where types = a :: String
|
||||
prop_mid_bytestring a = B.pack a == mid (B.pack a)
|
||||
where types = a :: String
|
||||
prop_mid_lazy_bytestring a = (L.pack a) == mid (L.pack a)
|
||||
where types = a :: String
|
||||
prop_mid_array_int a = a == mid a
|
||||
where types = a :: [Int]
|
||||
prop_mid_array_string a = a == mid a
|
||||
where types = a :: [String]
|
||||
prop_mid_pair2 a = a == mid a
|
||||
where types = a :: (Int, Int)
|
||||
prop_mid_pair3 a = a == mid a
|
||||
where types = a :: (Int, Int, Int)
|
||||
prop_mid_pair4 a = a == mid a
|
||||
where types = a :: (Int, Int, Int, Int)
|
||||
prop_mid_pair5 a = a == mid a
|
||||
where types = a :: (Int, Int, Int, Int, Int)
|
||||
prop_mid_list_int_double a = a == mid a
|
||||
where types = a :: [(Int, Double)]
|
||||
prop_mid_list_string_string a = a == mid a
|
||||
where types = a :: [(String, String)]
|
||||
prop_mid_map_string_int a = a == mid a
|
||||
where types = a :: Assoc [(String,Int)]
|
||||
|
||||
up <- newUnpacker defaultInitialBufferSize
|
||||
tests =
|
||||
[ testGroup "simple"
|
||||
[ testProperty "int" prop_mid_int
|
||||
, testProperty "nil" prop_mid_nil
|
||||
, testProperty "bool" prop_mid_bool
|
||||
, testProperty "double" prop_mid_double
|
||||
, testProperty "string" prop_mid_string
|
||||
, testProperty "bytestring" prop_mid_bytestring
|
||||
, testProperty "lazy-bytestring" prop_mid_lazy_bytestring
|
||||
, testProperty "[int]" prop_mid_array_int
|
||||
, testProperty "[string]" prop_mid_array_string
|
||||
, testProperty "(int, int)" prop_mid_pair2
|
||||
, testProperty "(int, int, int)" prop_mid_pair3
|
||||
, testProperty "(int, int, int, int)" prop_mid_pair4
|
||||
, testProperty "(int, int, int, int, int)" prop_mid_pair5
|
||||
, testProperty "[(int, double)]" prop_mid_list_int_double
|
||||
, testProperty "[(string, string)]" prop_mid_list_string_string
|
||||
, testProperty "Assoc [(string, int)]" prop_mid_map_string_int
|
||||
]
|
||||
]
|
||||
|
||||
unpackerFeed up bs
|
||||
|
||||
let f = do
|
||||
res <- unpackerExecute up
|
||||
when (res==1) $ do
|
||||
obj <- unpackerData up
|
||||
print obj
|
||||
f
|
||||
|
||||
f
|
||||
|
||||
return ()
|
||||
-}
|
||||
|
||||
main = do
|
||||
bs <- packb [(1,2),(2,3),(3::Int,4::Int)]
|
||||
print bs
|
||||
dat <- unpackb bs
|
||||
print (dat :: Result [(Int, Int)])
|
||||
main = defaultMain tests
|
||||
|
43
haskell/test/UserData.hs
Normal file
43
haskell/test/UserData.hs
Normal file
@@ -0,0 +1,43 @@
|
||||
{-# Language TemplateHaskell #-}
|
||||
|
||||
import Data.MessagePack
|
||||
import Data.MessagePack.Derive
|
||||
|
||||
data T
|
||||
= A Int String
|
||||
| B Double
|
||||
deriving (Show, Eq)
|
||||
|
||||
$(deriveObject ''T)
|
||||
|
||||
data U
|
||||
= C { c1 :: Int, c2 :: String }
|
||||
| D { d1 :: Double }
|
||||
deriving (Show, Eq)
|
||||
|
||||
$(deriveObject ''U)
|
||||
|
||||
data V
|
||||
= E String | F
|
||||
deriving (Show, Eq)
|
||||
|
||||
$(deriveObject ''V)
|
||||
|
||||
test :: (OBJECT a, Show a, Eq a) => a -> IO ()
|
||||
test v = do
|
||||
let bs = pack v
|
||||
print bs
|
||||
print (unpack bs == v)
|
||||
|
||||
let oa = toObject v
|
||||
print oa
|
||||
print (fromObject oa == v)
|
||||
|
||||
main = do
|
||||
test $ A 123 "hoge"
|
||||
test $ B 3.14
|
||||
test $ C 123 "hoge"
|
||||
test $ D 3.14
|
||||
test $ E "hello"
|
||||
test $ F
|
||||
return ()
|
@@ -1,5 +1,13 @@
|
||||
#Mon Apr 19 22:18:48 JST 2010
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
#Tue Apr 12 19:14:17 JST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
|
9
java/.settings/org.maven.ide.eclipse.prefs
Normal file
9
java/.settings/org.maven.ide.eclipse.prefs
Normal file
@@ -0,0 +1,9 @@
|
||||
#Mon Sep 27 07:43:27 JST 2010
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
fullBuildGoals=process-test-resources
|
||||
includeModules=false
|
||||
resolveWorkspaceProjects=true
|
||||
resourceFilterGoals=process-resources resources\:testResources
|
||||
skipCompilerPlugin=true
|
||||
version=1
|
1
java/AUTHORS
Normal file
1
java/AUTHORS
Normal file
@@ -0,0 +1 @@
|
||||
FURUHASHI Sadayuki <frsyuki _at_ users.sourceforge.jp>
|
@@ -1,3 +1,97 @@
|
||||
|
||||
Release 0.5.2 - 2011/04/23
|
||||
NEW FEATURES
|
||||
MSGPACK-6 Added TemplatePrecompiler program
|
||||
|
||||
Added built-in templates of BigDecimal and Date classes.
|
||||
|
||||
Added @MessagePackBeans annotation that enables you to serialize/deserialize
|
||||
JavaBeans.
|
||||
|
||||
BUG FIXES
|
||||
MSGPACK-4 Fixes the deserialization routine of Long value
|
||||
|
||||
IMPROVEMENTS
|
||||
#35 Improves handling of ClassLoader on Javassist template builder.
|
||||
|
||||
MSGPACK-7 Improves compatibility with Java 1.5. Now it runs on JRE 5.
|
||||
It is tested on Jenkins CI: http://ci.msgpack.org/job/java-jre5/
|
||||
|
||||
|
||||
Release 0.5.1 - 2010/12/14
|
||||
BUG FIXES
|
||||
Fixes cast error on GenericArrayType
|
||||
|
||||
Throws MessagePackException instead of NullPointerException if target is null
|
||||
on pack method.
|
||||
|
||||
|
||||
Release 0.5.0 - 2010/12/09
|
||||
NEW FEATURES
|
||||
Dynamic template builder is rewritten. New ReflectionTemplateBuilder
|
||||
supports DalvikVM.
|
||||
|
||||
Some optimization for dynamic code generator.
|
||||
|
||||
@MessagePackMessage accepts default filed option.
|
||||
Added new field annotations: @Ignore, @Requred and @Index.
|
||||
|
||||
Supported pack/unpack/convertion of arrays including multidimensional arrays.
|
||||
|
||||
Added native pack/unpack routine of ByteBuffer. It will be zero-copy optimized
|
||||
under a specific condition.
|
||||
|
||||
|
||||
Release 0.4.3 - 2010/11/10
|
||||
NEW FEATURES
|
||||
Added FieldList class and MessagePack.register(Class<T>, FieldList) method
|
||||
to specify optional/nullable options on runtime without annotations.
|
||||
|
||||
Changed annotation name: @MessagePackNullable -> @Nullable
|
||||
Changed annotation name: @MessagePackOptional -> @Optional
|
||||
|
||||
Supported pack/unpack/convertion of enums.
|
||||
|
||||
Added MessagePack.unpack(buffer, T to) and MessagePackObject.convert(T to)
|
||||
methods. They can unpack/convert buffer/object into existing object and
|
||||
eliminate re-allocation overhead.
|
||||
|
||||
|
||||
Release 0.4.2 - 2010/11/09
|
||||
NEW FEATURES
|
||||
Added MessagePackNullable annotation and Tempalte.tNullable(Template)
|
||||
method.
|
||||
|
||||
Added <T> T MessagePackObject.unpack(Class<T>) method.
|
||||
|
||||
|
||||
Release 0.4.1 - 2010/11/05
|
||||
BUG FIXES
|
||||
Fixed dynamic code generation of unpack methods
|
||||
|
||||
|
||||
Release 0.4.0 - 2010/10/25
|
||||
NEW FEATURES
|
||||
Added MessagePackObject class and org.msgpack.object package that
|
||||
represent unpacked (=dynamically typed) objects.
|
||||
Unpacker.unpack method returns MessagePackObject instead of Object.
|
||||
|
||||
Added Templates class and org.msgpack.template package that provide
|
||||
type conversion feature.
|
||||
|
||||
User-defined classes annotated with MessagePackMessage can be
|
||||
pack/unpack/converted.
|
||||
|
||||
User-defined classes registered with MessagePack.register(Class) can be
|
||||
pack/unpack/converted.
|
||||
|
||||
Added dynamic code generation feature for user-defined classes.
|
||||
|
||||
Added MessagePackOptional annotation.
|
||||
|
||||
Added MessagePack class that implements typical useful methods.
|
||||
|
||||
|
||||
Release 0.3 - 2010/05/23
|
||||
NEW FEATURES
|
||||
Added Unbuffered API + Direct Conversion API to the Unpacker.
|
||||
|
17
java/msgpack.iml
Normal file
17
java/msgpack.iml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.8.1" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
|
45
java/pom.xml
45
java/pom.xml
@@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.msgpack</groupId>
|
||||
<artifactId>msgpack</artifactId>
|
||||
<version>0.3</version>
|
||||
<version>0.5.2-devel</version>
|
||||
<description>MessagePack for Java</description>
|
||||
|
||||
<name>MessagePack for Java</name>
|
||||
@@ -29,6 +29,22 @@
|
||||
<version>4.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.4.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.4.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
<version>3.12.1.GA</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -47,8 +63,8 @@
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -65,6 +81,20 @@
|
||||
<connectionUrl>scm:git://github.com/msgpack/msgpack.git</connectionUrl>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -101,6 +131,13 @@
|
||||
<name>MessagePack Maven2 Repository</name>
|
||||
<url>http://msgpack.org/maven2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>repository.jboss.org</id>
|
||||
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<distributionManagement>
|
||||
@@ -109,12 +146,14 @@
|
||||
<id>msgpack.org</id>
|
||||
<name>Repository at msgpack.org</name>
|
||||
<url>file://${project.build.directory}/website/maven2/</url>
|
||||
<!--<url>${deploy-release-url}</url>-->
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<uniqueVersion>true</uniqueVersion>
|
||||
<id>msgpack.org</id>
|
||||
<name>Repository at msgpack.org</name>
|
||||
<url>file://${project.build.directory}/website/maven2/</url>
|
||||
<!--<url>${deploy-snapshot-url}</url>-->
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
|
26
java/src/main/java/org/msgpack/AbstractTemplate.java
Normal file
26
java/src/main/java/org/msgpack/AbstractTemplate.java
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class AbstractTemplate implements Template {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return convert(pac.unpackObject(), to);
|
||||
}
|
||||
}
|
@@ -25,6 +25,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
int offset = 0;
|
||||
int filled = 0;
|
||||
byte[] buffer = null;
|
||||
boolean bufferReferenced = false; // TODO zero-copy buffer
|
||||
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
|
||||
|
||||
abstract boolean fill() throws IOException;
|
||||
@@ -145,10 +146,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
advance(3);
|
||||
return (int)castBuffer.getShort(0);
|
||||
case 0xd2: // signed int 32
|
||||
more(4);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
advance(4);
|
||||
advance(5);
|
||||
return (int)castBuffer.getInt(0);
|
||||
case 0xd3: // signed int 64
|
||||
more(9);
|
||||
@@ -214,10 +215,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
advance(3);
|
||||
return (long)castBuffer.getShort(0);
|
||||
case 0xd2: // signed int 32
|
||||
more(4);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
advance(4);
|
||||
advance(5);
|
||||
return (long)castBuffer.getInt(0);
|
||||
case 0xd3: // signed int 64
|
||||
more(9);
|
||||
@@ -417,7 +418,18 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
|
||||
final byte[] unpackByteArray() throws IOException, MessageTypeException {
|
||||
int length = unpackRaw();
|
||||
return unpackRawBody(length);
|
||||
byte[] body = unpackRawBody(length);
|
||||
return body;
|
||||
}
|
||||
|
||||
final ByteBuffer unpackByteBuffer() throws IOException, MessageTypeException {
|
||||
// TODO zero-copy buffer
|
||||
int length = unpackRaw();
|
||||
more(length);
|
||||
ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length);
|
||||
bufferReferenced = true; // TODO fix magical code
|
||||
advance(length);
|
||||
return buf;
|
||||
}
|
||||
|
||||
final String unpackString() throws IOException, MessageTypeException {
|
||||
|
23
java/src/main/java/org/msgpack/MessageConverter.java
Normal file
23
java/src/main/java/org/msgpack/MessageConverter.java
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
public interface MessageConverter {
|
||||
Object convert(MessagePackObject from, Object to) throws MessageTypeException;
|
||||
}
|
||||
|
160
java/src/main/java/org/msgpack/MessagePack.java
Normal file
160
java/src/main/java/org/msgpack/MessagePack.java
Normal file
@@ -0,0 +1,160 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
import org.msgpack.template.FieldList;
|
||||
|
||||
public class MessagePack {
|
||||
public static byte[] pack(Object obj) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
new Packer(out).pack(obj);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static void pack(OutputStream out, Object obj) throws IOException {
|
||||
new Packer(out).pack(obj);
|
||||
}
|
||||
|
||||
public static byte[] pack(Object obj, Template tmpl) throws MessageTypeException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
new Packer(out).pack(obj, tmpl);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException, MessageTypeException {
|
||||
new Packer(out).pack(obj, tmpl);
|
||||
}
|
||||
|
||||
|
||||
public static MessagePackObject unpack(byte[] buffer) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpackObject();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object unpack(byte[] buffer, Template tmpl) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(tmpl);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, Template tmpl, T to) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(tmpl, to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, Class<T> klass) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(klass);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, T to) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static MessagePackObject unpack(InputStream in) throws IOException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
return pac.unpackObject();
|
||||
}
|
||||
|
||||
public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(tmpl);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, Template tmpl, T to) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(tmpl, to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, Class<T> klass) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(klass);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, T to) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(to);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) {
|
||||
TemplateRegistry.register(target);
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, FieldList flist) throws NoSuchFieldException {
|
||||
TemplateRegistry.register(target, flist);
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, Template tmpl) {
|
||||
TemplateRegistry.register(target, tmpl);
|
||||
}
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.math.BigInteger;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public abstract class MessagePackObject implements Cloneable, MessagePackable {
|
||||
public boolean isNil() {
|
||||
@@ -132,5 +133,28 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable {
|
||||
}
|
||||
|
||||
abstract public Object clone();
|
||||
|
||||
public Object convert(Template tmpl) throws MessageTypeException {
|
||||
return convert(tmpl, null);
|
||||
}
|
||||
|
||||
public <T> T convert(Template tmpl, T to) throws MessageTypeException {
|
||||
return (T)tmpl.convert(this, to);
|
||||
}
|
||||
|
||||
public <T> T convert(Class<T> klass) throws MessageTypeException {
|
||||
return convert(klass, null);
|
||||
}
|
||||
|
||||
public <T> T convert(T to) throws MessageTypeException {
|
||||
return convert((Class<T>)to.getClass(), to);
|
||||
}
|
||||
|
||||
public <T> T convert(Class<T> klass, T to) throws MessageTypeException {
|
||||
if(isNil()) {
|
||||
return null;
|
||||
}
|
||||
return (T)convert(TemplateRegistry.lookup(klass), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
public interface MessagePackTemplateProvider {
|
||||
Template getTemplate();
|
||||
}
|
25
java/src/main/java/org/msgpack/MessagePacker.java
Normal file
25
java/src/main/java/org/msgpack/MessagePacker.java
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface MessagePacker {
|
||||
void pack(Packer pk, Object target) throws IOException;
|
||||
}
|
||||
|
@@ -23,5 +23,13 @@ public class MessageTypeException extends RuntimeException {
|
||||
public MessageTypeException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public MessageTypeException(String s, Throwable t) {
|
||||
super(s, t);
|
||||
}
|
||||
|
||||
public MessageTypeException(Throwable t) {
|
||||
super(t);
|
||||
}
|
||||
}
|
||||
|
||||
|
25
java/src/main/java/org/msgpack/MessageUnpacker.java
Normal file
25
java/src/main/java/org/msgpack/MessageUnpacker.java
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface MessageUnpacker {
|
||||
Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException;
|
||||
}
|
||||
|
@@ -21,8 +21,11 @@ import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.math.BigInteger;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
/**
|
||||
* Packer enables you to serialize objects into OutputStream.
|
||||
@@ -41,8 +44,10 @@ import java.math.BigInteger;
|
||||
* You can serialize objects that implements {@link MessagePackable} interface.
|
||||
*/
|
||||
public class Packer {
|
||||
public static void load() { }
|
||||
|
||||
protected byte[] castBytes = new byte[9];
|
||||
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
|
||||
//protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
|
||||
protected OutputStream out;
|
||||
|
||||
public Packer(OutputStream out) {
|
||||
@@ -65,7 +70,9 @@ public class Packer {
|
||||
if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBuffer.putShort(1, d);
|
||||
// castBuffer.putShort(1, d);
|
||||
castBytes[1] = (byte)(d >> 8);
|
||||
castBytes[2] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
@@ -85,7 +92,9 @@ public class Packer {
|
||||
} else {
|
||||
// unsigned 16
|
||||
castBytes[0] = (byte)0xcd;
|
||||
castBuffer.putShort(1, d);
|
||||
// castBuffer.putShort(1, d);
|
||||
castBytes[1] = (byte)(d >> 8);
|
||||
castBytes[2] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
}
|
||||
}
|
||||
@@ -97,12 +106,18 @@ public class Packer {
|
||||
if(d < -(1<<15)) {
|
||||
// signed 32
|
||||
castBytes[0] = (byte)0xd2;
|
||||
castBuffer.putInt(1, d);
|
||||
// castBuffer.putInt(1, d);
|
||||
castBytes[1] = (byte)(d >> 24);
|
||||
castBytes[2] = (byte)(d >> 16);
|
||||
castBytes[3] = (byte)(d >> 8);
|
||||
castBytes[4] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
} else if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
// castBuffer.putShort(1, (short)d);
|
||||
castBytes[1] = (byte)(d >> 8);
|
||||
castBytes[2] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
@@ -122,12 +137,18 @@ public class Packer {
|
||||
} else if(d < (1<<16)) {
|
||||
// unsigned 16
|
||||
castBytes[0] = (byte)0xcd;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
// castBuffer.putShort(1, (short)d);
|
||||
castBytes[1] = (byte)(d >> 8);
|
||||
castBytes[2] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// unsigned 32
|
||||
castBytes[0] = (byte)0xce;
|
||||
castBuffer.putInt(1, d);
|
||||
// castBuffer.putInt(1, d);
|
||||
castBytes[1] = (byte)(d >> 24);
|
||||
castBytes[2] = (byte)(d >> 16);
|
||||
castBytes[3] = (byte)(d >> 8);
|
||||
castBytes[4] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
}
|
||||
@@ -140,19 +161,33 @@ public class Packer {
|
||||
if(d < -(1L<<31)) {
|
||||
// signed 64
|
||||
castBytes[0] = (byte)0xd3;
|
||||
castBuffer.putLong(1, d);
|
||||
// castBuffer.putLong(1, d);
|
||||
castBytes[1] = (byte)(d >> 56);
|
||||
castBytes[2] = (byte)(d >> 48);
|
||||
castBytes[3] = (byte)(d >> 40);
|
||||
castBytes[4] = (byte)(d >> 32);
|
||||
castBytes[5] = (byte)(d >> 24);
|
||||
castBytes[6] = (byte)(d >> 16);
|
||||
castBytes[7] = (byte)(d >> 8);
|
||||
castBytes[8] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 9);
|
||||
} else {
|
||||
// signed 32
|
||||
castBytes[0] = (byte)0xd2;
|
||||
castBuffer.putInt(1, (int)d);
|
||||
// castBuffer.putInt(1, (int)d);
|
||||
castBytes[1] = (byte)(d >> 24);
|
||||
castBytes[2] = (byte)(d >> 16);
|
||||
castBytes[3] = (byte)(d >> 8);
|
||||
castBytes[4] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
} else {
|
||||
if(d < -(1<<7)) {
|
||||
// signed 16
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
// castBuffer.putShort(1, (short)d);
|
||||
castBytes[1] = (byte)(d >> 8);
|
||||
castBytes[2] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
// signed 8
|
||||
@@ -174,7 +209,9 @@ public class Packer {
|
||||
} else {
|
||||
// unsigned 16
|
||||
castBytes[0] = (byte)0xcd;
|
||||
castBuffer.putShort(1, (short)d);
|
||||
// castBuffer.putShort(1, (short)d);
|
||||
castBytes[1] = (byte)((d & 0x0000ff00) >> 8);
|
||||
castBytes[2] = (byte)((d & 0x000000ff) >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
//System.out.println("pack uint 16 "+(short)d);
|
||||
}
|
||||
@@ -182,12 +219,24 @@ public class Packer {
|
||||
if(d < (1L<<32)) {
|
||||
// unsigned 32
|
||||
castBytes[0] = (byte)0xce;
|
||||
castBuffer.putInt(1, (int)d);
|
||||
// castBuffer.putInt(1, (int)d);
|
||||
castBytes[1] = (byte)((d & 0xff000000) >> 24);
|
||||
castBytes[2] = (byte)((d & 0x00ff0000) >> 16);
|
||||
castBytes[3] = (byte)((d & 0x0000ff00) >> 8);
|
||||
castBytes[4] = (byte)((d & 0x000000ff) >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
} else {
|
||||
// unsigned 64
|
||||
castBytes[0] = (byte)0xcf;
|
||||
castBuffer.putLong(1, d);
|
||||
// castBuffer.putLong(1, d);
|
||||
castBytes[1] = (byte)(d >> 56);
|
||||
castBytes[2] = (byte)(d >> 48);
|
||||
castBytes[3] = (byte)(d >> 40);
|
||||
castBytes[4] = (byte)(d >> 32);
|
||||
castBytes[5] = (byte)(d >> 24);
|
||||
castBytes[6] = (byte)(d >> 16);
|
||||
castBytes[7] = (byte)(d >> 8);
|
||||
castBytes[8] = (byte)(d >> 0);
|
||||
out.write(castBytes, 0, 9);
|
||||
}
|
||||
}
|
||||
@@ -209,7 +258,7 @@ public class Packer {
|
||||
castBytes[6] = barray[barray.length-3];
|
||||
castBytes[7] = barray[barray.length-2];
|
||||
castBytes[8] = barray[barray.length-1];
|
||||
out.write(castBytes);
|
||||
out.write(castBytes, 0, 9);
|
||||
return this;
|
||||
} else {
|
||||
throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff");
|
||||
@@ -218,14 +267,28 @@ public class Packer {
|
||||
|
||||
public Packer packFloat(float d) throws IOException {
|
||||
castBytes[0] = (byte)0xca;
|
||||
castBuffer.putFloat(1, d);
|
||||
// castBuffer.putFloat(1, d);
|
||||
int f = Float.floatToRawIntBits(d);
|
||||
castBytes[1] = (byte)(f >> 24);
|
||||
castBytes[2] = (byte)(f >> 16);
|
||||
castBytes[3] = (byte)(f >> 8);
|
||||
castBytes[4] = (byte)(f >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packDouble(double d) throws IOException {
|
||||
castBytes[0] = (byte)0xcb;
|
||||
castBuffer.putDouble(1, d);
|
||||
// castBuffer.putDouble(1, d);
|
||||
long f = Double.doubleToRawLongBits(d);
|
||||
castBytes[1] = (byte)(f >> 56);
|
||||
castBytes[2] = (byte)(f >> 48);
|
||||
castBytes[3] = (byte)(f >> 40);
|
||||
castBytes[4] = (byte)(f >> 32);
|
||||
castBytes[5] = (byte)(f >> 24);
|
||||
castBytes[6] = (byte)(f >> 16);
|
||||
castBytes[7] = (byte)(f >> 8);
|
||||
castBytes[8] = (byte)(f >> 0);
|
||||
out.write(castBytes, 0, 9);
|
||||
return this;
|
||||
}
|
||||
@@ -255,11 +318,17 @@ public class Packer {
|
||||
out.write((byte)d);
|
||||
} else if(n < 65536) {
|
||||
castBytes[0] = (byte)0xdc;
|
||||
castBuffer.putShort(1, (short)n);
|
||||
// castBuffer.putShort(1, (short)n);
|
||||
castBytes[1] = (byte)(n >> 8);
|
||||
castBytes[2] = (byte)(n >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
castBytes[0] = (byte)0xdd;
|
||||
castBuffer.putInt(1, n);
|
||||
// castBuffer.putInt(1, n);
|
||||
castBytes[1] = (byte)(n >> 24);
|
||||
castBytes[2] = (byte)(n >> 16);
|
||||
castBytes[3] = (byte)(n >> 8);
|
||||
castBytes[4] = (byte)(n >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
return this;
|
||||
@@ -271,11 +340,17 @@ public class Packer {
|
||||
out.write((byte)d);
|
||||
} else if(n < 65536) {
|
||||
castBytes[0] = (byte)0xde;
|
||||
castBuffer.putShort(1, (short)n);
|
||||
// castBuffer.putShort(1, (short)n);
|
||||
castBytes[1] = (byte)(n >> 8);
|
||||
castBytes[2] = (byte)(n >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
castBytes[0] = (byte)0xdf;
|
||||
castBuffer.putInt(1, n);
|
||||
// castBuffer.putInt(1, n);
|
||||
castBytes[1] = (byte)(n >> 24);
|
||||
castBytes[2] = (byte)(n >> 16);
|
||||
castBytes[3] = (byte)(n >> 8);
|
||||
castBytes[4] = (byte)(n >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
return this;
|
||||
@@ -287,11 +362,17 @@ public class Packer {
|
||||
out.write((byte)d);
|
||||
} else if(n < 65536) {
|
||||
castBytes[0] = (byte)0xda;
|
||||
castBuffer.putShort(1, (short)n);
|
||||
// castBuffer.putShort(1, (short)n);
|
||||
castBytes[1] = (byte)(n >> 8);
|
||||
castBytes[2] = (byte)(n >> 0);
|
||||
out.write(castBytes, 0, 3);
|
||||
} else {
|
||||
castBytes[0] = (byte)0xdb;
|
||||
castBuffer.putInt(1, n);
|
||||
// castBuffer.putInt(1, n);
|
||||
castBytes[1] = (byte)(n >> 24);
|
||||
castBytes[2] = (byte)(n >> 16);
|
||||
castBytes[3] = (byte)(n >> 8);
|
||||
castBytes[4] = (byte)(n >> 0);
|
||||
out.write(castBytes, 0, 5);
|
||||
}
|
||||
return this;
|
||||
@@ -308,6 +389,21 @@ public class Packer {
|
||||
}
|
||||
|
||||
|
||||
public Packer packByteArray(byte[] b) throws IOException {
|
||||
packRaw(b.length);
|
||||
return packRawBody(b, 0, b.length);
|
||||
}
|
||||
|
||||
public Packer packByteArray(byte[] b, int off, int length) throws IOException {
|
||||
packRaw(length);
|
||||
return packRawBody(b, off, length);
|
||||
}
|
||||
|
||||
public Packer packByteBuffer(ByteBuffer buf) throws IOException {
|
||||
packRaw(buf.remaining());
|
||||
return packRawBody(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
|
||||
}
|
||||
|
||||
public Packer packString(String s) throws IOException {
|
||||
byte[] b = ((String)s).getBytes("UTF-8");
|
||||
packRaw(b.length);
|
||||
@@ -396,18 +492,18 @@ public class Packer {
|
||||
return packString(o);
|
||||
}
|
||||
|
||||
public Packer pack(MessagePackable o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
o.messagePack(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer pack(byte[] o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
packRaw(o.length);
|
||||
return packRawBody(o);
|
||||
}
|
||||
|
||||
public Packer pack(ByteBuffer o) throws IOException {
|
||||
if (o == null) { return packNil(); }
|
||||
packRaw(o.remaining());
|
||||
return packRawBody(o.array(), o.arrayOffset() + o.position(), o.remaining());
|
||||
}
|
||||
|
||||
public Packer pack(List o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
packArray(o.size());
|
||||
@@ -425,57 +521,21 @@ public class Packer {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer pack(MessagePackable o) throws IOException {
|
||||
if(o == null) { return packNil(); }
|
||||
o.messagePack(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer pack(Object o) throws IOException {
|
||||
if(o == null) {
|
||||
return packNil();
|
||||
} else if(o instanceof String) {
|
||||
byte[] b = ((String)o).getBytes("UTF-8");
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
} else if(o instanceof MessagePackable) {
|
||||
((MessagePackable)o).messagePack(this);
|
||||
return this;
|
||||
} else if(o instanceof byte[]) {
|
||||
byte[] b = (byte[])o;
|
||||
packRaw(b.length);
|
||||
return packRawBody(b);
|
||||
} else if(o instanceof List) {
|
||||
List<Object> l = (List<Object>)o;
|
||||
packArray(l.size());
|
||||
for(Object i : l) { pack(i); }
|
||||
return this;
|
||||
} else if(o instanceof Map) {
|
||||
Map<Object,Object> m = (Map<Object,Object>)o;
|
||||
packMap(m.size());
|
||||
for(Map.Entry<Object,Object> e : m.entrySet()) {
|
||||
pack(e.getKey());
|
||||
pack(e.getValue());
|
||||
}
|
||||
return this;
|
||||
} else if(o instanceof Boolean) {
|
||||
if((Boolean)o) {
|
||||
return packTrue();
|
||||
} else {
|
||||
return packFalse();
|
||||
}
|
||||
} else if(o instanceof Integer) {
|
||||
return packInt((Integer)o);
|
||||
} else if(o instanceof Long) {
|
||||
return packLong((Long)o);
|
||||
} else if(o instanceof Short) {
|
||||
return packShort((Short)o);
|
||||
} else if(o instanceof Byte) {
|
||||
return packByte((Byte)o);
|
||||
} else if(o instanceof Float) {
|
||||
return packFloat((Float)o);
|
||||
} else if(o instanceof Double) {
|
||||
return packDouble((Double)o);
|
||||
} else if(o instanceof BigInteger) {
|
||||
return packBigInteger((BigInteger)o);
|
||||
} else {
|
||||
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
|
||||
}
|
||||
if(o == null) { return packNil(); }
|
||||
TemplateRegistry.lookup(o.getClass()).pack(this, o);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer pack(Object o, Template tmpl) throws IOException {
|
||||
tmpl.pack(this, o);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
21
java/src/main/java/org/msgpack/Template.java
Normal file
21
java/src/main/java/org/msgpack/Template.java
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
public interface Template extends MessagePacker, MessageUnpacker, MessageConverter {
|
||||
}
|
119
java/src/main/java/org/msgpack/Templates.java
Normal file
119
java/src/main/java/org/msgpack/Templates.java
Normal file
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import org.msgpack.template.*;
|
||||
|
||||
public class Templates {
|
||||
public static Template tNullable(Template elementTemplate) {
|
||||
return new NullableTemplate(elementTemplate);
|
||||
}
|
||||
|
||||
|
||||
public static final Template TAny = AnyTemplate.getInstance();
|
||||
public static Template tAny() {
|
||||
return TAny;
|
||||
}
|
||||
|
||||
|
||||
public static Template tList(Template elementTemplate) {
|
||||
return new ListTemplate(elementTemplate);
|
||||
}
|
||||
|
||||
public static Template tMap(Template keyTemplate, Template valueTemplate) {
|
||||
return new MapTemplate(keyTemplate, valueTemplate);
|
||||
}
|
||||
|
||||
public static Template tCollection(Template elementTemplate) {
|
||||
return new CollectionTemplate(elementTemplate);
|
||||
}
|
||||
|
||||
public static Template tClass(Class target) {
|
||||
Template tmpl = TemplateRegistry.lookup(target);
|
||||
if(tmpl == null) {
|
||||
// FIXME
|
||||
}
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
public static final Template TByte = ByteTemplate.getInstance();
|
||||
public static Template tByte() {
|
||||
return TByte;
|
||||
}
|
||||
|
||||
public static final Template TShort = ShortTemplate.getInstance();
|
||||
public static Template tShort() {
|
||||
return TShort;
|
||||
}
|
||||
|
||||
public static final Template TInteger = IntegerTemplate.getInstance();
|
||||
public static Template tInteger() {
|
||||
return TInteger;
|
||||
}
|
||||
|
||||
public static final Template TLong = LongTemplate.getInstance();
|
||||
public static Template tLong() {
|
||||
return TLong;
|
||||
}
|
||||
|
||||
public static final Template TBigInteger = BigIntegerTemplate.getInstance();
|
||||
public static Template tBigInteger() {
|
||||
return TBigInteger;
|
||||
}
|
||||
|
||||
public static final Template TBigDecimal = BigDecimalTemplate.getInstance();
|
||||
public static Template tBigDecimal() {
|
||||
return TBigDecimal;
|
||||
}
|
||||
|
||||
public static final Template TFloat = FloatTemplate.getInstance();
|
||||
public static Template tFloat() {
|
||||
return TFloat;
|
||||
}
|
||||
|
||||
public static final Template TDouble = DoubleTemplate.getInstance();
|
||||
public static Template tDouble() {
|
||||
return TDouble;
|
||||
}
|
||||
|
||||
public static final Template TBoolean = BooleanTemplate.getInstance();
|
||||
public static Template tBoolean() {
|
||||
return TBoolean;
|
||||
}
|
||||
|
||||
public static final Template TString = StringTemplate.getInstance();
|
||||
public static Template tString() {
|
||||
return TString;
|
||||
}
|
||||
|
||||
public static final Template TByteArray = ByteArrayTemplate.getInstance();
|
||||
public static Template tByteArray() {
|
||||
return TByteArray;
|
||||
}
|
||||
|
||||
public static final Template TByteBuffer = ByteBufferTemplate.getInstance();
|
||||
public static Template tByteBuffer() {
|
||||
return TByteBuffer;
|
||||
}
|
||||
|
||||
public static final Template TDate = DateTemplate.getInstance();
|
||||
public static Template tDate() {
|
||||
return TDate;
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.math.BigInteger;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
/**
|
||||
* Unpacker enables you to deserialize objects from stream.
|
||||
@@ -103,7 +104,6 @@ import java.math.BigInteger;
|
||||
* </pre>
|
||||
*/
|
||||
public class Unpacker implements Iterable<MessagePackObject> {
|
||||
|
||||
// buffer:
|
||||
// +---------------------------------------------+
|
||||
// | [object] | [obje| unparsed ... | unused ...|
|
||||
@@ -232,6 +232,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
impl.buffer = buffer;
|
||||
impl.offset = offset;
|
||||
impl.filled = length;
|
||||
impl.bufferReferenced = false; // TODO zero-copy buffer
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,13 +277,16 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
if(impl.buffer == null) {
|
||||
int nextSize = (bufferReserveSize < require) ? require : bufferReserveSize;
|
||||
impl.buffer = new byte[nextSize];
|
||||
impl.bufferReferenced = false; // TODO zero-copy buffer
|
||||
return;
|
||||
}
|
||||
|
||||
if(impl.filled <= impl.offset) {
|
||||
// rewind the buffer
|
||||
impl.filled = 0;
|
||||
impl.offset = 0;
|
||||
if(!impl.bufferReferenced) { // TODO zero-copy buffer
|
||||
if(impl.filled <= impl.offset) {
|
||||
// rewind the buffer
|
||||
impl.filled = 0;
|
||||
impl.offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(impl.buffer.length - impl.filled >= require) {
|
||||
@@ -301,6 +305,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
impl.buffer = tmp;
|
||||
impl.filled = notParsed;
|
||||
impl.offset = 0;
|
||||
impl.bufferReferenced = false; // TODO zero-copy buffer
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,8 +372,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
* @return offset position that is parsed.
|
||||
*/
|
||||
public int execute(byte[] buffer, int offset, int length) throws UnpackException {
|
||||
int noffset = impl.execute(buffer, offset + impl.offset, length);
|
||||
impl.offset = noffset - offset;
|
||||
int noffset = impl.execute(buffer, offset, length);
|
||||
if(impl.isFinished()) {
|
||||
impl.resetState();
|
||||
}
|
||||
@@ -536,14 +540,23 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
return impl.unpackRawBody(length);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets one raw bytes from the buffer.
|
||||
* Gets one raw object (header + body) from the buffer.
|
||||
* This method calls {@link fill()} method if needed.
|
||||
*/
|
||||
public byte[] unpackByteArray() throws IOException {
|
||||
return impl.unpackByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets one raw object (header + body) from the buffer.
|
||||
* This method calls {@link fill()} method if needed.
|
||||
*/
|
||||
public ByteBuffer unpackByteBuffer() throws IOException {
|
||||
return impl.unpackByteBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets one {@code String} value from the buffer.
|
||||
* This method calls {@link fill()} method if needed.
|
||||
@@ -561,12 +574,37 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
return impl.unpackObject();
|
||||
}
|
||||
|
||||
final public boolean tryUnpackNull() throws IOException {
|
||||
return impl.tryUnpackNull();
|
||||
}
|
||||
|
||||
final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
|
||||
obj.messageUnpack(this);
|
||||
}
|
||||
|
||||
final public boolean tryUnpackNull() throws IOException {
|
||||
return impl.tryUnpackNull();
|
||||
//final public MessagePackObject unpack() throws IOException {
|
||||
// return unpackObject();
|
||||
//}
|
||||
|
||||
final public <T> T unpack(T to) throws IOException, MessageTypeException {
|
||||
return unpack((Class<T>)to.getClass(), to);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
|
||||
return unpack(klass, null);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass, T to) throws IOException, MessageTypeException {
|
||||
if(tryUnpackNull()) { return null; }
|
||||
return (T)TemplateRegistry.lookup(klass).unpack(this, to);
|
||||
}
|
||||
|
||||
final public Object unpack(Template tmpl) throws IOException, MessageTypeException {
|
||||
return unpack(tmpl, null);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Template tmpl, T to) throws IOException, MessageTypeException {
|
||||
return (T)tmpl.unpack(this, to);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -309,6 +309,7 @@ public class UnpackerImpl {
|
||||
cs = ACS_RAW_VALUE;
|
||||
break _fixed_trail_again;
|
||||
case ACS_RAW_VALUE: {
|
||||
// TODO zero-copy buffer
|
||||
byte[] raw = new byte[trail];
|
||||
System.arraycopy(src, n, raw, 0, trail);
|
||||
obj = RawType.create(raw);
|
||||
|
28
java/src/main/java/org/msgpack/annotation/Ignore.java
Normal file
28
java/src/main/java/org/msgpack/annotation/Ignore.java
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Ignore {
|
||||
}
|
29
java/src/main/java/org/msgpack/annotation/Index.java
Normal file
29
java/src/main/java/org/msgpack/annotation/Index.java
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Index {
|
||||
int value();
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import org.msgpack.template.FieldOption;
|
||||
|
||||
/**
|
||||
* Annotation for java beans class
|
||||
* @author takeshita
|
||||
*
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MessagePackBeans {
|
||||
FieldOption value() default FieldOption.DEFAULT;
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MessagePackDelegate {
|
||||
String value();
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import org.msgpack.template.FieldOption;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MessagePackMessage {
|
||||
FieldOption value() default FieldOption.DEFAULT;
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MessagePackOrdinalEnum {
|
||||
}
|
28
java/src/main/java/org/msgpack/annotation/Nullable.java
Normal file
28
java/src/main/java/org/msgpack/annotation/Nullable.java
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Nullable {
|
||||
}
|
28
java/src/main/java/org/msgpack/annotation/Optional.java
Normal file
28
java/src/main/java/org/msgpack/annotation/Optional.java
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Optional {
|
||||
}
|
28
java/src/main/java/org/msgpack/annotation/Required.java
Normal file
28
java/src/main/java/org/msgpack/annotation/Required.java
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Required {
|
||||
}
|
462
java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java
Normal file
462
java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java
Normal file
@@ -0,0 +1,462 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.buffer;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.GatheringByteChannel;
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
|
||||
public class VectoredByteBuffer implements GatheringByteChannel, ScatteringByteChannel {
|
||||
private List<ByteBuffer> vec = new ArrayList<ByteBuffer>();
|
||||
private ByteBuffer internalBuffer;
|
||||
private ByteBuffer lastInternalBuffer;
|
||||
private int chunkSize;
|
||||
private int referenceThreshold;
|
||||
|
||||
public VectoredByteBuffer() {
|
||||
this(32*1024);
|
||||
}
|
||||
|
||||
public VectoredByteBuffer(int chunkSize) {
|
||||
this(chunkSize, 32);
|
||||
}
|
||||
|
||||
public VectoredByteBuffer(int chunkSize, int referenceThreshold) {
|
||||
this.chunkSize = chunkSize;
|
||||
this.referenceThreshold = referenceThreshold;
|
||||
internalBuffer = ByteBuffer.allocateDirect(chunkSize);
|
||||
}
|
||||
|
||||
|
||||
public void setChunkSize(int chunkSize) {
|
||||
this.chunkSize = chunkSize;
|
||||
}
|
||||
|
||||
public int getChunkSize(int chunkSize) {
|
||||
return this.chunkSize;
|
||||
}
|
||||
|
||||
public void setReferenceThreshold(int referenceThreshold) {
|
||||
this.referenceThreshold = referenceThreshold;
|
||||
}
|
||||
|
||||
public int getReferenceThreshold(int referenceThreshold) {
|
||||
return this.referenceThreshold;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return true; // FIXME?
|
||||
}
|
||||
|
||||
|
||||
public synchronized void reset() {
|
||||
vec.clear();
|
||||
lastInternalBuffer = null;
|
||||
}
|
||||
|
||||
|
||||
public void write(byte[] b) {
|
||||
write(b, 0, b.length);
|
||||
}
|
||||
|
||||
public void write(byte[] b, int off, int len) {
|
||||
if(off < 0 || len < 0 || b.length < off+len) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if(referenceThreshold >= 0 && len > referenceThreshold) {
|
||||
writeReference(b, off, len);
|
||||
} else {
|
||||
writeCopy(b, off, len);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(int b) {
|
||||
byte[] ba = new byte[1];
|
||||
ba[0] = (byte)b;
|
||||
write(ba);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int write(ByteBuffer src) {
|
||||
int slen = src.remaining();
|
||||
if(referenceThreshold >= 0 && slen > referenceThreshold) {
|
||||
writeCopy(src);
|
||||
} else {
|
||||
writeReference(src);
|
||||
}
|
||||
return slen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized long write(ByteBuffer[] srcs) {
|
||||
return write(srcs, 0, srcs.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized long write(ByteBuffer[] srcs, int offset, int length) {
|
||||
if(offset < 0 || length < 0 || srcs.length < offset+length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
long total = 0;
|
||||
for(int i=offset; offset < length; offset++) {
|
||||
ByteBuffer src = srcs[i];
|
||||
total += write(src);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private synchronized void writeCopy(byte[] b, int off, int len) {
|
||||
int ipos = internalBuffer.position();
|
||||
if(internalBuffer.capacity() - ipos < len) {
|
||||
// allocate new buffer
|
||||
int nsize = chunkSize > len ? chunkSize : len;
|
||||
internalBuffer = ByteBuffer.allocateDirect(nsize);
|
||||
ipos = 0;
|
||||
} else if(internalBuffer == lastInternalBuffer) {
|
||||
// optimization: concatenates to the last buffer instead
|
||||
// of adding new reference
|
||||
ByteBuffer dup = vec.get(vec.size()-1);
|
||||
internalBuffer.put(b, off, len);
|
||||
dup.limit(ipos + len);
|
||||
return;
|
||||
}
|
||||
internalBuffer.put(b, off, len);
|
||||
ByteBuffer dup = internalBuffer.duplicate();
|
||||
dup.position(ipos);
|
||||
dup.mark();
|
||||
dup.limit(ipos + len);
|
||||
vec.add(dup);
|
||||
lastInternalBuffer = internalBuffer;
|
||||
}
|
||||
|
||||
private synchronized void writeCopy(ByteBuffer src) {
|
||||
int slen = src.remaining();
|
||||
int ipos = internalBuffer.position();
|
||||
if(internalBuffer.capacity() - ipos < slen) {
|
||||
// allocate new buffer
|
||||
int nsize = chunkSize > slen ? chunkSize : slen;
|
||||
internalBuffer = ByteBuffer.allocateDirect(nsize);
|
||||
ipos = 0;
|
||||
} else if(internalBuffer == lastInternalBuffer) {
|
||||
// optimization: concatenates to the last buffer instead
|
||||
// of adding new reference
|
||||
ByteBuffer dup = vec.get(vec.size()-1);
|
||||
int dpos = dup.position();
|
||||
internalBuffer.put(src);
|
||||
ByteBuffer dup2 = internalBuffer.duplicate();
|
||||
dup2.position(dpos);
|
||||
dup2.limit(ipos + slen);
|
||||
vec.set(vec.size()-1, dup2);
|
||||
return;
|
||||
}
|
||||
internalBuffer.put(src);
|
||||
ByteBuffer dup = internalBuffer.duplicate();
|
||||
dup.position(ipos);
|
||||
dup.mark();
|
||||
dup.limit(ipos + slen);
|
||||
vec.add(dup);
|
||||
lastInternalBuffer = internalBuffer;
|
||||
}
|
||||
|
||||
private synchronized void writeReference(byte[] b, int off, int len) {
|
||||
ByteBuffer buf = ByteBuffer.wrap(b, off, len);
|
||||
vec.add(buf);
|
||||
lastInternalBuffer = null;
|
||||
}
|
||||
|
||||
private synchronized void writeReference(ByteBuffer src) {
|
||||
ByteBuffer buf = src.duplicate();
|
||||
vec.add(buf);
|
||||
lastInternalBuffer = null;
|
||||
}
|
||||
|
||||
|
||||
public synchronized void writeTo(java.io.OutputStream out) throws IOException {
|
||||
byte[] tmpbuf = null;
|
||||
for(int i=0; i < vec.size(); i++) {
|
||||
ByteBuffer r = vec.get(i);
|
||||
int rpos = r.position();
|
||||
int rlen = r.limit() - rpos;
|
||||
if(r.hasArray()) {
|
||||
byte[] array = r.array();
|
||||
out.write(array, rpos, rlen);
|
||||
} else {
|
||||
if(tmpbuf == null) {
|
||||
int max = rlen;
|
||||
for(int j=i+1; j < vec.size(); j++) {
|
||||
ByteBuffer c = vec.get(j);
|
||||
int clen = c.remaining();
|
||||
if(max < clen) {
|
||||
max = clen;
|
||||
}
|
||||
}
|
||||
tmpbuf = new byte[max];
|
||||
}
|
||||
r.get(tmpbuf, 0, rlen);
|
||||
r.position(rpos);
|
||||
out.write(tmpbuf, 0, rlen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized byte[] toByteArray() {
|
||||
byte[] out = new byte[available()];
|
||||
int off = 0;
|
||||
for(ByteBuffer r: vec) {
|
||||
int rpos = r.position();
|
||||
int rlen = r.limit() - rpos;
|
||||
r.get(out, off, rlen);
|
||||
r.position(rpos);
|
||||
off += rlen;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
public synchronized int available() {
|
||||
int total = 0;
|
||||
for(ByteBuffer r : vec) {
|
||||
total += r.remaining();
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public synchronized int read(byte[] b) {
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
|
||||
public synchronized int read(byte[] b, int off, int len) {
|
||||
if(off < 0 || len < 0 || b.length < off+len) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
int start = len;
|
||||
while(!vec.isEmpty()) {
|
||||
ByteBuffer r = vec.get(0);
|
||||
int rlen = r.remaining();
|
||||
if(rlen <= len) {
|
||||
r.get(b, off, rlen);
|
||||
vec.remove(0);
|
||||
off += rlen;
|
||||
len -= rlen;
|
||||
} else {
|
||||
r.get(b, off, len);
|
||||
return start;
|
||||
}
|
||||
}
|
||||
return start - len;
|
||||
}
|
||||
|
||||
public synchronized int read() {
|
||||
byte[] ba = new byte[1];
|
||||
if(read(ba) >= 1) {
|
||||
return ba[0];
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read(ByteBuffer dst) {
|
||||
int len = dst.remaining();
|
||||
int start = len;
|
||||
while(!vec.isEmpty()) {
|
||||
ByteBuffer r = vec.get(0);
|
||||
int rlen = r.remaining();
|
||||
if(rlen <= len) {
|
||||
dst.put(r);
|
||||
vec.remove(0);
|
||||
len -= rlen;
|
||||
} else {
|
||||
int blim = r.limit();
|
||||
r.limit(len);
|
||||
try {
|
||||
dst.put(r);
|
||||
} finally {
|
||||
r.limit(blim);
|
||||
}
|
||||
return start;
|
||||
}
|
||||
}
|
||||
return start - len;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized long read(ByteBuffer[] dsts) {
|
||||
return read(dsts, 0, dsts.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized long read(ByteBuffer[] dsts, int offset, int length) {
|
||||
if(offset < 0 || length < 0 || dsts.length < offset+length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
long total = 0;
|
||||
for(int i=offset; i < length; i++) {
|
||||
ByteBuffer dst = dsts[i];
|
||||
int dlen = dst.remaining();
|
||||
int rlen = read(dsts[i]);
|
||||
total += rlen;
|
||||
if(rlen < dlen) {
|
||||
return total;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public synchronized long read(GatheringByteChannel to) throws IOException {
|
||||
long total = to.write(vec.toArray(new ByteBuffer[0]));
|
||||
while(!vec.isEmpty()) {
|
||||
ByteBuffer r = vec.get(0);
|
||||
if(r.remaining() == 0) {
|
||||
vec.remove(0);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public synchronized long skip(long len) {
|
||||
if(len <= 0) {
|
||||
return 0;
|
||||
}
|
||||
long start = len;
|
||||
while(!vec.isEmpty()) {
|
||||
ByteBuffer r = vec.get(0);
|
||||
int rlen = r.remaining();
|
||||
if(rlen <= len) {
|
||||
r.position(r.position()+rlen);
|
||||
vec.remove(0);
|
||||
len -= rlen;
|
||||
} else {
|
||||
r.position((int)(r.position()+len));
|
||||
return start;
|
||||
}
|
||||
}
|
||||
return start - len;
|
||||
}
|
||||
|
||||
|
||||
public final static class OutputStream extends java.io.OutputStream {
|
||||
private VectoredByteBuffer vbb;
|
||||
|
||||
OutputStream(VectoredByteBuffer vbb) {
|
||||
this.vbb = vbb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) {
|
||||
vbb.write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) {
|
||||
vbb.write(b, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
vbb.write(b);
|
||||
}
|
||||
|
||||
public int write(ByteBuffer src) {
|
||||
return vbb.write(src);
|
||||
}
|
||||
|
||||
public long write(ByteBuffer[] srcs) {
|
||||
return vbb.write(srcs);
|
||||
}
|
||||
|
||||
public long write(ByteBuffer[] srcs, int offset, int length) {
|
||||
return vbb.write(srcs, offset, length);
|
||||
}
|
||||
|
||||
public void writeTo(OutputStream out) throws IOException {
|
||||
vbb.writeTo(out);
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
return vbb.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
public final static class InputStream extends java.io.InputStream {
|
||||
private VectoredByteBuffer vbb;
|
||||
|
||||
InputStream(VectoredByteBuffer vbb) {
|
||||
this.vbb = vbb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() {
|
||||
return vbb.available();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) {
|
||||
return vbb.read(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) {
|
||||
return vbb.read(b, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() {
|
||||
return vbb.read();
|
||||
}
|
||||
|
||||
public int read(ByteBuffer dst) {
|
||||
return vbb.read(dst);
|
||||
}
|
||||
|
||||
public long read(ByteBuffer[] dsts, int offset, int length) {
|
||||
return vbb.read(dsts, offset, length);
|
||||
}
|
||||
|
||||
public long read(GatheringByteChannel to) throws IOException {
|
||||
return vbb.read(to);
|
||||
}
|
||||
|
||||
public long skip(long len) {
|
||||
return vbb.skip(len);
|
||||
}
|
||||
}
|
||||
|
||||
public OutputStream outputStream() {
|
||||
return new OutputStream(this);
|
||||
}
|
||||
|
||||
public InputStream inputStream() {
|
||||
return new InputStream(this);
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -79,7 +79,7 @@ public class RawType extends MessagePackObject {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return bytes.hashCode();
|
||||
return Arrays.hashCode(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
54
java/src/main/java/org/msgpack/template/AnyTemplate.java
Normal file
54
java/src/main/java/org/msgpack/template/AnyTemplate.java
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class AnyTemplate implements Template {
|
||||
private AnyTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(target instanceof MessagePackObject) {
|
||||
pk.pack((MessagePackObject)target);
|
||||
} else if(target == null) {
|
||||
pk.packNil();
|
||||
} else {
|
||||
TemplateRegistry.lookup(target.getClass()).pack(pk, target);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackObject();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from;
|
||||
}
|
||||
|
||||
static public AnyTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final AnyTemplate instance = new AnyTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(MessagePackObject.class, instance);
|
||||
}
|
||||
}
|
||||
|
143
java/src/main/java/org/msgpack/template/BeansFieldEntry.java
Normal file
143
java/src/main/java/org/msgpack/template/BeansFieldEntry.java
Normal file
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.*;
|
||||
import org.msgpack.*;
|
||||
|
||||
/**
|
||||
* Field entry for Java beans property.
|
||||
* @author takeshita
|
||||
*
|
||||
*/
|
||||
public class BeansFieldEntry implements IFieldEntry {
|
||||
|
||||
PropertyDescriptor desc;
|
||||
FieldOption option = FieldOption.DEFAULT;
|
||||
|
||||
public BeansFieldEntry(PropertyDescriptor desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return desc.getDisplayName();
|
||||
}
|
||||
public String getGetterName(){
|
||||
return desc.getReadMethod().getName();
|
||||
}
|
||||
public String getSetterName(){
|
||||
return desc.getWriteMethod().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getType() {
|
||||
return desc.getPropertyType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getJavaTypeName() {
|
||||
Class<?> type = getType();
|
||||
if(type.isArray()) {
|
||||
return arrayTypeToString(type);
|
||||
} else {
|
||||
return type.getName();
|
||||
}
|
||||
}
|
||||
static String arrayTypeToString(Class<?> type) {
|
||||
int dim = 1;
|
||||
Class<?> baseType = type.getComponentType();
|
||||
while(baseType.isArray()) {
|
||||
baseType = baseType.getComponentType();
|
||||
dim += 1;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(baseType.getName());
|
||||
for (int i = 0; i < dim; ++i) {
|
||||
sb.append("[]");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getGenericType() {
|
||||
return desc.getReadMethod().getGenericReturnType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldOption getOption() {
|
||||
return option;
|
||||
}
|
||||
public void setOption(FieldOption option){
|
||||
this.option = option;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return option != FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isRequired()
|
||||
*/
|
||||
@Override
|
||||
public boolean isRequired() {
|
||||
return option == FieldOption.REQUIRED;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isOptional()
|
||||
*/
|
||||
@Override
|
||||
public boolean isOptional() {
|
||||
return option == FieldOption.OPTIONAL;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isNullable()
|
||||
*/
|
||||
@Override
|
||||
public boolean isNullable() {
|
||||
return option == FieldOption.NULLABLE;
|
||||
}
|
||||
|
||||
public Object get(Object target){
|
||||
try {
|
||||
return desc.getReadMethod().invoke(target);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new MessageTypeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MessageTypeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new MessageTypeException(e);
|
||||
}
|
||||
}
|
||||
public void set(Object target , Object value){
|
||||
try {
|
||||
desc.getWriteMethod().invoke(target, value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new MessageTypeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MessageTypeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new MessageTypeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,188 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.annotation.Ignore;
|
||||
import org.msgpack.annotation.Index;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.Nullable;
|
||||
import org.msgpack.annotation.Optional;
|
||||
import org.msgpack.annotation.Required;
|
||||
|
||||
/**
|
||||
* List up Java beans property methods.
|
||||
* @author takeshita
|
||||
*
|
||||
*/
|
||||
public class BeansFieldEntryReader implements IFieldEntryReader{
|
||||
|
||||
|
||||
public IFieldEntry[] convertFieldEntries(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
List<FieldList.Entry> src = flist.getList();
|
||||
FieldEntry[] result = new FieldEntry[src.size()];
|
||||
for(int i=0; i < src.size(); i++) {
|
||||
FieldList.Entry s = src.get(i);
|
||||
if(s.isAvailable()) {
|
||||
result[i] = new FieldEntry(targetClass.getDeclaredField(s.getName()), s.getOption());
|
||||
} else {
|
||||
result[i] = new FieldEntry();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFieldEntry[] readFieldEntries(Class<?> targetClass,
|
||||
FieldOption implicitOption) {
|
||||
BeanInfo desc;
|
||||
try {
|
||||
desc = Introspector.getBeanInfo(targetClass);
|
||||
} catch (IntrospectionException e1) {
|
||||
throw new TemplateBuildException("Class must be java beans class:" + targetClass.getName());
|
||||
}
|
||||
|
||||
PropertyDescriptor[] props = desc.getPropertyDescriptors();
|
||||
ArrayList<PropertyDescriptor> list = new ArrayList<PropertyDescriptor>();
|
||||
for(int i = 0;i < props.length;i++){
|
||||
PropertyDescriptor pd = props[i];
|
||||
if(!isIgnoreProp(pd)){
|
||||
list.add(pd);
|
||||
}
|
||||
}
|
||||
props = new PropertyDescriptor[list.size()];
|
||||
list.toArray(props);
|
||||
|
||||
BeansFieldEntry[] entries = new BeansFieldEntry[props.length];
|
||||
for(int i = 0;i < props.length;i++){
|
||||
PropertyDescriptor p = props[i];
|
||||
int index = readPropIndex(p);
|
||||
if(index >= 0){
|
||||
if(entries[index] != null){
|
||||
throw new TemplateBuildException("duplicated index: "+index);
|
||||
}
|
||||
if(index >= entries.length){
|
||||
throw new TemplateBuildException("invalid index: "+index);
|
||||
}
|
||||
entries[index] = new BeansFieldEntry(p);
|
||||
props[index] = null;
|
||||
}
|
||||
}
|
||||
int insertIndex = 0;
|
||||
for(int i = 0;i < props.length;i++){
|
||||
PropertyDescriptor p = props[i];
|
||||
if(p != null){
|
||||
while(entries[insertIndex] != null){
|
||||
insertIndex++;
|
||||
}
|
||||
entries[insertIndex] = new BeansFieldEntry(p);
|
||||
}
|
||||
|
||||
}
|
||||
for(int i = 0;i < entries.length;i++){
|
||||
BeansFieldEntry e = entries[i];
|
||||
FieldOption op = readPropOption(e.desc, implicitOption);
|
||||
e.setOption(op);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
public FieldOption readImplicitFieldOption(Class<?> targetClass) {
|
||||
MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class);
|
||||
if(a == null) {
|
||||
return FieldOption.DEFAULT;
|
||||
}
|
||||
return a.value();
|
||||
}
|
||||
|
||||
|
||||
private FieldOption readPropOption(PropertyDescriptor desc, FieldOption implicitOption) {
|
||||
|
||||
FieldOption forGetter = readMethodOption(desc.getReadMethod());
|
||||
if(forGetter != FieldOption.DEFAULT){
|
||||
return forGetter;
|
||||
}
|
||||
FieldOption forSetter = readMethodOption(desc.getWriteMethod());
|
||||
if(forSetter != FieldOption.DEFAULT){
|
||||
return forSetter;
|
||||
}else{
|
||||
return implicitOption;
|
||||
}
|
||||
|
||||
}
|
||||
private FieldOption readMethodOption(Method method){
|
||||
|
||||
if(isAnnotated(method, Ignore.class)) {
|
||||
return FieldOption.IGNORE;
|
||||
} else if(isAnnotated(method, Required.class)) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else if(isAnnotated(method, Optional.class)) {
|
||||
return FieldOption.OPTIONAL;
|
||||
} else if(isAnnotated(method, Nullable.class)) {
|
||||
if(method.getDeclaringClass().isPrimitive()) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else {
|
||||
return FieldOption.NULLABLE;
|
||||
}
|
||||
}
|
||||
return FieldOption.DEFAULT;
|
||||
}
|
||||
|
||||
private int readPropIndex(PropertyDescriptor desc) {
|
||||
|
||||
int forGetter = readMethodIndex(desc.getReadMethod());
|
||||
if(forGetter >= 0){
|
||||
return forGetter;
|
||||
}
|
||||
int forSetter = readMethodIndex(desc.getWriteMethod());
|
||||
return forSetter;
|
||||
}
|
||||
private int readMethodIndex(Method method){
|
||||
Index a = method.getAnnotation(Index.class);
|
||||
if(a == null) {
|
||||
return -1;
|
||||
} else {
|
||||
return a.value();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAnnotated(AccessibleObject ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}
|
||||
boolean isIgnoreProp(PropertyDescriptor desc){
|
||||
if(desc == null)return true;
|
||||
Method getter = desc.getReadMethod();
|
||||
Method setter = desc.getWriteMethod();
|
||||
return getter == null ||
|
||||
setter == null ||
|
||||
!Modifier.isPublic(getter.getModifiers()) ||
|
||||
!Modifier.isPublic(setter.getModifiers()) ||
|
||||
isAnnotated(getter,Ignore.class) ||
|
||||
isAnnotated(setter, Ignore.class);
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
|
||||
public class BigDecimalTemplate implements Template {
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
BigDecimal temp = (BigDecimal) target;
|
||||
try {
|
||||
pk.packString(temp.toString());
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
String temp = pac.unpackString();
|
||||
return new BigDecimal(temp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
String temp = from.asString();
|
||||
return new BigDecimal(temp);
|
||||
}
|
||||
|
||||
static public BigDecimalTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BigDecimalTemplate instance = new BigDecimalTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(BigDecimal.class, instance);
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BigIntegerTemplate implements Template {
|
||||
private BigIntegerTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packBigInteger((BigInteger)target);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackBigInteger();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asBigInteger();
|
||||
}
|
||||
|
||||
static public BigIntegerTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BigIntegerTemplate instance = new BigIntegerTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(BigInteger.class, instance);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BooleanArrayTemplate implements Template {
|
||||
private BooleanArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof boolean[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
boolean[] array = (boolean[])target;
|
||||
try {
|
||||
pk.packArray(array.length);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
for(boolean a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
boolean[] array;
|
||||
if(to != null && to instanceof boolean[] && ((boolean[])to).length == length) {
|
||||
array = (boolean[])to;
|
||||
} else {
|
||||
array = new boolean[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackBoolean();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
boolean[] array;
|
||||
if(to != null && to instanceof boolean[] && ((boolean[])to).length == src.length) {
|
||||
array = (boolean[])to;
|
||||
} else {
|
||||
array = new boolean[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asBoolean();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public BooleanArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BooleanArrayTemplate instance = new BooleanArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(boolean[].class, instance);
|
||||
}
|
||||
}
|
||||
|
53
java/src/main/java/org/msgpack/template/BooleanTemplate.java
Normal file
53
java/src/main/java/org/msgpack/template/BooleanTemplate.java
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BooleanTemplate implements Template {
|
||||
private BooleanTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packBoolean((Boolean)target);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackBoolean();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asBoolean();
|
||||
}
|
||||
|
||||
static public BooleanTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final BooleanTemplate instance = new BooleanTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(Boolean.class, instance);
|
||||
TemplateRegistry.register(boolean.class, instance);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
public class BuiltInTemplateLoader {
|
||||
public static void load() {
|
||||
AnyTemplate.getInstance();
|
||||
BigIntegerTemplate.getInstance();
|
||||
BigDecimalTemplate.getInstance();
|
||||
BooleanArrayTemplate.getInstance();
|
||||
BooleanTemplate.getInstance();
|
||||
ByteArrayTemplate.getInstance();
|
||||
ByteBufferTemplate.getInstance();
|
||||
ByteTemplate.getInstance();
|
||||
DoubleArrayTemplate.getInstance();
|
||||
DoubleTemplate.getInstance();
|
||||
DateTemplate.getInstance();
|
||||
FloatArrayTemplate.getInstance();
|
||||
FloatTemplate.getInstance();
|
||||
IntArrayTemplate.getInstance();
|
||||
IntegerTemplate.getInstance();
|
||||
LongArrayTemplate.getInstance();
|
||||
LongTemplate.getInstance();
|
||||
ShortArrayTemplate.getInstance();
|
||||
ShortTemplate.getInstance();
|
||||
StringTemplate.getInstance();
|
||||
|
||||
CollectionTemplate.load();
|
||||
ListTemplate.load();
|
||||
MapTemplate.load();
|
||||
NullableTemplate.load();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteArrayTemplate implements Template {
|
||||
private ByteArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packByteArray((byte[])target);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackByteArray();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asByteArray();
|
||||
}
|
||||
|
||||
static public ByteArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ByteArrayTemplate instance = new ByteArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(byte[].class, instance);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteBufferTemplate implements Template {
|
||||
private ByteBufferTemplate() {
|
||||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packByteBuffer((ByteBuffer) target);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackByteBuffer();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to)
|
||||
throws MessageTypeException {
|
||||
// FIXME
|
||||
byte[] bytes = from.asByteArray();
|
||||
return ByteBuffer.wrap(bytes);
|
||||
}
|
||||
|
||||
static public ByteBufferTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ByteBufferTemplate instance = new ByteBufferTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(ByteBuffer.class, instance);
|
||||
}
|
||||
}
|
53
java/src/main/java/org/msgpack/template/ByteTemplate.java
Normal file
53
java/src/main/java/org/msgpack/template/ByteTemplate.java
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteTemplate implements Template {
|
||||
private ByteTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packByte((Byte)target);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackByte();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asByte();
|
||||
}
|
||||
|
||||
static public ByteTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final ByteTemplate instance = new ByteTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(Byte.class, instance);
|
||||
TemplateRegistry.register(byte.class, instance);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
|
||||
public class CollectionTemplate implements Template {
|
||||
public static void load() { }
|
||||
|
||||
private Template elementTemplate;
|
||||
|
||||
public CollectionTemplate(Template elementTemplate) {
|
||||
this.elementTemplate = elementTemplate;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if (! (target instanceof Collection)) {
|
||||
if (target == null) {
|
||||
throw new MessageTypeException(new NullPointerException("target is null."));
|
||||
}
|
||||
throw new MessageTypeException("target is not Collection type: " + target.getClass());
|
||||
}
|
||||
Collection<Object> collection = (Collection<Object>) target;
|
||||
pk.packArray(collection.size());
|
||||
for(Object element : collection) {
|
||||
elementTemplate.pack(pk, element);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
Collection<Object> c;
|
||||
if(to == null) {
|
||||
c = new LinkedList<Object>();
|
||||
} else {
|
||||
// TODO: optimize if list is instanceof ArrayList
|
||||
c = (Collection<Object>) to;
|
||||
c.clear();
|
||||
}
|
||||
for(; length > 0; length--) {
|
||||
c.add(elementTemplate.unpack(pac, null));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] array = from.asArray();
|
||||
Collection<Object> c;
|
||||
if(to == null) {
|
||||
c = new LinkedList<Object>();
|
||||
} else {
|
||||
// TODO: optimize if list is instanceof ArrayList
|
||||
c = (Collection<Object>) to;
|
||||
c.clear();
|
||||
}
|
||||
for(MessagePackObject element : array) {
|
||||
c.add(elementTemplate.convert(element, null));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static {
|
||||
TemplateRegistry.registerGeneric(Collection.class, new GenericTemplate1(CollectionTemplate.class));
|
||||
TemplateRegistry.register(Collection.class, new CollectionTemplate(AnyTemplate.getInstance()));
|
||||
}
|
||||
}
|
||||
|
57
java/src/main/java/org/msgpack/template/DateTemplate.java
Normal file
57
java/src/main/java/org/msgpack/template/DateTemplate.java
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
|
||||
public class DateTemplate implements Template {
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
Date temp = (Date) target;
|
||||
pk.packLong(temp.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
Long temp = pac.unpackLong();
|
||||
return new Date(temp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
Long temp = from.asLong();
|
||||
return new Date(temp);
|
||||
}
|
||||
|
||||
static public DateTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final DateTemplate instance = new DateTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(Date.class, instance);
|
||||
}
|
||||
}
|
96
java/src/main/java/org/msgpack/template/DefaultTemplate.java
Normal file
96
java/src/main/java/org/msgpack/template/DefaultTemplate.java
Normal file
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DefaultTemplate implements Template {
|
||||
private Class<?> targetClass;
|
||||
private Type lookupType;
|
||||
private boolean messagePackable;
|
||||
private boolean messageUnpackable;
|
||||
private boolean messageConvertable;
|
||||
|
||||
public DefaultTemplate(Class<?> targetClass) {
|
||||
this(targetClass, (Type)targetClass);
|
||||
}
|
||||
|
||||
public DefaultTemplate(Class<?> targetClass, Type lookupType) {
|
||||
this.targetClass = targetClass;
|
||||
this.lookupType = lookupType;
|
||||
this.messagePackable = MessagePackable.class.isAssignableFrom(targetClass);
|
||||
this.messageUnpackable = MessageUnpackable.class.isAssignableFrom(targetClass);
|
||||
this.messageConvertable = MessageConvertable.class.isAssignableFrom(targetClass);
|
||||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(messagePackable) {
|
||||
if(target == null) {
|
||||
throw new MessageTypeException("target is null.");
|
||||
}
|
||||
((MessagePackable)target).messagePack(pk);
|
||||
return;
|
||||
}
|
||||
Template tmpl = TemplateRegistry.tryLookup(lookupType);
|
||||
if(tmpl == this || tmpl == null) {
|
||||
throw new MessageTypeException("Template lookup fail: " + lookupType.getClass().getName());
|
||||
}
|
||||
tmpl.pack(pk, target);
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
if(messageUnpackable) {
|
||||
if(to == null) {
|
||||
try {
|
||||
to = targetClass.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new MessageTypeException(e);
|
||||
}
|
||||
}
|
||||
((MessageUnpackable)to).messageUnpack(pac);
|
||||
return to;
|
||||
}
|
||||
Template tmpl = TemplateRegistry.tryLookup(lookupType);
|
||||
if(tmpl == this || tmpl == null) {
|
||||
throw new MessageTypeException("Template lookup fail: " + lookupType.getClass().getName());
|
||||
}
|
||||
return tmpl.unpack(pac, to);
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
if(messageConvertable) {
|
||||
if(to == null) {
|
||||
try {
|
||||
to = targetClass.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new MessageTypeException(e);
|
||||
}
|
||||
}
|
||||
((MessageConvertable)to).messageConvert(from);
|
||||
return from;
|
||||
}
|
||||
Template tmpl = TemplateRegistry.tryLookup(lookupType);
|
||||
if(tmpl == this || tmpl == null) {
|
||||
throw new MessageTypeException("Template lookup fail: " + lookupType.getClass().getName());
|
||||
}
|
||||
return tmpl.convert(from, to);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoubleArrayTemplate implements Template {
|
||||
private DoubleArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof double[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
double[] array = (double[])target;
|
||||
try {
|
||||
pk.packArray(array.length);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
for(double a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
double[] array;
|
||||
if(to != null && to instanceof double[] && ((double[])to).length == length) {
|
||||
array = (double[])to;
|
||||
} else {
|
||||
array = new double[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackDouble();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
double[] array;
|
||||
if(to != null && to instanceof double[] && ((double[])to).length == src.length) {
|
||||
array = (double[])to;
|
||||
} else {
|
||||
array = new double[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asDouble();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public DoubleArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final DoubleArrayTemplate instance = new DoubleArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(double[].class, instance);
|
||||
}
|
||||
}
|
||||
|
53
java/src/main/java/org/msgpack/template/DoubleTemplate.java
Normal file
53
java/src/main/java/org/msgpack/template/DoubleTemplate.java
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoubleTemplate implements Template {
|
||||
private DoubleTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packDouble(((Double)target));
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackDouble();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asDouble();
|
||||
}
|
||||
|
||||
static public DoubleTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final DoubleTemplate instance = new DoubleTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(Double.class, instance);
|
||||
TemplateRegistry.register(double.class, instance);
|
||||
}
|
||||
}
|
||||
|
143
java/src/main/java/org/msgpack/template/FieldEntry.java
Normal file
143
java/src/main/java/org/msgpack/template/FieldEntry.java
Normal file
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.annotation.*;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.annotation.*;
|
||||
|
||||
public class FieldEntry implements IFieldEntry {
|
||||
private Field field;
|
||||
private FieldOption option;
|
||||
|
||||
public FieldEntry() {
|
||||
this.field = null;
|
||||
this.option = FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
public FieldEntry(FieldEntry e) {
|
||||
this.field = e.field;
|
||||
this.option = e.option;
|
||||
}
|
||||
|
||||
public FieldEntry(Field field, FieldOption option) {
|
||||
this.field = field;
|
||||
this.option = option;
|
||||
}
|
||||
|
||||
public Field getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#getName()
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#getType()
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getType() {
|
||||
return field.getType();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#getJavaTypeName()
|
||||
*/
|
||||
@Override
|
||||
public String getJavaTypeName() {
|
||||
Class<?> type = field.getType();
|
||||
if(type.isArray()) {
|
||||
return arrayTypeToString(type);
|
||||
} else {
|
||||
return type.getName();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#getGenericType()
|
||||
*/
|
||||
@Override
|
||||
public Type getGenericType() {
|
||||
return field.getGenericType();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#getOption()
|
||||
*/
|
||||
@Override
|
||||
public FieldOption getOption() {
|
||||
return option;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isAvailable()
|
||||
*/
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return option != FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isRequired()
|
||||
*/
|
||||
@Override
|
||||
public boolean isRequired() {
|
||||
return option == FieldOption.REQUIRED;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isOptional()
|
||||
*/
|
||||
@Override
|
||||
public boolean isOptional() {
|
||||
return option == FieldOption.OPTIONAL;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.msgpack.template.IFieldEntry#isNullable()
|
||||
*/
|
||||
@Override
|
||||
public boolean isNullable() {
|
||||
return option == FieldOption.NULLABLE;
|
||||
}
|
||||
|
||||
static String arrayTypeToString(Class<?> type) {
|
||||
int dim = 1;
|
||||
Class<?> baseType = type.getComponentType();
|
||||
while(baseType.isArray()) {
|
||||
baseType = baseType.getComponentType();
|
||||
dim += 1;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(baseType.getName());
|
||||
for (int i = 0; i < dim; ++i) {
|
||||
sb.append("[]");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
182
java/src/main/java/org/msgpack/template/FieldEntryReader.java
Normal file
182
java/src/main/java/org/msgpack/template/FieldEntryReader.java
Normal file
@@ -0,0 +1,182 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.annotation.Ignore;
|
||||
import org.msgpack.annotation.Index;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.Nullable;
|
||||
import org.msgpack.annotation.Optional;
|
||||
import org.msgpack.annotation.Required;
|
||||
|
||||
public class FieldEntryReader implements IFieldEntryReader{
|
||||
|
||||
|
||||
public IFieldEntry[] convertFieldEntries(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
List<FieldList.Entry> src = flist.getList();
|
||||
FieldEntry[] result = new FieldEntry[src.size()];
|
||||
for(int i=0; i < src.size(); i++) {
|
||||
FieldList.Entry s = src.get(i);
|
||||
if(s.isAvailable()) {
|
||||
result[i] = new FieldEntry(targetClass.getDeclaredField(s.getName()), s.getOption());
|
||||
} else {
|
||||
result[i] = new FieldEntry();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFieldEntry[] readFieldEntries(Class<?> targetClass,
|
||||
FieldOption implicitOption) {
|
||||
Field[] allFields = readAllFields(targetClass);
|
||||
|
||||
/* index:
|
||||
* @Index(0) int field_a; // 0
|
||||
* int field_b; // 1
|
||||
* @Index(3) int field_c; // 3
|
||||
* int field_d; // 4
|
||||
* @Index(2) int field_e; // 2
|
||||
* int field_f; // 5
|
||||
*/
|
||||
List<FieldEntry> indexed = new ArrayList<FieldEntry>();
|
||||
int maxIndex = -1;
|
||||
for(Field f : allFields) {
|
||||
FieldOption opt = readFieldOption(f, implicitOption);
|
||||
if(opt == FieldOption.IGNORE) {
|
||||
// skip
|
||||
continue;
|
||||
}
|
||||
|
||||
int index = readFieldIndex(f, maxIndex);
|
||||
|
||||
if(indexed.size() > index && indexed.get(index) != null) {
|
||||
throw new TemplateBuildException("duplicated index: "+index);
|
||||
}
|
||||
if(index < 0) {
|
||||
throw new TemplateBuildException("invalid index: "+index);
|
||||
}
|
||||
|
||||
while(indexed.size() <= index) {
|
||||
indexed.add(null);
|
||||
}
|
||||
indexed.set(index, new FieldEntry(f, opt));
|
||||
|
||||
if(maxIndex < index) {
|
||||
maxIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
FieldEntry[] result = new FieldEntry[maxIndex+1];
|
||||
for(int i=0; i < indexed.size(); i++) {
|
||||
FieldEntry e = indexed.get(i);
|
||||
if(e == null) {
|
||||
result[i] = new FieldEntry();
|
||||
} else {
|
||||
result[i] = e;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public FieldOption readImplicitFieldOption(Class<?> targetClass) {
|
||||
MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class);
|
||||
if(a == null) {
|
||||
return FieldOption.DEFAULT;
|
||||
}
|
||||
return a.value();
|
||||
}
|
||||
|
||||
private Field[] readAllFields(Class<?> targetClass) {
|
||||
// order: [fields of super class, ..., fields of this class]
|
||||
List<Field[]> succ = new ArrayList<Field[]>();
|
||||
int total = 0;
|
||||
for(Class<?> c = targetClass; c != Object.class; c = c.getSuperclass()) {
|
||||
Field[] fields = c.getDeclaredFields();
|
||||
total += fields.length;
|
||||
succ.add(fields);
|
||||
}
|
||||
Field[] result = new Field[total];
|
||||
int off = 0;
|
||||
for(int i=succ.size()-1; i >= 0; i--) {
|
||||
Field[] fields = succ.get(i);
|
||||
System.arraycopy(fields, 0, result, off, fields.length);
|
||||
off += fields.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static FieldOption readFieldOption(Field field, FieldOption implicitOption) {
|
||||
int mod = field.getModifiers();
|
||||
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
|
||||
return FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
if(isAnnotated(field, Ignore.class)) {
|
||||
return FieldOption.IGNORE;
|
||||
} else if(isAnnotated(field, Required.class)) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else if(isAnnotated(field, Optional.class)) {
|
||||
return FieldOption.OPTIONAL;
|
||||
} else if(isAnnotated(field, Nullable.class)) {
|
||||
if(field.getDeclaringClass().isPrimitive()) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else {
|
||||
return FieldOption.NULLABLE;
|
||||
}
|
||||
}
|
||||
|
||||
if(implicitOption != FieldOption.DEFAULT) {
|
||||
return implicitOption;
|
||||
}
|
||||
|
||||
// default mode:
|
||||
// transient : Ignore
|
||||
// public : Required
|
||||
// others : Ignore
|
||||
if(Modifier.isTransient(mod)) {
|
||||
return FieldOption.IGNORE;
|
||||
} else if(Modifier.isPublic(mod)) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else {
|
||||
return FieldOption.IGNORE;
|
||||
}
|
||||
}
|
||||
|
||||
private static int readFieldIndex(Field field, int maxIndex) {
|
||||
Index a = field.getAnnotation(Index.class);
|
||||
if(a == null) {
|
||||
return maxIndex + 1;
|
||||
} else {
|
||||
return a.value();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAnnotated(AccessibleObject ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
}
|
96
java/src/main/java/org/msgpack/template/FieldList.java
Normal file
96
java/src/main/java/org/msgpack/template/FieldList.java
Normal file
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FieldList {
|
||||
public static class Entry {
|
||||
public Entry() {
|
||||
this.name = null;
|
||||
this.option = FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
public Entry(String name, FieldOption option) {
|
||||
this.name = name;
|
||||
this.option = option;
|
||||
}
|
||||
|
||||
private String name;
|
||||
private FieldOption option;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public FieldOption getOption() {
|
||||
return option;
|
||||
}
|
||||
|
||||
public boolean isAvailable() {
|
||||
return this.option != FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return this.option == FieldOption.REQUIRED;
|
||||
}
|
||||
|
||||
public boolean isOptional() {
|
||||
return this.option == FieldOption.OPTIONAL;
|
||||
}
|
||||
|
||||
public boolean isNullable() {
|
||||
return this.option == FieldOption.NULLABLE;
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<Entry> list;
|
||||
|
||||
public FieldList() {
|
||||
list = new ArrayList<Entry>();
|
||||
}
|
||||
|
||||
public void add(final String name) {
|
||||
add(name, FieldOption.REQUIRED);
|
||||
}
|
||||
|
||||
public void add(final String name, final FieldOption option) {
|
||||
list.add(new Entry(name, option));
|
||||
}
|
||||
|
||||
public void put(int index, final String name) {
|
||||
put(index, name, FieldOption.REQUIRED);
|
||||
}
|
||||
|
||||
public void put(int index, final String name, final FieldOption option) {
|
||||
if(list.size() < index) {
|
||||
do {
|
||||
list.add(new Entry());
|
||||
} while(list.size() < index);
|
||||
list.add(new Entry(name, option));
|
||||
} else {
|
||||
list.set(index, new Entry(name, option));
|
||||
}
|
||||
}
|
||||
|
||||
public List<Entry> getList() {
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
27
java/src/main/java/org/msgpack/template/FieldOption.java
Normal file
27
java/src/main/java/org/msgpack/template/FieldOption.java
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
public enum FieldOption {
|
||||
IGNORE,
|
||||
REQUIRED,
|
||||
OPTIONAL,
|
||||
NULLABLE,
|
||||
DEFAULT;
|
||||
}
|
||||
|
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatArrayTemplate implements Template {
|
||||
private FloatArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof float[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
float[] array = (float[])target;
|
||||
try {
|
||||
pk.packArray(array.length);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
for(float a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
float[] array;
|
||||
if(to != null && to instanceof float[] && ((float[])to).length == length) {
|
||||
array = (float[])to;
|
||||
} else {
|
||||
array = new float[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackFloat();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
float[] array;
|
||||
if(to != null && to instanceof float[] && ((float[])to).length == src.length) {
|
||||
array = (float[])to;
|
||||
} else {
|
||||
array = new float[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asFloat();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public FloatArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final FloatArrayTemplate instance = new FloatArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(float[].class, instance);
|
||||
}
|
||||
}
|
||||
|
53
java/src/main/java/org/msgpack/template/FloatTemplate.java
Normal file
53
java/src/main/java/org/msgpack/template/FloatTemplate.java
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatTemplate implements Template {
|
||||
private FloatTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
try {
|
||||
pk.packFloat((Float)target);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return pac.unpackFloat();
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
return from.asFloat();
|
||||
}
|
||||
|
||||
static public FloatTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final FloatTemplate instance = new FloatTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(Float.class, instance);
|
||||
TemplateRegistry.register(float.class, instance);
|
||||
}
|
||||
}
|
||||
|
25
java/src/main/java/org/msgpack/template/GenericTemplate.java
Normal file
25
java/src/main/java/org/msgpack/template/GenericTemplate.java
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import org.msgpack.Template;
|
||||
|
||||
public interface GenericTemplate {
|
||||
public Template build(Template[] params);
|
||||
}
|
||||
|
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import org.msgpack.Template;
|
||||
|
||||
public class GenericTemplate1 implements GenericTemplate {
|
||||
Constructor<? extends Template> constructor;
|
||||
|
||||
public GenericTemplate1(Class<? extends Template> tmpl) {
|
||||
try {
|
||||
this.constructor = tmpl.getConstructor(new Class<?>[]{Template.class});
|
||||
constructor.newInstance(new Object[]{AnyTemplate.getInstance()});
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Template build(Template[] params) {
|
||||
try {
|
||||
return constructor.newInstance(params);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import org.msgpack.Template;
|
||||
|
||||
public class GenericTemplate2 implements GenericTemplate {
|
||||
Constructor<? extends Template> constructor;
|
||||
|
||||
public GenericTemplate2(Class<? extends Template> tmpl) {
|
||||
try {
|
||||
this.constructor = tmpl.getConstructor(new Class<?>[]{Template.class, Template.class});
|
||||
constructor.newInstance(new Object[]{AnyTemplate.getInstance(), AnyTemplate.getInstance()});
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Template build(Template[] params) {
|
||||
try {
|
||||
return constructor.newInstance(params);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
42
java/src/main/java/org/msgpack/template/IFieldEntry.java
Normal file
42
java/src/main/java/org/msgpack/template/IFieldEntry.java
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public interface IFieldEntry {
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public abstract Class<?> getType();
|
||||
|
||||
public abstract String getJavaTypeName();
|
||||
|
||||
public abstract Type getGenericType();
|
||||
|
||||
public abstract FieldOption getOption();
|
||||
|
||||
public abstract boolean isAvailable();
|
||||
|
||||
public abstract boolean isRequired();
|
||||
|
||||
public abstract boolean isOptional();
|
||||
|
||||
public abstract boolean isNullable();
|
||||
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
public interface IFieldEntryReader {
|
||||
|
||||
public IFieldEntry[] convertFieldEntries(Class<?> targetClass, FieldList flist) throws NoSuchFieldException;
|
||||
public IFieldEntry[] readFieldEntries(Class<?> targetClass, FieldOption implicitOption);
|
||||
public FieldOption readImplicitFieldOption(Class<?> targetClass) ;
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class IntArrayTemplate implements Template {
|
||||
private IntArrayTemplate() { }
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof int[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
int[] array = (int[])target;
|
||||
try {
|
||||
pk.packArray(array.length);
|
||||
} catch (NullPointerException e) {
|
||||
throw new MessageTypeException("target is null.", e);
|
||||
}
|
||||
for(int a : array) {
|
||||
pk.pack(a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
int[] array;
|
||||
if(to != null && to instanceof int[] && ((int[])to).length == length) {
|
||||
array = (int[])to;
|
||||
} else {
|
||||
array = new int[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = pac.unpackInt();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
int[] array;
|
||||
if(to != null && to instanceof int[] && ((int[])to).length == src.length) {
|
||||
array = (int[])to;
|
||||
} else {
|
||||
array = new int[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = s.asInt();
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static public IntArrayTemplate getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static final IntArrayTemplate instance = new IntArrayTemplate();
|
||||
|
||||
static {
|
||||
TemplateRegistry.register(int[].class, instance);
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user