mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-21 07:45:02 +02:00
Compare commits
123 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0c331d2887 | ||
![]() |
39facd5dc6 | ||
![]() |
a2bd5ae638 | ||
![]() |
c57f616141 | ||
![]() |
3af10a1d00 | ||
![]() |
b3987e2402 | ||
![]() |
71dd44f430 | ||
![]() |
584462f9b9 | ||
![]() |
ff5d5d7cbc | ||
![]() |
370e92b1a6 | ||
![]() |
2469768a85 | ||
![]() |
acb8fa613e | ||
![]() |
83b4b7d83d | ||
![]() |
20de730541 | ||
![]() |
134c27c900 | ||
![]() |
123ae024c6 | ||
![]() |
34a29cd0a5 | ||
![]() |
9fffa9800a | ||
![]() |
358457f49d | ||
![]() |
90e305d789 | ||
![]() |
b471e52e28 | ||
![]() |
9b5fc37399 | ||
![]() |
537322e3b5 | ||
![]() |
279121f87f | ||
![]() |
a1b2b41cdc | ||
![]() |
0cca90c21d | ||
![]() |
ad052cb510 | ||
![]() |
57f0598373 | ||
![]() |
92d192277e | ||
![]() |
2cdfbd8970 | ||
![]() |
bc0c5f0cdc | ||
![]() |
230ee3a03b | ||
![]() |
ab0bf37d30 | ||
![]() |
b1e66256ce | ||
![]() |
fd80693420 | ||
![]() |
f222f5ed9b | ||
![]() |
59603b902a | ||
![]() |
82a5dd6cf9 | ||
![]() |
a97f9081a3 | ||
![]() |
b3e0ad1303 | ||
![]() |
251090406a | ||
![]() |
9c3ed173b1 | ||
![]() |
7cd41aeb72 | ||
![]() |
8ecaf7ad4c | ||
![]() |
d4049fe593 | ||
![]() |
989b14b519 | ||
![]() |
fb3e11408c | ||
![]() |
3d3af3284e | ||
![]() |
eabcf15790 | ||
![]() |
684bca203a | ||
![]() |
d42ecccf6f | ||
![]() |
5a92c861e3 | ||
![]() |
103b14ea3c | ||
![]() |
e49f091b4e | ||
![]() |
6056f93910 | ||
![]() |
18fa2d1af4 | ||
![]() |
062ed8a4c4 | ||
![]() |
49f3872d04 | ||
![]() |
d9b467098a | ||
![]() |
7d1e51437e | ||
![]() |
f5a7d444e2 | ||
![]() |
a0071c2f9f | ||
![]() |
98a5e43883 | ||
![]() |
f40ebe5b43 | ||
![]() |
d7d78d9a2b | ||
![]() |
5a12d36a0a | ||
![]() |
e61dc76ae1 | ||
![]() |
0da22193bd | ||
![]() |
d43921823e | ||
![]() |
6b5b76b0c9 | ||
![]() |
602971408b | ||
![]() |
2f5d83f07d | ||
![]() |
81b0c316cd | ||
![]() |
6df86384ca | ||
![]() |
3fbcde4bd7 | ||
![]() |
293293c23c | ||
![]() |
47185d757e | ||
![]() |
94c3998507 | ||
![]() |
5fa589691c | ||
![]() |
26bc835c7e | ||
![]() |
fc7da17fa2 | ||
![]() |
dbebe9771b | ||
![]() |
d0af8aa9f1 | ||
![]() |
f8173e93f5 | ||
![]() |
fa6ea6848f | ||
![]() |
5982970e21 | ||
![]() |
c43e5e0c95 | ||
![]() |
b4fc79c38e | ||
![]() |
b9cb270b8f | ||
![]() |
1fe35d7efe | ||
![]() |
ec8c19b1f0 | ||
![]() |
c2525bcc05 | ||
![]() |
985c31b378 | ||
![]() |
135a9f5586 | ||
![]() |
979ff80982 | ||
![]() |
6cde9f3a9d | ||
![]() |
5cad81bf4c | ||
![]() |
18967162cf | ||
![]() |
6ea75f3a9f | ||
![]() |
f51123d009 | ||
![]() |
be6376ee2d | ||
![]() |
120a85a3e5 | ||
![]() |
262fe96c29 | ||
![]() |
1864df5ed0 | ||
![]() |
09bae0a9e8 | ||
![]() |
ebe41a24f1 | ||
![]() |
f0f574a15b | ||
![]() |
9420436c09 | ||
![]() |
2b8f853b96 | ||
![]() |
2c2bf60d0c | ||
![]() |
62b82448d5 | ||
![]() |
2f12e6c3d0 | ||
![]() |
8ce23f8e3e | ||
![]() |
dbe760d6e2 | ||
![]() |
674c26d9c7 | ||
![]() |
7b68b04efd | ||
![]() |
e0b65bf196 | ||
![]() |
77f5cb1f1f | ||
![]() |
2a222737f8 | ||
![]() |
77d48f9cee | ||
![]() |
c77eac325e | ||
![]() |
517ced2a54 | ||
![]() |
70d2c47367 |
@@ -1,4 +1,14 @@
|
||||
|
||||
2010-07-06 version 0.5.1:
|
||||
|
||||
* Add msgpack_vrefbuffer_new and msgpack_vrefbuffer_free
|
||||
* Add msgpack_sbuffer_new and msgpack_sbuffer_free
|
||||
* Add msgpack_unpacker_next and msgpack_unpack_next
|
||||
* msgpack::unpack returns void
|
||||
* Add MSGPACK_VERSION{,_MAJOR,_MINOR} macros to check header version
|
||||
* Add msgpack_version{,_major,_minor} functions to check library version
|
||||
* ./configure supports --disable-cxx option not to build C++ API
|
||||
|
||||
2010-04-29 version 0.5.0:
|
||||
|
||||
* msgpack_object_type is changed. MSGPACK_OBJECT_NIL is now 0x00.
|
||||
|
1552
cpp/Doxyfile
Normal file
1552
cpp/Doxyfile
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,75 +1,6 @@
|
||||
SUBDIRS = src test
|
||||
|
||||
lib_LTLIBRARIES = libmsgpack.la
|
||||
|
||||
libmsgpack_la_SOURCES = \
|
||||
unpack.c \
|
||||
objectc.c \
|
||||
vrefbuffer.c \
|
||||
zone.c \
|
||||
object.cpp
|
||||
|
||||
# -version-info CURRENT:REVISION:AGE
|
||||
libmsgpack_la_LDFLAGS = -version-info 3:0:0
|
||||
|
||||
|
||||
# backward compatibility
|
||||
lib_LTLIBRARIES += libmsgpackc.la
|
||||
|
||||
libmsgpackc_la_SOURCES = \
|
||||
unpack.c \
|
||||
objectc.c \
|
||||
vrefbuffer.c \
|
||||
zone.c
|
||||
|
||||
libmsgpackc_la_LDFLAGS = -version-info 2:0:0
|
||||
|
||||
# work around for duplicated file name
|
||||
kumo_manager_CFLAGS = $(AM_CFLAGS)
|
||||
kumo_manager_CXXFLAGS = $(AM_CXXFLAGS)
|
||||
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
msgpack/pack_define.h \
|
||||
msgpack/pack_template.h \
|
||||
msgpack/unpack_define.h \
|
||||
msgpack/unpack_template.h \
|
||||
msgpack/sysdep.h \
|
||||
msgpack.h \
|
||||
msgpack/sbuffer.h \
|
||||
msgpack/vrefbuffer.h \
|
||||
msgpack/zbuffer.h \
|
||||
msgpack/pack.h \
|
||||
msgpack/unpack.h \
|
||||
msgpack/object.h \
|
||||
msgpack/zone.h \
|
||||
msgpack.hpp \
|
||||
msgpack/sbuffer.hpp \
|
||||
msgpack/vrefbuffer.hpp \
|
||||
msgpack/zbuffer.hpp \
|
||||
msgpack/pack.hpp \
|
||||
msgpack/unpack.hpp \
|
||||
msgpack/object.hpp \
|
||||
msgpack/zone.hpp \
|
||||
msgpack/type.hpp \
|
||||
msgpack/type/bool.hpp \
|
||||
msgpack/type/float.hpp \
|
||||
msgpack/type/int.hpp \
|
||||
msgpack/type/list.hpp \
|
||||
msgpack/type/deque.hpp \
|
||||
msgpack/type/map.hpp \
|
||||
msgpack/type/nil.hpp \
|
||||
msgpack/type/pair.hpp \
|
||||
msgpack/type/raw.hpp \
|
||||
msgpack/type/set.hpp \
|
||||
msgpack/type/string.hpp \
|
||||
msgpack/type/vector.hpp \
|
||||
msgpack/type/tuple.hpp \
|
||||
msgpack/type/define.hpp \
|
||||
msgpack/type/tr1/unordered_map.hpp \
|
||||
msgpack/type/tr1/unordered_set.hpp
|
||||
|
||||
|
||||
EXTRA_DIST = \
|
||||
DOC_FILES = \
|
||||
README.md \
|
||||
LICENSE \
|
||||
NOTICE \
|
||||
@@ -77,17 +8,11 @@ EXTRA_DIST = \
|
||||
msgpack_vc8.sln \
|
||||
msgpack_vc8.postbuild.bat
|
||||
|
||||
SUBDIRS = test
|
||||
EXTRA_DIST = \
|
||||
$(DOC_FILES)
|
||||
|
||||
check_PROGRAMS = \
|
||||
msgpackc_test \
|
||||
msgpack_test
|
||||
|
||||
msgpackc_test_SOURCES = msgpackc_test.cpp
|
||||
msgpackc_test_LDADD = libmsgpack.la -lgtest_main
|
||||
|
||||
msgpack_test_SOURCES = msgpack_test.cpp
|
||||
msgpack_test_LDADD = libmsgpack.la -lgtest_main
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
doxygen:
|
||||
./preprocess clean
|
||||
cd src && $(MAKE) doxygen
|
||||
./preprocess
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
AC_INIT(object.cpp)
|
||||
AC_INIT(src/object.cpp)
|
||||
AC_CONFIG_AUX_DIR(ac)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.5.0)
|
||||
AM_INIT_AUTOMAKE(msgpack, 0.5.1)
|
||||
AC_CONFIG_HEADER(config.h)
|
||||
|
||||
AC_SUBST(CFLAGS)
|
||||
@@ -9,17 +9,39 @@ CFLAGS="-O4 -Wall $CFLAGS"
|
||||
AC_SUBST(CXXFLAGS)
|
||||
CXXFLAGS="-O4 -Wall $CXXFLAGS"
|
||||
|
||||
|
||||
AC_PROG_CC
|
||||
AC_PROG_CXX
|
||||
|
||||
|
||||
AC_MSG_CHECKING([if C++ API is enabled])
|
||||
AC_ARG_ENABLE(cxx,
|
||||
AS_HELP_STRING([--disable-cxx],
|
||||
[don't build C++ API]) )
|
||||
AC_MSG_RESULT([$enable_cxx])
|
||||
if test "$enable_cxx" != "no"; then
|
||||
AC_PROG_CXX
|
||||
AM_PROG_CC_C_O
|
||||
fi
|
||||
AM_CONDITIONAL(ENABLE_CXX, test "$enable_cxx" != "no")
|
||||
|
||||
|
||||
AC_PROG_LIBTOOL
|
||||
AM_PROG_AS
|
||||
AM_PROG_CC_C_O
|
||||
|
||||
AC_LANG_PUSH([C++])
|
||||
AC_CHECK_HEADERS(tr1/unordered_map)
|
||||
AC_CHECK_HEADERS(tr1/unordered_set)
|
||||
AC_LANG_POP([C++])
|
||||
|
||||
AC_MSG_CHECKING([if debug option is enabled])
|
||||
AC_ARG_ENABLE(debug,
|
||||
AS_HELP_STRING([--disable-debug],
|
||||
[disable assert macros and omit -g option]) )
|
||||
AC_MSG_RESULT([$enable_debug])
|
||||
if test "$enable_debug" != "no"; then
|
||||
CXXFLAGS="$CXXFLAGS -g"
|
||||
CFLAGS="$CFLAGS -g"
|
||||
else
|
||||
CXXFLAGS="$CXXFLAGS -DNDEBUG"
|
||||
CFLAGS="$CFLAGS -DNDEBUG"
|
||||
fi
|
||||
|
||||
|
||||
AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [
|
||||
AC_TRY_LINK([
|
||||
@@ -39,5 +61,12 @@ add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as f
|
||||
])
|
||||
fi
|
||||
|
||||
AC_OUTPUT([Makefile test/Makefile])
|
||||
|
||||
major=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'`
|
||||
minor=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'`
|
||||
AC_SUBST(VERSION_MAJOR, $major)
|
||||
AC_SUBST(VERSION_MINOR, $minor)
|
||||
|
||||
|
||||
AC_OUTPUT([Makefile src/Makefile test/Makefile])
|
||||
|
||||
|
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
* MessagePack for C unpacking routine
|
||||
*
|
||||
* Copyright (C) 2008-2009 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.
|
||||
*/
|
||||
#ifndef MSGPACK_UNPACKER_H__
|
||||
#define MSGPACK_UNPACKER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include "msgpack/object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct msgpack_unpacker {
|
||||
char* buffer;
|
||||
size_t used;
|
||||
size_t free;
|
||||
size_t off;
|
||||
size_t parsed;
|
||||
msgpack_zone* z;
|
||||
size_t initial_buffer_size;
|
||||
void* ctx;
|
||||
} msgpack_unpacker;
|
||||
|
||||
|
||||
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
|
||||
void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
|
||||
|
||||
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
|
||||
void msgpack_unpacker_free(msgpack_unpacker* mpac);
|
||||
|
||||
static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
|
||||
static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
|
||||
static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
|
||||
static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
|
||||
|
||||
|
||||
int msgpack_unpacker_execute(msgpack_unpacker* mpac);
|
||||
|
||||
msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac);
|
||||
|
||||
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
|
||||
|
||||
void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac);
|
||||
|
||||
void msgpack_unpacker_reset(msgpack_unpacker* mpac);
|
||||
|
||||
static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
MSGPACK_UNPACK_SUCCESS = 2,
|
||||
MSGPACK_UNPACK_EXTRA_BYTES = 1,
|
||||
MSGPACK_UNPACK_CONTINUE = 0,
|
||||
MSGPACK_UNPACK_PARSE_ERROR = -1,
|
||||
} msgpack_unpack_return;
|
||||
|
||||
msgpack_unpack_return
|
||||
msgpack_unpack(const char* data, size_t len, size_t* off,
|
||||
msgpack_zone* z, msgpack_object* result);
|
||||
|
||||
|
||||
static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
|
||||
|
||||
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
|
||||
|
||||
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
|
||||
|
||||
bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
|
||||
{
|
||||
if(mpac->free >= size) { return true; }
|
||||
return msgpack_unpacker_expand_buffer(mpac, size);
|
||||
}
|
||||
|
||||
char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->buffer + mpac->used;
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->free;
|
||||
}
|
||||
|
||||
void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
|
||||
{
|
||||
mpac->used += size;
|
||||
mpac->free -= size;
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->parsed - mpac->off + mpac->used;
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->parsed;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/unpack.h */
|
||||
|
@@ -2,42 +2,43 @@ IF NOT EXIST include MKDIR include
|
||||
IF NOT EXIST include\msgpack MKDIR include\msgpack
|
||||
IF NOT EXIST include\msgpack\type MKDIR include\msgpack\type
|
||||
IF NOT EXIST include\msgpack\type\tr1 MKDIR include\msgpack\type\tr1
|
||||
copy msgpack\pack_define.h include\msgpack\
|
||||
copy msgpack\pack_template.h include\msgpack\
|
||||
copy msgpack\unpack_define.h include\msgpack\
|
||||
copy msgpack\unpack_template.h include\msgpack\
|
||||
copy msgpack\sysdep.h include\msgpack\
|
||||
copy msgpack.h include\
|
||||
copy msgpack\sbuffer.h include\msgpack\
|
||||
copy msgpack\vrefbuffer.h include\msgpack\
|
||||
copy msgpack\zbuffer.h include\msgpack\
|
||||
copy msgpack\pack.h include\msgpack\
|
||||
copy msgpack\unpack.h include\msgpack\
|
||||
copy msgpack\object.h include\msgpack\
|
||||
copy msgpack\zone.h include\msgpack\
|
||||
copy msgpack.hpp include\
|
||||
copy msgpack\sbuffer.hpp include\msgpack\
|
||||
copy msgpack\vrefbuffer.hpp include\msgpack\
|
||||
copy msgpack\zbuffer.hpp include\msgpack\
|
||||
copy msgpack\pack.hpp include\msgpack\
|
||||
copy msgpack\unpack.hpp include\msgpack\
|
||||
copy msgpack\object.hpp include\msgpack\
|
||||
copy msgpack\zone.hpp include\msgpack\
|
||||
copy msgpack\type.hpp include\msgpack\type\
|
||||
copy msgpack\type\bool.hpp include\msgpack\type\
|
||||
copy msgpack\type\float.hpp include\msgpack\type\
|
||||
copy msgpack\type\int.hpp include\msgpack\type\
|
||||
copy msgpack\type\list.hpp include\msgpack\type\
|
||||
copy msgpack\type\deque.hpp include\msgpack\type\
|
||||
copy msgpack\type\map.hpp include\msgpack\type\
|
||||
copy msgpack\type\nil.hpp include\msgpack\type\
|
||||
copy msgpack\type\pair.hpp include\msgpack\type\
|
||||
copy msgpack\type\raw.hpp include\msgpack\type\
|
||||
copy msgpack\type\set.hpp include\msgpack\type\
|
||||
copy msgpack\type\string.hpp include\msgpack\type\
|
||||
copy msgpack\type\vector.hpp include\msgpack\type\
|
||||
copy msgpack\type\tuple.hpp include\msgpack\type\
|
||||
copy msgpack\type\define.hpp include\msgpack\type\
|
||||
copy msgpack\type\tr1\unordered_map.hpp include\msgpack\type\
|
||||
copy msgpack\type\tr1\unordered_set.hpp include\msgpack\type\
|
||||
copy src\msgpack\pack_define.h include\msgpack\
|
||||
copy src\msgpack\pack_template.h include\msgpack\
|
||||
copy src\msgpack\unpack_define.h include\msgpack\
|
||||
copy src\msgpack\unpack_template.h include\msgpack\
|
||||
copy src\msgpack\sysdep.h include\msgpack\
|
||||
copy src\msgpack.h include\
|
||||
copy src\msgpack\sbuffer.h include\msgpack\
|
||||
copy src\msgpack\version.h include\msgpack\
|
||||
copy src\msgpack\vrefbuffer.h include\msgpack\
|
||||
copy src\msgpack\zbuffer.h include\msgpack\
|
||||
copy src\msgpack\pack.h include\msgpack\
|
||||
copy src\msgpack\unpack.h include\msgpack\
|
||||
copy src\msgpack\object.h include\msgpack\
|
||||
copy src\msgpack\zone.h include\msgpack\
|
||||
copy src\msgpack.hpp include\
|
||||
copy src\msgpack\sbuffer.hpp include\msgpack\
|
||||
copy src\msgpack\vrefbuffer.hpp include\msgpack\
|
||||
copy src\msgpack\zbuffer.hpp include\msgpack\
|
||||
copy src\msgpack\pack.hpp include\msgpack\
|
||||
copy src\msgpack\unpack.hpp include\msgpack\
|
||||
copy src\msgpack\object.hpp include\msgpack\
|
||||
copy src\msgpack\zone.hpp include\msgpack\
|
||||
copy src\msgpack\type.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\bool.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\float.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\int.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\list.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\deque.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\map.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\nil.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\pair.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\raw.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\set.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\string.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\vector.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tuple.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\define.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tr1\unordered_map.hpp include\msgpack\type\
|
||||
copy src\msgpack\type\tr1\unordered_set.hpp include\msgpack\type\
|
||||
|
||||
|
@@ -157,7 +157,7 @@
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\objectc.c"
|
||||
RelativePath=".\src\objectc.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
@@ -177,7 +177,7 @@
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\unpack.c"
|
||||
RelativePath=".\src\unpack.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
@@ -197,7 +197,7 @@
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\vrefbuffer.c"
|
||||
RelativePath=".\src\version.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
@@ -217,7 +217,7 @@
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\zone.c"
|
||||
RelativePath=".\src\vrefbuffer.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
@@ -237,7 +237,27 @@
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\object.cpp"
|
||||
RelativePath=".\src\zone.c"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
CompileAs="2"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\object.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
@@ -247,23 +267,23 @@
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\msgpack\pack_define.h"
|
||||
RelativePath=".\src\msgpack\pack_define.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\pack_template.h"
|
||||
RelativePath=".\src\msgpack\pack_template.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\sysdep.h"
|
||||
RelativePath=".\src\msgpack\sysdep.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\unpack_define.h"
|
||||
RelativePath=".\src\msgpack\unpack_define.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\msgpack\unpack_template.h"
|
||||
RelativePath=".\src\msgpack\unpack_template.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
|
@@ -6,17 +6,26 @@ preprocess() {
|
||||
echo ""
|
||||
echo "** preprocess failed **"
|
||||
echo ""
|
||||
exit 1
|
||||
else
|
||||
mv $1.tmp $1
|
||||
fi
|
||||
}
|
||||
|
||||
preprocess msgpack/type/tuple.hpp
|
||||
preprocess msgpack/type/define.hpp
|
||||
preprocess msgpack/zone.hpp
|
||||
cp -f ../msgpack/sysdep.h msgpack/
|
||||
cp -f ../msgpack/pack_define.h msgpack/
|
||||
cp -f ../msgpack/pack_template.h msgpack/
|
||||
cp -f ../msgpack/unpack_define.h msgpack/
|
||||
cp -f ../msgpack/unpack_template.h msgpack/
|
||||
if [ "$1" == "clean" ];then
|
||||
rm -f src/msgpack/type/tuple.hpp
|
||||
rm -f src/msgpack/type/define.hpp
|
||||
rm -f src/msgpack/zone.hpp
|
||||
else
|
||||
preprocess src/msgpack/type/tuple.hpp
|
||||
preprocess src/msgpack/type/define.hpp
|
||||
preprocess src/msgpack/zone.hpp
|
||||
fi
|
||||
cp -f ../msgpack/sysdep.h src/msgpack/
|
||||
cp -f ../msgpack/pack_define.h src/msgpack/
|
||||
cp -f ../msgpack/pack_template.h src/msgpack/
|
||||
cp -f ../msgpack/unpack_define.h src/msgpack/
|
||||
cp -f ../msgpack/unpack_template.h src/msgpack/
|
||||
cp -f ../test/cases.mpac test/
|
||||
cp -f ../test/cases_compact.mpac test/
|
||||
|
||||
|
107
cpp/src/Makefile.am
Normal file
107
cpp/src/Makefile.am
Normal file
@@ -0,0 +1,107 @@
|
||||
|
||||
lib_LTLIBRARIES = libmsgpack.la
|
||||
|
||||
libmsgpack_la_SOURCES = \
|
||||
unpack.c \
|
||||
objectc.c \
|
||||
version.c \
|
||||
vrefbuffer.c \
|
||||
zone.c
|
||||
|
||||
if ENABLE_CXX
|
||||
libmsgpack_la_SOURCES += \
|
||||
object.cpp
|
||||
endif
|
||||
|
||||
# -version-info CURRENT:REVISION:AGE
|
||||
libmsgpack_la_LDFLAGS = -version-info 3:0:0
|
||||
|
||||
|
||||
# backward compatibility
|
||||
lib_LTLIBRARIES += libmsgpackc.la
|
||||
|
||||
libmsgpackc_la_SOURCES = \
|
||||
unpack.c \
|
||||
objectc.c \
|
||||
version.c \
|
||||
vrefbuffer.c \
|
||||
zone.c
|
||||
|
||||
libmsgpackc_la_LDFLAGS = -version-info 2:0:0
|
||||
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
msgpack/pack_define.h \
|
||||
msgpack/pack_template.h \
|
||||
msgpack/unpack_define.h \
|
||||
msgpack/unpack_template.h \
|
||||
msgpack/sysdep.h \
|
||||
msgpack.h \
|
||||
msgpack/sbuffer.h \
|
||||
msgpack/version.h \
|
||||
msgpack/vrefbuffer.h \
|
||||
msgpack/zbuffer.h \
|
||||
msgpack/pack.h \
|
||||
msgpack/unpack.h \
|
||||
msgpack/object.h \
|
||||
msgpack/zone.h
|
||||
|
||||
if ENABLE_CXX
|
||||
nobase_include_HEADERS += \
|
||||
msgpack.hpp \
|
||||
msgpack/sbuffer.hpp \
|
||||
msgpack/vrefbuffer.hpp \
|
||||
msgpack/zbuffer.hpp \
|
||||
msgpack/pack.hpp \
|
||||
msgpack/unpack.hpp \
|
||||
msgpack/object.hpp \
|
||||
msgpack/zone.hpp \
|
||||
msgpack/type.hpp \
|
||||
msgpack/type/bool.hpp \
|
||||
msgpack/type/float.hpp \
|
||||
msgpack/type/int.hpp \
|
||||
msgpack/type/list.hpp \
|
||||
msgpack/type/deque.hpp \
|
||||
msgpack/type/map.hpp \
|
||||
msgpack/type/nil.hpp \
|
||||
msgpack/type/pair.hpp \
|
||||
msgpack/type/raw.hpp \
|
||||
msgpack/type/set.hpp \
|
||||
msgpack/type/string.hpp \
|
||||
msgpack/type/vector.hpp \
|
||||
msgpack/type/tuple.hpp \
|
||||
msgpack/type/define.hpp \
|
||||
msgpack/type/tr1/unordered_map.hpp \
|
||||
msgpack/type/tr1/unordered_set.hpp
|
||||
endif
|
||||
|
||||
EXTRA_DIST = \
|
||||
msgpack/version.h.in \
|
||||
msgpack/zone.hpp.erb \
|
||||
msgpack/type/define.hpp.erb \
|
||||
msgpack/type/tuple.hpp.erb
|
||||
|
||||
|
||||
msgpack/version.h: msgpack/version.h.in Makefile.in
|
||||
sed -e s/VERSION_UNDEFINED/$(VERSION)/ \
|
||||
-e s/VERSION_MAJOR_UNDEFINED/$(VERSION_MAJOR)/ \
|
||||
-e s/VERSION_MINOR_UNDEFINED/$(VERSION_MINOR)/ \
|
||||
$< > $@
|
||||
|
||||
|
||||
doxygen_c:
|
||||
cat ../Doxyfile > Doxyfile_c
|
||||
echo "FILE_PATTERNS = *.h" >> Doxyfile_c
|
||||
echo "OUTPUT_DIRECTORY = doc_c" >> Doxyfile_c
|
||||
echo "PROJECT_NAME = \"MessagePack for C\"" >> Doxyfile_c
|
||||
doxygen Doxyfile_c
|
||||
|
||||
doxygen_cpp:
|
||||
cat ../Doxyfile > Doxyfile_cpp
|
||||
echo "FILE_PATTERNS = *.hpp" >> Doxyfile_cpp
|
||||
echo "OUTPUT_DIRECTORY = doc_cpp" >> Doxyfile_cpp
|
||||
echo "PROJECT_NAME = \"MessagePack for C++\"" >> Doxyfile_cpp
|
||||
doxygen Doxyfile_cpp
|
||||
|
||||
doxygen: doxygen_c doxygen_cpp
|
||||
|
@@ -15,9 +15,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* @defgroup msgpack MessagePack C
|
||||
* @{
|
||||
* @}
|
||||
*/
|
||||
#include "msgpack/object.h"
|
||||
#include "msgpack/zone.h"
|
||||
#include "msgpack/pack.h"
|
||||
#include "msgpack/unpack.h"
|
||||
#include "msgpack/sbuffer.h"
|
||||
#include "msgpack/vrefbuffer.h"
|
||||
#include "msgpack/version.h"
|
||||
|
@@ -26,6 +26,12 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_object Dynamically typed object
|
||||
* @ingroup msgpack
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
MSGPACK_OBJECT_NIL = 0x00,
|
||||
MSGPACK_OBJECT_BOOLEAN = 0x01,
|
||||
@@ -81,6 +87,8 @@ void msgpack_object_print(FILE* out, msgpack_object o);
|
||||
|
||||
bool msgpack_object_equal(const msgpack_object x, const msgpack_object y);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
@@ -27,6 +27,19 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_buffer Buffers
|
||||
* @ingroup msgpack
|
||||
* @{
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_pack Serializer
|
||||
* @ingroup msgpack
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef int (*msgpack_packer_write)(void* data, const char* buf, unsigned int len);
|
||||
|
||||
typedef struct msgpack_packer {
|
||||
@@ -74,6 +87,8 @@ static int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
|
||||
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#define msgpack_pack_inline_func(name) \
|
||||
inline int msgpack_pack ## name
|
@@ -21,15 +21,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef MSGPACK_SBUFFER_INIT_SIZE
|
||||
#define MSGPACK_SBUFFER_INIT_SIZE 8192
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_sbuffer Simple buffer
|
||||
* @ingroup msgpack_buffer
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct msgpack_sbuffer {
|
||||
size_t size;
|
||||
char* data;
|
||||
@@ -46,6 +48,22 @@ static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf)
|
||||
free(sbuf->data);
|
||||
}
|
||||
|
||||
static inline msgpack_sbuffer* msgpack_sbuffer_new(void)
|
||||
{
|
||||
return (msgpack_sbuffer*)calloc(1, sizeof(msgpack_sbuffer));
|
||||
}
|
||||
|
||||
static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf)
|
||||
{
|
||||
if(sbuf == NULL) { return; }
|
||||
msgpack_sbuffer_destroy(sbuf);
|
||||
free(sbuf);
|
||||
}
|
||||
|
||||
#ifndef MSGPACK_SBUFFER_INIT_SIZE
|
||||
#define MSGPACK_SBUFFER_INIT_SIZE 8192
|
||||
#endif
|
||||
|
||||
static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
|
||||
@@ -77,6 +95,13 @@ static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf)
|
||||
{
|
||||
sbuf->size = 0;
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
@@ -72,6 +72,11 @@ public:
|
||||
return msgpack_sbuffer_release(this);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
msgpack_sbuffer_clear(this);
|
||||
}
|
||||
|
||||
private:
|
||||
void expand_buffer(size_t len)
|
||||
{
|
@@ -51,6 +51,14 @@ inline void operator<< (object::with_zone& o, type::nil v)
|
||||
{ static_cast<object&>(o) << v; }
|
||||
|
||||
|
||||
template <>
|
||||
inline void object::as<void>() const
|
||||
{
|
||||
msgpack::type::nil v;
|
||||
convert(&v);
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/nil.hpp */
|
@@ -141,7 +141,7 @@ type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> (
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
if(o.via.array.size < <%=i+1%>) { throw type_error(); }
|
||||
<%0.upto(i) {|j|%>
|
||||
o.via.array.ptr[<%=j%>].convert<A<%=j%>>(&v.template get<<%=j%>>());<%}%>
|
||||
o.via.array.ptr[<%=j%>].convert<typename type::tuple_type<A<%=j%>>::type>(&v.template get<<%=j%>>());<%}%>
|
||||
return v;
|
||||
}
|
||||
<%}%>
|
||||
@@ -187,5 +187,20 @@ inline void operator<< (
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
|
||||
//inline std::ostream& operator<< (std::ostream& o, const msgpack::type::tuple<>& v) {
|
||||
// return o << "[]";
|
||||
//}
|
||||
//<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
//template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
|
||||
//inline std::ostream& operator<< (std::ostream& o,
|
||||
// const msgpack::type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
|
||||
// return o << "["
|
||||
// <%0.upto(i) {|j|%>
|
||||
// <<<%if j != 0 then%> ", " <<<%end%> v.template get<<%=j%>>()<%}%>
|
||||
// << "]";
|
||||
//}
|
||||
//<%}%>
|
||||
|
||||
#endif /* msgpack/type/tuple.hpp */
|
||||
|
260
cpp/src/msgpack/unpack.h
Normal file
260
cpp/src/msgpack/unpack.h
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* MessagePack for C unpacking routine
|
||||
*
|
||||
* Copyright (C) 2008-2009 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.
|
||||
*/
|
||||
#ifndef MSGPACK_UNPACKER_H__
|
||||
#define MSGPACK_UNPACKER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include "msgpack/object.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_unpack Deserializer
|
||||
* @ingroup msgpack
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct msgpack_unpacked {
|
||||
msgpack_zone* zone;
|
||||
msgpack_object data;
|
||||
} msgpack_unpacked;
|
||||
|
||||
bool msgpack_unpack_next(msgpack_unpacked* result,
|
||||
const char* data, size_t len, size_t* off);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_unpacker Streaming deserializer
|
||||
* @ingroup msgpack
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct msgpack_unpacker {
|
||||
char* buffer;
|
||||
size_t used;
|
||||
size_t free;
|
||||
size_t off;
|
||||
size_t parsed;
|
||||
msgpack_zone* z;
|
||||
size_t initial_buffer_size;
|
||||
void* ctx;
|
||||
} msgpack_unpacker;
|
||||
|
||||
|
||||
#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
|
||||
#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Initializes a streaming deserializer.
|
||||
* The initialized deserializer must be destroyed by msgpack_unpacker_destroy(msgpack_unpacker*).
|
||||
*/
|
||||
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
|
||||
|
||||
/**
|
||||
* Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t).
|
||||
*/
|
||||
void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
|
||||
|
||||
|
||||
/**
|
||||
* Creates a streaming deserializer.
|
||||
* The created deserializer must be destroyed by msgpack_unpacker_free(msgpack_unpacker*).
|
||||
*/
|
||||
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
|
||||
|
||||
/**
|
||||
* Frees a streaming deserializer created by msgpack_unpacker_new(size_t).
|
||||
*/
|
||||
void msgpack_unpacker_free(msgpack_unpacker* mpac);
|
||||
|
||||
|
||||
#ifndef MSGPACK_UNPACKER_RESERVE_SIZE
|
||||
#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Reserves free space of the internal buffer.
|
||||
* Use this function to fill the internal buffer with
|
||||
* msgpack_unpacker_buffer(msgpack_unpacker*),
|
||||
* msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and
|
||||
* msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
|
||||
*/
|
||||
static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
|
||||
|
||||
/**
|
||||
* Gets pointer to the free space of the internal buffer.
|
||||
* Use this function to fill the internal buffer with
|
||||
* msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
|
||||
* msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and
|
||||
* msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
|
||||
*/
|
||||
static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
|
||||
|
||||
/**
|
||||
* Gets size of the free space of the internal buffer.
|
||||
* Use this function to fill the internal buffer with
|
||||
* msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
|
||||
* msgpack_unpacker_buffer(const msgpack_unpacker*) and
|
||||
* msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
|
||||
*/
|
||||
static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
|
||||
|
||||
/**
|
||||
* Notifies the deserializer that the internal buffer filled.
|
||||
* Use this function to fill the internal buffer with
|
||||
* msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
|
||||
* msgpack_unpacker_buffer(msgpack_unpacker*) and
|
||||
* msgpack_unpacker_buffer_capacity(const msgpack_unpacker*).
|
||||
*/
|
||||
static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes one object.
|
||||
* Returns true if it successes. Otherwise false is returned.
|
||||
* @param pac pointer to an initialized msgpack_unpacked object.
|
||||
*/
|
||||
bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac);
|
||||
|
||||
/**
|
||||
* Initializes a msgpack_unpacked object.
|
||||
* The initialized object must be destroyed by msgpack_unpacked_destroy(msgpack_unpacker*).
|
||||
* Use the object with msgpack_unpacker_next(msgpack_unpacker*, msgpack_unpacked*) or
|
||||
* msgpack_unpack_next(msgpack_unpacked*, const char*, size_t, size_t*).
|
||||
*/
|
||||
static inline void msgpack_unpacked_init(msgpack_unpacked* result);
|
||||
|
||||
/**
|
||||
* Destroys a streaming deserializer initialized by msgpack_unpacked().
|
||||
*/
|
||||
static inline void msgpack_unpacked_destroy(msgpack_unpacked* result);
|
||||
|
||||
/**
|
||||
* Releases the memory zone from msgpack_unpacked object.
|
||||
* The released zone must be freed by msgpack_zone_free(msgpack_zone*).
|
||||
*/
|
||||
static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result);
|
||||
|
||||
|
||||
int msgpack_unpacker_execute(msgpack_unpacker* mpac);
|
||||
|
||||
msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac);
|
||||
|
||||
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
|
||||
|
||||
void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac);
|
||||
|
||||
void msgpack_unpacker_reset(msgpack_unpacker* mpac);
|
||||
|
||||
static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
// obsolete
|
||||
typedef enum {
|
||||
MSGPACK_UNPACK_SUCCESS = 2,
|
||||
MSGPACK_UNPACK_EXTRA_BYTES = 1,
|
||||
MSGPACK_UNPACK_CONTINUE = 0,
|
||||
MSGPACK_UNPACK_PARSE_ERROR = -1,
|
||||
} msgpack_unpack_return;
|
||||
|
||||
// obsolete
|
||||
msgpack_unpack_return
|
||||
msgpack_unpack(const char* data, size_t len, size_t* off,
|
||||
msgpack_zone* result_zone, msgpack_object* result);
|
||||
|
||||
|
||||
static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
|
||||
|
||||
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
|
||||
|
||||
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
|
||||
|
||||
bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
|
||||
{
|
||||
if(mpac->free >= size) { return true; }
|
||||
return msgpack_unpacker_expand_buffer(mpac, size);
|
||||
}
|
||||
|
||||
char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->buffer + mpac->used;
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->free;
|
||||
}
|
||||
|
||||
void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
|
||||
{
|
||||
mpac->used += size;
|
||||
mpac->free -= size;
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->parsed - mpac->off + mpac->used;
|
||||
}
|
||||
|
||||
size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
|
||||
{
|
||||
return mpac->parsed;
|
||||
}
|
||||
|
||||
|
||||
void msgpack_unpacked_init(msgpack_unpacked* result)
|
||||
{
|
||||
memset(result, 0, sizeof(msgpack_unpacked));
|
||||
}
|
||||
|
||||
void msgpack_unpacked_destroy(msgpack_unpacked* result)
|
||||
{
|
||||
if(result->zone != NULL) {
|
||||
msgpack_zone_free(result->zone);
|
||||
result->zone = NULL;
|
||||
memset(&result->data, 0, sizeof(msgpack_object));
|
||||
}
|
||||
}
|
||||
|
||||
msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result)
|
||||
{
|
||||
if(result->zone != NULL) {
|
||||
msgpack_zone* z = result->zone;
|
||||
result->zone = NULL;
|
||||
return z;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/unpack.h */
|
||||
|
@@ -24,8 +24,9 @@
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
// backward compatibility
|
||||
#ifndef MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE
|
||||
#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE (32*1024)
|
||||
#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE MSGPACK_UNPACKER_INIT_BUFFER_SIZE
|
||||
#endif
|
||||
|
||||
namespace msgpack {
|
||||
@@ -64,12 +65,12 @@ private:
|
||||
|
||||
class unpacker : public msgpack_unpacker {
|
||||
public:
|
||||
unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE);
|
||||
unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
|
||||
~unpacker();
|
||||
|
||||
public:
|
||||
/*! 1. reserve buffer. at least `size' bytes of capacity will be ready */
|
||||
void reserve_buffer(size_t size);
|
||||
void reserve_buffer(size_t size = MSGPACK_UNPACKER_RESERVE_SIZE);
|
||||
|
||||
/*! 2. read data to the buffer() up to buffer_capacity() bytes */
|
||||
char* buffer();
|
||||
@@ -160,7 +161,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
static bool unpack(unpacked* result,
|
||||
static void unpack(unpacked* result,
|
||||
const char* data, size_t len, size_t* offset = NULL);
|
||||
|
||||
|
||||
@@ -312,7 +313,7 @@ inline void unpacker::remove_nonparsed_buffer()
|
||||
}
|
||||
|
||||
|
||||
inline bool unpack(unpacked* result,
|
||||
inline void unpack(unpacked* result,
|
||||
const char* data, size_t len, size_t* offset)
|
||||
{
|
||||
msgpack::object obj;
|
||||
@@ -326,12 +327,12 @@ inline bool unpack(unpacked* result,
|
||||
case UNPACK_SUCCESS:
|
||||
result->get() = obj;
|
||||
result->zone() = z;
|
||||
return false;
|
||||
return;
|
||||
|
||||
case UNPACK_EXTRA_BYTES:
|
||||
result->get() = obj;
|
||||
result->zone() = z;
|
||||
return true;
|
||||
return;
|
||||
|
||||
case UNPACK_CONTINUE:
|
||||
throw unpack_error("insufficient bytes");
|
40
cpp/src/msgpack/version.h.in
Normal file
40
cpp/src/msgpack/version.h.in
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* MessagePack for C version information
|
||||
*
|
||||
* Copyright (C) 2008-2009 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.
|
||||
*/
|
||||
#ifndef MSGPACK_VERSION_H__
|
||||
#define MSGPACK_VERSION_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
const char* msgpack_version(void);
|
||||
int msgpack_version_major(void);
|
||||
int msgpack_version_minor(void);
|
||||
|
||||
#define MSGPACK_VERSION "VERSION_UNDEFINED"
|
||||
#define MSGPACK_VERSION_MAJOR VERSION_MAJOR_UNDEFINED
|
||||
#define MSGPACK_VERSION_MINOR VERSION_MINOR_UNDEFINED
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* msgpack/version.h */
|
||||
|
@@ -19,6 +19,7 @@
|
||||
#define MSGPACK_VREFBUFFER_H__
|
||||
|
||||
#include "msgpack/zone.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/uio.h>
|
||||
@@ -29,19 +30,17 @@ struct iovec {
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
|
||||
#define MSGPACK_VREFBUFFER_REF_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
|
||||
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_vrefbuffer Vectored Referencing buffer
|
||||
* @ingroup msgpack_buffer
|
||||
* @{
|
||||
*/
|
||||
|
||||
struct msgpack_vrefbuffer_chunk;
|
||||
typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
|
||||
|
||||
@@ -63,10 +62,21 @@ typedef struct msgpack_vrefbuffer {
|
||||
} msgpack_vrefbuffer;
|
||||
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
|
||||
#define MSGPACK_VREFBUFFER_REF_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
|
||||
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
|
||||
#endif
|
||||
|
||||
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
|
||||
size_t ref_size, size_t chunk_size);
|
||||
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
|
||||
|
||||
static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size);
|
||||
static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf);
|
||||
|
||||
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len);
|
||||
|
||||
static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
|
||||
@@ -80,6 +90,28 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
||||
|
||||
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
|
||||
|
||||
void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
|
||||
{
|
||||
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer));
|
||||
if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) {
|
||||
free(vbuf);
|
||||
return NULL;
|
||||
}
|
||||
return vbuf;
|
||||
}
|
||||
|
||||
void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
|
||||
{
|
||||
if(vbuf == NULL) { return; }
|
||||
msgpack_vrefbuffer_destroy(vbuf);
|
||||
free(vbuf);
|
||||
}
|
||||
|
||||
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
|
||||
{
|
||||
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
|
@@ -78,6 +78,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
msgpack_vrefbuffer_clear(this);
|
||||
}
|
||||
|
||||
private:
|
||||
typedef msgpack_vrefbuffer base;
|
||||
|
@@ -23,30 +23,34 @@
|
||||
#include <string.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#ifndef MSGPACK_ZBUFFER_INIT_SIZE
|
||||
#define MSGPACK_ZBUFFER_INIT_SIZE 8192
|
||||
#endif
|
||||
|
||||
#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
|
||||
#define MSGPACK_ZBUFFER_RESERVE_SIZE 512
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_zbuffer Compressed buffer
|
||||
* @ingroup msgpack_buffer
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct msgpack_zbuffer {
|
||||
z_stream stream;
|
||||
char* data;
|
||||
size_t init_size;
|
||||
} msgpack_zbuffer;
|
||||
|
||||
#ifndef MSGPACK_ZBUFFER_INIT_SIZE
|
||||
#define MSGPACK_ZBUFFER_INIT_SIZE 8192
|
||||
#endif
|
||||
|
||||
static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf,
|
||||
int level, size_t init_size);
|
||||
static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf);
|
||||
|
||||
static inline msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size);
|
||||
static inline void msgpack_zbuffer_free(msgpack_zbuffer* zbuf);
|
||||
|
||||
static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf);
|
||||
|
||||
static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf);
|
||||
@@ -57,6 +61,10 @@ static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf);
|
||||
static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf);
|
||||
|
||||
|
||||
#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
|
||||
#define MSGPACK_ZBUFFER_RESERVE_SIZE 512
|
||||
#endif
|
||||
|
||||
static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len);
|
||||
|
||||
static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf);
|
||||
@@ -80,6 +88,23 @@ void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf)
|
||||
free(zbuf->data);
|
||||
}
|
||||
|
||||
msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size)
|
||||
{
|
||||
msgpack_zbuffer* zbuf = (msgpack_zbuffer*)malloc(sizeof(msgpack_zbuffer));
|
||||
if(!msgpack_zbuffer_init(zbuf, level, init_size)) {
|
||||
free(zbuf);
|
||||
return NULL;
|
||||
}
|
||||
return zbuf;
|
||||
}
|
||||
|
||||
void msgpack_zbuffer_free(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
if(zbuf == NULL) { return; }
|
||||
msgpack_zbuffer_destroy(zbuf);
|
||||
free(zbuf);
|
||||
}
|
||||
|
||||
bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf)
|
||||
{
|
||||
size_t used = (char*)zbuf->stream.next_out - zbuf->data;
|
||||
@@ -171,6 +196,8 @@ char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
@@ -25,6 +25,12 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup msgpack_zone Memory zone
|
||||
* @ingroup msgpack
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct msgpack_zone_finalizer {
|
||||
void (*func)(void* data);
|
||||
void* data;
|
||||
@@ -71,6 +77,7 @@ bool msgpack_zone_is_empty(msgpack_zone* zone);
|
||||
|
||||
void msgpack_zone_clear(msgpack_zone* zone);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#ifndef MSGPACK_ZONE_ALIGN
|
@@ -323,7 +323,7 @@ msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
|
||||
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
|
||||
{
|
||||
if(!msgpack_unpacker_flush_zone(mpac)) {
|
||||
return false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
||||
@@ -363,20 +363,46 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac)
|
||||
mpac->parsed = 0;
|
||||
}
|
||||
|
||||
bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result)
|
||||
{
|
||||
if(result->zone != NULL) {
|
||||
msgpack_zone_free(result->zone);
|
||||
}
|
||||
|
||||
int ret = msgpack_unpacker_execute(mpac);
|
||||
|
||||
if(ret <= 0) {
|
||||
result->zone = NULL;
|
||||
memset(&result->data, 0, sizeof(msgpack_object));
|
||||
return false;
|
||||
}
|
||||
|
||||
result->zone = msgpack_unpacker_release_zone(mpac);
|
||||
result->data = msgpack_unpacker_data(mpac);
|
||||
msgpack_unpacker_reset(mpac);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
msgpack_unpack_return
|
||||
msgpack_unpack(const char* data, size_t len, size_t* off,
|
||||
msgpack_zone* z, msgpack_object* result)
|
||||
msgpack_zone* result_zone, msgpack_object* result)
|
||||
{
|
||||
size_t noff = 0;
|
||||
if(off != NULL) { noff = *off; }
|
||||
|
||||
if(len <= noff) {
|
||||
// FIXME
|
||||
return MSGPACK_UNPACK_CONTINUE;
|
||||
}
|
||||
|
||||
template_context ctx;
|
||||
template_init(&ctx);
|
||||
|
||||
ctx.user.z = z;
|
||||
ctx.user.z = result_zone;
|
||||
ctx.user.referenced = false;
|
||||
|
||||
size_t noff = 0;
|
||||
if(off != NULL) { noff = *off; }
|
||||
|
||||
int e = template_execute(&ctx, data, len, &noff);
|
||||
if(e < 0) {
|
||||
return MSGPACK_UNPACK_PARSE_ERROR;
|
||||
@@ -397,3 +423,37 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
|
||||
return MSGPACK_UNPACK_SUCCESS;
|
||||
}
|
||||
|
||||
bool msgpack_unpack_next(msgpack_unpacked* result,
|
||||
const char* data, size_t len, size_t* off)
|
||||
{
|
||||
msgpack_unpacked_destroy(result);
|
||||
|
||||
size_t noff = 0;
|
||||
if(off != NULL) { noff = *off; }
|
||||
|
||||
if(len <= noff) {
|
||||
return false;
|
||||
}
|
||||
|
||||
msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
||||
|
||||
template_context ctx;
|
||||
template_init(&ctx);
|
||||
|
||||
ctx.user.z = z;
|
||||
ctx.user.referenced = false;
|
||||
|
||||
int e = template_execute(&ctx, data, len, &noff);
|
||||
if(e <= 0) {
|
||||
msgpack_zone_free(z);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(off != NULL) { *off = noff; }
|
||||
|
||||
result->zone = z;
|
||||
result->data = template_data(&ctx);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
17
cpp/src/version.c
Normal file
17
cpp/src/version.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "msgpack.h"
|
||||
|
||||
const char* msgpack_version(void)
|
||||
{
|
||||
return MSGPACK_VERSION;
|
||||
}
|
||||
|
||||
int msgpack_version_major(void)
|
||||
{
|
||||
return MSGPACK_VERSION_MAJOR;
|
||||
}
|
||||
|
||||
int msgpack_version_minor(void)
|
||||
{
|
||||
return MSGPACK_VERSION_MINOR;
|
||||
}
|
||||
|
@@ -75,6 +75,25 @@ void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
|
||||
free(vbuf->array);
|
||||
}
|
||||
|
||||
void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf)
|
||||
{
|
||||
msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head->next;
|
||||
msgpack_vrefbuffer_chunk* n;
|
||||
while(c != NULL) {
|
||||
n = c->next;
|
||||
free(c);
|
||||
c = n;
|
||||
}
|
||||
|
||||
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
|
||||
msgpack_vrefbuffer_chunk* chunk = ib->head;
|
||||
chunk->next = NULL;
|
||||
ib->free = vbuf->chunk_size;
|
||||
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
|
||||
|
||||
vbuf->tail = vbuf->array;
|
||||
}
|
||||
|
||||
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
|
||||
const char* buf, unsigned int len)
|
||||
{
|
@@ -204,7 +204,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size)
|
||||
|
||||
if(!init_chunk_list(&zone->chunk_list, chunk_size)) {
|
||||
free(zone);
|
||||
return false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
init_finalizer_array(&zone->finalizer_array);
|
||||
@@ -214,6 +214,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size)
|
||||
|
||||
void msgpack_zone_free(msgpack_zone* zone)
|
||||
{
|
||||
if(zone == NULL) { return; }
|
||||
msgpack_zone_destroy(zone);
|
||||
free(zone);
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
|
||||
CXXFLAGS += -Wall -g -I. -I.. -O4
|
||||
LDFLAGS +=
|
||||
|
||||
all: test
|
||||
|
||||
test: test.o unpack.o zone.o object.o pack.hpp unpack.hpp zone.hpp object.hpp
|
||||
$(CXX) test.o unpack.o zone.o object.o $(CXXFLAGS) $(LDFLAGS) -o $@
|
||||
|
@@ -1,15 +1,21 @@
|
||||
|
||||
AM_CPPFLAGS = -I..
|
||||
AM_C_CPPFLAGS = -I..
|
||||
AM_LDFLAGS = ../libmsgpack.la -lgtest_main
|
||||
AM_CPPFLAGS = -I../src
|
||||
AM_C_CPPFLAGS = -I../src
|
||||
AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main
|
||||
|
||||
check_PROGRAMS = \
|
||||
zone \
|
||||
pack_unpack \
|
||||
pack_unpack_c \
|
||||
streaming \
|
||||
streaming_c \
|
||||
object \
|
||||
convert \
|
||||
buffer
|
||||
buffer \
|
||||
cases \
|
||||
version \
|
||||
msgpackc_test \
|
||||
msgpack_test
|
||||
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
||||
@@ -17,8 +23,12 @@ zone_SOURCES = zone.cc
|
||||
|
||||
pack_unpack_SOURCES = pack_unpack.cc
|
||||
|
||||
pack_unpack_c_SOURCES = pack_unpack_c.cc
|
||||
|
||||
streaming_SOURCES = streaming.cc
|
||||
|
||||
streaming_c_SOURCES = streaming_c.cc
|
||||
|
||||
object_SOURCES = object.cc
|
||||
|
||||
convert_SOURCES = convert.cc
|
||||
@@ -26,3 +36,13 @@ convert_SOURCES = convert.cc
|
||||
buffer_SOURCES = buffer.cc
|
||||
buffer_LDADD = -lz
|
||||
|
||||
cases_SOURCES = cases.cc
|
||||
|
||||
version_SOURCES = version.cc
|
||||
|
||||
msgpackc_test_SOURCES = msgpackc_test.cpp
|
||||
|
||||
msgpack_test_SOURCES = msgpack_test.cpp
|
||||
|
||||
EXTRA_DIST = cases.mpac cases_compact.mpac
|
||||
|
||||
|
@@ -12,6 +12,14 @@ TEST(buffer, sbuffer)
|
||||
|
||||
EXPECT_EQ(3, sbuf.size());
|
||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
||||
|
||||
sbuf.clear();
|
||||
sbuf.write("a", 1);
|
||||
sbuf.write("a", 1);
|
||||
sbuf.write("a", 1);
|
||||
|
||||
EXPECT_EQ(3, sbuf.size());
|
||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +40,23 @@ TEST(buffer, vrefbuffer)
|
||||
|
||||
EXPECT_EQ(3, sbuf.size());
|
||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
||||
|
||||
|
||||
vbuf.clear();
|
||||
vbuf.write("a", 1);
|
||||
vbuf.write("a", 1);
|
||||
vbuf.write("a", 1);
|
||||
|
||||
vec = vbuf.vector();
|
||||
veclen = vbuf.vector_size();
|
||||
|
||||
sbuf.clear();
|
||||
for(size_t i=0; i < veclen; ++i) {
|
||||
sbuf.write((const char*)vec[i].iov_base, vec[i].iov_len);
|
||||
}
|
||||
|
||||
EXPECT_EQ(3, sbuf.size());
|
||||
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
|
||||
}
|
||||
|
||||
|
||||
|
36
cpp/test/cases.cc
Normal file
36
cpp/test/cases.cc
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <fstream>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
static void feed_file(msgpack::unpacker& pac, const char* path)
|
||||
{
|
||||
std::ifstream fin(path);
|
||||
while(true) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
fin.read(pac.buffer(), pac.buffer_capacity());
|
||||
if(fin.bad()) {
|
||||
throw std::runtime_error("read failed");
|
||||
}
|
||||
pac.buffer_consumed(fin.gcount());
|
||||
if(fin.fail()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(cases, format)
|
||||
{
|
||||
msgpack::unpacker pac;
|
||||
msgpack::unpacker pac_compact;
|
||||
|
||||
feed_file(pac, "cases.mpac");
|
||||
feed_file(pac_compact, "cases_compact.mpac");
|
||||
|
||||
msgpack::unpacked result;
|
||||
while(pac.next(&result)) {
|
||||
msgpack::unpacked result_compact;
|
||||
EXPECT_TRUE( pac_compact.next(&result_compact) );
|
||||
EXPECT_EQ(result_compact.get(), result.get());
|
||||
}
|
||||
}
|
||||
|
@@ -77,21 +77,17 @@ TEST(unpack, sequence)
|
||||
msgpack::pack(sbuf, 2);
|
||||
msgpack::pack(sbuf, 3);
|
||||
|
||||
bool cont;
|
||||
size_t offset = 0;
|
||||
|
||||
msgpack::unpacked msg;
|
||||
|
||||
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_TRUE(cont);
|
||||
msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_EQ(1, msg.get().as<int>());
|
||||
|
||||
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_TRUE(cont);
|
||||
msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_EQ(2, msg.get().as<int>());
|
||||
|
||||
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_FALSE(cont);
|
||||
msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
|
||||
EXPECT_EQ(3, msg.get().as<int>());
|
||||
}
|
||||
|
||||
|
70
cpp/test/pack_unpack_c.cc
Normal file
70
cpp/test/pack_unpack_c.cc
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <msgpack.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TEST(pack, num)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
|
||||
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
|
||||
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
|
||||
msgpack_sbuffer_free(sbuf);
|
||||
msgpack_packer_free(pk);
|
||||
}
|
||||
|
||||
|
||||
TEST(pack, array)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
|
||||
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
|
||||
|
||||
EXPECT_EQ(0, msgpack_pack_array(pk, 3));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
|
||||
msgpack_sbuffer_free(sbuf);
|
||||
msgpack_packer_free(pk);
|
||||
}
|
||||
|
||||
|
||||
TEST(unpack, sequence)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
|
||||
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
|
||||
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
|
||||
msgpack_packer_free(pk);
|
||||
|
||||
bool success;
|
||||
size_t offset = 0;
|
||||
|
||||
msgpack_unpacked msg;
|
||||
msgpack_unpacked_init(&msg);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_TRUE(success);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
|
||||
EXPECT_EQ(1, msg.data.via.u64);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_TRUE(success);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
|
||||
EXPECT_EQ(2, msg.data.via.u64);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_TRUE(success);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
|
||||
EXPECT_EQ(3, msg.data.via.u64);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_FALSE(success);
|
||||
|
||||
msgpack_sbuffer_free(sbuf);
|
||||
msgpack_unpacked_destroy(&msg);
|
||||
}
|
||||
|
@@ -2,28 +2,33 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
TEST(streaming, basic)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
msgpack::packer<std::ostream> pk(&stream);
|
||||
msgpack::sbuffer buffer;
|
||||
|
||||
msgpack::packer<msgpack::sbuffer> pk(&buffer);
|
||||
pk.pack(1);
|
||||
pk.pack(2);
|
||||
pk.pack(3);
|
||||
|
||||
std::istringstream input(stream.str());
|
||||
const char* input = buffer.data();
|
||||
const char* const eof = input + buffer.size();
|
||||
|
||||
msgpack::unpacker pac;
|
||||
msgpack::unpacked result;
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
// read buffer into pac.buffer() upto
|
||||
// pac.buffer_capacity() bytes.
|
||||
size_t len = 1;
|
||||
memcpy(pac.buffer(), input, len);
|
||||
input += len;
|
||||
|
||||
pac.buffer_consumed(len);
|
||||
|
||||
msgpack::unpacked result;
|
||||
while(pac.next(&result)) {
|
||||
msgpack::object obj = result.get();
|
||||
switch(count++) {
|
||||
@@ -38,6 +43,8 @@ TEST(streaming, basic)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(input < eof);
|
||||
}
|
||||
}
|
||||
|
||||
|
57
cpp/test/streaming_c.cc
Normal file
57
cpp/test/streaming_c.cc
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <msgpack.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TEST(streaming, basic)
|
||||
{
|
||||
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
|
||||
|
||||
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
msgpack_packer_free(pk);
|
||||
|
||||
const char* input = buffer->data;
|
||||
const char* const eof = input + buffer->size;
|
||||
|
||||
msgpack_unpacker pac;
|
||||
msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
|
||||
|
||||
msgpack_unpacked result;
|
||||
msgpack_unpacked_init(&result);
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
msgpack_unpacker_reserve_buffer(&pac, 32*1024);
|
||||
|
||||
/* read buffer into msgpack_unapcker_buffer(&pac) upto
|
||||
* msgpack_unpacker_buffer_capacity(&pac) bytes. */
|
||||
size_t len = 1;
|
||||
memcpy(msgpack_unpacker_buffer(&pac), input, len);
|
||||
input += len;
|
||||
|
||||
msgpack_unpacker_buffer_consumed(&pac, len);
|
||||
|
||||
while(msgpack_unpacker_next(&pac, &result)) {
|
||||
msgpack_object obj = result.data;
|
||||
switch(count++) {
|
||||
case 0:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(1, result.data.via.u64);
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(2, result.data.via.u64);
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(3, result.data.via.u64);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(input < eof);
|
||||
}
|
||||
}
|
||||
|
13
cpp/test/version.cc
Normal file
13
cpp/test/version.cc
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(version, print)
|
||||
{
|
||||
printf("MSGPACK_VERSION : %s\n", MSGPACK_VERSION);
|
||||
printf("MSGPACK_VERSION_MAJOR : %d\n", MSGPACK_VERSION_MAJOR);
|
||||
printf("MSGPACK_VERSION_MINOR : %d\n", MSGPACK_VERSION_MINOR);
|
||||
printf("msgpack_version() : %s\n", msgpack_version());
|
||||
printf("msgpack_version_major() : %d\n", msgpack_version_major());
|
||||
printf("msgpack_version_minor() : %d\n", msgpack_version_minor());
|
||||
}
|
||||
|
4
erlang/.gitignore
vendored
Normal file
4
erlang/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
MANIFEST
|
||||
*.beam
|
||||
.omakedb*
|
||||
*.omc
|
42
erlang/OMakefile
Normal file
42
erlang/OMakefile
Normal file
@@ -0,0 +1,42 @@
|
||||
########################################################################
|
||||
# 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 #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 $<
|
||||
|
||||
test: msgpack.beam
|
||||
erl -noshell -s msgpack test -s init stop
|
||||
|
||||
clean:
|
||||
-rm *.beam
|
45
erlang/OMakeroot
Normal file
45
erlang/OMakeroot
Normal file
@@ -0,0 +1,45 @@
|
||||
########################################################################
|
||||
# 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: .
|
9
erlang/README.md
Normal file
9
erlang/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
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.
|
362
erlang/msgpack.erl
Normal file
362
erlang/msgpack.erl
Normal file
@@ -0,0 +1,362 @@
|
||||
%%
|
||||
%% 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.
|
||||
|
||||
-module(msgpack).
|
||||
-author('kuenishi+msgpack@gmail.com').
|
||||
|
||||
%% tuples, atoms are not supported. lists, integers, double, and so on.
|
||||
%% see http://msgpack.sourceforge.jp/spec for
|
||||
%% supported formats. APIs are almost compatible
|
||||
%% for C API (http://msgpack.sourceforge.jp/c:doc)
|
||||
%% except buffering functions (both copying and zero-copying).
|
||||
-export([pack/1, unpack/1, unpack_all/1]).
|
||||
-export([pack_map/1]).
|
||||
|
||||
% compile:
|
||||
% erl> c(msgpack).
|
||||
% erl> S = <some term>.
|
||||
% erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
|
||||
-type reason() :: enomem | badarg | no_code_matches.
|
||||
-type msgpack_term() :: [msgpack_term()]
|
||||
| {[{msgpack_term(),msgpack_term()}]}
|
||||
| integer() | float() | binary().
|
||||
|
||||
% ===== external APIs ===== %
|
||||
-spec pack(Term::msgpack_term()) -> binary().
|
||||
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(Map) when is_tuple(Map), element(1,Map)=:=dict ->
|
||||
pack_map(dict:to_list(Map));
|
||||
pack(_Other) ->
|
||||
{error, undefined}.
|
||||
|
||||
% unpacking.
|
||||
% if failed in decoding and not end, get more data
|
||||
% and feed more Bin into this function.
|
||||
% TODO: error case for imcomplete format when short for any type formats.
|
||||
-spec unpack( Bin::binary() )-> {msgpack_term(), binary()} |
|
||||
{more, non_neg_integer()} | {more, undefined} |
|
||||
{error, reason()}.
|
||||
unpack(Bin) when not is_binary(Bin)->
|
||||
{error, badarg};
|
||||
unpack(Bin) when bit_size(Bin) >= 8 ->
|
||||
unpack_(Bin);
|
||||
unpack(<<>>)->
|
||||
{more, 1};
|
||||
unpack(_) ->
|
||||
{more, undefined}.
|
||||
|
||||
-spec unpack_all( binary() ) -> [msgpack_term()].
|
||||
unpack_all(Data)->
|
||||
case unpack(Data) of
|
||||
{ Term, Binary } when bit_size(Binary) =:= 0 ->
|
||||
[Term];
|
||||
{ Term, Binary } when is_binary(Binary) ->
|
||||
[Term|unpack_all(Binary)]
|
||||
end.
|
||||
|
||||
-spec pack_map(M::[{msgpack_term(),msgpack_term()}])-> binary().
|
||||
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.
|
||||
|
||||
% ===== internal APIs ===== %
|
||||
|
||||
% 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 >>.
|
||||
|
||||
% negative fixnum
|
||||
pack_int_(N) when is_integer(N) , 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 >>.
|
||||
|
||||
% 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 >>.
|
||||
|
||||
% 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.
|
||||
|
||||
% list / tuple
|
||||
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>>).
|
||||
|
||||
% FIXME! this should be without lists:reverse/1
|
||||
unpack_array_(<<>>, 0, RetList) -> {lists:reverse(RetList), <<>>};
|
||||
unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetList), Remain};
|
||||
unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 -> {more, undefined};
|
||||
unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
|
||||
case unpack(Bin) of
|
||||
{more, _} -> {more, undefined};
|
||||
{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
|
||||
end.
|
||||
|
||||
pack_map_([], Acc) -> Acc;
|
||||
pack_map_([{Key,Value}|Tail], Acc) ->
|
||||
pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
|
||||
|
||||
% FIXME! this should be without lists:reverse/1
|
||||
-spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
|
||||
{more, non_neg_integer()} | { any(), binary()}.
|
||||
unpack_map_(Bin, 0, Acc) -> {{lists:reverse(Acc)}, Bin};
|
||||
unpack_map_(Bin, Len, Acc) ->
|
||||
case unpack(Bin) of
|
||||
{more, _} -> {more, undefined};
|
||||
{Key, Rest} ->
|
||||
case unpack(Rest) of
|
||||
{more, _} -> {more, undefined};
|
||||
{Value, Rest2} ->
|
||||
unpack_map_(Rest2,Len-1,[{Key,Value}|Acc])
|
||||
end
|
||||
end.
|
||||
|
||||
-spec unpack_(Payload::binary()) ->
|
||||
{more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
|
||||
unpack_(Binary)->
|
||||
case Binary 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
|
||||
|
||||
% Incomplete / invalid data
|
||||
<<16#CA, Rest/binary>> -> {more, 4-byte_size(Rest)};
|
||||
<<16#CB, Rest/binary>> -> {more, 8-byte_size(Rest)};
|
||||
<<16#CC>> -> {more, 1};
|
||||
<<16#CD, Rest/binary>> -> {more, 2-byte_size(Rest)};
|
||||
<<16#CE, Rest/binary>> -> {more, 4-byte_size(Rest)};
|
||||
<<16#CF, Rest/binary>> -> {more, 8-byte_size(Rest)};
|
||||
<<16#D0>> -> {more, 1};
|
||||
<<16#D1, Rest/binary>> -> {more, 2-byte_size(Rest)};
|
||||
<<16#D2, Rest/binary>> -> {more, 4-byte_size(Rest)};
|
||||
<<16#D3, Rest/binary>> -> {more, 8-byte_size(Rest)};
|
||||
<<16#DA, Rest/binary>> -> {more, 16-byte_size(Rest)};
|
||||
<<16#DB, Rest/binary>> -> {more, 32-byte_size(Rest)};
|
||||
<<16#DC, Rest/binary>> -> {more, 2-byte_size(Rest)};
|
||||
<<16#DD, Rest/binary>> -> {more, 4-byte_size(Rest)};
|
||||
<<16#DE, Rest/binary>> -> {more, 2-byte_size(Rest)};
|
||||
<<16#DF, Rest/binary>> -> {more, 4-byte_size(Rest)};
|
||||
<<2#101:3, L:5, Rest/binary>> -> {more, L-byte_size(Rest)};
|
||||
|
||||
<<>> -> {more, 1};
|
||||
<<2#101:3, _/binary>> -> {more, undefined};
|
||||
<<F:8, Rest/binary>> when F==16#C1;
|
||||
F==16#C7; F==16#C8; F==16#C9; F==16#D5;
|
||||
F==16#D6; F==16#D7; F==16#D8; F==16#D9->
|
||||
{error, {badarg, <<F, Rest/binary>>}};
|
||||
Other ->
|
||||
{error, {badarg, Other}}
|
||||
end.
|
||||
|
||||
|
||||
% ===== test codes ===== %
|
||||
-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]) ->
|
||||
LH=RH,
|
||||
compare_all(LTL, RTL).
|
||||
|
||||
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(),
|
||||
{[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
|
||||
Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
|
||||
true = port_command(Port, msgpack:pack(Tests) ),
|
||||
receive
|
||||
{Port, {data, Data}}-> {Tests, <<>>}=msgpack:unpack(Data)
|
||||
after 1024-> ?assert(false) end,
|
||||
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,
|
||||
case msgpack:unpack(Bin) of
|
||||
{more, N} when not is_integer(N) ->
|
||||
?assertEqual(undefined, N);
|
||||
{more, N} ->
|
||||
?assert( N < Len )
|
||||
end.
|
||||
|
||||
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),
|
||||
% Longest = lists:seq(0,12345),
|
||||
{Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)),
|
||||
% {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
|
||||
ok.
|
||||
|
||||
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()->
|
||||
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}]},
|
||||
-234, -50000,
|
||||
42
|
||||
],
|
||||
Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
|
||||
receive
|
||||
{Port, {data, Data}}->
|
||||
compare_all(Tests, msgpack:unpack_all(Data))
|
||||
after 1024-> ?assert(false) end,
|
||||
port_close(Port).
|
||||
|
||||
test_([]) -> 0;
|
||||
test_([Before|Rest])->
|
||||
Pack = msgpack:pack(Before),
|
||||
{After, <<>>} = msgpack:unpack( Pack ),
|
||||
?assertEqual(Before, After),
|
||||
1+test_(Rest).
|
||||
|
||||
other_test()->
|
||||
{more,1}=msgpack:unpack(<<>>).
|
||||
|
||||
-endif.
|
65
erlang/testcase_generator.rb
Normal file
65
erlang/testcase_generator.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
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
|
||||
|
@@ -1,5 +1,5 @@
|
||||
Name: msgpack
|
||||
Version: 0.2.0
|
||||
Version: 0.2.2
|
||||
License: BSD3
|
||||
License-File: LICENSE
|
||||
Author: Hideyuki Tanaka
|
||||
|
@@ -297,7 +297,7 @@ foreign import ccall "msgpack_pack_raw_body_wrap" msgpack_pack_raw_body ::
|
||||
-- | Pack a single byte stream. It calls 'packRAW' and 'packRAWBody'.
|
||||
packRAW' :: Packer -> ByteString -> IO Int
|
||||
packRAW' pc bs = do
|
||||
packRAW pc (BS.length bs)
|
||||
_ <- packRAW pc (BS.length bs)
|
||||
packRAWBody pc bs
|
||||
|
||||
type Unpacker = ForeignPtr ()
|
||||
@@ -475,7 +475,7 @@ peekObject ptr = do
|
||||
(#const MSGPACK_OBJECT_MAP) ->
|
||||
peekObjectMap ptr
|
||||
_ ->
|
||||
fail "peekObject: unknown object type"
|
||||
fail $ "peekObject: unknown object type (" ++ show typ ++ ")"
|
||||
|
||||
peekObjectBool :: Ptr a -> IO Object
|
||||
peekObjectBool ptr = do
|
||||
@@ -506,20 +506,22 @@ peekObjectRAW ptr = do
|
||||
|
||||
peekObjectArray :: Ptr a -> IO Object
|
||||
peekObjectArray ptr = do
|
||||
size <- (#peek msgpack_object, via.array.size) ptr
|
||||
p <- (#peek msgpack_object, via.array.ptr) ptr
|
||||
objs <- mapM (\i -> peekObject $ p `plusPtr`
|
||||
csize <- (#peek msgpack_object, via.array.size) ptr
|
||||
let size = fromIntegral (csize :: Word32)
|
||||
p <- (#peek msgpack_object, via.array.ptr) ptr
|
||||
objs <- mapM (\i -> peekObject $ p `plusPtr`
|
||||
((#size msgpack_object) * i))
|
||||
[0..size-1]
|
||||
[0..size-1]
|
||||
return $ ObjectArray objs
|
||||
|
||||
peekObjectMap :: Ptr a -> IO Object
|
||||
peekObjectMap ptr = do
|
||||
size <- (#peek msgpack_object, via.map.size) ptr
|
||||
p <- (#peek msgpack_object, via.map.ptr) ptr
|
||||
dat <- mapM (\i -> peekObjectKV $ p `plusPtr`
|
||||
csize <- (#peek msgpack_object, via.map.size) ptr
|
||||
let size = fromIntegral (csize :: Word32)
|
||||
p <- (#peek msgpack_object, via.map.ptr) ptr
|
||||
dat <- mapM (\i -> peekObjectKV $ p `plusPtr`
|
||||
((#size msgpack_object_kv) * i))
|
||||
[0..size-1]
|
||||
[0..size-1]
|
||||
return $ ObjectMap dat
|
||||
|
||||
peekObjectKV :: Ptr a -> IO (Object, Object)
|
||||
@@ -541,11 +543,11 @@ packObject pc (ObjectDouble d) = packDouble pc d >> return ()
|
||||
packObject pc (ObjectRAW bs) = packRAW' pc bs >> return ()
|
||||
|
||||
packObject pc (ObjectArray ls) = do
|
||||
packArray pc (length ls)
|
||||
_ <- packArray pc (length ls)
|
||||
mapM_ (packObject pc) ls
|
||||
|
||||
packObject pc (ObjectMap ls) = do
|
||||
packMap pc (length ls)
|
||||
_ <- packMap pc (length ls)
|
||||
mapM_ (\(a, b) -> packObject pc a >> packObject pc b) ls
|
||||
|
||||
data UnpackReturn =
|
||||
@@ -560,6 +562,7 @@ unpackObject z dat =
|
||||
allocaBytes (#size msgpack_object) $ \ptr ->
|
||||
BS.useAsCStringLen dat $ \(str, len) ->
|
||||
alloca $ \poff -> do
|
||||
poke poff 0
|
||||
ret <- msgpack_unpack str (fromIntegral len) poff z ptr
|
||||
case ret of
|
||||
(#const MSGPACK_UNPACK_SUCCESS) -> do
|
||||
|
@@ -27,7 +27,6 @@ module Data.MessagePack.Class(
|
||||
import Control.Monad.Error
|
||||
import Data.ByteString.Char8 (ByteString)
|
||||
import qualified Data.ByteString.Char8 as C8
|
||||
import Data.Either
|
||||
|
||||
import Data.MessagePack.Base
|
||||
|
||||
@@ -46,6 +45,11 @@ instance OBJECT Object where
|
||||
fromObjectError :: String
|
||||
fromObjectError = "fromObject: cannot cast"
|
||||
|
||||
instance OBJECT () where
|
||||
toObject = const ObjectNil
|
||||
fromObject ObjectNil = Right ()
|
||||
fromObject _ = Left fromObjectError
|
||||
|
||||
instance OBJECT Int where
|
||||
toObject = ObjectInteger
|
||||
fromObject (ObjectInteger n) = Right n
|
||||
|
@@ -21,7 +21,6 @@ module Data.MessagePack.Feed(
|
||||
feederFromString,
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.IORef
|
||||
@@ -33,12 +32,16 @@ type Feeder = IO (Maybe ByteString)
|
||||
-- | Feeder from Handle
|
||||
feederFromHandle :: Handle -> IO Feeder
|
||||
feederFromHandle h = return $ do
|
||||
bs <- BS.hGet h bufSize
|
||||
bs <- BS.hGetNonBlocking h bufSize
|
||||
if BS.length bs > 0
|
||||
then return $ Just bs
|
||||
then do return $ Just bs
|
||||
else do
|
||||
hClose h
|
||||
return Nothing
|
||||
c <- BS.hGet h 1
|
||||
if BS.length c > 0
|
||||
then do return $ Just c
|
||||
else do
|
||||
hClose h
|
||||
return Nothing
|
||||
where
|
||||
bufSize = 4096
|
||||
|
||||
|
@@ -79,10 +79,10 @@ packToString :: MonadIO m => PackerT m r -> m ByteString
|
||||
packToString m = do
|
||||
sb <- liftIO $ newSimpleBuffer
|
||||
pc <- liftIO $ newPacker sb
|
||||
runPackerT m pc
|
||||
_ <- runPackerT m pc
|
||||
liftIO $ simpleBufferData sb
|
||||
|
||||
-- | Execcute given serializer and write byte sequence to Handle.
|
||||
-- | Execute given serializer and write byte sequence to Handle.
|
||||
packToHandle :: MonadIO m => Handle -> PackerT m r -> m ()
|
||||
packToHandle h m = do
|
||||
sb <- packToString m
|
||||
@@ -115,18 +115,21 @@ instance MonadIO m => MonadIO (UnpackerT m) where
|
||||
|
||||
instance MonadIO m => MonadUnpacker (UnpackerT m) where
|
||||
get = UnpackerT $ \up feed -> liftIO $ do
|
||||
resp <- unpackerExecute up
|
||||
guard $ resp>=0
|
||||
when (resp==0) $ do
|
||||
Just bs <- feed
|
||||
unpackerFeed up bs
|
||||
resp2 <- unpackerExecute up
|
||||
guard $ resp2==1
|
||||
executeOne up feed
|
||||
obj <- unpackerData up
|
||||
freeZone =<< unpackerReleaseZone up
|
||||
unpackerReset up
|
||||
let Right r = fromObject obj
|
||||
return r
|
||||
|
||||
where
|
||||
executeOne up feed = do
|
||||
resp <- unpackerExecute up
|
||||
guard $ resp>=0
|
||||
when (resp==0) $ do
|
||||
Just bs <- feed
|
||||
unpackerFeed up bs
|
||||
executeOne up feed
|
||||
|
||||
-- | Execute deserializer using given feeder.
|
||||
unpackFrom :: MonadIO m => Feeder -> UnpackerT m r -> m r
|
||||
|
@@ -19,9 +19,7 @@ module Data.MessagePack.Stream(
|
||||
unpackObjectsFromString,
|
||||
) where
|
||||
|
||||
import Control.Monad
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as BS
|
||||
import System.IO
|
||||
import System.IO.Unsafe
|
||||
|
||||
|
9
java/CHANGES.txt
Normal file
9
java/CHANGES.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
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
Normal file
202
java/LICENSE.txt
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
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.
|
79
java/pom.xml
79
java/pom.xml
@@ -3,10 +3,12 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.msgpack</groupId>
|
||||
<artifactId>msgpack</artifactId>
|
||||
<name>MessagePack for Java</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<version>0.3</version>
|
||||
<description>MessagePack for Java</description>
|
||||
|
||||
<name>MessagePack for Java</name>
|
||||
<url>http://msgpack.sourceforge.net/</url>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
@@ -16,9 +18,19 @@
|
||||
</licenses>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git://github.com/msgpack/msgpack.git</connection>
|
||||
<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>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
@@ -83,27 +95,50 @@
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>msgpack.sourceforge.net</id>
|
||||
<name>MessagePack Maven2 Repository</name>
|
||||
<url>http://msgpack.sourceforge.net/maven2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>msgpack.sourceforge.net</id>
|
||||
<name>MessagePack Maven2 Snapshot Repository</name>
|
||||
<url>http://msgpack.sourceforge.net/maven2-snapshot</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<uniqueVersion>false</uniqueVersion>
|
||||
<id>shell.sourceforge.net</id>
|
||||
<name>Repository at sourceforge.net</name>
|
||||
<url>scp://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<uniqueVersion>true</uniqueVersion>
|
||||
<id>shell.sourceforge.net</id>
|
||||
<name>Repository Name</name>
|
||||
<url>scp://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2-snapshot/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
<profiles>
|
||||
<!-- for sending artifacts to sourceforge.net repository -->
|
||||
<profile>
|
||||
<id>sourceforge</id>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>sourceforge.net</id>
|
||||
<name>Repository at sourceforge.net</name>
|
||||
<url>scpexe://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
<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>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
426
java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
Normal file
426
java/src/main/java/org/msgpack/BufferedUnpackerImpl.java
Normal file
@@ -0,0 +1,426 @@
|
||||
//
|
||||
// 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;
|
||||
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());
|
||||
|
||||
Object 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(4);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
advance(4);
|
||||
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) {
|
||||
// FIXME
|
||||
throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported");
|
||||
}
|
||||
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(4);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
advance(4);
|
||||
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 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();
|
||||
return unpackRawBody(length);
|
||||
}
|
||||
|
||||
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 Object unpackObject() throws IOException {
|
||||
UnpackResult result = new UnpackResult();
|
||||
if(!next(result)) {
|
||||
super.reset();
|
||||
throw new UnpackException("insufficient buffer");
|
||||
}
|
||||
return result.getData();
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
public interface MessageMergeable {
|
||||
public void messageMerge(Object obj) throws MessageTypeException;
|
||||
public interface MessageConvertable {
|
||||
public void messageConvert(Object obj) throws MessageTypeException;
|
||||
}
|
||||
|
@@ -17,9 +17,7 @@
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MessageTypeException extends IOException {
|
||||
public class MessageTypeException extends RuntimeException {
|
||||
public MessageTypeException() { }
|
||||
|
||||
public MessageTypeException(String s) {
|
||||
|
25
java/src/main/java/org/msgpack/MessageUnpackable.java
Normal file
25
java/src/main/java/org/msgpack/MessageUnpackable.java
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface MessageUnpackable {
|
||||
public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException;
|
||||
}
|
||||
|
@@ -23,6 +23,22 @@ import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
protected byte[] castBytes = new byte[9];
|
||||
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
|
||||
@@ -34,7 +50,7 @@ public class Packer {
|
||||
|
||||
public Packer packByte(byte d) throws IOException {
|
||||
if(d < -(1<<5)) {
|
||||
castBytes[0] = (byte)0xd1;
|
||||
castBytes[0] = (byte)0xd0;
|
||||
castBytes[1] = d;
|
||||
out.write(castBytes, 0, 2);
|
||||
} else {
|
||||
@@ -207,6 +223,10 @@ public class Packer {
|
||||
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;
|
||||
@@ -401,7 +421,7 @@ public class Packer {
|
||||
} else if(o instanceof Double) {
|
||||
return packDouble((Double)o);
|
||||
} else {
|
||||
throw new IOException("unknown object "+o+" ("+o.getClass()+")");
|
||||
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,28 +20,13 @@ package org.msgpack;
|
||||
import java.io.Writer;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.schema.SSchemaParser;
|
||||
import org.msgpack.schema.ClassGenerator;
|
||||
//import org.msgpack.schema.ClassGenerator;
|
||||
|
||||
public abstract class Schema {
|
||||
private String expression;
|
||||
private String name;
|
||||
public Schema() { }
|
||||
|
||||
public Schema(String name) {
|
||||
this.expression = expression;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return name;
|
||||
}
|
||||
public abstract String getClassName();
|
||||
public abstract String getExpression();
|
||||
|
||||
public static Schema parse(String source) {
|
||||
return SSchemaParser.parse(source);
|
||||
@@ -51,83 +36,43 @@ public abstract class Schema {
|
||||
return SSchemaParser.load(source);
|
||||
}
|
||||
|
||||
public void write(Writer output) throws IOException {
|
||||
ClassGenerator.write(this, output);
|
||||
}
|
||||
|
||||
public abstract void pack(Packer pk, Object obj) throws IOException;
|
||||
|
||||
public abstract Object convert(Object obj) throws MessageTypeException;
|
||||
|
||||
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object createFromBoolean(boolean v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromByte(byte v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromShort(short v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromInt(int v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromLong(long v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromFloat(float v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromDouble(double v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
/* FIXME
|
||||
public Object createFromBoolean(boolean v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromByte(byte v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromShort(short v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromInt(int v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromLong(long v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromFloat(float v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromDouble(double v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@@ -1,82 +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 org.msgpack.impl.UnpackerImpl;
|
||||
|
||||
public class UnbufferedUnpacker extends UnpackerImpl {
|
||||
private int offset;
|
||||
private boolean finished;
|
||||
private Object data;
|
||||
|
||||
public UnbufferedUnpacker() {
|
||||
super();
|
||||
this.offset = 0;
|
||||
this.finished = false;
|
||||
}
|
||||
|
||||
public UnbufferedUnpacker useSchema(Schema s) {
|
||||
super.setSchema(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
super.reset();
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
void setOffset(int offset) {
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
public int execute(byte[] buffer) throws UnpackException {
|
||||
return execute(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
// FIXME
|
||||
public int execute(byte[] buffer, int offset, int length) throws UnpackException
|
||||
{
|
||||
int noffset = super.execute(buffer, offset + this.offset, length);
|
||||
this.offset = noffset - offset;
|
||||
if(super.isFinished()) {
|
||||
this.data = super.getData();
|
||||
this.finished = true;
|
||||
super.reset();
|
||||
} else {
|
||||
this.finished = false;
|
||||
}
|
||||
return noffset;
|
||||
}
|
||||
}
|
||||
|
@@ -21,41 +21,28 @@ import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class UnpackIterator implements Iterator<Object> {
|
||||
public class UnpackIterator extends UnpackResult implements Iterator<Object> {
|
||||
private Unpacker pac;
|
||||
private boolean have;
|
||||
private Object data;
|
||||
|
||||
UnpackIterator(Unpacker pac) {
|
||||
super();
|
||||
this.pac = pac;
|
||||
this.have = false;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
if(have) { return true; }
|
||||
if(finished) { return true; }
|
||||
try {
|
||||
while(true) {
|
||||
if(pac.execute()) {
|
||||
data = pac.getData();
|
||||
pac.reset();
|
||||
have = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(!pac.fill()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return pac.next(this);
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
if(!have && !hasNext()) {
|
||||
if(!finished && !hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
have = false;
|
||||
finished = false;
|
||||
return data;
|
||||
}
|
||||
|
||||
|
42
java/src/main/java/org/msgpack/UnpackResult.java
Normal file
42
java/src/main/java/org/msgpack/UnpackResult.java
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// 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 Object data = null;
|
||||
|
||||
public boolean isFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
finished = false;
|
||||
data = null;
|
||||
}
|
||||
|
||||
void done(Object obj) {
|
||||
finished = true;
|
||||
data = obj;
|
||||
}
|
||||
}
|
||||
|
@@ -22,144 +22,391 @@ import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.msgpack.impl.UnpackerImpl;
|
||||
|
||||
public class Unpacker extends UnpackerImpl implements Iterable<Object> {
|
||||
/**
|
||||
* Unpacker enables you to deserialize objects from stream.
|
||||
*
|
||||
* Unpacker provides Buffered API, Unbuffered API, Schema 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(Object 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(Object 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(Object 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()) {
|
||||
* Object obj = pac.getData();
|
||||
* // ...
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class Unpacker implements Iterable<Object> {
|
||||
|
||||
public static final int DEFAULT_BUFFER_SIZE = 32*1024;
|
||||
// buffer:
|
||||
// +---------------------------------------------+
|
||||
// | [object] | [obje| unparsed ... | unused ...|
|
||||
// +---------------------------------------------+
|
||||
// ^ parsed
|
||||
// ^ offset
|
||||
// ^ filled
|
||||
// ^ buffer.length
|
||||
|
||||
private int used;
|
||||
private int offset;
|
||||
private int parsed;
|
||||
private byte[] buffer;
|
||||
private int bufferReserveSize;
|
||||
private InputStream stream;
|
||||
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) {
|
||||
super();
|
||||
this.used = 0;
|
||||
this.offset = 0;
|
||||
this.parsed = 0;
|
||||
this.buffer = new byte[bufferReserveSize];
|
||||
this.bufferReserveSize = bufferReserveSize/2;
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets schema to convert deserialized object into specific type.
|
||||
* Default schema is {@link GenericSchema} that leaves objects for generic type. Use {@link MessageConvertable#messageConvert(Object)} method to convert the generic object.
|
||||
* @param s schem to use
|
||||
*/
|
||||
public Unpacker useSchema(Schema s) {
|
||||
super.setSchema(s);
|
||||
impl.setSchema(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void reserveBuffer(int size) {
|
||||
if(buffer.length - used >= size) {
|
||||
return;
|
||||
}
|
||||
/*
|
||||
if(used == parsed && buffer.length >= size) {
|
||||
// rewind buffer
|
||||
used = 0;
|
||||
offset = 0;
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
int nextSize = buffer.length * 2;
|
||||
while(nextSize < size + used) {
|
||||
nextSize *= 2;
|
||||
}
|
||||
|
||||
byte[] tmp = new byte[nextSize];
|
||||
System.arraycopy(buffer, offset, tmp, 0, used - offset);
|
||||
|
||||
buffer = tmp;
|
||||
used -= offset;
|
||||
offset = 0;
|
||||
/**
|
||||
* Gets the input stream.
|
||||
* @return the input stream. it may be null.
|
||||
*/
|
||||
public InputStream getStream() {
|
||||
return this.stream;
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
return buffer;
|
||||
/**
|
||||
* Sets the input stream.
|
||||
* @param stream the input stream to set.
|
||||
*/
|
||||
public void setStream(InputStream stream) {
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
public int getBufferOffset() {
|
||||
return used;
|
||||
}
|
||||
|
||||
public int getBufferCapacity() {
|
||||
return buffer.length - used;
|
||||
}
|
||||
|
||||
public void bufferConsumed(int size) {
|
||||
used += size;
|
||||
}
|
||||
|
||||
public void feed(ByteBuffer buffer) {
|
||||
int length = buffer.remaining();
|
||||
if (length == 0) return;
|
||||
reserveBuffer(length);
|
||||
buffer.get(this.buffer, this.offset, length);
|
||||
bufferConsumed(length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, this.buffer, this.offset, length);
|
||||
System.arraycopy(buffer, offset, impl.buffer, impl.offset, length);
|
||||
bufferConsumed(length);
|
||||
}
|
||||
|
||||
public boolean fill() throws IOException {
|
||||
if(stream == null) {
|
||||
return false;
|
||||
}
|
||||
reserveBuffer(bufferReserveSize);
|
||||
int rl = stream.read(getBuffer(), getBufferOffset(), getBufferCapacity());
|
||||
if(rl <= 0) {
|
||||
return false;
|
||||
}
|
||||
bufferConsumed(rl);
|
||||
return true;
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Object> 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];
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = super.execute(buffer, offset, used);
|
||||
if(noffset <= offset) {
|
||||
int noffset = impl.execute(impl.buffer, impl.offset, impl.filled);
|
||||
if(noffset <= impl.offset) {
|
||||
return false;
|
||||
}
|
||||
parsed += noffset - offset;
|
||||
offset = noffset;
|
||||
return super.isFinished();
|
||||
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 + impl.offset, length);
|
||||
impl.offset = noffset - offset;
|
||||
if(impl.isFinished()) {
|
||||
impl.resetState();
|
||||
}
|
||||
return noffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the object deserialized by {@link execute(byte[])} method.
|
||||
*/
|
||||
public Object getData() {
|
||||
return super.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() {
|
||||
super.reset();
|
||||
parsed = 0;
|
||||
impl.reset();
|
||||
}
|
||||
|
||||
public int getMessageSize() {
|
||||
return parsed - offset + used;
|
||||
return parsed - impl.offset + impl.filled;
|
||||
}
|
||||
|
||||
public int getParsedSize() {
|
||||
@@ -167,88 +414,159 @@ public class Unpacker extends UnpackerImpl implements Iterable<Object> {
|
||||
}
|
||||
|
||||
public int getNonParsedSize() {
|
||||
return used - offset;
|
||||
return impl.filled - impl.offset;
|
||||
}
|
||||
|
||||
public void skipNonparsedBuffer(int size) {
|
||||
offset += size;
|
||||
impl.offset += size;
|
||||
}
|
||||
|
||||
public void removeNonparsedBuffer() {
|
||||
used = offset;
|
||||
impl.filled = impl.offset;
|
||||
}
|
||||
|
||||
/*
|
||||
public static class Context {
|
||||
private boolean finished;
|
||||
private Object data;
|
||||
private int offset;
|
||||
private UnpackerImpl impl;
|
||||
|
||||
public Context()
|
||||
{
|
||||
this.finished = false;
|
||||
this.impl = new UnpackerImpl();
|
||||
}
|
||||
|
||||
public boolean isFinished()
|
||||
{
|
||||
return finished;
|
||||
}
|
||||
|
||||
public Object getData()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
int getOffset()
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
void setFinished(boolean finished)
|
||||
{
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
void setData(Object data)
|
||||
{
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
void setOffset(int offset)
|
||||
{
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
UnpackerImpl getImpl()
|
||||
{
|
||||
return impl;
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
public static int unpack(Context ctx, byte[] buffer) throws UnpackException
|
||||
{
|
||||
return unpack(ctx, buffer, 0, buffer.length);
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
public static int unpack(Context ctx, byte[] buffer, int offset, int length) throws UnpackException
|
||||
{
|
||||
UnpackerImpl impl = ctx.getImpl();
|
||||
int noffset = impl.execute(buffer, offset + ctx.getOffset(), length);
|
||||
ctx.setOffset(noffset - offset);
|
||||
if(impl.isFinished()) {
|
||||
ctx.setData(impl.getData());
|
||||
ctx.setFinished(false);
|
||||
impl.reset();
|
||||
} else {
|
||||
ctx.setData(null);
|
||||
ctx.setFinished(true);
|
||||
}
|
||||
int parsed = noffset - offset;
|
||||
ctx.setOffset(parsed);
|
||||
return noffset;
|
||||
/**
|
||||
* 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 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 bytes from the buffer.
|
||||
* This method calls {@link fill()} method if needed.
|
||||
*/
|
||||
public byte[] unpackByteArray() throws IOException {
|
||||
return impl.unpackByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Object unpackObject() throws IOException {
|
||||
return impl.unpackObject();
|
||||
}
|
||||
|
||||
final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
|
||||
obj.messageUnpack(this);
|
||||
}
|
||||
|
||||
final public boolean tryUnpackNull() throws IOException {
|
||||
return impl.tryUnpackNull();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@@ -15,7 +15,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.impl;
|
||||
package org.msgpack;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
//import java.math.BigInteger;
|
||||
@@ -47,7 +47,7 @@ public class UnpackerImpl {
|
||||
static final int CT_MAP_KEY = 0x01;
|
||||
static final int CT_MAP_VALUE = 0x02;
|
||||
|
||||
static final int MAX_STACK_SIZE = 16;
|
||||
static final int MAX_STACK_SIZE = 32;
|
||||
|
||||
private int cs;
|
||||
private int trail;
|
||||
@@ -67,41 +67,45 @@ public class UnpackerImpl {
|
||||
private static final Schema GENERIC_SCHEMA = new GenericSchema();
|
||||
private Schema rootSchema;
|
||||
|
||||
protected UnpackerImpl()
|
||||
public UnpackerImpl()
|
||||
{
|
||||
setSchema(GENERIC_SCHEMA);
|
||||
}
|
||||
|
||||
protected void setSchema(Schema schema)
|
||||
public void setSchema(Schema schema)
|
||||
{
|
||||
this.rootSchema = schema;
|
||||
reset();
|
||||
}
|
||||
|
||||
protected Object getData()
|
||||
public final Object getData()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
protected boolean isFinished()
|
||||
public final boolean isFinished()
|
||||
{
|
||||
return finished;
|
||||
}
|
||||
|
||||
protected void reset()
|
||||
{
|
||||
public final void resetState() {
|
||||
cs = CS_HEADER;
|
||||
top = -1;
|
||||
finished = false;
|
||||
data = null;
|
||||
top_ct = 0;
|
||||
top_count = 0;
|
||||
top_obj = null;
|
||||
top_schema = rootSchema;
|
||||
}
|
||||
|
||||
public final void reset()
|
||||
{
|
||||
resetState();
|
||||
finished = false;
|
||||
data = null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected int execute(byte[] src, int off, int length) throws UnpackException
|
||||
public final int execute(byte[] src, int off, int length) throws UnpackException
|
||||
{
|
||||
if(off >= length) { return off; }
|
||||
|
||||
@@ -153,7 +157,10 @@ public class UnpackerImpl {
|
||||
count = b & 0x0f;
|
||||
//System.out.println("fixarray count:"+count);
|
||||
obj = new Object[count];
|
||||
if(count == 0) { break _push; } // FIXME check IArraySchema
|
||||
if(count == 0) {
|
||||
obj = ((IArraySchema)top_schema).createFromArray((Object[])obj);
|
||||
break _push;
|
||||
}
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
@@ -175,7 +182,10 @@ public class UnpackerImpl {
|
||||
}
|
||||
count = b & 0x0f;
|
||||
obj = new Object[count*2];
|
||||
if(count == 0) { break _push; } // FIXME check IMapSchema
|
||||
if(count == 0) {
|
||||
obj = ((IMapSchema)top_schema).createFromMap((Object[])obj);
|
||||
break _push;
|
||||
}
|
||||
//System.out.println("fixmap count:"+count);
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
@@ -338,7 +348,10 @@ public class UnpackerImpl {
|
||||
castBuffer.put(src, n, 2);
|
||||
count = ((int)castBuffer.getShort(0)) & 0xffff;
|
||||
obj = new Object[count];
|
||||
if(count == 0) { break _push; } // FIXME check IArraySchema
|
||||
if(count == 0) {
|
||||
obj = ((IArraySchema)top_schema).createFromArray((Object[])obj);
|
||||
break _push;
|
||||
}
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
@@ -361,7 +374,10 @@ public class UnpackerImpl {
|
||||
// FIXME overflow check
|
||||
count = castBuffer.getInt(0) & 0x7fffffff;
|
||||
obj = new Object[count];
|
||||
if(count == 0) { break _push; } // FIXME check IArraySchema
|
||||
if(count == 0) {
|
||||
obj = ((IArraySchema)top_schema).createFromArray((Object[])obj);
|
||||
break _push;
|
||||
}
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
stack_ct[top] = top_ct;
|
||||
@@ -383,7 +399,10 @@ public class UnpackerImpl {
|
||||
castBuffer.put(src, n, 2);
|
||||
count = ((int)castBuffer.getShort(0)) & 0xffff;
|
||||
obj = new Object[count*2];
|
||||
if(count == 0) { break _push; } // FIXME check IMapSchema
|
||||
if(count == 0) {
|
||||
obj = ((IMapSchema)top_schema).createFromMap((Object[])obj);
|
||||
break _push;
|
||||
}
|
||||
//System.out.println("fixmap count:"+count);
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
||||
@@ -407,7 +426,10 @@ public class UnpackerImpl {
|
||||
// FIXME overflow check
|
||||
count = castBuffer.getInt(0) & 0x7fffffff;
|
||||
obj = new Object[count*2];
|
||||
if(count == 0) { break _push; } // FIXME check IMapSchema
|
||||
if(count == 0) {
|
||||
obj = ((IMapSchema)top_schema).createFromMap((Object[])obj);
|
||||
break _push;
|
||||
}
|
||||
//System.out.println("fixmap count:"+count);
|
||||
++top;
|
||||
stack_obj[top] = top_obj;
|
8
java/src/main/java/org/msgpack/package-info.java
Normal file
8
java/src/main/java/org/msgpack/package-info.java
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* Use {@link Packer} to serialize and {@link Unpacker} to deserialize.
|
||||
*/
|
||||
package org.msgpack;
|
64
java/src/main/java/org/msgpack/schema/BooleanSchema.java
Normal file
64
java/src/main/java/org/msgpack/schema/BooleanSchema.java
Normal file
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// 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.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BooleanSchema extends Schema {
|
||||
public BooleanSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Boolean";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "boolean";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Boolean) {
|
||||
pk.packBoolean((Boolean)obj);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final boolean convertBoolean(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Boolean) {
|
||||
return (Boolean)obj;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertBoolean(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromBoolean(boolean v) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
@@ -22,59 +22,48 @@ import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class RawSchema extends Schema {
|
||||
public RawSchema() {
|
||||
super("raw");
|
||||
}
|
||||
public class ByteArraySchema extends Schema {
|
||||
public ByteArraySchema() { }
|
||||
|
||||
public String getFullName() {
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "byte[]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "raw";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof byte[]) {
|
||||
byte[] d = (byte[])obj;
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
if(!d.hasArray()) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
pk.packRaw(d.capacity());
|
||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
||||
|
||||
byte[] b = (byte[])obj;
|
||||
pk.packRaw(b.length);
|
||||
pk.packRawBody(b);
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
byte[] b = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(b.length);
|
||||
pk.packRawBody(b);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
// FIXME instanceof GenericObject
|
||||
public static final byte[] convertByteArray(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof byte[]) {
|
||||
// FIXME copy?
|
||||
//byte[] d = (byte[])obj;
|
||||
//byte[] v = new byte[d.length];
|
||||
//System.arraycopy(d, 0, v, 0, d.length);
|
||||
//return v;
|
||||
return obj;
|
||||
|
||||
return (byte[])obj;
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
byte[] v = new byte[d.capacity()];
|
||||
@@ -82,19 +71,22 @@ public class RawSchema extends Schema {
|
||||
d.get(v);
|
||||
d.position(pos);
|
||||
return v;
|
||||
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
return ((String)obj).getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertByteArray(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
byte[] d = new byte[length];
|
@@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteSchema extends Schema {
|
||||
public ByteSchema() {
|
||||
super("Byte");
|
||||
public ByteSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Byte";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,27 +36,32 @@ public class ByteSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packByte( ((Number)obj).byteValue() );
|
||||
|
||||
short value = ((Number)obj).shortValue();
|
||||
if(value > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
pk.packByte((byte)value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final byte convertByte(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
short value = ((Number)obj).shortValue();
|
||||
if(value > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)value;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Byte) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).byteValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertByte(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -63,26 +71,25 @@ public class ByteSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
if(v > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
if(v > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
if(v > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)v;
|
||||
}
|
||||
}
|
||||
|
@@ -77,8 +77,11 @@ public class ClassGenerator {
|
||||
for(FieldSchema f : cs.getFields()) {
|
||||
findSubclassSchema(dst, f.getSchema());
|
||||
}
|
||||
} else if(s instanceof ArraySchema) {
|
||||
ArraySchema as = (ArraySchema)s;
|
||||
} else if(s instanceof ListSchema) {
|
||||
ListSchema as = (ListSchema)s;
|
||||
findSubclassSchema(dst, as.getElementSchema(0));
|
||||
} else if(s instanceof SetSchema) {
|
||||
SetSchema as = (SetSchema)s;
|
||||
findSubclassSchema(dst, as.getElementSchema(0));
|
||||
} else if(s instanceof MapSchema) {
|
||||
MapSchema as = (MapSchema)s;
|
||||
@@ -105,7 +108,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeClass() throws IOException {
|
||||
line();
|
||||
line("public final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
|
||||
line("public final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
@@ -117,7 +120,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeSubclass() throws IOException {
|
||||
line();
|
||||
line("final class "+schema.getName()+" implements MessagePackable, MessageMergeable");
|
||||
line("final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
@@ -135,7 +138,7 @@ public class ClassGenerator {
|
||||
private void writeMemberVariables() throws IOException {
|
||||
line();
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("public "+f.getSchema().getFullName()+" "+f.getName()+";");
|
||||
line("public "+f.getSchema().getClassName()+" "+f.getName()+";");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,13 +153,13 @@ public class ClassGenerator {
|
||||
writeConstructors();
|
||||
writeAccessors();
|
||||
writePackFunction();
|
||||
writeMergeFunction();
|
||||
writeConvertFunction();
|
||||
writeFactoryFunction();
|
||||
}
|
||||
|
||||
private void writeConstructors() throws IOException {
|
||||
line();
|
||||
line("public "+schema.getName()+"() { }");
|
||||
line("public "+schema.getClassName()+"() { }");
|
||||
}
|
||||
|
||||
private void writeAccessors() throws IOException {
|
||||
@@ -184,18 +187,18 @@ public class ClassGenerator {
|
||||
line("}");
|
||||
}
|
||||
|
||||
private void writeMergeFunction() throws IOException {
|
||||
private void writeConvertFunction() throws IOException {
|
||||
line();
|
||||
line("@Override");
|
||||
line("@SuppressWarnings(\"unchecked\")");
|
||||
line("public void messageMerge(Object obj) throws MessageTypeException");
|
||||
line("public void messageConvert(Object obj) throws MessageTypeException");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line("Object[] _source = ((List)obj).toArray();");
|
||||
line("FieldSchema[] _fields = _SCHEMA.getFields();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getFullName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
||||
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getClassName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
||||
++i;
|
||||
}
|
||||
popIndent();
|
||||
@@ -205,13 +208,13 @@ public class ClassGenerator {
|
||||
private void writeFactoryFunction() throws IOException {
|
||||
line();
|
||||
line("@SuppressWarnings(\"unchecked\")");
|
||||
line("public static "+schema.getName()+" createFromMessage(Object[] _message)");
|
||||
line("public static "+schema.getClassName()+" createFromMessage(Object[] _message)");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line(schema.getName()+" _self = new "+schema.getName()+"();");
|
||||
line(schema.getClassName()+" _self = new "+schema.getClassName()+"();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getFullName()+")_message["+i+"];");
|
||||
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getClassName()+")_message["+i+"];");
|
||||
++i;
|
||||
}
|
||||
line("return _self;");
|
||||
|
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import org.msgpack.*;
|
||||
|
||||
public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
protected String name;
|
||||
protected FieldSchema[] fields;
|
||||
protected List<String> imports;
|
||||
protected String namespace;
|
||||
@@ -30,7 +31,7 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
public ClassSchema(
|
||||
String name, String namespace,
|
||||
List<String> imports, List<FieldSchema> fields) {
|
||||
super(name);
|
||||
this.name = name;
|
||||
this.namespace = namespace;
|
||||
this.imports = imports; // FIXME clone?
|
||||
this.fields = new FieldSchema[fields.size()];
|
||||
@@ -42,6 +43,31 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
StringBuffer b = new StringBuffer();
|
||||
b.append("(class ");
|
||||
b.append(name);
|
||||
if(namespace != null) {
|
||||
b.append(" (package "+namespace+")");
|
||||
}
|
||||
for(FieldSchema f : fields) {
|
||||
b.append(" "+f.getExpression());
|
||||
}
|
||||
b.append(")");
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public boolean equals(ClassSchema o) {
|
||||
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
||||
name.equals(o.name);
|
||||
}
|
||||
|
||||
public final FieldSchema[] getFields() {
|
||||
return fields;
|
||||
}
|
||||
@@ -61,35 +87,5 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
void setImports(List<String> imports) {
|
||||
this.imports = imports; // FIXME clone?
|
||||
}
|
||||
|
||||
//@Override
|
||||
//public String getFullName()
|
||||
//{
|
||||
// if(namespace == null) {
|
||||
// return getName();
|
||||
// } else {
|
||||
// return namespace+"."+getName();
|
||||
// }
|
||||
//}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
StringBuffer b = new StringBuffer();
|
||||
b.append("(class ");
|
||||
b.append(getName());
|
||||
if(namespace != null) {
|
||||
b.append(" (package "+namespace+")");
|
||||
}
|
||||
for(FieldSchema f : fields) {
|
||||
b.append(" "+f.getExpression());
|
||||
}
|
||||
b.append(")");
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public boolean equals(SpecificClassSchema o) {
|
||||
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
||||
getName().equals(o.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoubleSchema extends Schema {
|
||||
public DoubleSchema() {
|
||||
super("Double");
|
||||
public DoubleSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Double";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,43 +35,30 @@ public class DoubleSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packDouble( ((Number)obj).doubleValue() );
|
||||
|
||||
if(obj instanceof Double) {
|
||||
pk.packDouble((Double)obj);
|
||||
} else if(obj instanceof Float) {
|
||||
pk.packFloat((Float)obj);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final double convertDouble(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return (Double)obj;
|
||||
} else if(obj instanceof Float) {
|
||||
return ((Float)obj).doubleValue();
|
||||
} else {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).doubleValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (double)v;
|
||||
return convertDouble(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatSchema extends Schema {
|
||||
public FloatSchema() {
|
||||
super("Float");
|
||||
public FloatSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Float";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,43 +35,30 @@ public class FloatSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packFloat( ((Number)obj).floatValue() );
|
||||
|
||||
if(obj instanceof Double) {
|
||||
pk.packDouble((Double)obj);
|
||||
} else if(obj instanceof Float) {
|
||||
pk.packFloat((Float)obj);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final float convertFloat(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return ((Double)obj).floatValue();
|
||||
} else if(obj instanceof Float) {
|
||||
return (Float)obj;
|
||||
} else {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Float) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).floatValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (float)v;
|
||||
return convertFloat(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -41,10 +41,8 @@ public class GenericClassSchema extends ClassSchema {
|
||||
FieldSchema f = fields[i];
|
||||
f.getSchema().pack(pk, d.get(f.getName()));
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
@@ -55,7 +53,6 @@ public class GenericClassSchema extends ClassSchema {
|
||||
if(obj instanceof Collection) {
|
||||
// FIXME optimize
|
||||
return createFromArray( ((Collection)obj).toArray() );
|
||||
|
||||
} else if(obj instanceof Map) {
|
||||
HashMap<String,Object> m = new HashMap<String,Object>(fields.length);
|
||||
Map d = (Map)obj;
|
||||
@@ -65,7 +62,6 @@ public class GenericClassSchema extends ClassSchema {
|
||||
m.put(fieldName, f.getSchema().convert(d.get(fieldName)));
|
||||
}
|
||||
return m;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
@@ -22,11 +22,13 @@ import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
//import org.msgpack.generic.*;
|
||||
|
||||
public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
||||
public GenericSchema() {
|
||||
super("Object");
|
||||
public GenericSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Object";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,70 +125,5 @@ public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromBoolean(boolean v) {
|
||||
return new GenericBoolean(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFromByte(byte v) {
|
||||
return new GenericByte(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return new GenericShort(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return new GenericInt(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return new GenericLong(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return new GenericFloat(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return new GenericDouble(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
return new GenericRaw(b, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj) {
|
||||
// FIXME GenericArray
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromMap(Object[] obj) {
|
||||
GenericMap m = new GenericMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class IntSchema extends Schema {
|
||||
public IntSchema() {
|
||||
super("Integer");
|
||||
public IntSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Integer";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,27 +36,38 @@ public class IntSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packInt( ((Number)obj).intValue() );
|
||||
|
||||
int value = ((Number)obj).intValue();
|
||||
if(value >= Short.MAX_VALUE) {
|
||||
long lvalue = ((Number)obj).longValue();
|
||||
if(lvalue > Integer.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
pk.packInt(value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final int convertInt(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
int value = ((Number)obj).intValue();
|
||||
if(value >= Integer.MAX_VALUE) {
|
||||
long lvalue = ((Number)obj).longValue();
|
||||
if(lvalue > Integer.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Integer) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).intValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertInt(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,16 +87,9 @@ public class IntSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
if(v > Integer.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (int)v;
|
||||
}
|
||||
}
|
||||
|
@@ -22,37 +22,32 @@ import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.RandomAccess;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ArraySchema extends Schema implements IArraySchema {
|
||||
public class ListSchema extends Schema implements IArraySchema {
|
||||
private Schema elementSchema;
|
||||
|
||||
public ArraySchema(Schema elementSchema)
|
||||
{
|
||||
super("array");
|
||||
public ListSchema(Schema elementSchema) {
|
||||
this.elementSchema = elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName()
|
||||
{
|
||||
return "List<"+elementSchema.getFullName()+">";
|
||||
public String getClassName() {
|
||||
return "List<"+elementSchema.getClassName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression()
|
||||
{
|
||||
public String getExpression() {
|
||||
return "(array "+elementSchema.getExpression()+")";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object obj) throws IOException
|
||||
{
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof List) {
|
||||
ArrayList<Object> d = (ArrayList<Object>)obj;
|
||||
List<Object> d = (List<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
@@ -63,62 +58,53 @@ public class ArraySchema extends Schema implements IArraySchema {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
}
|
||||
|
||||
} else if(obj instanceof Set) {
|
||||
Set<Object> d = (Set<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
for(Object e : d) {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object obj) throws MessageTypeException
|
||||
{
|
||||
if(obj instanceof List) {
|
||||
List d = (List)obj;
|
||||
ArrayList ar = new ArrayList(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
ar.add( elementSchema.convert(d.get(i)) );
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
ar.add( elementSchema.convert(e) );
|
||||
}
|
||||
}
|
||||
return ar;
|
||||
|
||||
} else if(obj instanceof Collection) {
|
||||
Collection d = (Collection)obj;
|
||||
ArrayList ar = new ArrayList(d.size());
|
||||
for(Object e : (Collection)obj) {
|
||||
ar.add( elementSchema.convert(e) );
|
||||
}
|
||||
return ar;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
public static final <T> List<T> convertList(Object obj,
|
||||
Schema elementSchema, List<T> dest) throws MessageTypeException {
|
||||
if(!(obj instanceof List)) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
List<Object> d = (List<Object>)obj;
|
||||
if(dest == null) {
|
||||
dest = new ArrayList<T>(d.size());
|
||||
}
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
dest.add( (T)elementSchema.convert(d.get(i)) );
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
dest.add( (T)elementSchema.convert(e) );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index)
|
||||
{
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertList(obj, elementSchema, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index) {
|
||||
return elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj)
|
||||
{
|
||||
public Object createFromArray(Object[] obj) {
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
}
|
@@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class LongSchema extends Schema {
|
||||
public LongSchema() {
|
||||
super("Long");
|
||||
public LongSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Long";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,27 +36,25 @@ public class LongSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packLong( ((Number)obj).longValue() );
|
||||
|
||||
long value = ((Number)obj).longValue();
|
||||
pk.packLong(value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final long convertLong(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
return ((Number)obj).longValue();
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Long) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).longValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertLong(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,15 +76,5 @@ public class LongSchema extends Schema {
|
||||
public Object createFromLong(long v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (long)v;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -27,14 +27,13 @@ public class MapSchema extends Schema implements IMapSchema {
|
||||
private Schema valueSchema;
|
||||
|
||||
public MapSchema(Schema keySchema, Schema valueSchema) {
|
||||
super("map");
|
||||
this.keySchema = keySchema;
|
||||
this.valueSchema = valueSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return "HashList<"+keySchema.getFullName()+", "+valueSchema.getFullName()+">";
|
||||
public String getClassName() {
|
||||
return "Map<"+keySchema.getClassName()+", "+valueSchema.getClassName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,29 +51,33 @@ public class MapSchema extends Schema implements IMapSchema {
|
||||
keySchema.pack(pk, e.getKey());
|
||||
valueSchema.pack(pk, e.getValue());
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Map) {
|
||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||
Map<Object,Object> m = new HashMap<Object,Object>();
|
||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||
m.put(keySchema.convert(e.getKey()), valueSchema.convert(e.getValue()));
|
||||
}
|
||||
return m;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
public static final <K,V> Map<K,V> convertMap(Object obj,
|
||||
Schema keySchema, Schema valueSchema, Map<K,V> dest) throws MessageTypeException {
|
||||
if(!(obj instanceof Map)) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||
if(dest == null) {
|
||||
dest = new HashMap<K,V>(d.size());
|
||||
}
|
||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||
dest.put((K)keySchema.convert(e.getKey()),
|
||||
(V)valueSchema.convert(e.getValue()));
|
||||
}
|
||||
return (Map<K,V>)d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertMap(obj, keySchema, valueSchema, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,14 +93,14 @@ public class MapSchema extends Schema implements IMapSchema {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object createFromMap(Object[] obj) {
|
||||
HashMap m = new HashMap(obj.length / 2);
|
||||
HashMap dest = new HashMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
dest.put(k, v);
|
||||
}
|
||||
return m;
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user