mirror of
				https://github.com/msgpack/msgpack-c.git
				synced 2025-10-22 08:02:08 +02:00 
			
		
		
		
	Compare commits
	
		
			183 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | 0c60cfc5c0 | ||
|   | deb6b0e334 | ||
|   | 27777dcd31 | ||
|   | bdb397e043 | ||
|   | cb4d851761 | ||
|   | 7ac16f4834 | ||
|   | bf7fece440 | ||
|   | c04ef9efe5 | ||
|   | b774c07d19 | ||
|   | 229467cb29 | ||
|   | 949b472f7e | ||
|   | be67d3e362 | ||
|   | 3e2ae7cc78 | ||
|   | 505660e1fa | ||
|   | 09d90e1231 | ||
|   | 5a9f89ae1f | ||
|   | 6f56345dd8 | ||
|   | edef040688 | ||
|   | 9fc196e750 | ||
|   | dc1698eaf4 | ||
|   | 53ded063a0 | ||
|   | 8a2c50c374 | ||
|   | f71148f3a6 | ||
|   | 172105828c | ||
|   | 6cca6cb20d | ||
|   | b7336f817c | ||
|   | 88356b79be | ||
|   | ce9e543882 | ||
|   | 3ff3ba83f4 | ||
|   | edb525a030 | ||
|   | ea5314f076 | ||
|   | be8e53163b | ||
|   | 79151f517f | ||
|   | e8ffe7ef96 | ||
|   | b5a7b5e277 | ||
|   | 85b5e1088f | ||
|   | 680ddb1557 | ||
|   | 44fff522bd | ||
|   | 90815f0d27 | ||
|   | f07950c60a | ||
|   | f8c4125892 | ||
|   | 1f30cc203a | ||
|   | 764bfdcb9f | ||
|   | f290814f64 | ||
|   | 3a2508545d | ||
|   | 79a06a338a | ||
|   | 509f27eee1 | ||
|   | e511c32a36 | ||
|   | b463d9b687 | ||
|   | e96e20ccfd | ||
|   | 06ebdbfd37 | ||
|   | df9fd047b4 | ||
|   | 85d65c59d2 | ||
|   | 754126644f | ||
|   | 220436f4f9 | ||
|   | b62a6ae1d0 | ||
|   | eae0532b8c | ||
|   | d642487f99 | ||
|   | 320510506b | ||
|   | b2839ac78b | ||
|   | 834d5a0e72 | ||
|   | 13ce808c47 | ||
|   | 980a6529c1 | ||
|   | c92384fe68 | ||
|   | 9f563f87b9 | ||
|   | 44f37b8d1b | ||
|   | 5f41c1cf3f | ||
|   | 2a664b9ae6 | ||
|   | 5456814199 | ||
|   | 33ceaeb208 | ||
|   | b33e60b3ae | ||
|   | 9dd0a6eb86 | ||
|   | ed6faf4e65 | ||
|   | df2ee5de49 | ||
|   | 36fef91a67 | ||
|   | 3399148d75 | ||
|   | 0eea92de66 | ||
|   | 92975bb21d | ||
|   | bf18e04134 | ||
|   | 4a0d7f18fd | ||
|   | 65c360a2ca | ||
|   | 71919f7523 | ||
|   | 31a49db68b | ||
|   | 0d615442ba | ||
|   | 8f35dbc306 | ||
|   | 154bcbc4f7 | ||
|   | c96a39d315 | ||
|   | 06ba74c366 | ||
|   | 8786a8e6e0 | ||
|   | 748236fef0 | ||
|   | ae1b06f20b | ||
|   | b8aa93ce30 | ||
|   | 49d40a42f1 | ||
|   | 1845f34b81 | ||
|   | 43903d446a | ||
|   | 256da2124b | ||
|   | 7869e96bac | ||
|   | 79b51a6e4b | ||
|   | cad9f6d46e | ||
|   | da2960880a | ||
|   | 0eacc58e29 | ||
|   | 3bec736aee | ||
|   | 3241ffd1b4 | ||
|   | d0d7b266bc | ||
|   | c24948258b | ||
|   | d36666bd98 | ||
|   | 2794b1d310 | ||
|   | f456fa03da | ||
|   | 356b7b43f8 | ||
|   | 92718e2140 | ||
|   | 4021160a64 | ||
|   | 6f043e3326 | ||
|   | 74b0b1da21 | ||
|   | 3a5f676687 | ||
|   | 4fa7cffc37 | ||
|   | 79b83e78a5 | ||
|   | 048a3835e7 | ||
|   | 4a4891036a | ||
|   | ff00c83f15 | ||
|   | 79d31b7452 | ||
|   | 20cc6f7463 | ||
|   | 8acabaa135 | ||
|   | ba7183a5b4 | ||
|   | 28f4338a6c | ||
|   | 800a93a859 | ||
|   | 2e969e3a0a | ||
|   | 3fb31f651d | ||
|   | 682d25b551 | ||
|   | bcad8d4c4c | ||
|   | 96bab8e02e | ||
|   | fa4a615d0f | ||
|   | f1265c4fed | ||
|   | df01cd9a09 | ||
|   | 255bac642d | ||
|   | 6d8c3bd13d | ||
|   | 75b8272710 | ||
|   | 2f80e154f2 | ||
|   | cc0114c482 | ||
|   | e5e2b9095c | ||
|   | 6977edc032 | ||
|   | 177cc55ee5 | ||
|   | feb7ec03ba | ||
|   | 6630a643d3 | ||
|   | d70e64a434 | ||
|   | 896ad51102 | ||
|   | be6d6560a7 | ||
|   | 709d0cc33e | ||
|   | bd73742552 | ||
|   | ed4518e09a | ||
|   | 6c8301eae8 | ||
|   | 6e30d504e3 | ||
|   | cee09a0261 | ||
|   | 76a72558b3 | ||
|   | 22124a33ce | ||
|   | 3731373de1 | ||
|   | bbaf8c2f67 | ||
|   | f50694cc96 | ||
|   | 4930ea7dab | ||
|   | 96bb4cf49d | ||
|   | 5e7563ac5b | ||
|   | f199b80453 | ||
|   | 5de8eec206 | ||
|   | 6cfea98501 | ||
|   | 33498d3673 | ||
|   | ed3ead06fe | ||
|   | 94e5b0d78f | ||
|   | 0812eb1c04 | ||
|   | 8cb5ccad99 | ||
|   | 743d69ec0b | ||
|   | f6de4c9479 | ||
|   | fb59927d32 | ||
|   | 08974f00ed | ||
|   | 60643f023f | ||
|   | 68a98d3dd0 | ||
|   | ca9015452e | ||
|   | 05ac2603e6 | ||
|   | 8e923777b8 | ||
|   | 8949551c2e | ||
|   | 0b45e9442b | ||
|   | 1d1a9d7933 | ||
|   | 1f372cf0cc | ||
|   | c43d597aa9 | ||
|   | df14629a25 | 
							
								
								
									
										62
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										62
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,8 +1,58 @@ | ||||
| # Files generated by the bootstrap script. | ||||
| /INSTALL | ||||
| /NEWS | ||||
| /README | ||||
| /ac/ | ||||
| /aclocal.m4 | ||||
| /autom4te.cache/ | ||||
| /config.h.in | ||||
| /configure | ||||
| /msgpack_vc2008.sln | ||||
| /msgpack_vc2008.vcproj | ||||
| /src/msgpack/pack_define.h | ||||
| /src/msgpack/pack_template.h | ||||
| /src/msgpack/sysdep.h | ||||
| /src/msgpack/type/define.hpp | ||||
| /src/msgpack/type/tuple.hpp | ||||
| /src/msgpack/unpack_define.h | ||||
| /src/msgpack/unpack_template.h | ||||
| /src/msgpack/zone.hpp | ||||
| /test/cases.mpac | ||||
| /test/cases_compact.mpac | ||||
| Makefile.in | ||||
|  | ||||
| # Files generated by the configure script. | ||||
|  | ||||
| /config.h | ||||
| /config.log | ||||
| /config.status | ||||
| /libtool | ||||
| /msgpack.pc | ||||
| /src/msgpack/version.h | ||||
| /stamp-h1 | ||||
| Makefile | ||||
| .deps | ||||
| .libs | ||||
|  | ||||
| # Files generated by make. | ||||
| *.o | ||||
| *.so | ||||
| ruby/Makefile | ||||
| *.5 | ||||
| *.8 | ||||
| *.6 | ||||
| _obj | ||||
| _test | ||||
| *.lo | ||||
| *.la | ||||
|  | ||||
| # Files generated by make check. | ||||
| # TODO: Replace these with something like /test/*_test | ||||
| /test/buffer | ||||
| /test/cases | ||||
| /test/convert | ||||
| /test/fixint | ||||
| /test/fixint_c | ||||
| /test/msgpack_test | ||||
| /test/msgpackc_test | ||||
| /test/object | ||||
| /test/pack_unpack | ||||
| /test/pack_unpack_c | ||||
| /test/streaming | ||||
| /test/streaming_c | ||||
| /test/version | ||||
| /test/zone | ||||
|   | ||||
| @@ -1,3 +1,14 @@ | ||||
| 2013-12-23 version 0.5.8: | ||||
| 
 | ||||
|   * Move to the new github repository msgpack/msgpack-c | ||||
|   * Support the new deserialization specification | ||||
|   * fixes the problem of unpack helpers for array and map with 32bit compilers (#37, #38) | ||||
|   * Other bug fixes and refactoring: #46, #41, #36, #35, #33, #32, #30, #29, #28, #27, #26, #25, #8, #3 | ||||
|   * Update of documents: #23, #18, #17 | ||||
| 
 | ||||
| 2011-08-08 version 0.5.7: | ||||
| 
 | ||||
|   * fixes compile error problem with llvm-gcc and Mac OS X Lion | ||||
| 
 | ||||
| 2011-04-24 version 0.5.6: | ||||
| 
 | ||||
| @@ -13,6 +13,9 @@ DOC_FILES = \ | ||||
| EXTRA_DIST = \
 | ||||
| 		$(DOC_FILES) | ||||
| 
 | ||||
| pkgconfigdir = $(libdir)/pkgconfig | ||||
| pkgconfig_DATA = msgpack.pc | ||||
| 
 | ||||
| doxygen: | ||||
| 	./preprocess clean | ||||
| 	cd src && $(MAKE) doxygen | ||||
							
								
								
									
										194
									
								
								QUICKSTART-C.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										194
									
								
								QUICKSTART-C.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,194 @@ | ||||
| # Implementation Status | ||||
|  | ||||
| The serialization library is production-ready. | ||||
|  | ||||
| Currently, no RPC implementation is not available. | ||||
|  | ||||
| # Install | ||||
|  | ||||
|  | ||||
| ## Mac OS X with MacPorts | ||||
|  | ||||
| On Mac OS X, you can install MessagePack for C using MacPorts. | ||||
|  | ||||
| ``` | ||||
| $ sudo port install msgpack | ||||
| ``` | ||||
|  | ||||
| You might need to run `sudo port selfupdate` before installing to update the package repository. | ||||
|  | ||||
| You can also install via Homebrew. | ||||
|  | ||||
| ``` | ||||
| $ sudo brew install msgpack | ||||
| ``` | ||||
|  | ||||
| ## FreeBSD with Ports Collection | ||||
|  | ||||
| On FreeBSD, you can use Ports Collection. Install [net/msgpack|http://www.freebsd.org/cgi/cvsweb.cgi/ports/devel/msgpack/] package. | ||||
|  | ||||
| ## Gentoo Linux with Portage | ||||
|  | ||||
| On Gentoo Linux, you can use emerge. Install [dev-libs/msgpack|http://gentoo-portage.com/dev-libs/msgpack] package. | ||||
|  | ||||
| ## Other UNIX-like platform with ./configure | ||||
|  | ||||
| On the other UNIX-like platforms, download source package from [Releases|http://msgpack.org/releases/cpp/] and run `./configure && make && make install`. | ||||
|  | ||||
| ``` | ||||
| $ wget http://msgpack.org/releases/cpp/msgpack-0.5.5.tar.gz | ||||
| $ tar zxvf msgpack-0.5.5.tar.gz | ||||
| $ cd msgpack-0.5.5 | ||||
| $ ./configure | ||||
| $ make | ||||
| $ sudo make install | ||||
| ``` | ||||
|  | ||||
| ## Windows | ||||
|  | ||||
| On Windows, download source package from [here|https://sourceforge.net/projects/msgpack/files/] and extract it. | ||||
| Then open `msgpack_vc8.vcproj` file and build it using batch build. It builds libraries on `lib/` folder and header files on `include/` folder. | ||||
|  | ||||
| You can build using command line as follows: | ||||
|  | ||||
| ``` | ||||
| > vcbuild msgpack_vc2008.vcproj | ||||
| > dir lib       % DLL files are here | ||||
| > dir include   % header files are here | ||||
| ``` | ||||
|  | ||||
| ## Install from git repository | ||||
|  | ||||
| You need to install gcc (4.1.0 or higher), autotools. | ||||
|  | ||||
| ``` | ||||
| $ git clone git@github.com:msgpack/msgpack.git | ||||
| $ cd msgpack/cpp | ||||
| $ ./bootstrap | ||||
| $ ./configure | ||||
| $ make | ||||
| $ sudo make install | ||||
| ``` | ||||
|  | ||||
| # Serialization QuickStart for C | ||||
|  | ||||
| ## First program | ||||
|  | ||||
| Include `msgpack.h` header and link `msgpack` library to use MessagePack on your program. | ||||
|  | ||||
| ```c | ||||
| #include <msgpack.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| int main(void) { | ||||
|  | ||||
|         /* creates buffer and serializer instance. */ | ||||
|         msgpack_sbuffer* buffer = msgpack_sbuffer_new(); | ||||
|         msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); | ||||
|  | ||||
|         /* serializes ["Hello", "MessagePack"]. */ | ||||
|         msgpack_pack_array(pk, 2); | ||||
|         msgpack_pack_raw(pk, 5); | ||||
|         msgpack_pack_raw_body(pk, "Hello", 5); | ||||
|         msgpack_pack_raw(pk, 11); | ||||
|         msgpack_pack_raw_body(pk, "MessagePack", 11); | ||||
|  | ||||
|         /* deserializes it. */ | ||||
|         msgpack_unpacked msg; | ||||
|         msgpack_unpacked_init(&msg); | ||||
|         bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL); | ||||
|  | ||||
|         /* prints the deserialized object. */ | ||||
|         msgpack_object obj = msg.data; | ||||
|         msgpack_object_print(stdout, obj);  /*=> ["Hello", "MessagePack"] */ | ||||
|  | ||||
|         /* cleaning */ | ||||
|         msgpack_sbuffer_free(buffer); | ||||
|         msgpack_packer_free(pk); | ||||
| } | ||||
| ``` | ||||
|  | ||||
| ## Simple program with a loop | ||||
|  | ||||
| ```c | ||||
| #include <msgpack.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| int main(void) { | ||||
|  | ||||
|         /* creates buffer and serializer instance. */ | ||||
|         msgpack_sbuffer* buffer = msgpack_sbuffer_new(); | ||||
|         msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); | ||||
|          | ||||
|         int j; | ||||
|  | ||||
|         for(j = 0; j<23; j++) { | ||||
|            /* NB: the buffer needs to be cleared on each iteration */ | ||||
|            msgpack_sbuffer_clear(buffer); | ||||
|  | ||||
|            /* serializes ["Hello", "MessagePack"]. */ | ||||
|            msgpack_pack_array(pk, 3); | ||||
|            msgpack_pack_raw(pk, 5); | ||||
|            msgpack_pack_raw_body(pk, "Hello", 5); | ||||
|            msgpack_pack_raw(pk, 11); | ||||
|            msgpack_pack_raw_body(pk, "MessagePack", 11); | ||||
|            msgpack_pack_int(pk, j); | ||||
|  | ||||
|            /* deserializes it. */ | ||||
|            msgpack_unpacked msg; | ||||
|            msgpack_unpacked_init(&msg); | ||||
|            bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL); | ||||
|  | ||||
|            /* prints the deserialized object. */ | ||||
|            msgpack_object obj = msg.data; | ||||
|            msgpack_object_print(stdout, obj);  /*=> ["Hello", "MessagePack"] */ | ||||
|            puts(""); | ||||
|         } | ||||
|  | ||||
|         /* cleaning */ | ||||
|         msgpack_sbuffer_free(buffer); | ||||
|         msgpack_packer_free(pk); | ||||
| } | ||||
| ``` | ||||
|  | ||||
| ## Streaming feature | ||||
|  | ||||
| ```c | ||||
| #include <msgpack.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| int main(void) { | ||||
|         /* serializes multiple objects using msgpack_packer. */ | ||||
|         msgpack_sbuffer* buffer = msgpack_sbuffer_new(); | ||||
|         msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); | ||||
|         msgpack_pack_int(pk, 1); | ||||
|         msgpack_pack_int(pk, 2); | ||||
|         msgpack_pack_int(pk, 3); | ||||
|  | ||||
|         /* deserializes these objects using msgpack_unpacker. */ | ||||
|         msgpack_unpacker pac; | ||||
|         msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE); | ||||
|  | ||||
|         /* feeds the buffer. */ | ||||
|         msgpack_unpacker_reserve_buffer(&pac, buffer->size); | ||||
|         memcpy(msgpack_unpacker_buffer(&pac), buffer->data, buffer->size); | ||||
|         msgpack_unpacker_buffer_consumed(&pac, buffer->size); | ||||
|  | ||||
|         /* now starts streaming deserialization. */ | ||||
|         msgpack_unpacked result; | ||||
|         msgpack_unpacked_init(&result); | ||||
|  | ||||
|         while(msgpack_unpacker_next(&pac, &result)) { | ||||
|             msgpack_object_print(stdout, result.data); | ||||
|             puts(""); | ||||
|         } | ||||
|  | ||||
|         /* results: | ||||
|          * $ gcc stream.cc -lmsgpack -o stream | ||||
|          * $ ./stream | ||||
|          * 1 | ||||
|          * 2 | ||||
|          * 3 | ||||
|          */ | ||||
| } | ||||
| ``` | ||||
							
								
								
									
										159
									
								
								QUICKSTART-CPP.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										159
									
								
								QUICKSTART-CPP.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,159 @@ | ||||
| # Implementation Status | ||||
|  | ||||
| The serialization library is production-ready. | ||||
|  | ||||
| Currently, RPC implementation is testing phase. Requires newer kernel, not running on RHEL5/CentOS5. | ||||
|  | ||||
| # Install | ||||
|  | ||||
| Same as QuickStart for C Language. | ||||
|  | ||||
| # Serialization QuickStart for C+\+ | ||||
|  | ||||
| ## First program | ||||
|  | ||||
| Include `msgpack.hpp` header and link `msgpack` library to use MessagePack on your program. | ||||
|  | ||||
| ```cpp | ||||
| #include <msgpack.hpp> | ||||
| #include <vector> | ||||
| #include <string> | ||||
| #include <iostream> | ||||
|  | ||||
| int main(void) { | ||||
|         // serializes this object. | ||||
|         std::vector<std::string> vec; | ||||
|         vec.push_back("Hello"); | ||||
|         vec.push_back("MessagePack"); | ||||
|  | ||||
|         // serialize it into simple buffer. | ||||
|         msgpack::sbuffer sbuf; | ||||
|         msgpack::pack(sbuf, vec); | ||||
|  | ||||
|         // deserialize it. | ||||
|         msgpack::unpacked msg; | ||||
|         msgpack::unpack(&msg, sbuf.data(), sbuf.size()); | ||||
|  | ||||
|         // print the deserialized object. | ||||
|         msgpack::object obj = msg.get(); | ||||
|         std::cout << obj << std::endl;  //=> ["Hello", "MessagePack"] | ||||
|  | ||||
|         // convert it into statically typed object. | ||||
|         std::vector<std::string> rvec; | ||||
|         obj.convert(&rvec); | ||||
| } | ||||
| ``` | ||||
|  | ||||
| Compile it as follows: | ||||
|  | ||||
| ``` | ||||
| $ g++ hello.cc -lmsgpack -o hello | ||||
| $ ./hello | ||||
| ["Hello", "MessagePack"] | ||||
| ``` | ||||
|  | ||||
| ## Streaming feature | ||||
|  | ||||
| ```cpp | ||||
| #include <msgpack.hpp> | ||||
| #include <iostream> | ||||
| #include <string> | ||||
|  | ||||
| int main(void) { | ||||
|         // serializes multiple objects using msgpack::packer. | ||||
|         msgpack::sbuffer buffer; | ||||
|  | ||||
|         msgpack::packer<msgpack::sbuffer> pk(&buffer); | ||||
|         pk.pack(std::string("Log message ... 1")); | ||||
|         pk.pack(std::string("Log message ... 2")); | ||||
|         pk.pack(std::string("Log message ... 3")); | ||||
|  | ||||
|         // deserializes these objects using msgpack::unpacker. | ||||
|         msgpack::unpacker pac; | ||||
|  | ||||
|         // feeds the buffer. | ||||
|         pac.reserve_buffer(buffer.size()); | ||||
|         memcpy(pac.buffer(), buffer.data(), buffer.size()); | ||||
|         pac.buffer_consumed(buffer.size()); | ||||
|  | ||||
|         // now starts streaming deserialization. | ||||
|         msgpack::unpacked result; | ||||
|         while(pac.next(&result)) { | ||||
|             std::cout << result.get() << std::endl; | ||||
|         } | ||||
|  | ||||
|         // results: | ||||
|         // $ g++ stream.cc -lmsgpack -o stream | ||||
|         // $ ./stream | ||||
|         // "Log message ... 1" | ||||
|         // "Log message ... 2" | ||||
|         // "Log message ... 3" | ||||
| } | ||||
| ``` | ||||
|  | ||||
| ### Streaming into an array or map | ||||
|  | ||||
| ```cpp | ||||
| #include <msgpack.hpp> | ||||
| #include <iostream> | ||||
| #include <string> | ||||
|  | ||||
| int main(void) { | ||||
|         // serializes multiple objects into one message containing an array using msgpack::packer. | ||||
|         msgpack::sbuffer buffer; | ||||
|  | ||||
|         msgpack::packer<msgpack::sbuffer> pk(&buffer); | ||||
|         pk.pack_array(3) | ||||
|         pk.pack(std::string("Log message ... 1")); | ||||
|         pk.pack(std::string("Log message ... 2")); | ||||
|         pk.pack(std::string("Log message ... 3")); | ||||
|  | ||||
|         // serializes multiple objects into one message containing a map using msgpack::packer. | ||||
|         msgpack::sbuffer buffer2; | ||||
|  | ||||
|         msgpack::packer<msgpack::sbuffer> pk2(&buffer2); | ||||
|         pk2.pack_map(2) | ||||
|         pk2.pack(std::string("x")); | ||||
|         pk2.pack(3); | ||||
|         pk2.pack(std::string("y)); | ||||
|         pk2.pack(3.4321); | ||||
|  | ||||
| } | ||||
| ``` | ||||
|  | ||||
|  | ||||
| ## User-defined classes | ||||
|  | ||||
| You can use serialize/deserializes user-defined classes using MSGPACK_DEFINE macro. | ||||
|  | ||||
| ```cpp | ||||
| #include <msgpack.hpp> | ||||
| #include <vector> | ||||
| #include <string> | ||||
|  | ||||
| class myclass { | ||||
| private: | ||||
|     std::string m_str; | ||||
|     std::vector<int> m_vec; | ||||
| public: | ||||
|     MSGPACK_DEFINE(m_str, m_vec); | ||||
| }; | ||||
|  | ||||
| int main(void) { | ||||
|         std::vector<myclass> vec; | ||||
|         // add some elements into vec... | ||||
|  | ||||
|         // you can serialize myclass directly | ||||
|         msgpack::sbuffer sbuf; | ||||
|         msgpack::pack(sbuf, vec); | ||||
|  | ||||
|         msgpack::unpacked msg; | ||||
|         msgpack::unpack(&msg, sbuf.data(), sbuf.size()); | ||||
|  | ||||
|         msgpack::object obj = msg.get(); | ||||
|  | ||||
|         // you can convert object to myclass directly | ||||
|         std::vector<myclass> rvec; | ||||
|         obj.convert(&rvec); | ||||
| } | ||||
| ``` | ||||
							
								
								
									
										78
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								README.md
									
									
									
									
									
								
							| @@ -1,37 +1,73 @@ | ||||
| MessagePack | ||||
| =========== | ||||
| Extremely efficient object serialization library. It's like JSON, but very fast and small. | ||||
| MessagePack for C/C++ | ||||
| ===================== | ||||
| Binary-based efficient object serialization library. | ||||
|  | ||||
|  | ||||
| ## What's MessagePack? | ||||
| ## Installation | ||||
|  | ||||
| MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small. | ||||
| Download latest package from [releases of MessagePack](http://sourceforge.net/projects/msgpack/files/) and extract it. | ||||
|  | ||||
| Typical small integer (like flags or error code) is saved only in 1 byte, and typical short string only needs 1 byte except the length of the string itself. \[1,2,3\] (3 elements array) is serialized in 4 bytes using MessagePack as follows: | ||||
| On UNIX-like platform, run ./configure && make && sudo make install: | ||||
|  | ||||
|     require 'msgpack' | ||||
|     msg = [1,2,3].to_msgpack  #=> "\x93\x01\x02\x03" | ||||
|     MessagePack.unpack(msg)   #=> [1,2,3] | ||||
|     $ ./configure | ||||
|     $ make | ||||
|     $ sudo make install | ||||
|  | ||||
| On Windows, open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. DLLs are built on lib folder, | ||||
| and the headers are built on include folder. | ||||
|  | ||||
| To use the library in your program, include msgpack.hpp header and link "msgpack" library. | ||||
|  | ||||
|  | ||||
| ## Performance | ||||
| ## Example | ||||
| ```CPP | ||||
| #include <msgpack.hpp> | ||||
| #include <vector> | ||||
|      | ||||
|  | ||||
| int main(void) { | ||||
|     // This is target object. | ||||
|     std::vector<std::string> target; | ||||
|     target.push_back("Hello,"); | ||||
|     target.push_back("World!"); | ||||
|      | ||||
| In this test, it measured the elapsed time of serializing and deserializing 200,000 target objects. The target object consists of the three integers and 512 bytes string. | ||||
| The source code of this test is available from [frsyuki' serializer-speed-test repository.](http://github.com/frsyuki/serializer-speed-test) | ||||
|     // Serialize it. | ||||
|     msgpack::sbuffer sbuf;  // simple buffer | ||||
|     msgpack::pack(&sbuf, target); | ||||
|      | ||||
|     // Deserialize the serialized data. | ||||
|     msgpack::unpacked msg;    // includes memory pool and deserialized object | ||||
|     msgpack::unpack(&msg, sbuf.data(), sbuf.size()); | ||||
|     msgpack::object obj = msg.get(); | ||||
|      | ||||
| ## Getting Started | ||||
|     // Print the deserialized object to stdout. | ||||
|     std::cout << obj << std::endl;    // ["Hello," "World!"] | ||||
|      | ||||
| Usage and other documents about implementations in each language are found at [the web site.](http://msgpack.sourceforge.net/) | ||||
|     // Convert the deserialized object to staticaly typed object. | ||||
|     std::vector<std::string> result; | ||||
|     obj.convert(&result); | ||||
|      | ||||
|     // If the type is mismatched, it throws msgpack::type_error. | ||||
|     obj.as<int>();  // type is mismatched, msgpack::type_error is thrown | ||||
| } | ||||
| ``` | ||||
|  | ||||
| ## Learn More | ||||
| See [QuickStart for C](QUICKSTART-C.md) and [QuickStart for C++](QUICKSTART-CPP.md) for other example codes. | ||||
|  | ||||
|   - [Project Web Site](http://msgpack.sourceforge.net/) | ||||
|   - [MessagePack format specification](http://msgpack.sourceforge.net/spec) | ||||
|   - [Repository at github](http://github.com/msgpack/msgpack) | ||||
|   - [Wiki](http://msgpack.sourceforge.net/start) | ||||
|   - [MessagePack-RPC](http://github.com/msgpack/msgpack-rpc) | ||||
| ## License | ||||
|  | ||||
|     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. | ||||
|        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. | ||||
|  | ||||
| See also NOTICE file. | ||||
|  | ||||
|   | ||||
| @@ -1,13 +1,13 @@ | ||||
| AC_INIT(src/object.cpp) | ||||
| AC_CONFIG_AUX_DIR(ac) | ||||
| AM_INIT_AUTOMAKE(msgpack, 0.5.6) | ||||
| AM_INIT_AUTOMAKE(msgpack, 0.5.8) | ||||
| AC_CONFIG_HEADER(config.h) | ||||
| 
 | ||||
| AC_SUBST(CFLAGS) | ||||
| CFLAGS="-O4 -Wall $CFLAGS" | ||||
| CFLAGS="-O3 -Wall $CFLAGS" | ||||
| 
 | ||||
| AC_SUBST(CXXFLAGS) | ||||
| CXXFLAGS="-O4 -Wall $CXXFLAGS" | ||||
| CXXFLAGS="-O3 -Wall $CXXFLAGS" | ||||
| 
 | ||||
| 
 | ||||
| AC_PROG_CC | ||||
| @@ -16,7 +16,7 @@ AC_PROG_CC | ||||
| AC_MSG_CHECKING([if C++ API is enabled]) | ||||
| AC_ARG_ENABLE(cxx, | ||||
| 	AS_HELP_STRING([--disable-cxx], | ||||
| 				   [don't build C++ API]) ) | ||||
| 				   [don't build C++ API]) )   #' | ||||
| AC_MSG_RESULT([$enable_cxx]) | ||||
| if test "$enable_cxx" != "no"; then | ||||
| 	AC_PROG_CXX | ||||
| @@ -47,20 +47,44 @@ AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [ | ||||
| 	AC_TRY_LINK([ | ||||
| 		int atomic_sub(int i) { return __sync_sub_and_fetch(&i, 1); } | ||||
| 		int atomic_add(int i) { return __sync_add_and_fetch(&i, 1); } | ||||
| 	], [], msgpack_cv_atomic_ops="yes") | ||||
| 	], [atomic_sub(1); atomic_add(1);], msgpack_cv_atomic_ops="yes") | ||||
| 	]) | ||||
| if test "$msgpack_cv_atomic_ops" != "yes"; then | ||||
| 	AC_MSG_ERROR([__sync_* atomic operations are not supported. | ||||
| 	if test "$enable_cxx" = "no"; then | ||||
| 		AC_MSG_ERROR([__sync_* atomic operations are not found. Try to enable C++ support. | ||||
| 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: | ||||
| 
 | ||||
| Note that gcc < 4.1 is not supported. | ||||
|   $ ./configure CFLAGS="-march=i686" CXXFLAGS="-march=i686" | ||||
| 					  ]) | ||||
| 	fi | ||||
| 
 | ||||
| 	AC_LANG_PUSH([C++]) | ||||
| 	AC_CACHE_CHECK([for __gnu_cxx::__exchange_and_add], msgpack_cv_gcc_cxx_atomic_ops, [ | ||||
| 		AC_TRY_LINK([ | ||||
| 			#include <bits/atomicity.h> | ||||
| 			int atomic_sub(int i) { return __gnu_cxx::__exchange_and_add(&i, -1) - 1; } | ||||
| 			int atomic_add(int i) { return __gnu_cxx::__exchange_and_add(&i, 1) + 1; } | ||||
| 		], [atomic_sub(1); atomic_add(1);], msgpack_cv_gcc_cxx_atomic_ops="yes") | ||||
| 		]) | ||||
| 	AC_LANG_POP([C++]) | ||||
| 
 | ||||
| 	if test "$msgpack_cv_gcc_cxx_atomic_ops" != "yes"; then | ||||
| 		AC_MSG_ERROR([__sync_* atomic operations nor __gnu_cxx::__exchange_and_add are not found. | ||||
| 
 | ||||
| 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: | ||||
| 
 | ||||
|   $ ./configure CFLAGS="-march=i686" CXXFLAGS="-march=i686" | ||||
| ]) | ||||
| 
 | ||||
| 	else | ||||
| 		enable_gcc_cxx_atomic=yes | ||||
| 	fi | ||||
| fi | ||||
| 
 | ||||
| AM_CONDITIONAL(ENABLE_GCC_CXX_ATOMIC, test "$enable_gcc_cxx_atomic" = "yes") | ||||
| 
 | ||||
| 
 | ||||
| major=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'` | ||||
| minor=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'` | ||||
| @@ -69,6 +93,7 @@ AC_SUBST(VERSION_MINOR, $minor) | ||||
| 
 | ||||
| 
 | ||||
| AC_OUTPUT([Makefile | ||||
| 		   msgpack.pc | ||||
| 		   src/Makefile | ||||
| 		   src/msgpack/version.h | ||||
| 		   test/Makefile]) | ||||
| @@ -1,73 +0,0 @@ | ||||
| MessagePack for C/C++ | ||||
| ===================== | ||||
| Binary-based efficient object serialization library. | ||||
|  | ||||
|  | ||||
| ## Installation | ||||
|  | ||||
| Download latest package from [releases of MessagePack](http://sourceforge.net/projects/msgpack/files/) and extract it. | ||||
|  | ||||
| On UNIX-like platform, run ./configure && make && sudo make install: | ||||
|  | ||||
|     $ ./configure | ||||
|     $ make | ||||
|     $ sudo make install | ||||
|  | ||||
| On Windows, open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. DLLs are built on lib folder, | ||||
| and the headers are built on include folder. | ||||
|  | ||||
| To use the library in your program, include msgpack.hpp header and link "msgpack" library. | ||||
|  | ||||
|  | ||||
| ## Example | ||||
|  | ||||
|     #include <msgpack.hpp> | ||||
|     #include <vector> | ||||
|      | ||||
|     int main(void) { | ||||
|         // This is target object. | ||||
|         std::vector<std::string> target; | ||||
|         target.push_back("Hello,"); | ||||
|         target.push_back("World!"); | ||||
|      | ||||
|         // Serialize it. | ||||
|         msgpack::sbuffer buffer;  // simple buffer | ||||
|         msgpack::pack(&buffer, target); | ||||
|      | ||||
|         // Deserialize the serialized data. | ||||
|         msgpack::unpacked msg;    // includes memory pool and deserialized object | ||||
|         msgpack::unpack(&msg, sbuf.data(), sbuf.size()); | ||||
|         msgpack::object obj = msg.get(); | ||||
|      | ||||
|         // Print the deserialized object to stdout. | ||||
|         std::cout << obj << std::endl;    // ["Hello," "World!"] | ||||
|      | ||||
|         // Convert the deserialized object to staticaly typed object. | ||||
|         std::vector<std::string> result; | ||||
|         obj.convert(&result); | ||||
|      | ||||
|         // If the type is mismatched, it throws msgpack::type_error. | ||||
|         obj.as<int>();  // type is mismatched, msgpack::type_error is thrown | ||||
|     } | ||||
|  | ||||
| API documents and other example codes are available at the [wiki.](http://redmine.msgpack.org/projects/msgpack/wiki) | ||||
|  | ||||
|  | ||||
| ## License | ||||
|  | ||||
|     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. | ||||
|        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. | ||||
|  | ||||
| See also NOTICE file. | ||||
|  | ||||
							
								
								
									
										5
									
								
								erlang/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								erlang/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,5 +0,0 @@ | ||||
| MANIFEST | ||||
| *.beam | ||||
| .omakedb* | ||||
| *.omc | ||||
| *~ | ||||
| @@ -1,51 +0,0 @@ | ||||
| ######################################################################## | ||||
| # Permission is hereby granted, free of charge, to any person | ||||
| # obtaining a copy of this file, to deal in the File without | ||||
| # restriction, including without limitation the rights to use, | ||||
| # copy, modify, merge, publish, distribute, sublicense, and/or | ||||
| # sell copies of the File, and to permit persons to whom the | ||||
| # File is furnished to do so, subject to the following condition: | ||||
| # | ||||
| # THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
| # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||||
| # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||||
| # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||||
| # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||||
| # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR | ||||
| # THE USE OR OTHER DEALINGS IN THE FILE. | ||||
|  | ||||
| ######################################################################## | ||||
| # The standard OMakefile. | ||||
| # You will usually need to modify this file for your project. | ||||
|  | ||||
| ######################################################################## | ||||
| # Phony targets are scoped, so you probably want to declare them first. | ||||
| # | ||||
|  | ||||
| .PHONY: all clean test edoc dialyzer #install | ||||
|  | ||||
| ######################################################################## | ||||
| # Subdirectories. | ||||
| # You may want to include some subdirectories in this project. | ||||
| # If so, define the subdirectory targets and uncomment this section. | ||||
| # | ||||
|  | ||||
| .DEFAULT: msgpack.beam | ||||
|  | ||||
| msgpack.beam: msgpack.erl | ||||
| 	erlc -Wall +debug_info $< | ||||
|  | ||||
| msgpack.html: msgpack.erl | ||||
| 	erl -noshell -run edoc_run file $< | ||||
|  | ||||
| test: msgpack.beam | ||||
| 	erl -noshell -s msgpack test -s init stop | ||||
|  | ||||
| edoc: msgpack.erl | ||||
| 	erl -noshell -eval 'ok=edoc:files(["msgpack.erl"], [{dir, "edoc"}]).' -s init stop | ||||
|  | ||||
| dialyzer: msgpack.erl | ||||
| 	dialyzer --src $< | ||||
|  | ||||
| clean: | ||||
| 	-rm -f *.beam *.html | ||||
| @@ -1,45 +0,0 @@ | ||||
| ######################################################################## | ||||
| # Permission is hereby granted, free of charge, to any person | ||||
| # obtaining a copy of this file, to deal in the File without | ||||
| # restriction, including without limitation the rights to use, | ||||
| # copy, modify, merge, publish, distribute, sublicense, and/or | ||||
| # sell copies of the File, and to permit persons to whom the | ||||
| # File is furnished to do so, subject to the following condition: | ||||
| # | ||||
| # THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
| # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||||
| # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||||
| # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||||
| # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||||
| # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR | ||||
| # THE USE OR OTHER DEALINGS IN THE FILE. | ||||
|  | ||||
| ######################################################################## | ||||
| # The standard OMakeroot file. | ||||
| # You will not normally need to modify this file. | ||||
| # By default, your changes should be placed in the | ||||
| # OMakefile in this directory. | ||||
| # | ||||
| # If you decide to modify this file, note that it uses exactly | ||||
| # the same syntax as the OMakefile. | ||||
| # | ||||
|  | ||||
| # | ||||
| # Include the standard installed configuration files. | ||||
| # Any of these can be deleted if you are not using them, | ||||
| # but you probably want to keep the Common file. | ||||
| # | ||||
| open build/C | ||||
| open build/OCaml | ||||
| open build/LaTeX | ||||
|  | ||||
| # | ||||
| # The command-line variables are defined *after* the | ||||
| # standard configuration has been loaded. | ||||
| # | ||||
| DefineCommandVars() | ||||
|  | ||||
| # | ||||
| # Include the OMakefile in this directory. | ||||
| # | ||||
| .SUBDIRS: . | ||||
| @@ -1,9 +0,0 @@ | ||||
| MessagePack for Erlang | ||||
| ====================== | ||||
| Binary-based efficient object serialization library. | ||||
|  | ||||
| see wiki ( http://redmine.msgpack.org/projects/msgpack/wiki/QuickStartErlang ) for details | ||||
|  | ||||
| # Status | ||||
|  | ||||
| 0.1.0 released. | ||||
							
								
								
									
										4
									
								
								erlang/edoc/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								erlang/edoc/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,4 +0,0 @@ | ||||
| *.html | ||||
| *.css | ||||
| *.png | ||||
| edoc-info | ||||
| @@ -1,395 +0,0 @@ | ||||
| %% | ||||
| %% MessagePack for Erlang | ||||
| %% | ||||
| %% Copyright (C) 2009-2010 UENISHI Kota | ||||
| %% | ||||
| %%    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. | ||||
|  | ||||
|  | ||||
| %% @doc <a href="http://msgpack.org/">MessagePack</a> codec for Erlang. | ||||
| %% | ||||
| %%      APIs are almost compatible with <a href="http://redmine.msgpack.org/projects/msgpack/wiki/QuickStartC">C API</a> | ||||
| %%      except for buffering functions (both copying and zero-copying), which are unavailable. | ||||
| %% | ||||
| %%   <table border="1"> | ||||
| %%     <caption>Equivalence between Erlang and <a href="http://msgpack.sourceforge.jp/spec">Msgpack type</a> :</caption> | ||||
| %%     <tr><th>    erlang    </th><th>                            msgpack                                      </th></tr> | ||||
| %%     <tr><td> integer()    </td><td> pos_fixnum/neg_fixnum/uint8/uint16/uint32/uint64/int8/int16/int32/int64 </td></tr> | ||||
| %%     <tr><td> float()      </td><td> float/double                                                            </td></tr> | ||||
| %%     <tr><td> nil          </td><td> nil                                                                     </td></tr> | ||||
| %%     <tr><td> boolean()    </td><td> boolean                                                                 </td></tr> | ||||
| %%     <tr><td> binary()     </td><td> fix_raw/raw16/raw32                                                     </td></tr> | ||||
| %%     <tr><td> list()       </td><td> fix_array/array16/array32                                               </td></tr> | ||||
| %%     <tr><td> {proplist()} </td><td> fix_map/map16/map32                                                     </td></tr> | ||||
| %%   </table> | ||||
| %% @end | ||||
|  | ||||
| -module(msgpack). | ||||
| -author('kuenishi+msgpack@gmail.com'). | ||||
|  | ||||
| -export([pack/1, unpack/1, unpack_all/1]). | ||||
|  | ||||
| % @type msgpack_term() = [msgpack_term()] | ||||
| %                      | {[{msgpack_term(),msgpack_term()}]} | ||||
| %                      | integer() | float() | binary(). | ||||
| % Erlang representation of msgpack data. | ||||
| -type msgpack_term() :: [msgpack_term()] | ||||
| 		      | {[{msgpack_term(),msgpack_term()}]} | ||||
| 		      | integer() | float() | binary(). | ||||
|  | ||||
|  | ||||
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||
| % external APIs | ||||
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||
|  | ||||
| % @doc Encode an erlang term into an msgpack binary. | ||||
| %      Returns {error, {badarg, term()}} if the input is illegal. | ||||
| % @spec pack(Term::msgpack_term()) -> binary() | {error, {badarg, term()}} | ||||
| -spec pack(Term::msgpack_term()) -> binary() | {error, {badarg, term()}}. | ||||
| pack(Term)-> | ||||
|     try | ||||
| 	pack_(Term) | ||||
|     catch | ||||
| 	throw:Exception -> | ||||
| 	    {error, Exception} | ||||
|     end. | ||||
|  | ||||
| % @doc Decode an msgpack binary into an erlang term. | ||||
| %      It only decodes the first msgpack packet contained in the binary; the rest is returned as is. | ||||
| %      Returns {error, {badarg, term()}} if the input is corrupted. | ||||
| %      Returns {error, incomplete} if the input is not a full msgpack packet (caller should gather more data and try again). | ||||
| % @spec unpack(Bin::binary()) -> {msgpack_term(), binary()} | {error, incomplete} | {error, {badarg, term()}} | ||||
| -spec unpack(Bin::binary()) -> {msgpack_term(), binary()} | {error, incomplete} | {error, {badarg, term()}}. | ||||
| unpack(Bin) when is_binary(Bin) -> | ||||
|     try | ||||
| 	unpack_(Bin) | ||||
|     catch | ||||
| 	throw:Exception -> | ||||
| 	    {error, Exception} | ||||
|     end; | ||||
| unpack(Other) -> | ||||
|     {error, {badarg, Other}}. | ||||
|  | ||||
| % @doc Decode an msgpack binary into an erlang terms. | ||||
| %      It only decodes ALL msgpack packets contained in the binary. No packets should not remain. | ||||
| %      Returns {error, {badarg, term()}} if the input is corrupted. | ||||
| %      Returns {error, incomplete} if the input is not a full msgpack packet (caller should gather more data and try again). | ||||
| % @spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}} | ||||
| -spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}}. | ||||
| unpack_all(Data)-> | ||||
|     try | ||||
| 	unpack_all_(Data) | ||||
|     catch | ||||
| 	throw:Exception -> | ||||
| 	    {error, Exception} | ||||
|     end. | ||||
| unpack_all_(Data)-> | ||||
|     case unpack_(Data) of | ||||
| 	{ Term, <<>> } -> | ||||
| 	    [Term]; | ||||
| 	{ Term, Binary } when is_binary(Binary) -> | ||||
| 	    [Term|unpack_all_(Binary)] | ||||
|     end. | ||||
|  | ||||
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||
| % internal APIs | ||||
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||
|  | ||||
| % pack them all | ||||
| -spec pack_(msgpack_term()) -> binary() | no_return(). | ||||
| pack_(I) when is_integer(I) andalso I < 0 -> | ||||
|     pack_int_(I); | ||||
| pack_(I) when is_integer(I) -> | ||||
|     pack_uint_(I); | ||||
| pack_(F) when is_float(F) -> | ||||
|     pack_double(F); | ||||
| pack_(nil) -> | ||||
|     << 16#C0:8 >>; | ||||
| pack_(true) -> | ||||
|     << 16#C3:8 >>; | ||||
| pack_(false) -> | ||||
|     << 16#C2:8 >>; | ||||
| pack_(Bin) when is_binary(Bin) -> | ||||
|     pack_raw(Bin); | ||||
| pack_(List)  when is_list(List) -> | ||||
|     pack_array(List); | ||||
| pack_({Map}) when is_list(Map) -> | ||||
|     pack_map(Map); | ||||
| pack_(Other) -> | ||||
|     throw({badarg, Other}). | ||||
|  | ||||
|  | ||||
| -spec pack_uint_(non_neg_integer()) -> binary(). | ||||
| % positive fixnum | ||||
| pack_uint_(N) when N < 128 -> | ||||
|     << 2#0:1, N:7 >>; | ||||
| % uint 8 | ||||
| pack_uint_(N) when N < 256 -> | ||||
|     << 16#CC:8, N:8 >>; | ||||
| % uint 16 | ||||
| pack_uint_(N) when N < 65536 -> | ||||
|     << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>; | ||||
| % uint 32 | ||||
| pack_uint_(N) when N < 16#FFFFFFFF-> | ||||
|     << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>; | ||||
| % uint 64 | ||||
| pack_uint_(N) -> | ||||
|     << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>. | ||||
|  | ||||
| -spec pack_int_(integer()) -> binary(). | ||||
| % negative fixnum | ||||
| pack_int_(N) when N >= -32-> | ||||
|     << 2#111:3, N:5 >>; | ||||
| % int 8 | ||||
| pack_int_(N) when N > -128 -> | ||||
|     << 16#D0:8, N:8/big-signed-integer-unit:1 >>; | ||||
| % int 16 | ||||
| pack_int_(N) when N > -32768 -> | ||||
|     << 16#D1:8, N:16/big-signed-integer-unit:1 >>; | ||||
| % int 32 | ||||
| pack_int_(N) when N > -16#FFFFFFFF -> | ||||
|     << 16#D2:8, N:32/big-signed-integer-unit:1 >>; | ||||
| % int 64 | ||||
| pack_int_(N) -> | ||||
|     << 16#D3:8, N:64/big-signed-integer-unit:1 >>. | ||||
|  | ||||
|  | ||||
| -spec pack_double(float()) -> binary(). | ||||
| % float : erlang's float is always IEEE 754 64bit format. | ||||
| % pack_float(F) when is_float(F)-> | ||||
| %    << 16#CA:8, F:32/big-float-unit:1 >>. | ||||
| %    pack_double(F). | ||||
| % double | ||||
| pack_double(F) -> | ||||
|     << 16#CB:8, F:64/big-float-unit:1 >>. | ||||
|  | ||||
|  | ||||
| -spec pack_raw(binary()) -> binary(). | ||||
| % raw bytes | ||||
| pack_raw(Bin) -> | ||||
|     case byte_size(Bin) of | ||||
| 	Len when Len < 6-> | ||||
| 	    << 2#101:3, Len:5, Bin/binary >>; | ||||
| 	Len when Len < 16#10000 -> % 65536 | ||||
| 	    << 16#DA:8, Len:16/big-unsigned-integer-unit:1, Bin/binary >>; | ||||
| 	Len -> | ||||
| 	    << 16#DB:8, Len:32/big-unsigned-integer-unit:1, Bin/binary >> | ||||
|     end. | ||||
|  | ||||
|  | ||||
| -spec pack_array([msgpack_term()]) -> binary() | no_return(). | ||||
| % list | ||||
| pack_array(L) -> | ||||
|     case length(L) of | ||||
|  	Len when Len < 16 -> | ||||
|  	    << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L, <<>>))/binary >>; | ||||
| 	Len when Len < 16#10000 -> % 65536 | ||||
| 	    << 16#DC:8, Len:16/big-unsigned-integer-unit:1, (pack_array_(L, <<>>))/binary >>; | ||||
| 	Len -> | ||||
| 	    << 16#DD:8, Len:32/big-unsigned-integer-unit:1, (pack_array_(L, <<>>))/binary >> | ||||
|     end. | ||||
|  | ||||
| pack_array_([], Acc) -> Acc; | ||||
| pack_array_([Head|Tail], Acc) -> | ||||
|     pack_array_(Tail, <<Acc/binary,  (pack_(Head))/binary>>). | ||||
|  | ||||
| % Users SHOULD NOT send too long list: this uses lists:reverse/1 | ||||
| -spec unpack_array_(binary(), non_neg_integer(), [msgpack_term()]) -> {[msgpack_term()], binary()} | no_return(). | ||||
| unpack_array_(Bin, 0,   Acc) -> {lists:reverse(Acc), Bin}; | ||||
| unpack_array_(Bin, Len, Acc) -> | ||||
|     {Term, Rest} = unpack_(Bin), | ||||
|     unpack_array_(Rest, Len-1, [Term|Acc]). | ||||
|  | ||||
|  | ||||
| -spec pack_map(M::[{msgpack_term(),msgpack_term()}]) -> binary() | no_return(). | ||||
| pack_map(M)-> | ||||
|     case length(M) of | ||||
| 	Len when Len < 16 -> | ||||
| 	    << 2#1000:4, Len:4/integer-unit:1, (pack_map_(M, <<>>))/binary >>; | ||||
| 	Len when Len < 16#10000 -> % 65536 | ||||
| 	    << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>; | ||||
| 	Len -> | ||||
| 	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >> | ||||
|     end. | ||||
|  | ||||
| pack_map_([], Acc) -> Acc; | ||||
| pack_map_([{Key,Value}|Tail], Acc) -> | ||||
|     pack_map_(Tail, << Acc/binary, (pack_(Key))/binary, (pack_(Value))/binary>>). | ||||
|  | ||||
| % Users SHOULD NOT send too long list: this uses lists:reverse/1 | ||||
| -spec unpack_map_(binary(), non_neg_integer(), [{msgpack_term(), msgpack_term()}]) -> | ||||
| 			 {{[{msgpack_term(), msgpack_term()}]}, binary()} | no_return(). | ||||
| unpack_map_(Bin, 0,   Acc) -> {{lists:reverse(Acc)}, Bin}; | ||||
| unpack_map_(Bin, Len, Acc) -> | ||||
|     {Key, Rest} = unpack_(Bin), | ||||
|     {Value, Rest2} = unpack_(Rest), | ||||
|     unpack_map_(Rest2, Len-1, [{Key,Value}|Acc]). | ||||
|  | ||||
| % unpack them all | ||||
| -spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | no_return(). | ||||
| unpack_(Bin) -> | ||||
|     case Bin of | ||||
| % ATOMS | ||||
| 	<<16#C0, Rest/binary>> -> {nil, Rest}; | ||||
| 	<<16#C2, Rest/binary>> -> {false, Rest}; | ||||
| 	<<16#C3, Rest/binary>> -> {true, Rest}; | ||||
| % Floats | ||||
| 	<<16#CA, V:32/float-unit:1, Rest/binary>> ->   {V, Rest}; | ||||
| 	<<16#CB, V:64/float-unit:1, Rest/binary>> ->   {V, Rest}; | ||||
| % Unsigned integers | ||||
| 	<<16#CC, V:8/unsigned-integer, Rest/binary>> ->             {V, Rest}; | ||||
| 	<<16#CD, V:16/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest}; | ||||
| 	<<16#CE, V:32/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest}; | ||||
| 	<<16#CF, V:64/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest}; | ||||
| % Signed integers | ||||
| 	<<16#D0, V:8/signed-integer, Rest/binary>> ->             {V, Rest}; | ||||
| 	<<16#D1, V:16/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest}; | ||||
| 	<<16#D2, V:32/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest}; | ||||
| 	<<16#D3, V:64/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest}; | ||||
| % Raw bytes | ||||
| 	<<16#DA, L:16/unsigned-integer-unit:1, V:L/binary, Rest/binary>> -> {V, Rest}; | ||||
| 	<<16#DB, L:32/unsigned-integer-unit:1, V:L/binary, Rest/binary>> -> {V, Rest}; | ||||
| % Arrays | ||||
| 	<<16#DC, L:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, L, []); | ||||
| 	<<16#DD, L:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, L, []); | ||||
| % Maps | ||||
| 	<<16#DE, L:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, L, []); | ||||
| 	<<16#DF, L:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, L, []); | ||||
|  | ||||
| % Tag-encoded lengths (kept last, for speed) | ||||
| 	<<0:1, V:7, Rest/binary>> ->                 {V, Rest};                  % positive int | ||||
| 	<<2#111:3, V:5, Rest/binary>> ->             {V - 2#100000, Rest};       % negative int | ||||
| 	<<2#101:3, L:5, V:L/binary, Rest/binary>> -> {V, Rest};                  % raw bytes | ||||
| 	<<2#1001:4, L:4, Rest/binary>> ->            unpack_array_(Rest, L, []); % array | ||||
| 	<<2#1000:4, L:4, Rest/binary>> ->            unpack_map_(Rest, L, []);   % map | ||||
|  | ||||
| % Invalid data | ||||
| 	<<F, R/binary>> when F==16#C1; | ||||
| 	                     F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9; | ||||
| 	                     F==16#D4; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9 -> | ||||
| 	    throw({badarg, <<F, R/binary>>}); | ||||
| % Incomplete data (we've covered every complete/invalid case; anything left is incomplete) | ||||
| 	_ -> | ||||
| 	    throw(incomplete) | ||||
|     end. | ||||
|  | ||||
|  | ||||
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||
| % unit tests | ||||
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||
| -include_lib("eunit/include/eunit.hrl"). | ||||
| -ifdef(EUNIT). | ||||
|  | ||||
| compare_all([], [])-> ok; | ||||
| compare_all([],  R)-> {toomuchrhs, R}; | ||||
| compare_all(L,  [])-> {toomuchlhs, L}; | ||||
| compare_all([LH|LTL], [RH|RTL]) -> | ||||
|     ?assertEqual(LH, RH), | ||||
|     compare_all(LTL, RTL). | ||||
|  | ||||
| port_receive(Port) -> | ||||
|     port_receive(Port, <<>>). | ||||
| port_receive(Port, Acc) -> | ||||
|     receive | ||||
|         {Port, {data, Data}} -> port_receive(Port, <<Acc/binary, Data/binary>>); | ||||
|         {Port, eof} -> Acc | ||||
|     after 1000 -> Acc | ||||
|     end. | ||||
|  | ||||
| test_([]) -> 0; | ||||
| test_([Term|Rest])-> | ||||
|     Pack = msgpack:pack(Term), | ||||
|     ?assertEqual({Term, <<>>}, msgpack:unpack( Pack )), | ||||
|     1+test_(Rest). | ||||
|  | ||||
| test_data()-> | ||||
|     [true, false, nil, | ||||
|      0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF, | ||||
|      -1, -23, -512, -1230, -567898, -16#FFFFFFFFFF, | ||||
|      123.123, -234.4355, 1.0e-34, 1.0e64, | ||||
|      [23, 234, 0.23], | ||||
|      <<"hogehoge">>, <<"243546rf7g68h798j", 0, 23, 255>>, | ||||
|      <<"hoasfdafdas][">>, | ||||
|      [0,42, <<"sum">>, [1,2]], [1,42, nil, [3]], | ||||
|      -234, -40000, -16#10000000, -16#100000000, | ||||
|      42 | ||||
|     ]. | ||||
|  | ||||
| basic_test()-> | ||||
|     Tests = test_data(), | ||||
|     Passed = test_(Tests), | ||||
|     Passed = length(Tests). | ||||
|  | ||||
| port_test()-> | ||||
|     Tests = test_data(), | ||||
|     ?assertEqual({[Tests],<<>>}, msgpack:unpack(msgpack:pack([Tests]))), | ||||
|  | ||||
|     Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary, eof]), | ||||
|     true = port_command(Port, msgpack:pack(Tests)), | ||||
|     ?assertEqual({Tests, <<>>}, msgpack:unpack(port_receive(Port))), | ||||
|     port_close(Port). | ||||
|  | ||||
| test_p(Len,Term,OrigBin,Len) -> | ||||
|     {Term, <<>>}=msgpack:unpack(OrigBin); | ||||
| test_p(I,_,OrigBin,Len) when I < Len-> | ||||
|     <<Bin:I/binary, _/binary>> = OrigBin, | ||||
|     ?assertEqual({error,incomplete}, msgpack:unpack(Bin)). | ||||
|  | ||||
| partial_test()-> % error handling test. | ||||
|     Term = lists:seq(0, 45), | ||||
|     Bin=msgpack:pack(Term), | ||||
|     BinLen = byte_size(Bin), | ||||
|     [test_p(X, Term, Bin, BinLen) || X <- lists:seq(0,BinLen)]. | ||||
|  | ||||
| long_test()-> | ||||
|     Longer = lists:seq(0, 655), | ||||
|     {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)). | ||||
|  | ||||
| map_test()-> | ||||
|     Ints = lists:seq(0, 65), | ||||
|     Map = {[ {X, X*2} || X <- Ints ] ++ [{<<"hage">>, 324}, {43542, [nil, true, false]}]}, | ||||
|     {Map2, <<>>} = msgpack:unpack(msgpack:pack(Map)), | ||||
|     ?assertEqual(Map, Map2), | ||||
|     ok. | ||||
|  | ||||
| unknown_test()-> | ||||
|     Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary, eof]), | ||||
|     Tests = [0, 1, 2, 123, 512, 1230, 678908, | ||||
| 	     -1, -23, -512, -1230, -567898, | ||||
| 	     <<"hogehoge">>, <<"243546rf7g68h798j">>, | ||||
| 	     123.123, | ||||
| 	     -234.4355, 1.0e-34, 1.0e64, | ||||
| 	     [23, 234, 0.23], | ||||
| 	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]], | ||||
| 	     {[{1,2},{<<"hoge">>,nil}]}, % map | ||||
| 	     -234, -50000, | ||||
| 	     42 | ||||
| 	    ], | ||||
|     ?assertEqual(ok, compare_all(Tests, msgpack:unpack_all(port_receive(Port)))), | ||||
|     port_close(Port). | ||||
|  | ||||
| other_test()-> | ||||
|     ?assertEqual({error,incomplete},msgpack:unpack(<<>>)). | ||||
|  | ||||
| benchmark_test()-> | ||||
|     Data=[test_data() || _ <- lists:seq(0, 10000)], | ||||
|     S=?debugTime("  serialize", msgpack:pack(Data)), | ||||
|     {Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)), | ||||
|     ?debugFmt("for ~p KB test data.", [byte_size(S) div 1024]). | ||||
|  | ||||
| error_test()-> | ||||
|     ?assertEqual({error,{badarg, atom}}, msgpack:pack(atom)), | ||||
|     Term = {"hoge", "hage", atom}, | ||||
|     ?assertEqual({error,{badarg, Term}}, msgpack:pack(Term)). | ||||
|  | ||||
| -endif. | ||||
| @@ -1,65 +0,0 @@ | ||||
| begin | ||||
| require 'rubygems' | ||||
| rescue LoadError | ||||
| end | ||||
| require 'msgpack' | ||||
|  | ||||
| def usage | ||||
| 	puts <<EOF | ||||
| Usage: #{$0} [out-file] | ||||
|  | ||||
| This tool is for testing of accepting MessagePack random-term. | ||||
| This does following behavior: | ||||
|  | ||||
|   1. serializes the objects in this file, using Ruby implementation | ||||
|      of MessagePack (Note that Ruby implementation is considered valid) | ||||
|   2. Writes the serialized binaries into <out-file> (default: stdout) | ||||
|  | ||||
| EOF | ||||
| 	exit 1 | ||||
| end | ||||
|  | ||||
| code = 1 | ||||
| outio = $stdout | ||||
|  | ||||
| if ARGV.length > 2 | ||||
| 	usage | ||||
| end | ||||
|  | ||||
| if fname = ARGV[0] | ||||
| 	unless fname == "-" | ||||
| 		begin | ||||
| 			outio = File.open(fname, "w") | ||||
| 		rescue | ||||
| 			puts "can't open output file: #{$!}" | ||||
| 			exit 1 | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
|  | ||||
| objs = [0, 1, 2, 123, 512, 1230, 678908, | ||||
|         -1, -23, -512, -1230, -567898, | ||||
|         "hogehoge", "243546rf7g68h798j", | ||||
|         123.123, | ||||
|        -234.4355, 1.0e-34, 1.0e64, | ||||
|         [23, 234, 0.23], | ||||
|         [0,42,"sum", [1,2]], [1,42, nil, [3]], | ||||
|         { 1 => 2, "hoge" => nil }, | ||||
|         -234, -50000, | ||||
|         42 | ||||
|        ] | ||||
| begin | ||||
|   objs.each do |obj| | ||||
|     outio.write MessagePack.pack(obj) | ||||
|     outio.flush | ||||
|   end | ||||
| rescue EOFError | ||||
|   code=0 | ||||
| rescue | ||||
|   $stderr.puts $! | ||||
|   code=1 | ||||
| end | ||||
|  | ||||
| outio.close | ||||
| exit code | ||||
|  | ||||
| @@ -17,7 +17,7 @@ namespace myprotocol { | ||||
|  | ||||
| 	struct Put : define< tuple<uint32_t, std::string, raw_ref> > { | ||||
| 		Put() { } | ||||
| 		Put(uint32_t f, const std::string& k, const char* valref, size_t vallen) : | ||||
| 		Put(uint32_t f, const std::string& k, const char* valref, uint32_t vallen) : | ||||
| 			define_type(msgpack_type( f, k, raw_ref(valref,vallen) )) { } | ||||
| 		uint32_t&    flags() { return get<0>(); } | ||||
| 		std::string& key()   { return get<1>(); } | ||||
|   | ||||
| @@ -1,5 +0,0 @@ | ||||
| require 'msgpack' | ||||
|  | ||||
| serialized = [1, -1, true, false, nil, {"key" => "value"}].to_msgpack | ||||
| p MessagePack.unpack(serialized) | ||||
|  | ||||
							
								
								
									
										36
									
								
								example/speed_test_uint32_array.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								example/speed_test_uint32_array.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| #include <msgpack.h> | ||||
| #include <assert.h> | ||||
|  | ||||
| void test() | ||||
| { | ||||
| 	size_t size = 10000000; | ||||
| 	msgpack_sbuffer buf; | ||||
| 	msgpack_sbuffer_init(&buf); | ||||
|  | ||||
| 	msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write); | ||||
|  | ||||
| 	msgpack_pack_array(pk, size); | ||||
| 	{ | ||||
| 		int idx = 0; | ||||
| 		for (; idx < size; ++idx) | ||||
| 			msgpack_pack_uint32(pk, 1); | ||||
| 	} | ||||
| 	msgpack_packer_free(pk); | ||||
|  | ||||
|  | ||||
| 	size_t upk_pos = 0; | ||||
| 	msgpack_unpacked msg; | ||||
| 	msgpack_unpacked_init(&msg); | ||||
|  | ||||
| 	while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) { | ||||
| 	} | ||||
|  | ||||
| 	msgpack_sbuffer_destroy(&buf); | ||||
| } | ||||
|  | ||||
| int main(int argc, char **argv) | ||||
| { | ||||
| 	int i = 0; | ||||
| 	for (; i < 10; ++i) test(); | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										37
									
								
								example/speed_test_uint64_array.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								example/speed_test_uint64_array.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| #include <msgpack.h> | ||||
| #include <assert.h> | ||||
|  | ||||
| void test() | ||||
| { | ||||
| 	uint64_t test_u64 = 0xFFF0000000000001LL; | ||||
| 	size_t size = 10000000; | ||||
| 	msgpack_sbuffer buf; | ||||
| 	msgpack_sbuffer_init(&buf); | ||||
|  | ||||
| 	msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write); | ||||
|  | ||||
| 	msgpack_pack_array(pk, size); | ||||
| 	{ | ||||
| 		int idx = 0; | ||||
| 		for (; idx < size; ++idx) | ||||
| 			msgpack_pack_uint64(pk, test_u64); | ||||
| 	} | ||||
| 	msgpack_packer_free(pk); | ||||
|  | ||||
|  | ||||
| 	size_t upk_pos = 0; | ||||
| 	msgpack_unpacked msg; | ||||
| 	msgpack_unpacked_init(&msg); | ||||
|  | ||||
| 	while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) { | ||||
| 	} | ||||
|  | ||||
| 	msgpack_sbuffer_destroy(&buf); | ||||
| } | ||||
|  | ||||
| int main(int argc, char **argv) | ||||
| { | ||||
| 	int i = 0; | ||||
| 	for (; i < 10; ++i) test(); | ||||
| 	return 0; | ||||
| } | ||||
| @@ -1,72 +0,0 @@ | ||||
| require 'msgpack' | ||||
|  | ||||
| class Server | ||||
| 	def initialize(sock) | ||||
| 		@sock = sock | ||||
| 		@pk = MessagePack::Unpacker.new | ||||
| 		@buffer = '' | ||||
| 		@nread = 0 | ||||
| 	end | ||||
|  | ||||
| 	def run | ||||
| 		while true | ||||
| 			begin | ||||
| 				data = @sock.sysread(1024) | ||||
| 			rescue | ||||
| 				puts "connection closed (#{$!})" | ||||
| 				return | ||||
| 			end | ||||
| 			receive_data(data) | ||||
| 		end | ||||
| 	end | ||||
|  | ||||
| 	private | ||||
| 	def receive_data(data) | ||||
| 		@buffer << data | ||||
|  | ||||
| 		while true | ||||
| 			@nread = @pk.execute(@buffer, @nread) | ||||
|  | ||||
| 			if @pk.finished? | ||||
| 				msg = @pk.data | ||||
| 				process_message(msg) | ||||
|  | ||||
| 				@pk.reset | ||||
| 				@buffer.slice!(0, @nread) | ||||
| 				@nread = 0 | ||||
|  | ||||
| 				next unless @buffer.empty? | ||||
| 			end | ||||
|  | ||||
| 			break | ||||
| 		end | ||||
|  | ||||
| 		if @buffer.length > 10*1024*1024 | ||||
| 			raise "message is too large" | ||||
| 		end | ||||
|  | ||||
| 	rescue | ||||
| 		puts "error while processing client packet: #{$!}" | ||||
| 	end | ||||
|  | ||||
| 	def process_message(msg) | ||||
| 		puts "message reached: #{msg.inspect}" | ||||
| 	end | ||||
| end | ||||
|  | ||||
|  | ||||
| rpipe, wpipe = IO.pipe | ||||
|  | ||||
| # run server thread | ||||
| thread = Thread.new(Server.new(rpipe)) {|srv| | ||||
| 	srv.run | ||||
| } | ||||
|  | ||||
| # client thread: | ||||
| wpipe.write ["put", "apple", "red"].to_msgpack | ||||
| wpipe.write ["put", "lemon", "yellow"].to_msgpack | ||||
| wpipe.write ["get", "apple"].to_msgpack | ||||
| wpipe.close | ||||
|  | ||||
| thread.join | ||||
|  | ||||
							
								
								
									
										11
									
								
								go/Makefile
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								go/Makefile
									
									
									
									
									
								
							| @@ -1,11 +0,0 @@ | ||||
| include $(GOROOT)/src/Make.inc | ||||
|  | ||||
| TARG=msgpack | ||||
|  | ||||
| GOFILES=pack.go unpack.go | ||||
|  | ||||
| include $(GOROOT)/src/Make.pkg | ||||
|  | ||||
| %: install %.go | ||||
| 	$(GC) $*.go | ||||
| 	$(LD) -o $@ $*.$O | ||||
| @@ -1,218 +0,0 @@ | ||||
| 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
									
									
									
									
									
								
							
							
						
						
									
										591
									
								
								go/pack.go
									
									
									
									
									
								
							| @@ -1,591 +0,0 @@ | ||||
| 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
									
									
									
									
									
								
							
							
						
						
									
										288
									
								
								go/unpack.go
									
									
									
									
									
								
							| @@ -1,288 +0,0 @@ | ||||
| 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,24 +0,0 @@ | ||||
| Copyright (c) 2009-2010, Hideyuki Tanaka | ||||
| All rights reserved. | ||||
|  | ||||
| Redistribution and use in source and binary forms, with or without | ||||
| modification, are permitted provided that the following conditions are met: | ||||
|     * Redistributions of source code must retain the above copyright | ||||
|       notice, this list of conditions and the following disclaimer. | ||||
|     * Redistributions in binary form must reproduce the above copyright | ||||
|       notice, this list of conditions and the following disclaimer in the | ||||
|       documentation and/or other materials provided with the distribution. | ||||
|     * Neither the name of the Hideyuki Tanaka nor the | ||||
|       names of its contributors may be used to endorse or promote products | ||||
|       derived from this software without specific prior written permission. | ||||
|  | ||||
| THIS SOFTWARE IS PROVIDED BY Hideyuki Tanaka ''AS IS'' AND ANY | ||||
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||||
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||
| DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||||
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||||
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||||
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||||
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||||
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| @@ -1,3 +0,0 @@ | ||||
| #!/usr/bin/env runhaskell | ||||
| > import Distribution.Simple | ||||
| > main = defaultMain | ||||
| @@ -1,50 +0,0 @@ | ||||
| Name:               msgpack | ||||
| Version:            0.6.1.1 | ||||
| Synopsis:           A Haskell implementation of MessagePack | ||||
| Description: | ||||
|   A Haskell implementation of MessagePack <http://msgpack.org/> | ||||
|  | ||||
| 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.Assoc | ||||
|     Data.MessagePack.Pack | ||||
|     Data.MessagePack.Unpack | ||||
|     Data.MessagePack.Object | ||||
|     Data.MessagePack.Derive | ||||
|  | ||||
|   Other-modules: | ||||
|     Data.MessagePack.Internal.Utf8 | ||||
|  | ||||
| Source-repository head | ||||
|   Type:     git | ||||
|   Location: git://github.com/msgpack/msgpack.git | ||||
| @@ -1,27 +0,0 @@ | ||||
| -------------------------------------------------------------------- | ||||
| -- | | ||||
| -- Module    : Data.MessagePack | ||||
| -- Copyright : (c) Hideyuki Tanaka, 2009-2010 | ||||
| -- License   : BSD3 | ||||
| -- | ||||
| -- Maintainer:  tanaka.hideyuki@gmail.com | ||||
| -- Stability :  experimental | ||||
| -- Portability: portable | ||||
| -- | ||||
| -- Simple interface to pack and unpack MessagePack data. | ||||
| -- | ||||
| -------------------------------------------------------------------- | ||||
|  | ||||
| module Data.MessagePack( | ||||
|   module Data.MessagePack.Assoc, | ||||
|   module Data.MessagePack.Pack, | ||||
|   module Data.MessagePack.Unpack, | ||||
|   module Data.MessagePack.Object, | ||||
|   module Data.MessagePack.Derive, | ||||
|   ) where | ||||
|  | ||||
| import Data.MessagePack.Assoc | ||||
| import Data.MessagePack.Pack | ||||
| import Data.MessagePack.Unpack | ||||
| import Data.MessagePack.Object | ||||
| import Data.MessagePack.Derive | ||||
| @@ -1,28 +0,0 @@ | ||||
| {-# 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,106 +0,0 @@ | ||||
| {-# 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,28 +0,0 @@ | ||||
| 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,334 +0,0 @@ | ||||
| {-# 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 | ||||
|  | ||||
| @@ -1,204 +0,0 @@ | ||||
| {-# 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,323 +0,0 @@ | ||||
| {-# 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,21 +0,0 @@ | ||||
| {-# Language OverloadedStrings #-} | ||||
|  | ||||
| import Control.Monad.IO.Class | ||||
| import qualified Data.ByteString as B | ||||
| import Data.MessagePack | ||||
|  | ||||
| main = do | ||||
|   sb <- return $ packToString $ do | ||||
|     put [1,2,3::Int] | ||||
|     put (3.14 :: Double) | ||||
|     put ("Hoge" :: B.ByteString) | ||||
|    | ||||
|   print sb | ||||
|    | ||||
|   r <- unpackFromString sb $ do | ||||
|     arr <- get | ||||
|     dbl <- get | ||||
|     str <- get | ||||
|     return (arr :: [Int], dbl :: Double, str :: B.ByteString) | ||||
|    | ||||
|   print r | ||||
| @@ -1,70 +0,0 @@ | ||||
| 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 | ||||
|  | ||||
| instance Arbitrary a => Arbitrary (Assoc a) where | ||||
|   arbitrary = liftM Assoc arbitrary | ||||
|  | ||||
| mid :: (Packable a, Unpackable a) => a -> a | ||||
| mid = unpack . pack | ||||
|  | ||||
| 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)] | ||||
|  | ||||
| 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 | ||||
|     ] | ||||
|   ] | ||||
|  | ||||
| main = defaultMain tests | ||||
| @@ -1,43 +0,0 @@ | ||||
| {-# 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 () | ||||
							
								
								
									
										4
									
								
								java/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								java/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,4 +0,0 @@ | ||||
| target | ||||
| .project | ||||
| .classpath | ||||
| *~ | ||||
| @@ -1,13 +0,0 @@ | ||||
| #Tue Apr 12 19:14:17 JST 2011 | ||||
| eclipse.preferences.version=1 | ||||
| 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 | ||||
| @@ -1,9 +0,0 @@ | ||||
| #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 +0,0 @@ | ||||
| FURUHASHI Sadayuki <frsyuki _at_ users.sourceforge.jp> | ||||
							
								
								
									
										103
									
								
								java/CHANGES.txt
									
									
									
									
									
								
							
							
						
						
									
										103
									
								
								java/CHANGES.txt
									
									
									
									
									
								
							| @@ -1,103 +0,0 @@ | ||||
|  | ||||
| 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. | ||||
|  | ||||
|   BUG FIXES | ||||
|     Zero-length Array and Map is deserialized as List and Map, instead of the | ||||
|     array of the Object. | ||||
|  | ||||
|     fixed the bug around Packer.packByte(). | ||||
							
								
								
									
										202
									
								
								java/LICENSE.txt
									
									
									
									
									
								
							
							
						
						
									
										202
									
								
								java/LICENSE.txt
									
									
									
									
									
								
							| @@ -1,202 +0,0 @@ | ||||
|  | ||||
|                                  Apache License | ||||
|                            Version 2.0, January 2004 | ||||
|                         http://www.apache.org/licenses/ | ||||
|  | ||||
|    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||||
|  | ||||
|    1. Definitions. | ||||
|  | ||||
|       "License" shall mean the terms and conditions for use, reproduction, | ||||
|       and distribution as defined by Sections 1 through 9 of this document. | ||||
|  | ||||
|       "Licensor" shall mean the copyright owner or entity authorized by | ||||
|       the copyright owner that is granting the License. | ||||
|  | ||||
|       "Legal Entity" shall mean the union of the acting entity and all | ||||
|       other entities that control, are controlled by, or are under common | ||||
|       control with that entity. For the purposes of this definition, | ||||
|       "control" means (i) the power, direct or indirect, to cause the | ||||
|       direction or management of such entity, whether by contract or | ||||
|       otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||||
|       outstanding shares, or (iii) beneficial ownership of such entity. | ||||
|  | ||||
|       "You" (or "Your") shall mean an individual or Legal Entity | ||||
|       exercising permissions granted by this License. | ||||
|  | ||||
|       "Source" form shall mean the preferred form for making modifications, | ||||
|       including but not limited to software source code, documentation | ||||
|       source, and configuration files. | ||||
|  | ||||
|       "Object" form shall mean any form resulting from mechanical | ||||
|       transformation or translation of a Source form, including but | ||||
|       not limited to compiled object code, generated documentation, | ||||
|       and conversions to other media types. | ||||
|  | ||||
|       "Work" shall mean the work of authorship, whether in Source or | ||||
|       Object form, made available under the License, as indicated by a | ||||
|       copyright notice that is included in or attached to the work | ||||
|       (an example is provided in the Appendix below). | ||||
|  | ||||
|       "Derivative Works" shall mean any work, whether in Source or Object | ||||
|       form, that is based on (or derived from) the Work and for which the | ||||
|       editorial revisions, annotations, elaborations, or other modifications | ||||
|       represent, as a whole, an original work of authorship. For the purposes | ||||
|       of this License, Derivative Works shall not include works that remain | ||||
|       separable from, or merely link (or bind by name) to the interfaces of, | ||||
|       the Work and Derivative Works thereof. | ||||
|  | ||||
|       "Contribution" shall mean any work of authorship, including | ||||
|       the original version of the Work and any modifications or additions | ||||
|       to that Work or Derivative Works thereof, that is intentionally | ||||
|       submitted to Licensor for inclusion in the Work by the copyright owner | ||||
|       or by an individual or Legal Entity authorized to submit on behalf of | ||||
|       the copyright owner. For the purposes of this definition, "submitted" | ||||
|       means any form of electronic, verbal, or written communication sent | ||||
|       to the Licensor or its representatives, including but not limited to | ||||
|       communication on electronic mailing lists, source code control systems, | ||||
|       and issue tracking systems that are managed by, or on behalf of, the | ||||
|       Licensor for the purpose of discussing and improving the Work, but | ||||
|       excluding communication that is conspicuously marked or otherwise | ||||
|       designated in writing by the copyright owner as "Not a Contribution." | ||||
|  | ||||
|       "Contributor" shall mean Licensor and any individual or Legal Entity | ||||
|       on behalf of whom a Contribution has been received by Licensor and | ||||
|       subsequently incorporated within the Work. | ||||
|  | ||||
|    2. Grant of Copyright License. Subject to the terms and conditions of | ||||
|       this License, each Contributor hereby grants to You a perpetual, | ||||
|       worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||||
|       copyright license to reproduce, prepare Derivative Works of, | ||||
|       publicly display, publicly perform, sublicense, and distribute the | ||||
|       Work and such Derivative Works in Source or Object form. | ||||
|  | ||||
|    3. Grant of Patent License. Subject to the terms and conditions of | ||||
|       this License, each Contributor hereby grants to You a perpetual, | ||||
|       worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||||
|       (except as stated in this section) patent license to make, have made, | ||||
|       use, offer to sell, sell, import, and otherwise transfer the Work, | ||||
|       where such license applies only to those patent claims licensable | ||||
|       by such Contributor that are necessarily infringed by their | ||||
|       Contribution(s) alone or by combination of their Contribution(s) | ||||
|       with the Work to which such Contribution(s) was submitted. If You | ||||
|       institute patent litigation against any entity (including a | ||||
|       cross-claim or counterclaim in a lawsuit) alleging that the Work | ||||
|       or a Contribution incorporated within the Work constitutes direct | ||||
|       or contributory patent infringement, then any patent licenses | ||||
|       granted to You under this License for that Work shall terminate | ||||
|       as of the date such litigation is filed. | ||||
|  | ||||
|    4. Redistribution. You may reproduce and distribute copies of the | ||||
|       Work or Derivative Works thereof in any medium, with or without | ||||
|       modifications, and in Source or Object form, provided that You | ||||
|       meet the following conditions: | ||||
|  | ||||
|       (a) You must give any other recipients of the Work or | ||||
|           Derivative Works a copy of this License; and | ||||
|  | ||||
|       (b) You must cause any modified files to carry prominent notices | ||||
|           stating that You changed the files; and | ||||
|  | ||||
|       (c) You must retain, in the Source form of any Derivative Works | ||||
|           that You distribute, all copyright, patent, trademark, and | ||||
|           attribution notices from the Source form of the Work, | ||||
|           excluding those notices that do not pertain to any part of | ||||
|           the Derivative Works; and | ||||
|  | ||||
|       (d) If the Work includes a "NOTICE" text file as part of its | ||||
|           distribution, then any Derivative Works that You distribute must | ||||
|           include a readable copy of the attribution notices contained | ||||
|           within such NOTICE file, excluding those notices that do not | ||||
|           pertain to any part of the Derivative Works, in at least one | ||||
|           of the following places: within a NOTICE text file distributed | ||||
|           as part of the Derivative Works; within the Source form or | ||||
|           documentation, if provided along with the Derivative Works; or, | ||||
|           within a display generated by the Derivative Works, if and | ||||
|           wherever such third-party notices normally appear. The contents | ||||
|           of the NOTICE file are for informational purposes only and | ||||
|           do not modify the License. You may add Your own attribution | ||||
|           notices within Derivative Works that You distribute, alongside | ||||
|           or as an addendum to the NOTICE text from the Work, provided | ||||
|           that such additional attribution notices cannot be construed | ||||
|           as modifying the License. | ||||
|  | ||||
|       You may add Your own copyright statement to Your modifications and | ||||
|       may provide additional or different license terms and conditions | ||||
|       for use, reproduction, or distribution of Your modifications, or | ||||
|       for any such Derivative Works as a whole, provided Your use, | ||||
|       reproduction, and distribution of the Work otherwise complies with | ||||
|       the conditions stated in this License. | ||||
|  | ||||
|    5. Submission of Contributions. Unless You explicitly state otherwise, | ||||
|       any Contribution intentionally submitted for inclusion in the Work | ||||
|       by You to the Licensor shall be under the terms and conditions of | ||||
|       this License, without any additional terms or conditions. | ||||
|       Notwithstanding the above, nothing herein shall supersede or modify | ||||
|       the terms of any separate license agreement you may have executed | ||||
|       with Licensor regarding such Contributions. | ||||
|  | ||||
|    6. Trademarks. This License does not grant permission to use the trade | ||||
|       names, trademarks, service marks, or product names of the Licensor, | ||||
|       except as required for reasonable and customary use in describing the | ||||
|       origin of the Work and reproducing the content of the NOTICE file. | ||||
|  | ||||
|    7. Disclaimer of Warranty. Unless required by applicable law or | ||||
|       agreed to in writing, Licensor provides the Work (and each | ||||
|       Contributor provides its Contributions) on an "AS IS" BASIS, | ||||
|       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||||
|       implied, including, without limitation, any warranties or conditions | ||||
|       of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||||
|       PARTICULAR PURPOSE. You are solely responsible for determining the | ||||
|       appropriateness of using or redistributing the Work and assume any | ||||
|       risks associated with Your exercise of permissions under this License. | ||||
|  | ||||
|    8. Limitation of Liability. In no event and under no legal theory, | ||||
|       whether in tort (including negligence), contract, or otherwise, | ||||
|       unless required by applicable law (such as deliberate and grossly | ||||
|       negligent acts) or agreed to in writing, shall any Contributor be | ||||
|       liable to You for damages, including any direct, indirect, special, | ||||
|       incidental, or consequential damages of any character arising as a | ||||
|       result of this License or out of the use or inability to use the | ||||
|       Work (including but not limited to damages for loss of goodwill, | ||||
|       work stoppage, computer failure or malfunction, or any and all | ||||
|       other commercial damages or losses), even if such Contributor | ||||
|       has been advised of the possibility of such damages. | ||||
|  | ||||
|    9. Accepting Warranty or Additional Liability. While redistributing | ||||
|       the Work or Derivative Works thereof, You may choose to offer, | ||||
|       and charge a fee for, acceptance of support, warranty, indemnity, | ||||
|       or other liability obligations and/or rights consistent with this | ||||
|       License. However, in accepting such obligations, You may act only | ||||
|       on Your own behalf and on Your sole responsibility, not on behalf | ||||
|       of any other Contributor, and only if You agree to indemnify, | ||||
|       defend, and hold each Contributor harmless for any liability | ||||
|       incurred by, or claims asserted against, such Contributor by reason | ||||
|       of your accepting any such warranty or additional liability. | ||||
|  | ||||
|    END OF TERMS AND CONDITIONS | ||||
|  | ||||
|    APPENDIX: How to apply the Apache License to your work. | ||||
|  | ||||
|       To apply the Apache License to your work, attach the following | ||||
|       boilerplate notice, with the fields enclosed by brackets "[]" | ||||
|       replaced with your own identifying information. (Don't include | ||||
|       the brackets!)  The text should be enclosed in the appropriate | ||||
|       comment syntax for the file format. We also recommend that a | ||||
|       file or class name and description of purpose be included on the | ||||
|       same "printed page" as the copyright notice for easier | ||||
|       identification within third-party archives. | ||||
|  | ||||
|    Copyright [yyyy] [name of copyright owner] | ||||
|  | ||||
|    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. | ||||
| @@ -1,24 +0,0 @@ | ||||
|  | ||||
| .PHONY: compile test eclipse clean package | ||||
|  | ||||
| all: | ||||
| 	compile | ||||
|  | ||||
| package: | ||||
| 	mvn package | ||||
|  | ||||
| install: | ||||
| 	mvn install | ||||
|  | ||||
| compile: | ||||
| 	mvn compile | ||||
|  | ||||
| test: | ||||
| 	mvn test | ||||
|  | ||||
| # generate .project and .classpath file for Eclipse | ||||
| eclipse: | ||||
| 	mvn eclipse:eclipse | ||||
|  | ||||
| clean: | ||||
| 	mvn clean | ||||
							
								
								
									
										28
									
								
								java/README
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								java/README
									
									
									
									
									
								
							| @@ -1,28 +0,0 @@ | ||||
|  | ||||
| To build the JAR file of Message Pack, you need to install Maven (http://maven.apache.org), then type the following command: | ||||
|  | ||||
| $ mvn package | ||||
|  | ||||
| To locally install the project, type | ||||
| $ mvn install | ||||
|  | ||||
| To generate project files (.project, .classpath) for Eclipse, do  | ||||
|  | ||||
| $ mvn eclipse:eclipse | ||||
|  | ||||
| then import the folder from your Eclipse.  | ||||
|  | ||||
| Next, open the preference page in Eclipse and add the CLASSPATH variable: | ||||
|  | ||||
| M2_REPO = $HOME/.m2/repository | ||||
|  | ||||
| where $HOME is your home directory. In Windows XP, $HOME is: | ||||
| C:/Documents and Settings/(user name)/.m2/repository  | ||||
|  | ||||
|  | ||||
| # How to release the project (compile, test, tagging, deploy) | ||||
|  | ||||
| $ mvn release:prepare | ||||
| $ mvn release:perform | ||||
|  | ||||
|  | ||||
							
								
								
									
										303
									
								
								java/build.xml
									
									
									
									
									
								
							
							
						
						
									
										303
									
								
								java/build.xml
									
									
									
									
									
								
							| @@ -1,303 +0,0 @@ | ||||
| <project name="MessagePack for Java" default="jar" | ||||
|     xmlns:ivy="antlib:org.apache.ivy.ant" | ||||
|     xmlns:mvn="urn:maven-artifact-ant"> | ||||
|  | ||||
|   <property name="Name" value="MessagePack"/> | ||||
|   <property name="name" value="msgpack"/> | ||||
|   <property name="version" value="0.0.1"/> | ||||
|   <property name="fullname" value="${name}-${version}"/> | ||||
|   <property name="year" value="2010"/> | ||||
|  | ||||
|   <!-- Load user's default properties. --> | ||||
|   <property file="${user.home}/build.properties" /> | ||||
|  | ||||
|   <property name="src.dir" value="${basedir}/src/main/java"/>  	 | ||||
|   <property name="java.src.dir" value="${src.dir}/"/> | ||||
|   <property name="build.dir" value="${basedir}/build"/> | ||||
|   <property name="lib.dir" value="${basedir}/lib"/> | ||||
|   <property name="dist.dir" value="${basedir}/dist"/> | ||||
|  | ||||
|   <property name="build.classes" value="${build.dir}/classes"/> | ||||
|   <property name="build.doc" value="${build.dir}/doc"/> | ||||
|   <property name="build.javadoc" value="${build.doc}/api/"/> | ||||
|   <property name="build.javadoc.log" value="${build.dir}/javadoc.log"/> | ||||
|  | ||||
|   <property name="test.count" value="100"/> | ||||
|   <property name="test.junit.output.format" value="plain"/> | ||||
|   <property name="test.java.src.dir" value="${basedir}/src/test/java"/> | ||||
|   <property name="test.java.build.dir" value="${build.dir}/test"/> | ||||
|   <property name="test.java.classes" value="${test.java.build.dir}/classes"/> | ||||
|   <property name="test.java.include" value="Test*"/> | ||||
|   | ||||
|   <property name="javac.encoding" value="ISO-8859-1"/> | ||||
|   <property name="javac.debug" value="on"/> | ||||
|   <property name="javac.optimize" value="on"/> | ||||
|   <property name="javac.deprecation" value="off"/> | ||||
|   <property name="javac.version" value="1.6"/> | ||||
|   <property name="javac.args" value=""/> | ||||
|   <property name="javac.args.warnings" value="-Xlint:unchecked"/> | ||||
|  | ||||
|   <property name="javadoc.link.java" | ||||
| 	    value="http://java.sun.com/javase/6/docs/api/"/> | ||||
|   <property name="javadoc.packages" value="org.${name}.*"/> | ||||
|  | ||||
|   <!-- ivy settings --> | ||||
|   <property name="ivy.version" value="2.1.0"/> | ||||
|   <property name="ivy.url" | ||||
| 	    value="http://repo2.maven.org/maven2/org/apache/ivy/ivy" /> | ||||
|   <property name="ivy.home" value="${user.home}/.ant" /> | ||||
|   <property name="ivy.lib" value="${build.dir}/lib"/> | ||||
|   <property name="ivy.test.lib" value="${build.dir}/test/lib"/> | ||||
|   <property name="mvn.repo" | ||||
| 	    value="https://repository.apache.org/content/repositories/snapshots"/> | ||||
|  | ||||
|   <!-- the normal classpath --> | ||||
|   <path id="libs"> | ||||
|     <fileset dir="${ivy.lib}"> | ||||
|       <include name="**/*.jar" /> | ||||
|     </fileset> | ||||
|   </path> | ||||
|   <path id="java.classpath"> | ||||
|     <pathelement location="${build.classes}"/> | ||||
|     <fileset dir="${lib.dir}"> | ||||
|       <include name="**/*.jar" /> | ||||
|       <exclude name="**/excluded/" /> | ||||
|     </fileset> | ||||
|     <fileset dir="${ant.home}/lib"> | ||||
|       <include name="ant.jar" /> | ||||
|     </fileset> | ||||
|     <path refid="libs" /> | ||||
|   </path> | ||||
|   <path id="test.libs"> | ||||
|     <fileset dir="${ivy.test.lib}"> | ||||
|       <include name="**/*.jar" /> | ||||
|     </fileset> | ||||
|   </path> | ||||
|   <path id="test.java.classpath"> | ||||
|     <pathelement location="${test.java.classes}" /> | ||||
|     <path refid="java.classpath"/> | ||||
|     <path refid="test.libs"/> | ||||
|   </path> | ||||
|  | ||||
|   <!-- init & clean --> | ||||
|   <target name="init"> | ||||
|     <mkdir dir="${build.dir}" /> | ||||
|     <mkdir dir="${lib.dir}" /> | ||||
|     <mkdir dir="${build.classes}" /> | ||||
|  | ||||
|     <mkdir dir="${test.java.build.dir}"/> | ||||
|     <mkdir dir="${test.java.classes}"/> | ||||
|  | ||||
|     <mkdir dir="${ivy.lib}"/> | ||||
|     <mkdir dir="${ivy.test.lib}"/> | ||||
|     <condition property="ivy.jar.exists"> | ||||
|       <available file="${lib.dir}/ivy-${ivy.version}.jar"/> | ||||
|     </condition> | ||||
|   </target> | ||||
|   <target name="clean"> | ||||
|     <delete dir="${build.dir}" /> | ||||
|   </target> | ||||
|  | ||||
|   <!-- ivy targets --> | ||||
|   <target name="ivy-download" unless="ivy.jar.exists" depends="init"> | ||||
|     <delete dir="${lib.dir}" | ||||
| 	    includes="ivy-*.jar" excludes="ivy-${ivy.version}.jar"/> | ||||
|     <get src="${ivy.url}/${ivy.version}/ivy-${ivy.version}.jar" | ||||
|          dest="${lib.dir}/ivy-${ivy.version}.jar" usetimestamp="true"/> | ||||
|   </target> | ||||
|   <target name="ivy-init" depends="ivy-download" unless="ivy.initialized"> | ||||
|     <taskdef resource="org/apache/ivy/ant/antlib.xml" | ||||
|              uri="antlib:org.apache.ivy.ant" classpathref="java.classpath"/> | ||||
|     <!-- ensure that ivy taskdef is only run once, otw ant will error --> | ||||
|     <property name="ivy.initialized" value="true"/> | ||||
|   </target> | ||||
|     | ||||
|  <target name="ivy-retrieve-build" depends="init,ivy-init"> | ||||
|     <ivy:retrieve type="jar" conf="build" | ||||
| 		  pattern="${ivy.lib}/[artifact]-[revision].[ext]"/> | ||||
|  </target> | ||||
|  <target name="ivy-retrieve-test" depends="init,ivy-init"> | ||||
|     <ivy:retrieve type="jar" conf="test" | ||||
| 		  pattern="${ivy.test.lib}/[artifact]-[revision].[ext]"/> | ||||
|  </target> | ||||
|  | ||||
|   <!-- compiler --> | ||||
|   <macrodef name="java-compiler"> | ||||
|     <attribute name="dest" default="${build.classes}"/> | ||||
|     <attribute name="includes" default="**/*.java"/> | ||||
|     <attribute name="excludes" default=""/> | ||||
|     <attribute name="classpath" default="java.classpath"/> | ||||
|     <element name="src" implicit="yes"/> | ||||
|     <sequential> | ||||
|       <javac  | ||||
| 	 destdir="@{dest}" | ||||
|          includes="@{includes}" | ||||
|          excludes="@{excludes}"  | ||||
| 	 encoding="${javac.encoding}"  | ||||
| 	 debug="${javac.debug}" | ||||
| 	 optimize="${javac.optimize}" | ||||
| 	 target="${javac.version}" | ||||
| 	 source="${javac.version}" | ||||
| 	 deprecation="${javac.deprecation}"> | ||||
| 	<compilerarg line="${javac.args} ${javac.args.warnings}" /> | ||||
| 	<classpath refid="@{classpath}"/> | ||||
|         <src /> | ||||
|       </javac> | ||||
|     </sequential> | ||||
|   </macrodef> | ||||
|  | ||||
|   <!-- compile --> | ||||
|   <target name="compile" depends="init,ivy-retrieve-build"> | ||||
|     <java-compiler> | ||||
|       <src path="${java.src.dir}"/> | ||||
|     </java-compiler> | ||||
|   </target> | ||||
|  | ||||
|   <!-- test --> | ||||
|   <macrodef name="test-runner"> | ||||
|     <attribute name="files.location" /> | ||||
|     <attribute name="tests.pattern" /> | ||||
|     <attribute name="test.dir" default="${test.java.build.dir}" /> | ||||
|     <sequential> | ||||
|       <junit showoutput="yes" | ||||
|              printsummary="withOutAndErr" | ||||
|              haltonfailure="no" | ||||
|              fork="yes" forkMode="once" | ||||
|              errorProperty="tests.failed" failureProperty="tests.failed"> | ||||
|         <sysproperty key="test.count" value="${test.count}"/> | ||||
|         <sysproperty key="test.dir" value="@{test.dir}"/> | ||||
|         <classpath refid="test.java.classpath"/> | ||||
|         <formatter type="${test.junit.output.format}"/> | ||||
|         <batchtest todir="${test.java.build.dir}" unless="testcase"> | ||||
|           <fileset dir="@{files.location}" | ||||
| 		   includes="@{tests.pattern}" | ||||
| 		   excludes="**/${test.java.exclude}.java" /> | ||||
|         </batchtest> | ||||
|         <batchtest todir="${test.java.build.dir}" if="testcase"> | ||||
|           <fileset dir="@{files.location}" includes="**/${testcase}.java"/> | ||||
|         </batchtest> | ||||
|       </junit> | ||||
|       <fail if="tests.failed">Tests Failed!</fail> | ||||
|     </sequential> | ||||
|   </macrodef> | ||||
|   <target name="compile-test" depends="ivy-retrieve-test,compile"> | ||||
|     <java-compiler dest="${test.java.classes}" | ||||
| 		   classpath="test.java.classpath"> | ||||
|       <src path="${test.java.src.dir}/org" /> | ||||
|     </java-compiler> | ||||
|   </target> | ||||
|   <target name="test" depends="init,compile-test"> | ||||
|     <test-runner files.location="${test.java.src.dir}" | ||||
|                  tests.pattern="**/${test.java.include}.java"/> | ||||
|   </target> | ||||
|  | ||||
|   <!-- jar --> | ||||
|   <target name="jar" depends="compile"> | ||||
|     <jar jarfile="${build.dir}/${name}-${version}.jar" basedir="${build.classes}" > | ||||
|       <manifest> | ||||
|         <section name="org/${name}"> | ||||
|           <attribute name="Implementation-Title" value="${Name}"/> | ||||
|           <attribute name="Implementation-Version" value="${version}"/> | ||||
|         </section> | ||||
|       </manifest> | ||||
|     </jar> | ||||
|   </target> | ||||
|  | ||||
|   <!-- javadoc --> | ||||
|   <target name="javadoc" depends="compile" description="Generate javadoc"> | ||||
|     <mkdir dir="${build.javadoc}"/> | ||||
|     <record name="${build.javadoc.log}" action="start"/> | ||||
|     <javadoc | ||||
|       Locale="en_US" | ||||
|       packagenames="org.${org}.${name}.*" | ||||
|       destdir="${build.javadoc}" | ||||
|       encoding="UTF-8" | ||||
|       docencoding="UTF-8" | ||||
|       author="true" | ||||
|       version="true" | ||||
|       use="true" | ||||
|       windowtitle="${Name} ${version} API" | ||||
|       doctitle="${Name} ${version} API" | ||||
|       bottom="Copyright &copy; ${year} The ${Name} Project" | ||||
|       > | ||||
|         <packageset dir="${java.src.dir}"/> | ||||
|         <link href="${javadoc.link.java}"/> | ||||
|         <classpath > | ||||
|           <path refid="java.classpath" /> | ||||
|         </classpath> | ||||
|     </javadoc> | ||||
|     <record name="${build.javadoc.log}" action="stop"/> | ||||
|     <condition property="javadoc.warnings"> | ||||
|       <isfileselected file="${build.javadoc.log}"> | ||||
| 	<contains text=": warning - "/> | ||||
|       </isfileselected> | ||||
|     </condition> | ||||
|     <fail if="javadoc.warnings">Javadoc warnings!</fail> | ||||
|   </target> | ||||
|   <target name="javadoc-jar" depends="javadoc"> | ||||
|     <jar jarfile="${build.dir}/${fullname}-javadoc.jar"> | ||||
|       <fileset dir="${build.javadoc}" includes="**/*"/> | ||||
|     </jar> | ||||
|   </target> | ||||
|  | ||||
|   <!-- sources --> | ||||
|   <target name="source"> | ||||
|     <jar jarfile="${build.dir}/${fullname}-sources.jar"> | ||||
|       <fileset dir="${java.src.dir}" includes="**/*.java"/> | ||||
|     </jar> | ||||
|   </target> | ||||
|  | ||||
|   <!-- pom --> | ||||
|   <target name="pom" depends="ivy-init"> | ||||
|     <ivy:makepom ivyfile="${basedir}/ivy.xml" | ||||
| 		 pomfile="${dist.dir}/${fullname}.pom"> | ||||
|       <mapping conf="default" scope="compile"/> | ||||
|       <mapping conf="test" scope="test"/> | ||||
|     </ivy:makepom> | ||||
|   </target> | ||||
|  | ||||
|   <!-- dist --> | ||||
|   <target name="dist" depends="jar, pom, source, javadoc-jar" | ||||
| 	  description="Build distribution"> | ||||
|     <mkdir dir="${dist.dir}"/> | ||||
|     <copy todir="${dist.dir}">  | ||||
|       <fileset file="${build.dir}/${fullname}.jar"/> | ||||
|       <fileset file="${build.dir}/${fullname}-sources.jar"/> | ||||
|       <fileset file="${build.dir}/${fullname}-javadoc.jar"/> | ||||
|     </copy> | ||||
|   </target> | ||||
|  | ||||
|   <!-- maven: install msgpack into local m2 cache --> | ||||
|   <target name="mvn-install" depends="jar, pom, source, javadoc-jar" | ||||
| 	  description="Installs msgpack to local m2 cache"> | ||||
|     <typedef resource="org/apache/maven/artifact/ant/antlib.xml" | ||||
| 	     uri="urn:maven-artifact-ant" | ||||
| 	     classpathref="java.classpath"/> | ||||
|     <mvn:pom file="${dist.dir}/${fullname}.pom" id="msgpack"/> | ||||
|     <mvn:install file="${build.dir}/${fullname}.jar"> | ||||
|       <attach file="${build.dir}/${fullname}-sources.jar" | ||||
|            classifier="sources" /> | ||||
|       <attach file="${build.dir}/${fullname}-javadoc.jar" | ||||
|            classifier="javadoc" /> | ||||
|       <pom refid="msgpack"/> | ||||
|     </mvn:install> | ||||
|   </target> | ||||
|  | ||||
|   <!-- maven: create local repository into ${basedir}/maven2 --> | ||||
|   <target name="mvn-deploy" depends="jar, pom, source, javadoc-jar" | ||||
|      description="Deploys MessagePack to Maven repo."> | ||||
|     <typedef resource="org/apache/maven/artifact/ant/antlib.xml" | ||||
| 	     uri="urn:maven-artifact-ant" | ||||
| 	     classpathref="java.classpath"/> | ||||
|     <mvn:pom file="${dist.dir}/${fullname}.pom" id="msgpack"/> | ||||
|     <mvn:deploy file="${build.dir}/${fullname}.jar"> | ||||
|       <remoteRepository url="file://localhost/${basedir}/maven2/"/> | ||||
|       <attach file="${build.dir}/${fullname}-sources.jar" | ||||
|            classifier="sources" /> | ||||
|       <attach file="${build.dir}/${fullname}-javadoc.jar" | ||||
|            classifier="javadoc" /> | ||||
|       <pom refid="msgpack"/> | ||||
|     </mvn:deploy> | ||||
|   </target> | ||||
|  | ||||
| </project> | ||||
							
								
								
									
										19
									
								
								java/ivy.xml
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								java/ivy.xml
									
									
									
									
									
								
							| @@ -1,19 +0,0 @@ | ||||
| <ivy-module version="2.0" | ||||
|             xmlns:e="http://ant.apache.org/ivy/extra"> | ||||
|  | ||||
|   <info organisation="org" | ||||
|     module="${name}" revision="${version}"> | ||||
|     <ivyauthor name="MessagePack Project" url="http://msgpack.sourceforge.net/"/> | ||||
|     <description>MessagePack</description> | ||||
|   </info> | ||||
|  | ||||
|   <configurations defaultconfmapping="default"> | ||||
|     <conf name="default"/> <!-- "runtime" configuration --> | ||||
|     <conf name="test"/> | ||||
|     <conf name="build" extends="default"/> | ||||
|   </configurations> | ||||
|  | ||||
|   <dependencies> | ||||
|     <dependency org="junit" name="junit" rev="4.8.1" conf="test->default"/> | ||||
|   </dependencies> | ||||
| </ivy-module> | ||||
| @@ -1,17 +0,0 @@ | ||||
| <?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> | ||||
|  | ||||
							
								
								
									
										178
									
								
								java/pom.xml
									
									
									
									
									
								
							
							
						
						
									
										178
									
								
								java/pom.xml
									
									
									
									
									
								
							| @@ -1,178 +0,0 @@ | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||||
|  | ||||
|   <modelVersion>4.0.0</modelVersion> | ||||
|   <groupId>org.msgpack</groupId> | ||||
|   <artifactId>msgpack</artifactId> | ||||
|   <version>0.5.2-devel</version> | ||||
|   <description>MessagePack for Java</description> | ||||
|  | ||||
|   <name>MessagePack for Java</name> | ||||
|   <url>http://msgpack.org/</url> | ||||
|  | ||||
|   <licenses> | ||||
|     <license> | ||||
|       <name>The Apache Software License, Version 2.0</name> | ||||
|       <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> | ||||
|       <distribution>repo</distribution> | ||||
|     </license> | ||||
|   </licenses> | ||||
|  | ||||
|   <scm> | ||||
|     <connection>scm:git:git://github.com/msgpack/msgpack.git</connection> | ||||
|     <url>scm:git:git://github.com/msgpack/msgpack.git</url> | ||||
|   </scm> | ||||
|  | ||||
|   <dependencies> | ||||
|     <dependency> | ||||
|       <groupId>junit</groupId> | ||||
|       <artifactId>junit</artifactId> | ||||
|       <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> | ||||
|     <resources> | ||||
|       <resource> | ||||
|         <directory>src/main/resources</directory> | ||||
|       </resource> | ||||
|     </resources> | ||||
|     <testResources> | ||||
|       <testResource> | ||||
|         <directory>src/test/resources</directory> | ||||
|       </testResource> | ||||
|     </testResources> | ||||
|  | ||||
|     <plugins> | ||||
|       <plugin> | ||||
|         <artifactId>maven-compiler-plugin</artifactId> | ||||
|         <configuration> | ||||
|           <source>1.5</source> | ||||
|           <target>1.5</target> | ||||
|         </configuration> | ||||
|       </plugin> | ||||
|  | ||||
|       <plugin> | ||||
|       	<artifactId>maven-eclipse-plugin</artifactId> | ||||
|       	<version>2.5.1</version> | ||||
|       </plugin> | ||||
|        | ||||
|       <plugin> | ||||
|         <artifactId>maven-release-plugin</artifactId> | ||||
|         <configuration> | ||||
|           <!--  do not run site-deploy goal, which is included in the default setting  --> | ||||
|           <goals>deploy</goals> | ||||
|           <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> | ||||
|  | ||||
|  | ||||
|   <reporting> | ||||
|     <plugins> | ||||
|       <!-- Generating JavaDoc --> | ||||
|       <plugin> | ||||
|         <groupId>org.apache.maven.plugins</groupId> | ||||
|         <artifactId>maven-javadoc-plugin</artifactId> | ||||
|         <configuration> | ||||
|           <doctitle>${project.name} ${project.version} API</doctitle> | ||||
|           <aggregate>true</aggregate> | ||||
|           <locale>en_US</locale> | ||||
| 	  <encoding>UTF-8</encoding> | ||||
|         </configuration> | ||||
|       </plugin> | ||||
|        | ||||
|       <plugin> | ||||
|         <groupId>org.apache.maven.plugins</groupId> | ||||
|         <artifactId>maven-jxr-plugin</artifactId> | ||||
|       </plugin> | ||||
|        | ||||
|       <plugin> | ||||
| 	<groupId>org.apache.maven.plugins</groupId> | ||||
| 	<artifactId>maven-surefire-report-plugin</artifactId> | ||||
|       </plugin> | ||||
|     </plugins> | ||||
|   </reporting> | ||||
|  | ||||
|   <repositories> | ||||
|     <repository> | ||||
|       <id>msgpack.org</id> | ||||
|       <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> | ||||
|     <repository> | ||||
|       <uniqueVersion>false</uniqueVersion> | ||||
|       <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> | ||||
|  | ||||
|   <profiles> | ||||
|     <profile> | ||||
|       <id>release</id> | ||||
|       <build> | ||||
|         <plugins> | ||||
|           <plugin> | ||||
|             <inherited>true</inherited> | ||||
|             <groupId>org.apache.maven.plugins</groupId> | ||||
|             <artifactId>maven-deploy-plugin</artifactId> | ||||
|             <version>2.4</version> | ||||
|             <configuration> | ||||
|               <updateReleaseInfo>true</updateReleaseInfo> | ||||
|             </configuration> | ||||
|           </plugin> | ||||
|         </plugins> | ||||
|       </build> | ||||
|     </profile> | ||||
|   </profiles> | ||||
| </project> | ||||
| @@ -1,26 +0,0 @@ | ||||
| // | ||||
| // 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); | ||||
| 	} | ||||
| } | ||||
| @@ -1,457 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| import java.nio.ByteBuffer; | ||||
| import java.math.BigInteger; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| 	final boolean next(UnpackResult result) throws IOException, UnpackException { | ||||
| 		if(filled == 0) { | ||||
| 			if(!fill()) { | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		do { | ||||
| 			int noffset = super.execute(buffer, offset, filled); | ||||
| 			if(noffset <= offset) { | ||||
| 				if(!fill()) { | ||||
| 					return false; | ||||
| 				} | ||||
| 				continue; | ||||
| 			} | ||||
| 			offset = noffset; | ||||
| 		} while(!super.isFinished()); | ||||
|  | ||||
| 		MessagePackObject obj = super.getData(); | ||||
| 		super.reset(); | ||||
| 		result.done(obj); | ||||
|  | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	private final void more(int require) throws IOException, UnpackException { | ||||
| 		while(filled - offset < require) { | ||||
| 			if(!fill()) { | ||||
| 				// FIXME | ||||
| 				throw new UnpackException("insufficient buffer"); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	private final boolean tryMore(int require) throws IOException, UnpackException { | ||||
| 		while(filled - offset < require) { | ||||
| 			if(!fill()) { | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	private final void advance(int length) { | ||||
| 		offset += length; | ||||
| 	} | ||||
|  | ||||
| 	final byte unpackByte() throws IOException, MessageTypeException { | ||||
| 		int o = unpackInt(); | ||||
| 		if(0x7f < o || o < -0x80) { | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 		return (byte)o; | ||||
| 	} | ||||
|  | ||||
| 	final short unpackShort() throws IOException, MessageTypeException { | ||||
| 		int o = unpackInt(); | ||||
| 		if(0x7fff < o || o < -0x8000) { | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 		return (short)o; | ||||
| 	} | ||||
|  | ||||
| 	final int unpackInt() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) {  // Fixnum | ||||
| 			advance(1); | ||||
| 			return (int)b; | ||||
| 		} | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xcc:  // unsigned int  8 | ||||
| 			more(2); | ||||
| 			advance(2); | ||||
| 			return (int)((short)(buffer[offset-1]) & 0xff); | ||||
| 		case 0xcd:  // unsigned int 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (int)((int)castBuffer.getShort(0) & 0xffff); | ||||
| 		case 0xce:  // unsigned int 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			{ | ||||
| 				int o = castBuffer.getInt(0); | ||||
| 				if(o < 0) { | ||||
| 					throw new MessageTypeException(); | ||||
| 				} | ||||
| 				advance(5); | ||||
| 				return o; | ||||
| 			} | ||||
| 		case 0xcf:  // unsigned int 64 | ||||
| 			more(9); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 8); | ||||
| 			{ | ||||
| 				long o = castBuffer.getLong(0); | ||||
| 				if(o < 0 || o > 0x7fffffffL) { | ||||
| 					throw new MessageTypeException(); | ||||
| 				} | ||||
| 				advance(9); | ||||
| 				return (int)o; | ||||
| 			} | ||||
| 		case 0xd0:  // signed int  8 | ||||
| 			more(2); | ||||
| 			advance(2); | ||||
| 			return (int)buffer[offset-1]; | ||||
| 		case 0xd1:  // signed int 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (int)castBuffer.getShort(0); | ||||
| 		case 0xd2:  // signed int 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			return (int)castBuffer.getInt(0); | ||||
| 		case 0xd3:  // signed int 64 | ||||
| 			more(9); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 8); | ||||
| 			{ | ||||
| 				long o = castBuffer.getLong(0); | ||||
| 				if(0x7fffffffL < o || o < -0x80000000L) { | ||||
| 					throw new MessageTypeException(); | ||||
| 				} | ||||
| 				advance(9); | ||||
| 				return (int)o; | ||||
| 			} | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final long unpackLong() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) {  // Fixnum | ||||
| 			advance(1); | ||||
| 			return (long)b; | ||||
| 		} | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xcc:  // unsigned int  8 | ||||
| 			more(2); | ||||
| 			advance(2); | ||||
| 			return (long)((short)(buffer[offset-1]) & 0xff); | ||||
| 		case 0xcd:  // unsigned int 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (long)((int)castBuffer.getShort(0) & 0xffff); | ||||
| 		case 0xce:  // unsigned int 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			return ((long)castBuffer.getInt(0) & 0xffffffffL); | ||||
| 		case 0xcf:  // unsigned int 64 | ||||
| 			more(9); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 8); | ||||
| 			{ | ||||
| 				long o = castBuffer.getLong(0); | ||||
| 				if(o < 0) { | ||||
| 					throw new MessageTypeException(); | ||||
| 				} | ||||
| 				advance(9); | ||||
| 				return o; | ||||
| 			} | ||||
| 		case 0xd0:  // signed int  8 | ||||
| 			more(2); | ||||
| 			advance(2); | ||||
| 			return (long)buffer[offset-1]; | ||||
| 		case 0xd1:  // signed int 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (long)castBuffer.getShort(0); | ||||
| 		case 0xd2:  // signed int 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			return (long)castBuffer.getInt(0); | ||||
| 		case 0xd3:  // signed int 64 | ||||
| 			more(9); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 8); | ||||
| 			advance(9); | ||||
| 			return (long)castBuffer.getLong(0); | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final BigInteger unpackBigInteger() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		if((b & 0xff) != 0xcf) { | ||||
| 			return BigInteger.valueOf(unpackLong()); | ||||
| 		} | ||||
|  | ||||
| 		// unsigned int 64 | ||||
| 		more(9); | ||||
| 		castBuffer.rewind(); | ||||
| 		castBuffer.put(buffer, offset+1, 8); | ||||
| 		advance(9); | ||||
| 		long o = castBuffer.getLong(0); | ||||
| 		if(o < 0) { | ||||
| 			return new BigInteger(1, castBuffer.array()); | ||||
| 		} else { | ||||
| 			return BigInteger.valueOf(o); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final float unpackFloat() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xca:  // float | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			return castBuffer.getFloat(0); | ||||
| 		case 0xcb:  // double | ||||
| 			more(9); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 8); | ||||
| 			advance(9); | ||||
| 			// FIXME overflow check | ||||
| 			return (float)castBuffer.getDouble(0); | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final double unpackDouble() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xca:  // float | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			return (double)castBuffer.getFloat(0); | ||||
| 		case 0xcb:  // double | ||||
| 			more(9); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 8); | ||||
| 			advance(9); | ||||
| 			return castBuffer.getDouble(0); | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final Object unpackNull() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset] & 0xff; | ||||
| 		if(b != 0xc0) {  // nil | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 		advance(1); | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| 	final boolean tryUnpackNull() throws IOException { | ||||
| 		if(!tryMore(1)) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		int b = buffer[offset] & 0xff; | ||||
| 		if(b != 0xc0) {  // nil | ||||
| 			return false; | ||||
| 		} | ||||
| 		advance(1); | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	final boolean unpackBoolean() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset] & 0xff; | ||||
| 		if(b == 0xc2) {  // false | ||||
| 			advance(1); | ||||
| 			return false; | ||||
| 		} else if(b == 0xc3) {  // true | ||||
| 			advance(1); | ||||
| 			return true; | ||||
| 		} else { | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final int unpackArray() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		if((b & 0xf0) == 0x90) {  // FixArray | ||||
| 			advance(1); | ||||
| 			return (int)(b & 0x0f); | ||||
| 		} | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xdc:  // array 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (int)castBuffer.getShort(0) & 0xffff; | ||||
| 		case 0xdd:  // array 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			// FIXME overflow check | ||||
| 			return castBuffer.getInt(0) & 0x7fffffff; | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final int unpackMap() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		if((b & 0xf0) == 0x80) {  // FixMap | ||||
| 			advance(1); | ||||
| 			return (int)(b & 0x0f); | ||||
| 		} | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xde:  // map 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (int)castBuffer.getShort(0) & 0xffff; | ||||
| 		case 0xdf:  // map 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			// FIXME overflow check | ||||
| 			return castBuffer.getInt(0) & 0x7fffffff; | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final int unpackRaw() throws IOException, MessageTypeException { | ||||
| 		more(1); | ||||
| 		int b = buffer[offset]; | ||||
| 		if((b & 0xe0) == 0xa0) {  // FixRaw | ||||
| 			advance(1); | ||||
| 			return (int)(b & 0x1f); | ||||
| 		} | ||||
| 		switch(b & 0xff) { | ||||
| 		case 0xda:  // raw 16 | ||||
| 			more(3); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 2); | ||||
| 			advance(3); | ||||
| 			return (int)castBuffer.getShort(0) & 0xffff; | ||||
| 		case 0xdb:  // raw 32 | ||||
| 			more(5); | ||||
| 			castBuffer.rewind(); | ||||
| 			castBuffer.put(buffer, offset+1, 4); | ||||
| 			advance(5); | ||||
| 			// FIXME overflow check | ||||
| 			return castBuffer.getInt(0) & 0x7fffffff; | ||||
| 		default: | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	final byte[] unpackRawBody(int length) throws IOException { | ||||
| 		more(length); | ||||
| 		byte[] bytes = new byte[length]; | ||||
| 		System.arraycopy(buffer, offset, bytes, 0, length); | ||||
| 		advance(length); | ||||
| 		return bytes; | ||||
| 	} | ||||
|  | ||||
| 	final byte[] unpackByteArray() throws IOException, MessageTypeException { | ||||
| 		int length = unpackRaw(); | ||||
| 		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 { | ||||
| 		int length = unpackRaw(); | ||||
| 		more(length); | ||||
| 		String s; | ||||
| 		try { | ||||
| 			s = new String(buffer, offset, length, "UTF-8"); | ||||
| 		} catch (Exception e) { | ||||
| 			throw new MessageTypeException(); | ||||
| 		} | ||||
| 		advance(length); | ||||
| 		return s; | ||||
| 	} | ||||
|  | ||||
| 	final MessagePackObject unpackObject() throws IOException { | ||||
| 		UnpackResult result = new UnpackResult(); | ||||
| 		if(!next(result)) { | ||||
| 			super.reset(); | ||||
| 			throw new UnpackException("insufficient buffer"); | ||||
| 		} | ||||
| 		return result.getData(); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,23 +0,0 @@ | ||||
| // | ||||
| // 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 MessageConvertable { | ||||
| 	public void messageConvert(MessagePackObject obj) throws MessageTypeException; | ||||
| } | ||||
|  | ||||
| @@ -1,23 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| } | ||||
|  | ||||
| @@ -1,160 +0,0 @@ | ||||
| // | ||||
| // 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); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,160 +0,0 @@ | ||||
| // | ||||
| // 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.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() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean isBooleanType() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean isIntegerType() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean isFloatType() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean isArrayType() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean isMapType() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean isRawType() { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	public boolean asBoolean() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public byte asByte() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public short asShort() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public int asInt() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public long asLong() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public BigInteger asBigInteger() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public float asFloat() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public double asDouble() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public byte[] asByteArray() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public String asString() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public MessagePackObject[] asArray() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public List<MessagePackObject> asList() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public Map<MessagePackObject, MessagePackObject> asMap() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public byte byteValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public short shortValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public int intValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public long longValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public BigInteger bigIntegerValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public float floatValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	public double doubleValue() { | ||||
| 		throw new MessageTypeException("type error"); | ||||
| 	} | ||||
|  | ||||
| 	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); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,22 +0,0 @@ | ||||
| // | ||||
| // 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(); | ||||
| } | ||||
| @@ -1,25 +0,0 @@ | ||||
| // | ||||
| // 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 MessagePackable { | ||||
| 	public void messagePack(Packer pk) throws IOException; | ||||
| } | ||||
|  | ||||
| @@ -1,25 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| } | ||||
|  | ||||
| @@ -1,35 +0,0 @@ | ||||
| // | ||||
| // 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 class MessageTypeException extends RuntimeException { | ||||
| 	public MessageTypeException() { } | ||||
|  | ||||
| 	public MessageTypeException(String s) { | ||||
| 		super(s); | ||||
| 	} | ||||
| 	 | ||||
| 	public MessageTypeException(String s, Throwable t) { | ||||
| 		super(s, t); | ||||
| 	} | ||||
| 	 | ||||
| 	public MessageTypeException(Throwable t) { | ||||
| 		super(t); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,25 +0,0 @@ | ||||
| // | ||||
| // 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 MessageUnpackable { | ||||
| 	public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException; | ||||
| } | ||||
|  | ||||
| @@ -1,25 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| } | ||||
|  | ||||
| @@ -1,541 +0,0 @@ | ||||
| // | ||||
| // 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.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. | ||||
|  * | ||||
|  * <pre> | ||||
|  * // create a packer with output stream | ||||
|  * Packer pk = new Packer(System.out); | ||||
|  * | ||||
|  * // store an object with pack() method. | ||||
|  * pk.pack(1); | ||||
|  * | ||||
|  * // you can store String, List, Map, byte[] and primitive types. | ||||
|  * pk.pack(new ArrayList()); | ||||
|  * </pre> | ||||
|  * | ||||
|  * 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 OutputStream out; | ||||
|  | ||||
| 	public Packer(OutputStream out) { | ||||
| 		this.out = out; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packByte(byte d) throws IOException { | ||||
| 		if(d < -(1<<5)) { | ||||
| 			castBytes[0] = (byte)0xd0; | ||||
| 			castBytes[1] = d; | ||||
| 			out.write(castBytes, 0, 2); | ||||
| 		} else { | ||||
| 			out.write(d); | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packShort(short d) throws IOException { | ||||
| 		if(d < -(1<<5)) { | ||||
| 			if(d < -(1<<7)) { | ||||
| 				// signed 16 | ||||
| 				castBytes[0] = (byte)0xd1; | ||||
| //				castBuffer.putShort(1, d); | ||||
| 				castBytes[1] = (byte)(d >>  8); | ||||
| 				castBytes[2] = (byte)(d >>  0); | ||||
| 				out.write(castBytes, 0, 3); | ||||
| 			} else { | ||||
| 				// signed 8 | ||||
| 				castBytes[0] = (byte)0xd0; | ||||
| 				castBytes[1] = (byte)d; | ||||
| 				out.write(castBytes, 0, 2); | ||||
| 			} | ||||
| 		} else if(d < (1<<7)) { | ||||
| 			// fixnum | ||||
| 			out.write((byte)d); | ||||
| 		} else { | ||||
| 			if(d < (1<<8)) { | ||||
| 				// unsigned 8 | ||||
| 				castBytes[0] = (byte)0xcc; | ||||
| 				castBytes[1] = (byte)d; | ||||
| 				out.write(castBytes, 0, 2); | ||||
| 			} else { | ||||
| 				// unsigned 16 | ||||
| 				castBytes[0] = (byte)0xcd; | ||||
| //				castBuffer.putShort(1, d); | ||||
| 				castBytes[1] = (byte)(d >>  8); | ||||
| 				castBytes[2] = (byte)(d >>  0); | ||||
| 				out.write(castBytes, 0, 3); | ||||
| 			} | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packInt(int d) throws IOException { | ||||
| 		if(d < -(1<<5)) { | ||||
| 			if(d < -(1<<15)) { | ||||
| 				// signed 32 | ||||
| 				castBytes[0] = (byte)0xd2; | ||||
| //				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); | ||||
| 				castBytes[1] = (byte)(d >>  8); | ||||
| 				castBytes[2] = (byte)(d >>  0); | ||||
| 				out.write(castBytes, 0, 3); | ||||
| 			} else { | ||||
| 				// signed 8 | ||||
| 				castBytes[0] = (byte)0xd0; | ||||
| 				castBytes[1] = (byte)d; | ||||
| 				out.write(castBytes, 0, 2); | ||||
| 			} | ||||
| 		} else if(d < (1<<7)) { | ||||
| 			// fixnum | ||||
| 			out.write((byte)d); | ||||
| 		} else { | ||||
| 			if(d < (1<<8)) { | ||||
| 				// unsigned 8 | ||||
| 				castBytes[0] = (byte)0xcc; | ||||
| 				castBytes[1] = (byte)d; | ||||
| 				out.write(castBytes, 0, 2); | ||||
| 			} else if(d < (1<<16)) { | ||||
| 				// unsigned 16 | ||||
| 				castBytes[0] = (byte)0xcd; | ||||
| //				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); | ||||
| 				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); | ||||
| 			} | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packLong(long d) throws IOException { | ||||
| 		if(d < -(1L<<5)) { | ||||
| 			if(d < -(1L<<15)) { | ||||
| 				if(d < -(1L<<31)) { | ||||
| 					// signed 64 | ||||
| 					castBytes[0] = (byte)0xd3; | ||||
| //					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); | ||||
| 					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); | ||||
| 					castBytes[1] = (byte)(d >>  8); | ||||
| 					castBytes[2] = (byte)(d >>  0); | ||||
| 					out.write(castBytes, 0, 3); | ||||
| 				} else { | ||||
| 					// signed 8 | ||||
| 					castBytes[0] = (byte)0xd0; | ||||
| 					castBytes[1] = (byte)d; | ||||
| 					out.write(castBytes, 0, 2); | ||||
| 				} | ||||
| 			} | ||||
| 		} else if(d < (1<<7)) { | ||||
| 			// fixnum | ||||
| 			out.write((byte)d); | ||||
| 		} else { | ||||
| 			if(d < (1L<<16)) { | ||||
| 				if(d < (1<<8)) { | ||||
| 					// unsigned 8 | ||||
| 					castBytes[0] = (byte)0xcc; | ||||
| 					castBytes[1] = (byte)d; | ||||
| 					out.write(castBytes, 0, 2); | ||||
| 				} else { | ||||
| 					// unsigned 16 | ||||
| 					castBytes[0] = (byte)0xcd; | ||||
| //					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); | ||||
| 				} | ||||
| 			} else { | ||||
| 				if(d < (1L<<32)) { | ||||
| 					// unsigned 32 | ||||
| 					castBytes[0] = (byte)0xce; | ||||
| //					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); | ||||
| 					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); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packBigInteger(BigInteger d) throws IOException { | ||||
| 		if(d.bitLength() <= 63) { | ||||
| 			return packLong(d.longValue()); | ||||
| 		} else if(d.bitLength() <= 64 && d.signum() >= 0) { | ||||
| 			castBytes[0] = (byte)0xcf; | ||||
| 			byte[] barray = d.toByteArray(); | ||||
| 			castBytes[1] = barray[barray.length-8]; | ||||
| 			castBytes[2] = barray[barray.length-7]; | ||||
| 			castBytes[3] = barray[barray.length-6]; | ||||
| 			castBytes[4] = barray[barray.length-5]; | ||||
| 			castBytes[5] = barray[barray.length-4]; | ||||
| 			castBytes[6] = barray[barray.length-3]; | ||||
| 			castBytes[7] = barray[barray.length-2]; | ||||
| 			castBytes[8] = barray[barray.length-1]; | ||||
| 			out.write(castBytes, 0, 9); | ||||
| 			return this; | ||||
| 		} else { | ||||
| 			throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff"); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public Packer packFloat(float d) throws IOException { | ||||
| 		castBytes[0] = (byte)0xca; | ||||
| //		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); | ||||
| 		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; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packNil() throws IOException { | ||||
| 		out.write((byte)0xc0); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packTrue() throws IOException { | ||||
| 		out.write((byte)0xc3); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packFalse() throws IOException { | ||||
| 		out.write((byte)0xc2); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packBoolean(boolean d) throws IOException { | ||||
| 		return d ? packTrue() : packFalse(); | ||||
| 	} | ||||
|  | ||||
| 	public Packer packArray(int n) throws IOException { | ||||
| 		if(n < 16) { | ||||
| 			final int d = 0x90 | n; | ||||
| 			out.write((byte)d); | ||||
| 		} else if(n < 65536) { | ||||
| 			castBytes[0] = (byte)0xdc; | ||||
| //			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); | ||||
| 			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; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packMap(int n) throws IOException { | ||||
| 		if(n < 16) { | ||||
| 			final int d = 0x80 | n; | ||||
| 			out.write((byte)d); | ||||
| 		} else if(n < 65536) { | ||||
| 			castBytes[0] = (byte)0xde; | ||||
| //			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); | ||||
| 			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; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packRaw(int n) throws IOException { | ||||
| 		if(n < 32) { | ||||
| 			final int d = 0xa0 | n; | ||||
| 			out.write((byte)d); | ||||
| 		} else if(n < 65536) { | ||||
| 			castBytes[0] = (byte)0xda; | ||||
| //			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); | ||||
| 			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; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packRawBody(byte[] b) throws IOException { | ||||
| 		out.write(b); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer packRawBody(byte[] b, int off, int length) throws IOException { | ||||
| 		out.write(b, off, length); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	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); | ||||
| 		return packRawBody(b); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	public Packer pack(boolean o) throws IOException { | ||||
| 		if(o) { | ||||
| 			return packTrue(); | ||||
| 		} else { | ||||
| 			return packFalse(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(byte o) throws IOException { | ||||
| 		return packByte(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(short o) throws IOException { | ||||
| 		return packShort(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(int o) throws IOException { | ||||
| 		return packInt(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(long o) throws IOException { | ||||
| 		return packLong(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(float o) throws IOException { | ||||
| 		return packFloat(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(double o) throws IOException { | ||||
| 		return packDouble(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Boolean o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		if(o) { | ||||
| 			return packTrue(); | ||||
| 		} else { | ||||
| 			return packFalse(); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Byte o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packByte(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Short o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packShort(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Integer o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packInt(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Long o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packLong(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(BigInteger o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packBigInteger(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Float o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packFloat(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Double o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packDouble(o); | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(String o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		return packString(o); | ||||
| 	} | ||||
|  | ||||
| 	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()); | ||||
| 		for(Object i : o) { pack(i); } | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Map o) throws IOException { | ||||
| 		if(o == null) { return packNil(); } | ||||
| 		packMap(o.size()); | ||||
| 		for(Map.Entry<Object,Object> e : ((Map<Object,Object>)o).entrySet()) { | ||||
| 			pack(e.getKey()); | ||||
| 			pack(e.getValue()); | ||||
| 		} | ||||
| 		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(); } | ||||
| 		TemplateRegistry.lookup(o.getClass()).pack(this, o); | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public Packer pack(Object o, Template tmpl) throws IOException { | ||||
| 		tmpl.pack(this, o); | ||||
| 		return this; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,21 +0,0 @@ | ||||
| // | ||||
| // 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 { | ||||
| } | ||||
| @@ -1,119 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,29 +0,0 @@ | ||||
| // | ||||
| // 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 class UnpackException extends IOException { | ||||
| 	public UnpackException() { } | ||||
|  | ||||
| 	public UnpackException(String s) { | ||||
| 		super(s); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,53 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| import java.util.Iterator; | ||||
| import java.util.NoSuchElementException; | ||||
|  | ||||
| public class UnpackIterator extends UnpackResult implements Iterator<MessagePackObject> { | ||||
| 	private Unpacker pac; | ||||
|  | ||||
| 	UnpackIterator(Unpacker pac) { | ||||
| 		super(); | ||||
| 		this.pac = pac; | ||||
| 	} | ||||
|  | ||||
| 	public boolean hasNext() { | ||||
| 		if(finished) { return true; } | ||||
| 		try { | ||||
| 			return pac.next(this); | ||||
| 		} catch (IOException e) { | ||||
| 			return false; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public MessagePackObject next() { | ||||
| 		if(!finished && !hasNext()) { | ||||
| 			throw new NoSuchElementException(); | ||||
| 		} | ||||
| 		finished = false; | ||||
| 		return data; | ||||
| 	} | ||||
|  | ||||
| 	public void remove() { | ||||
| 		throw new UnsupportedOperationException(); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,42 +0,0 @@ | ||||
| // | ||||
| // 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 class UnpackResult { | ||||
| 	protected boolean finished = false; | ||||
| 	protected MessagePackObject data = null; | ||||
|  | ||||
| 	public boolean isFinished() { | ||||
| 		return finished; | ||||
| 	} | ||||
|  | ||||
| 	public MessagePackObject getData() { | ||||
| 		return data; | ||||
| 	} | ||||
|  | ||||
| 	public void reset() { | ||||
| 		finished = false; | ||||
| 		data = null; | ||||
| 	} | ||||
|  | ||||
| 	void done(MessagePackObject obj) { | ||||
| 		finished = true; | ||||
| 		data = obj; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,610 +0,0 @@ | ||||
| // | ||||
| // 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.lang.Iterable; | ||||
| import java.io.InputStream; | ||||
| 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. | ||||
|  * | ||||
|  * Unpacker provides Buffered API, Unbuffered API and | ||||
|  * Direct Conversion API. | ||||
|  * | ||||
|  * Buffered API uses the internal buffer of the Unpacker. | ||||
|  * Following code uses Buffered API with an InputStream: | ||||
|  * <pre> | ||||
|  * // create an unpacker with input stream | ||||
|  * Unpacker pac = new Unpacker(System.in); | ||||
|  * | ||||
|  * // take a object out using next() method, or ... | ||||
|  * UnpackResult result = pac.next(); | ||||
|  * | ||||
|  * // use an iterator. | ||||
|  * for(MessagePackObject obj : pac) { | ||||
|  *   // use MessageConvertable interface to convert the | ||||
|  *   // the generic object to the specific type. | ||||
|  * } | ||||
|  * </pre> | ||||
|  * | ||||
|  * Following code doesn't use the input stream and feeds buffer | ||||
|  * using {@link feed(byte[])} method. This is useful to use | ||||
|  * special stream like zlib or event-driven I/O library. | ||||
|  * <pre> | ||||
|  * // create an unpacker without input stream | ||||
|  * Unpacker pac = new Unpacker(); | ||||
|  * | ||||
|  * // feed buffer to the internal buffer. | ||||
|  * pac.feed(input_bytes); | ||||
|  * | ||||
|  * // use next() method or iterators. | ||||
|  * for(MessagePackObject obj : pac) { | ||||
|  *   // ... | ||||
|  * } | ||||
|  * </pre> | ||||
|  * | ||||
|  * The combination of {@link reserveBuffer()}, {@link getBuffer()}, | ||||
|  * {@link getBufferOffset()}, {@link getBufferCapacity()} and | ||||
|  * {@link bufferConsumed()} is useful to omit copying. | ||||
|  * <pre> | ||||
|  * // create an unpacker without input stream | ||||
|  * Unpacker pac = new Unpacker(); | ||||
|  * | ||||
|  * // reserve internal buffer at least 1024 bytes. | ||||
|  * pac.reserveBuffer(1024); | ||||
|  * | ||||
|  * // feed buffer to the internal buffer upto pac.getBufferCapacity() bytes. | ||||
|  * System.in.read(pac.getBuffer(), pac.getBufferOffset(), pac.getBufferCapacity()); | ||||
|  * | ||||
|  * // use next() method or iterators. | ||||
|  * for(MessagePackObject obj : pac) { | ||||
|  *     // ... | ||||
|  * } | ||||
|  * </pre> | ||||
|  * | ||||
|  * Unbuffered API doesn't initialize the internal buffer. | ||||
|  * You can manage the buffer manually. | ||||
|  * <pre> | ||||
|  * // create an unpacker with input stream | ||||
|  * Unpacker pac = new Unpacker(System.in); | ||||
|  * | ||||
|  * // manage the buffer manually. | ||||
|  * byte[] buffer = new byte[1024]; | ||||
|  * int filled = System.in.read(buffer); | ||||
|  * int offset = 0; | ||||
|  * | ||||
|  * // deserialize objects using execute() method. | ||||
|  * int nextOffset = pac.execute(buffer, offset, filled); | ||||
|  * | ||||
|  * // take out object if deserialized object is ready. | ||||
|  * if(pac.isFinished()) { | ||||
|  *     MessagePackObject obj = pac.getData(); | ||||
|  *     // ... | ||||
|  * } | ||||
|  * </pre> | ||||
|  */ | ||||
| public class Unpacker implements Iterable<MessagePackObject> { | ||||
| 	// buffer: | ||||
| 	// +---------------------------------------------+ | ||||
| 	// | [object] | [obje| unparsed ... | unused  ...| | ||||
| 	// +---------------------------------------------+ | ||||
| 	//            ^ parsed | ||||
| 	//                   ^ offset | ||||
| 	//                                  ^ filled | ||||
| 	//                                               ^ buffer.length | ||||
|  | ||||
| 	private static final int DEFAULT_BUFFER_SIZE = 32*1024; | ||||
|  | ||||
| 	protected int parsed; | ||||
| 	protected int bufferReserveSize; | ||||
| 	protected InputStream stream; | ||||
|  | ||||
| 	final class BufferedUnpackerMixin extends BufferedUnpackerImpl { | ||||
| 		boolean fill() throws IOException { | ||||
| 			if(stream == null) { | ||||
| 				return false; | ||||
| 			} | ||||
| 			reserveBuffer(bufferReserveSize); | ||||
| 			int rl = stream.read(buffer, filled, buffer.length - filled); | ||||
| 			// equals: stream.read(getBuffer(), getBufferOffset(), getBufferCapacity()); | ||||
| 			if(rl <= 0) { | ||||
| 				return false; | ||||
| 			} | ||||
| 			bufferConsumed(rl); | ||||
| 			return true; | ||||
| 		} | ||||
| 	}; | ||||
|  | ||||
| 	final BufferedUnpackerMixin impl = new BufferedUnpackerMixin(); | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Calls {@link Unpacker(DEFAULT_BUFFER_SIZE)} | ||||
| 	 */ | ||||
| 	public Unpacker() { | ||||
| 		this(DEFAULT_BUFFER_SIZE); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Calls {@link Unpacker(null, bufferReserveSize)} | ||||
| 	 */ | ||||
| 	public Unpacker(int bufferReserveSize) { | ||||
| 		this(null, bufferReserveSize); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Calls {@link Unpacker(stream, DEFAULT_BUFFER_SIZE)} | ||||
| 	 */ | ||||
| 	public Unpacker(InputStream stream) { | ||||
| 		this(stream, DEFAULT_BUFFER_SIZE); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Constructs the unpacker. | ||||
| 	 * The stream is used to fill the buffer when more buffer is required by {@link next()} or {@link UnpackIterator#hasNext()} method. | ||||
| 	 * @param stream input stream to fill the buffer | ||||
| 	 * @param bufferReserveSize threshold size to expand the size of buffer | ||||
| 	 */ | ||||
| 	public Unpacker(InputStream stream, int bufferReserveSize) { | ||||
| 		this.parsed = 0; | ||||
| 		this.bufferReserveSize = bufferReserveSize/2; | ||||
| 		this.stream = stream; | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets the input stream. | ||||
| 	 * @return the input stream. it may be null. | ||||
| 	 */ | ||||
| 	public InputStream getStream() { | ||||
| 		return this.stream; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Sets the input stream. | ||||
| 	 * @param stream the input stream to set. | ||||
| 	 */ | ||||
| 	public void setStream(InputStream stream) { | ||||
| 		this.stream = stream; | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Fills the buffer with the specified buffer. | ||||
| 	 */ | ||||
| 	public void feed(byte[] buffer) { | ||||
| 		feed(buffer, 0, buffer.length); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Fills the buffer with the specified buffer. | ||||
| 	 */ | ||||
| 	public void feed(byte[] buffer, int offset, int length) { | ||||
| 		reserveBuffer(length); | ||||
| 		System.arraycopy(buffer, offset, impl.buffer, impl.offset, length); | ||||
| 		bufferConsumed(length); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Fills the buffer with the specified buffer. | ||||
| 	 */ | ||||
| 	public void feed(ByteBuffer buffer) { | ||||
| 		int length = buffer.remaining(); | ||||
| 		if (length == 0) return; | ||||
| 		reserveBuffer(length); | ||||
| 		buffer.get(impl.buffer, impl.offset, length); | ||||
| 		bufferConsumed(length); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Swaps the internal buffer with the specified buffer. | ||||
| 	 * This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method. | ||||
| 	 */ | ||||
| 	public void wrap(byte[] buffer) { | ||||
| 		wrap(buffer, 0, buffer.length); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Swaps the internal buffer with the specified buffer. | ||||
| 	 * This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method. | ||||
| 	 */ | ||||
| 	public void wrap(byte[] buffer, int offset, int length) { | ||||
| 		impl.buffer = buffer; | ||||
| 		impl.offset = offset; | ||||
| 		impl.filled = length; | ||||
| 		impl.bufferReferenced = false;  // TODO zero-copy buffer | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Fills the internal using the input stream. | ||||
| 	 * @return false if the stream is null or stream.read returns <= 0. | ||||
| 	 */ | ||||
| 	public boolean fill() throws IOException { | ||||
| 		return impl.fill(); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns the iterator that calls {@link next()} method repeatedly. | ||||
| 	 */ | ||||
| 	public Iterator<MessagePackObject> iterator() { | ||||
| 		return new UnpackIterator(this); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Deserializes one object and returns it. | ||||
| 	 * @return {@link UnpackResult#isFinished()} returns false if the buffer is insufficient to deserialize one object. | ||||
| 	 */ | ||||
| 	public UnpackResult next() throws IOException, UnpackException { | ||||
| 		UnpackResult result = new UnpackResult(); | ||||
| 		impl.next(result); | ||||
| 		return result; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Deserializes one object and returns it. | ||||
| 	 * @return false if the buffer is insufficient to deserialize one object. | ||||
| 	 */ | ||||
| 	public boolean next(UnpackResult result) throws IOException, UnpackException { | ||||
| 		return impl.next(result); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Reserve free space of the internal buffer at least specified size and expands {@link getBufferCapacity()}. | ||||
| 	 */ | ||||
| 	public void reserveBuffer(int require) { | ||||
| 		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.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) { | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		int nextSize = impl.buffer.length * 2; | ||||
| 		int notParsed = impl.filled - impl.offset; | ||||
| 		while(nextSize < require + notParsed) { | ||||
| 			nextSize *= 2; | ||||
| 		} | ||||
|  | ||||
| 		byte[] tmp = new byte[nextSize]; | ||||
| 		System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset); | ||||
|  | ||||
| 		impl.buffer = tmp; | ||||
| 		impl.filled = notParsed; | ||||
| 		impl.offset = 0; | ||||
| 		impl.bufferReferenced = false;  // TODO zero-copy buffer | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns the internal buffer. | ||||
| 	 */ | ||||
| 	public byte[] getBuffer() { | ||||
| 		return impl.buffer; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns the size of free space of the internal buffer. | ||||
| 	 */ | ||||
| 	public int getBufferCapacity() { | ||||
| 		return impl.buffer.length - impl.filled; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns the offset of free space in the internal buffer. | ||||
| 	 */ | ||||
| 	public int getBufferOffset() { | ||||
| 		return impl.filled; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Moves front the offset of the free space in the internal buffer. | ||||
| 	 * Call this method after fill the buffer manually using {@link reserveBuffer()}, {@link getBuffer()}, {@link getBufferOffset()} and {@link getBufferCapacity()} methods. | ||||
| 	 */ | ||||
| 	public void bufferConsumed(int size) { | ||||
| 		impl.filled += size; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Deserializes one object upto the offset of the internal buffer. | ||||
| 	 * Call {@link reset()} method before calling this method again. | ||||
| 	 * @return true if one object is deserialized. Use {@link getData()} to get the deserialized object. | ||||
| 	 */ | ||||
| 	public boolean execute() throws UnpackException { | ||||
| 		int noffset = impl.execute(impl.buffer, impl.offset, impl.filled); | ||||
| 		if(noffset <= impl.offset) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		parsed += noffset - impl.offset; | ||||
| 		impl.offset = noffset; | ||||
| 		return impl.isFinished(); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Deserializes one object over the specified buffer. | ||||
| 	 * This method doesn't use the internal buffer. | ||||
| 	 * Use {@link isFinished()} method to known a object is ready to get. | ||||
| 	 * Call {@link reset()} method before calling this method again. | ||||
| 	 * @return offset position that is parsed. | ||||
| 	 */ | ||||
| 	public int execute(byte[] buffer) throws UnpackException { | ||||
| 		return execute(buffer, 0, buffer.length); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Deserializes one object over the specified buffer. | ||||
| 	 * This method doesn't use the internal buffer. | ||||
| 	 * Use {@link isFinished()} method to known a object is ready to get. | ||||
| 	 * Call {@link reset()} method before calling this method again. | ||||
| 	 * @return offset position that is parsed. | ||||
| 	 */ | ||||
| 	public int execute(byte[] buffer, int offset, int length) throws UnpackException { | ||||
| 		int noffset = impl.execute(buffer, offset, length); | ||||
| 		if(impl.isFinished()) { | ||||
| 			impl.resetState(); | ||||
| 		} | ||||
| 		return noffset; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets the object deserialized by {@link execute(byte[])} method. | ||||
| 	 */ | ||||
| 	public MessagePackObject getData() { | ||||
| 		return impl.getData(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Returns true if an object is ready to get with {@link getData()} method. | ||||
| 	 */ | ||||
| 	public boolean isFinished() { | ||||
| 		return impl.isFinished(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Resets the internal state of the unpacker. | ||||
| 	 */ | ||||
| 	public void reset() { | ||||
| 		impl.reset(); | ||||
| 	} | ||||
|  | ||||
| 	public int getMessageSize() { | ||||
| 		return parsed - impl.offset + impl.filled; | ||||
| 	} | ||||
|  | ||||
| 	public int getParsedSize() { | ||||
| 		return parsed; | ||||
| 	} | ||||
|  | ||||
| 	public int getNonParsedSize() { | ||||
| 		return impl.filled - impl.offset; | ||||
| 	} | ||||
|  | ||||
| 	public void skipNonparsedBuffer(int size) { | ||||
| 		impl.offset += size; | ||||
| 	} | ||||
|  | ||||
| 	public void removeNonparsedBuffer() { | ||||
| 		impl.filled = impl.offset; | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code byte} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code byte}. | ||||
| 	 */ | ||||
| 	public byte unpackByte() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackByte(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code short} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code short}. | ||||
| 	 */ | ||||
| 	public short unpackShort() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackShort(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code int} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code int}. | ||||
| 	 */ | ||||
| 	public int unpackInt() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackInt(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code long} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code long}. | ||||
| 	 */ | ||||
| 	public long unpackLong() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackLong(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code BigInteger} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code BigInteger}. | ||||
| 	 */ | ||||
| 	public BigInteger unpackBigInteger() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackBigInteger(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code float} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code float}. | ||||
| 	 */ | ||||
| 	public float unpackFloat() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackFloat(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code double} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code double}. | ||||
| 	 */ | ||||
| 	public double unpackDouble() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackDouble(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code null} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code null}. | ||||
| 	 */ | ||||
| 	public Object unpackNull() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackNull(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code boolean} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code boolean}. | ||||
| 	 */ | ||||
| 	public boolean unpackBoolean() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackBoolean(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one array header from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @return the length of the map. There are {@code retval} objects to get. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a array. | ||||
| 	 */ | ||||
| 	public int unpackArray() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackArray(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one map header from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @return the length of the map. There are {@code retval * 2} objects to get. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a map. | ||||
| 	 */ | ||||
| 	public int unpackMap() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackMap(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one raw header from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 * @return the length of the raw bytes. There are {@code retval} bytes to get. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a raw bytes. | ||||
| 	 */ | ||||
| 	public int unpackRaw() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackRaw(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one raw body from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 */ | ||||
| 	public byte[] unpackRawBody(int length) throws IOException { | ||||
| 		return impl.unpackRawBody(length); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * 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. | ||||
| 	 * @throws MessageTypeException the first value of the buffer is not a {@code String}. | ||||
| 	 */ | ||||
| 	final public String unpackString() throws IOException, MessageTypeException { | ||||
| 		return impl.unpackString(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Gets one {@code Object} value from the buffer. | ||||
| 	 * This method calls {@link fill()} method if needed. | ||||
| 	 */ | ||||
| 	final public MessagePackObject unpackObject() throws IOException { | ||||
| 		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 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); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,473 +0,0 @@ | ||||
| // | ||||
| // 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.nio.ByteBuffer; | ||||
| import java.math.BigInteger; | ||||
| import org.msgpack.object.*; | ||||
|  | ||||
| public class UnpackerImpl { | ||||
| 	static final int CS_HEADER      = 0x00; | ||||
| 	static final int CS_FLOAT       = 0x0a; | ||||
| 	static final int CS_DOUBLE      = 0x0b; | ||||
| 	static final int CS_UINT_8      = 0x0c; | ||||
| 	static final int CS_UINT_16     = 0x0d; | ||||
| 	static final int CS_UINT_32     = 0x0e; | ||||
| 	static final int CS_UINT_64     = 0x0f; | ||||
| 	static final int CS_INT_8       = 0x10; | ||||
| 	static final int CS_INT_16      = 0x11; | ||||
| 	static final int CS_INT_32      = 0x12; | ||||
| 	static final int CS_INT_64      = 0x13; | ||||
| 	static final int CS_RAW_16      = 0x1a; | ||||
| 	static final int CS_RAW_32      = 0x1b; | ||||
| 	static final int CS_ARRAY_16    = 0x1c; | ||||
| 	static final int CS_ARRAY_32    = 0x1d; | ||||
| 	static final int CS_MAP_16      = 0x1e; | ||||
| 	static final int CS_MAP_32      = 0x1f; | ||||
| 	static final int ACS_RAW_VALUE  = 0x20; | ||||
| 	static final int CT_ARRAY_ITEM  = 0x00; | ||||
| 	static final int CT_MAP_KEY     = 0x01; | ||||
| 	static final int CT_MAP_VALUE   = 0x02; | ||||
|  | ||||
| 	static final int MAX_STACK_SIZE = 32; | ||||
|  | ||||
| 	private int cs; | ||||
| 	private int trail; | ||||
| 	private int top; | ||||
| 	private int[]    stack_ct       = new int[MAX_STACK_SIZE]; | ||||
| 	private int[]    stack_count    = new int[MAX_STACK_SIZE]; | ||||
| 	private Object[] stack_obj      = new Object[MAX_STACK_SIZE]; | ||||
| 	private int top_ct; | ||||
| 	private int top_count; | ||||
| 	private Object top_obj; | ||||
| 	private ByteBuffer castBuffer   = ByteBuffer.allocate(8); | ||||
| 	private boolean finished = false; | ||||
| 	private MessagePackObject data = null; | ||||
|  | ||||
| 	public UnpackerImpl() | ||||
| 	{ | ||||
| 		reset(); | ||||
| 	} | ||||
|  | ||||
| 	public final MessagePackObject getData() | ||||
| 	{ | ||||
| 		return data; | ||||
| 	} | ||||
|  | ||||
| 	public final boolean isFinished() | ||||
| 	{ | ||||
| 		return finished; | ||||
| 	} | ||||
|  | ||||
| 	public final void resetState() { | ||||
| 		cs = CS_HEADER; | ||||
| 		top = -1; | ||||
| 		top_ct = 0; | ||||
| 		top_count = 0; | ||||
| 		top_obj = null; | ||||
| 	} | ||||
|  | ||||
| 	public final void reset() | ||||
| 	{ | ||||
| 		resetState(); | ||||
| 		finished = false; | ||||
| 		data = null; | ||||
| 	} | ||||
|  | ||||
| 	@SuppressWarnings("unchecked") | ||||
| 	public final int execute(byte[] src, int off, int length) throws UnpackException | ||||
| 	{ | ||||
| 		if(off >= length) { return off; } | ||||
|  | ||||
| 		int limit = length; | ||||
| 		int i = off; | ||||
| 		int count; | ||||
|  | ||||
| 		Object obj = null; | ||||
|  | ||||
| 		_out: do { | ||||
| 		_header_again: { | ||||
| 			//System.out.println("while i:"+i+" limit:"+limit); | ||||
|  | ||||
| 			int b = src[i]; | ||||
|  | ||||
| 			_push: { | ||||
| 				_fixed_trail_again: | ||||
| 				if(cs == CS_HEADER) { | ||||
| 	 | ||||
| 					if((b & 0x80) == 0) {  // Positive Fixnum | ||||
| 						//System.out.println("positive fixnum "+b); | ||||
| 						obj = IntegerType.create((byte)b); | ||||
| 						break _push; | ||||
| 					} | ||||
| 	 | ||||
| 					if((b & 0xe0) == 0xe0) {  // Negative Fixnum | ||||
| 						//System.out.println("negative fixnum "+b); | ||||
| 						obj = IntegerType.create((byte)b); | ||||
| 						break _push; | ||||
| 					} | ||||
| 	 | ||||
| 					if((b & 0xe0) == 0xa0) {  // FixRaw | ||||
| 						trail = b & 0x1f; | ||||
| 						if(trail == 0) { | ||||
| 							obj = RawType.create(new byte[0]); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						cs = ACS_RAW_VALUE; | ||||
| 						break _fixed_trail_again; | ||||
| 					} | ||||
| 	 | ||||
| 					if((b & 0xf0) == 0x90) {  // FixArray | ||||
| 						if(top >= MAX_STACK_SIZE) { | ||||
| 							throw new UnpackException("parse error"); | ||||
| 						} | ||||
| 						count = b & 0x0f; | ||||
| 						//System.out.println("fixarray count:"+count); | ||||
| 						obj = new MessagePackObject[count]; | ||||
| 						if(count == 0) { | ||||
| 							obj = ArrayType.create((MessagePackObject[])obj); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						++top; | ||||
| 						stack_obj[top]    = top_obj; | ||||
| 						stack_ct[top]     = top_ct; | ||||
| 						stack_count[top]  = top_count; | ||||
| 						top_obj    = obj; | ||||
| 						top_ct     = CT_ARRAY_ITEM; | ||||
| 						top_count  = count; | ||||
| 						break _header_again; | ||||
| 					} | ||||
| 	 | ||||
| 					if((b & 0xf0) == 0x80) {  // FixMap | ||||
| 						if(top >= MAX_STACK_SIZE) { | ||||
| 							throw new UnpackException("parse error"); | ||||
| 						} | ||||
| 						count = b & 0x0f; | ||||
| 						obj = new MessagePackObject[count*2]; | ||||
| 						if(count == 0) { | ||||
| 							obj = MapType.create((MessagePackObject[])obj); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						//System.out.println("fixmap count:"+count); | ||||
| 						++top; | ||||
| 						stack_obj[top]    = top_obj; | ||||
| 						stack_ct[top]     = top_ct; | ||||
| 						stack_count[top]  = top_count; | ||||
| 						top_obj    = obj; | ||||
| 						top_ct     = CT_MAP_KEY; | ||||
| 						top_count  = count; | ||||
| 						break _header_again; | ||||
| 					} | ||||
| 	 | ||||
| 					switch(b & 0xff) {    // FIXME | ||||
| 					case 0xc0:  // nil | ||||
| 						obj = NilType.create(); | ||||
| 						break _push; | ||||
| 					case 0xc2:  // false | ||||
| 						obj = BooleanType.create(false); | ||||
| 						break _push; | ||||
| 					case 0xc3:  // true | ||||
| 						obj = BooleanType.create(true); | ||||
| 						break _push; | ||||
| 					case 0xca:  // float | ||||
| 					case 0xcb:  // double | ||||
| 					case 0xcc:  // unsigned int  8 | ||||
| 					case 0xcd:  // unsigned int 16 | ||||
| 					case 0xce:  // unsigned int 32 | ||||
| 					case 0xcf:  // unsigned int 64 | ||||
| 					case 0xd0:  // signed int  8 | ||||
| 					case 0xd1:  // signed int 16 | ||||
| 					case 0xd2:  // signed int 32 | ||||
| 					case 0xd3:  // signed int 64 | ||||
| 						trail = 1 << (b & 0x03); | ||||
| 						cs = b & 0x1f; | ||||
| 						//System.out.println("a trail "+trail+"  cs:"+cs); | ||||
| 						break _fixed_trail_again; | ||||
| 					case 0xda:  // raw 16 | ||||
| 					case 0xdb:  // raw 32 | ||||
| 					case 0xdc:  // array 16 | ||||
| 					case 0xdd:  // array 32 | ||||
| 					case 0xde:  // map 16 | ||||
| 					case 0xdf:  // map 32 | ||||
| 						trail = 2 << (b & 0x01); | ||||
| 						cs = b & 0x1f; | ||||
| 						//System.out.println("b trail "+trail+"  cs:"+cs); | ||||
| 						break _fixed_trail_again; | ||||
| 					default: | ||||
| 						//System.out.println("unknown b "+(b&0xff)); | ||||
| 						throw new UnpackException("parse error"); | ||||
| 					} | ||||
| 	 | ||||
| 				}  // _fixed_trail_again | ||||
|  | ||||
| 				do { | ||||
| 				_fixed_trail_again: { | ||||
| 	 | ||||
| 					if(limit - i <= trail) { break _out; } | ||||
| 					int n = i + 1; | ||||
| 					i += trail; | ||||
|  | ||||
| 					switch(cs) { | ||||
| 					case CS_FLOAT: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 4); | ||||
| 						obj = FloatType.create( castBuffer.getFloat(0) ); | ||||
| 						//System.out.println("float "+obj); | ||||
| 						break _push; | ||||
| 					case CS_DOUBLE: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 8); | ||||
| 						obj = FloatType.create( castBuffer.getDouble(0) ); | ||||
| 						//System.out.println("double "+obj); | ||||
| 						break _push; | ||||
| 					case CS_UINT_8: | ||||
| 						//System.out.println(n); | ||||
| 						//System.out.println(src[n]); | ||||
| 						//System.out.println(src[n+1]); | ||||
| 						//System.out.println(src[n-1]); | ||||
| 						obj = IntegerType.create( (short)((src[n]) & 0xff) ); | ||||
| 						//System.out.println("uint8 "+obj); | ||||
| 						break _push; | ||||
| 					case CS_UINT_16: | ||||
| 						//System.out.println(src[n]); | ||||
| 						//System.out.println(src[n+1]); | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 2); | ||||
| 						obj = IntegerType.create( ((int)castBuffer.getShort(0)) & 0xffff ); | ||||
| 						//System.out.println("uint 16 "+obj); | ||||
| 						break _push; | ||||
| 					case CS_UINT_32: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 4); | ||||
| 						obj = IntegerType.create( ((long)castBuffer.getInt(0)) & 0xffffffffL ); | ||||
| 						//System.out.println("uint 32 "+obj); | ||||
| 						break _push; | ||||
| 					case CS_UINT_64: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 8); | ||||
| 						{ | ||||
| 							long o = castBuffer.getLong(0); | ||||
| 							if(o < 0) { | ||||
| 								obj = IntegerType.create(new BigInteger(1, castBuffer.array())); | ||||
| 							} else { | ||||
| 								obj = IntegerType.create(o); | ||||
| 							} | ||||
| 						} | ||||
| 						break _push; | ||||
| 					case CS_INT_8: | ||||
| 						obj = IntegerType.create( src[n] ); | ||||
| 						break _push; | ||||
| 					case CS_INT_16: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 2); | ||||
| 						obj = IntegerType.create( castBuffer.getShort(0) ); | ||||
| 						break _push; | ||||
| 					case CS_INT_32: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 4); | ||||
| 						obj = IntegerType.create( castBuffer.getInt(0) ); | ||||
| 						break _push; | ||||
| 					case CS_INT_64: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 8); | ||||
| 						obj = IntegerType.create( castBuffer.getLong(0) ); | ||||
| 						break _push; | ||||
| 					case CS_RAW_16: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 2); | ||||
| 						trail = ((int)castBuffer.getShort(0)) & 0xffff; | ||||
| 						if(trail == 0) { | ||||
| 							obj = RawType.create(new byte[0]); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						cs = ACS_RAW_VALUE; | ||||
| 						break _fixed_trail_again; | ||||
| 					case CS_RAW_32: | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 4); | ||||
| 						// FIXME overflow check | ||||
| 						trail = castBuffer.getInt(0) & 0x7fffffff; | ||||
| 						if(trail == 0) { | ||||
| 							obj = RawType.create(new byte[0]); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						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); | ||||
| 						} | ||||
| 						break _push; | ||||
| 					case CS_ARRAY_16: | ||||
| 						if(top >= MAX_STACK_SIZE) { | ||||
| 							throw new UnpackException("parse error"); | ||||
| 						} | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 2); | ||||
| 						count = ((int)castBuffer.getShort(0)) & 0xffff; | ||||
| 						obj = new MessagePackObject[count]; | ||||
| 						if(count == 0) { | ||||
| 							obj = ArrayType.create((MessagePackObject[])obj); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						++top; | ||||
| 						stack_obj[top]    = top_obj; | ||||
| 						stack_ct[top]     = top_ct; | ||||
| 						stack_count[top]  = top_count; | ||||
| 						top_obj    = obj; | ||||
| 						top_ct     = CT_ARRAY_ITEM; | ||||
| 						top_count  = count; | ||||
| 						break _header_again; | ||||
| 					case CS_ARRAY_32: | ||||
| 						if(top >= MAX_STACK_SIZE) { | ||||
| 							throw new UnpackException("parse error"); | ||||
| 						} | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 4); | ||||
| 						// FIXME overflow check | ||||
| 						count = castBuffer.getInt(0) & 0x7fffffff; | ||||
| 						obj = new MessagePackObject[count]; | ||||
| 						if(count == 0) { | ||||
| 							obj = ArrayType.create((MessagePackObject[])obj); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						++top; | ||||
| 						stack_obj[top]    = top_obj; | ||||
| 						stack_ct[top]     = top_ct; | ||||
| 						stack_count[top]  = top_count; | ||||
| 						top_obj    = obj; | ||||
| 						top_ct     = CT_ARRAY_ITEM; | ||||
| 						top_count  = count; | ||||
| 						break _header_again; | ||||
| 					case CS_MAP_16: | ||||
| 						if(top >= MAX_STACK_SIZE) { | ||||
| 							throw new UnpackException("parse error"); | ||||
| 						} | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 2); | ||||
| 						count = ((int)castBuffer.getShort(0)) & 0xffff; | ||||
| 						obj = new MessagePackObject[count*2]; | ||||
| 						if(count == 0) { | ||||
| 							obj = MapType.create((MessagePackObject[])obj); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						//System.out.println("fixmap count:"+count); | ||||
| 						++top; | ||||
| 						stack_obj[top]    = top_obj; | ||||
| 						stack_ct[top]     = top_ct; | ||||
| 						stack_count[top]  = top_count; | ||||
| 						top_obj    = obj; | ||||
| 						top_ct     = CT_MAP_KEY; | ||||
| 						top_count  = count; | ||||
| 						break _header_again; | ||||
| 					case CS_MAP_32: | ||||
| 						if(top >= MAX_STACK_SIZE) { | ||||
| 							throw new UnpackException("parse error"); | ||||
| 						} | ||||
| 						castBuffer.rewind(); | ||||
| 						castBuffer.put(src, n, 4); | ||||
| 						// FIXME overflow check | ||||
| 						count = castBuffer.getInt(0) & 0x7fffffff; | ||||
| 						obj = new MessagePackObject[count*2]; | ||||
| 						if(count == 0) { | ||||
| 							obj = MapType.create((MessagePackObject[])obj); | ||||
| 							break _push; | ||||
| 						} | ||||
| 						//System.out.println("fixmap count:"+count); | ||||
| 						++top; | ||||
| 						stack_obj[top]    = top_obj; | ||||
| 						stack_ct[top]     = top_ct; | ||||
| 						stack_count[top]  = top_count; | ||||
| 						top_obj    = obj; | ||||
| 						top_ct     = CT_MAP_KEY; | ||||
| 						top_count  = count; | ||||
| 						break _header_again; | ||||
| 					default: | ||||
| 						throw new UnpackException("parse error"); | ||||
| 					} | ||||
|  | ||||
| 				}  // _fixed_trail_again | ||||
| 				}  while(true); | ||||
| 			}  // _push | ||||
|  | ||||
| 			do { | ||||
| 			_push: { | ||||
| 				//System.out.println("push top:"+top); | ||||
| 				if(top == -1) { | ||||
| 					++i; | ||||
| 					data = (MessagePackObject)obj; | ||||
| 					finished = true; | ||||
| 					break _out; | ||||
| 				} | ||||
|  | ||||
| 				switch(top_ct) { | ||||
| 				case CT_ARRAY_ITEM: { | ||||
| 						//System.out.println("array item "+obj); | ||||
| 						Object[] ar = (Object[])top_obj; | ||||
| 						ar[ar.length - top_count] = obj; | ||||
| 						if(--top_count == 0) { | ||||
| 							top_obj    = stack_obj[top]; | ||||
| 							top_ct     = stack_ct[top]; | ||||
| 							top_count  = stack_count[top]; | ||||
| 							obj = ArrayType.create((MessagePackObject[])ar); | ||||
| 							stack_obj[top] = null; | ||||
| 							--top; | ||||
| 							break _push; | ||||
| 						} | ||||
| 						break _header_again; | ||||
| 					} | ||||
| 				case CT_MAP_KEY: { | ||||
| 						//System.out.println("map key:"+top+" "+obj); | ||||
| 						Object[] mp = (Object[])top_obj; | ||||
| 						mp[mp.length - top_count*2] = obj; | ||||
| 						top_ct = CT_MAP_VALUE; | ||||
| 						break _header_again; | ||||
| 					} | ||||
| 				case CT_MAP_VALUE: { | ||||
| 						//System.out.println("map value:"+top+" "+obj); | ||||
| 						Object[] mp = (Object[])top_obj; | ||||
| 						mp[mp.length - top_count*2 + 1] = obj; | ||||
| 						if(--top_count == 0) { | ||||
| 							top_obj    = stack_obj[top]; | ||||
| 							top_ct     = stack_ct[top]; | ||||
| 							top_count  = stack_count[top]; | ||||
| 							obj = MapType.create((MessagePackObject[])mp); | ||||
| 							stack_obj[top] = null; | ||||
| 							--top; | ||||
| 							break _push; | ||||
| 						} | ||||
| 						top_ct = CT_MAP_KEY; | ||||
| 						break _header_again; | ||||
| 					} | ||||
| 				default: | ||||
| 					throw new UnpackException("parse error"); | ||||
| 				} | ||||
| 			}  // _push | ||||
| 			} while(true); | ||||
|  | ||||
| 		}  // _header_again | ||||
| 		cs = CS_HEADER; | ||||
| 		++i; | ||||
| 		} while(i < limit);  // _out | ||||
|  | ||||
| 		return i; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,28 +0,0 @@ | ||||
| // | ||||
| // 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 { | ||||
| } | ||||
| @@ -1,29 +0,0 @@ | ||||
| // | ||||
| // 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(); | ||||
| } | ||||
| @@ -1,35 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| } | ||||
| @@ -1,29 +0,0 @@ | ||||
| // | ||||
| // 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(); | ||||
| } | ||||
| @@ -1,30 +0,0 @@ | ||||
| // | ||||
| // 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; | ||||
| } | ||||
| @@ -1,28 +0,0 @@ | ||||
| // | ||||
| // 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 { | ||||
| } | ||||
| @@ -1,28 +0,0 @@ | ||||
| // | ||||
| // 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 { | ||||
| } | ||||
| @@ -1,28 +0,0 @@ | ||||
| // | ||||
| // 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 { | ||||
| } | ||||
| @@ -1,28 +0,0 @@ | ||||
| // | ||||
| // 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 { | ||||
| } | ||||
| @@ -1,462 +0,0 @@ | ||||
| // | ||||
| // 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,81 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Arrays; | ||||
| import java.io.IOException; | ||||
| import org.msgpack.*; | ||||
|  | ||||
| public class ArrayType extends MessagePackObject { | ||||
| 	private MessagePackObject[] array; | ||||
|  | ||||
| 	ArrayType(MessagePackObject[] array) { | ||||
| 		this.array = array; | ||||
| 	} | ||||
|  | ||||
| 	public static ArrayType create(MessagePackObject[] array) { | ||||
| 		return new ArrayType(array); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean isArrayType() { | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public MessagePackObject[] asArray() { | ||||
| 		return array; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public List<MessagePackObject> asList() { | ||||
| 		return Arrays.asList(array); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void messagePack(Packer pk) throws IOException { | ||||
| 		pk.packArray(array.length); | ||||
| 		for(int i=0; i < array.length; i++) { | ||||
| 			array[i].messagePack(pk); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object obj) { | ||||
| 		if(obj.getClass() != getClass()) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		return Arrays.equals(((ArrayType)obj).array, array); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return array.hashCode(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Object clone() { | ||||
| 		MessagePackObject[] copy = new MessagePackObject[array.length]; | ||||
| 		for(int i=0; i < array.length; i++) { | ||||
| 			copy[i] = (MessagePackObject)array[i].clone(); | ||||
| 		} | ||||
| 		return copy; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,131 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import java.math.BigInteger; | ||||
| import java.io.IOException; | ||||
| import org.msgpack.*; | ||||
|  | ||||
| class BigIntegerTypeIMPL extends IntegerType { | ||||
| 	private BigInteger value; | ||||
|  | ||||
| 	BigIntegerTypeIMPL(BigInteger value) { | ||||
| 		this.value = value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public byte asByte() { | ||||
| 		if(value.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) > 0) { | ||||
| 			throw new MessageTypeException("type error"); | ||||
| 		} | ||||
| 		return value.byteValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public short asShort() { | ||||
| 		if(value.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) > 0) { | ||||
| 			throw new MessageTypeException("type error"); | ||||
| 		} | ||||
| 		return value.shortValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int asInt() { | ||||
| 		if(value.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) > 0) { | ||||
| 			throw new MessageTypeException("type error"); | ||||
| 		} | ||||
| 		return value.intValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public long asLong() { | ||||
| 		if(value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { | ||||
| 			throw new MessageTypeException("type error"); | ||||
| 		} | ||||
| 		return value.longValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public BigInteger asBigInteger() { | ||||
| 		return value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public byte byteValue() { | ||||
| 		return value.byteValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public short shortValue() { | ||||
| 		return value.shortValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int intValue() { | ||||
| 		return value.intValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public long longValue() { | ||||
| 		return value.longValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public BigInteger bigIntegerValue() { | ||||
| 		return value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public float floatValue() { | ||||
| 		return value.floatValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public double doubleValue() { | ||||
| 		return value.doubleValue(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void messagePack(Packer pk) throws IOException { | ||||
| 		pk.packBigInteger(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object obj) { | ||||
| 		if(obj.getClass() != getClass()) { | ||||
| 			if(obj.getClass() == ShortIntegerTypeIMPL.class) { | ||||
| 				return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value); | ||||
| 			} else if(obj.getClass() == LongIntegerTypeIMPL.class) { | ||||
| 				return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); | ||||
| 			} | ||||
| 			return false; | ||||
| 		} | ||||
| 		return ((BigIntegerTypeIMPL)obj).value.equals(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return value.hashCode(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Object clone() { | ||||
| 		return new BigIntegerTypeIMPL(value); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,71 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import java.io.IOException; | ||||
| import org.msgpack.*; | ||||
|  | ||||
| public class BooleanType extends MessagePackObject { | ||||
| 	private boolean value; | ||||
|  | ||||
| 	BooleanType(boolean value) { | ||||
| 		this.value = value; | ||||
| 	} | ||||
|  | ||||
| 	public static BooleanType create(boolean value) { | ||||
| 		return new BooleanType(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean isBooleanType() { | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean asBoolean() { | ||||
| 		return value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void messagePack(Packer pk) throws IOException { | ||||
| 		pk.packBoolean(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object obj) { | ||||
| 		if(obj.getClass() != getClass()) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		return ((BooleanType)obj).value == value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		if(value) { | ||||
| 			return 1231; | ||||
| 		} else { | ||||
| 			return 1237; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Object clone() { | ||||
| 		return new BooleanType(value); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,101 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import java.math.BigInteger; | ||||
| import java.io.IOException; | ||||
| import org.msgpack.*; | ||||
|  | ||||
| class DoubleTypeIMPL extends FloatType { | ||||
| 	private double value; | ||||
|  | ||||
| 	public DoubleTypeIMPL(double value) { | ||||
| 		this.value = value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public float asFloat() { | ||||
| 		// FIXME check overflow, underflow? | ||||
| 		return (float)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public double asDouble() { | ||||
| 		return value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public byte byteValue() { | ||||
| 		return (byte)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public short shortValue() { | ||||
| 		return (short)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int intValue() { | ||||
| 		return (int)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public long longValue() { | ||||
| 		return (long)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public BigInteger bigIntegerValue() { | ||||
| 		return BigInteger.valueOf((long)value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public float floatValue() { | ||||
| 		return (float)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public double doubleValue() { | ||||
| 		return (double)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void messagePack(Packer pk) throws IOException { | ||||
| 		pk.packDouble(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object obj) { | ||||
| 		if(obj.getClass() != getClass()) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		return ((DoubleTypeIMPL)obj).value == value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		long v = Double.doubleToLongBits(value); | ||||
| 		return (int)(v^(v>>>32)); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Object clone() { | ||||
| 		return new DoubleTypeIMPL(value); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,36 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import org.msgpack.*; | ||||
|  | ||||
| public abstract class FloatType extends MessagePackObject { | ||||
| 	@Override | ||||
| 	public boolean isFloatType() { | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	public static FloatType create(float value) { | ||||
| 		return new FloatTypeIMPL(value); | ||||
| 	} | ||||
|  | ||||
| 	public static FloatType create(double value) { | ||||
| 		return new DoubleTypeIMPL(value); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,94 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import java.math.BigInteger; | ||||
| import java.io.IOException; | ||||
| import org.msgpack.*; | ||||
|  | ||||
| class FloatTypeIMPL extends FloatType { | ||||
| 	private float value; | ||||
|  | ||||
| 	public FloatTypeIMPL(float value) { | ||||
| 		this.value = value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public float asFloat() { | ||||
| 		return value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public double asDouble() { | ||||
| 		return (double)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public byte byteValue() { | ||||
| 		return (byte)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public short shortValue() { | ||||
| 		return (short)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int intValue() { | ||||
| 		return (int)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public long longValue() { | ||||
| 		return (long)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public float floatValue() { | ||||
| 		return (float)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public double doubleValue() { | ||||
| 		return (double)value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void messagePack(Packer pk) throws IOException { | ||||
| 		pk.packFloat(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean equals(Object obj) { | ||||
| 		if(obj.getClass() != getClass()) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		return ((FloatTypeIMPL)obj).value == value; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public int hashCode() { | ||||
| 		return Float.floatToIntBits(value); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Object clone() { | ||||
| 		return new FloatTypeIMPL(value); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,49 +0,0 @@ | ||||
| // | ||||
| // 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.object; | ||||
|  | ||||
| import java.math.BigInteger; | ||||
| import org.msgpack.*; | ||||
|  | ||||
| public abstract class IntegerType extends MessagePackObject { | ||||
| 	@Override | ||||
| 	public boolean isIntegerType() { | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	public static IntegerType create(byte value) { | ||||
| 		return new ShortIntegerTypeIMPL((int)value); | ||||
| 	} | ||||
|  | ||||
| 	public static IntegerType create(short value) { | ||||
| 		return new ShortIntegerTypeIMPL((int)value); | ||||
| 	} | ||||
|  | ||||
| 	public static IntegerType create(int value) { | ||||
| 		return new ShortIntegerTypeIMPL(value); | ||||
| 	} | ||||
|  | ||||
| 	public static IntegerType create(long value) { | ||||
| 		return new LongIntegerTypeIMPL(value); | ||||
| 	} | ||||
|  | ||||
| 	public static IntegerType create(BigInteger value) { | ||||
| 		return new BigIntegerTypeIMPL(value); | ||||
| 	} | ||||
| } | ||||
|  | ||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user