all files / taf-stream/ libjce.js

78.52% Statements 797/1015
55.42% Branches 179/323
80% Functions 192/240
79.17% Lines 707/893
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433                                                                                                                                                        64× 64×                                                         104× 104×                       66× 66× 66× 66×     69× 40× 10×                                 35×   66×                                                                           435× 435× 435× 435× 435× 435×     32× 109× 32× 182× 130× 18× 93× 93×   89× 89×                       30×     30×   89×                                       438×     435×           606×                 293×   241×   293× 293× 293× 293×     110× 772×   35×     35×               32× 32×     1512× 1365×     147× 147× 147× 147× 147×     147×           151× 151× 151× 151× 151×     945× 945× 945× 945× 945×     13× 13× 13× 13× 13×                   48× 48× 48× 48× 48×                   73× 73×   73×     73×   69× 69× 35× 35×   34× 34× 34×                   73× 73×   73× 73×             168× 24× 24× 24× 24× 24×     144× 144×   144×     144× 144×     55×   55× 55× 55× 55×     55× 55×   55× 55× 55× 55×     151×     13× 13×     31× 31×     1662× 1662×                       73×                     73× 73× 73×   73× 69× 35×   34× 34×                           165× 165× 165× 24× 24× 24×     141× 141× 141×                 141×     141×     55× 55×                   55× 55× 55× 55×     53× 53× 53×                                     108×     780× 780×                                 274× 274× 123×   151× 151×       266× 266× 253×   13× 13×       255× 255× 244×   11× 11×       76× 76×   73× 73×                                               165× 165× 24× 24×       24× 24×     24× 24×     141× 141×       141× 141×     141×     55× 55× 55× 55×   20× 20×     20×   20×   20× 36×       94× 94×   94× 94× 94× 75×   94× 91× 91×       102×           107× 107×     32× 32×     1497× 1497× 1497×   1497× 1497×     717× 717× 717×   717×                                                                                                                 717× 717×   717×       717× 717× 717×                           21×   21× 21× 18×           22×   22× 22×   18×           252×   252× 252× 105× 130×           76×   76× 76×       73×                                                 165×   165× 165× 165×                               55×   55× 55× 55× 55×       55× 55×       55× 55× 55×           20×   20× 20×       20× 20×         20×     20×   20× 20× 36×   20×     94×   94× 94×       94× 94×       94× 94× 75×   94×   94× 91× 91× 91×     94×           37× 37× 37× 37× 37×   37× 37×       32× 32× 32× 32×     32×                                   32× 32×     32× 32×   32× 32×               18× 18× 18× 18×             18× 18× 18×         37× 37× 37× 37× 37× 37× 37× 37× 37× 37× 37×   37× 37× 37× 37× 37× 37× 37× 37×       18× 18× 18× 18× 18× 18× 18× 18× 18× 18× 18× 18×   18× 18×   18× 18× 18× 18×   18×     18× 18× 18× 18× 18× 18× 18× 18× 18× 18×   18×     18× 18×     18× 18× 18×     18× 18×              
/**
 * KevinTian@tencent.com
 *
 * NodeJs版本JCE编解码
 */
var assert = require("assert");
var util   = require("util");
var Taf    = module.exports = {};
var BIT32 = 4294967296;
var BIT31 = BIT32 / 2;
var Big = typeof Buffer.prototype.writeBigInt64BE === "function" ? null : require('big.js');
 
/**
 * 异常类
 */
Taf.JceException = function ($code, $message) {
    this.code    = $code;
    this.message = $message;
}
util.inherits(Taf.JceException, Error);
 
Taf.JceEncodeException = function ($message) {
    this.code    = -2;
    this.message = $message;
}
util.inherits(Taf.JceEncodeException, Taf.JceException);
 
Taf.JceDecodeException = function ($message) {
    this.code    = -1;
    this.message = $message;
}
util.inherits(Taf.JceDecodeException, Taf.JceException);
 
Taf.JceDecodeMismatch = function ($message) {
    this.code    = -1;
    this.message = $message;
}
util.inherits(Taf.JceDecodeMismatch, Taf.JceDecodeException);
 
Taf.JceDecodeRequireNotExist = function ($message) {
    this.code    = -1;
    this.message = $message;
}
util.inherits(Taf.JceDecodeRequireNotExist, Taf.JceDecodeException);
 
Taf.JceDecodeInvalidValue = function ($message) {
    this.code    = -1;
    this.message = $message;
}
util.inherits(Taf.JceDecodeInvalidValue, Taf.JceDecodeException);
 
Taf.WupNotFoundKey = function ($message) {
    this.code    = -1;
    this.message = $message;
}
util.inherits(Taf.WupNotFoundKey, Taf.JceException);
 
/**
 * JCE编解码底层辅助类
 */
Taf.DataHelp = {
    EN_INT8         : 0,
    EN_INT16        : 1,
    EN_INT32        : 2,
    EN_INT64        : 3,
    EN_FLOAT        : 4,
    EN_DOUBLE       : 5,
    EN_STRING1      : 6,
    EN_STRING4      : 7,
    EN_MAP          : 8,
    EN_LIST         : 9,
    EN_STRUCTBEGIN  : 10,
    EN_STRUCTEND    : 11,
    EN_ZERO         : 12,
    EN_SIMPLELIST   : 13
};
 
Taf.Boolean = {
    _write      : function(os, tag, val) { return os.writeBoolean(tag, val);      },
    _read       : function(is, tag, def) { return is.readBoolean(tag, true, def); },
    _classname  : "bool"
};
 
Taf.Int8 = {
    _write      : function(os, tag, val) { return os.writeInt8(tag, val); },
    _read       : function(is, tag, def) { return is.readInt8(tag, true, def); },
    _classname  : "char"
};
 
Taf.Int16 = {
    _write      : function(os, tag, val) { return os.writeInt16(tag, val); },
    _read       : function(is, tag, def) { return is.readInt16(tag, true, def); },
    _classname  : "short"
};
 
Taf.Int32 = {
    _write      : function(os, tag, val) { return os.writeInt32(tag, val); },
    _read       : function(is, tag, def) { return is.readInt32(tag, true, def); },
    _classname  : "int32"
};
 
Taf.Int64 = {
    _write      : function(os, tag, val, bString) { return os.writeInt64(tag, val, bString); },
    _read       : function(is, tag, def, bString) { return is.readInt64(tag, true, def, bString); },
    _classname  : "int64"
};
 
Taf.UInt8 = {
    _write      : function(os, tag, val) { return os.writeInt16(tag, val); },
    _read       : function(is, tag, def) { return is.readInt16(tag, true, def); },
    _classname  : "short"
};
 
Taf.UInt16 = {
    _write      : function(os, tag, val) { return os.writeInt32(tag, val); },
    _read       : function(is, tag, def) { return is.readInt32(tag, true, def); },
    _classname  : "int32"
};
 
Taf.UInt32 = {
    _write      : function(os, tag, val) { return os.writeInt64(tag, val);      },
    _read       : function(is, tag, def) { return is.readInt64(tag, true, def); },
    _classname  : "int64"
};
 
Taf.Float = {
    _write      : function(os, tag, val) { return os.writeFloat(tag, val);      },
    _read       : function(is, tag, def) { return is.readFloat(tag, true, def); },
    _classname  : "float"
};
 
Taf.Double = {
    _write      : function(os, tag, val) { return os.writeDouble(tag, val);      },
    _read       : function(is, tag, def) { return is.readDouble(tag, true, def); },
    _classname  : "double"
};
 
Taf.String = {
    _write      : function(os, tag, val, bRaw) { return os.writeString(tag, val, bRaw);      },
    _read       : function(is, tag, def, bRaw) { return is.readString(tag, true, def, bRaw); },
    _classname  : "string"
};
 
Taf.Enum    = {
    _write      : function(os, tag, val) { return os.writeInt32(tag, val);        },
    _read       : function(is, tag, def) { return is.readInt32(tag, true, def);   },
    _classname  : "int32"
};
 
/**
 * TAF-List实现类
 */
var HeroList = function (proto, bValue) {
    this._proto     = proto;
    this._bValue = bValue || 0;
    this.value      = new Array();
    this._classname = "List<" + proto._classname + ">";
};
 
HeroList.prototype._write  = function(os, tag, val, bRaw, bString) { return os.writeList(tag, val, bRaw, bString); };
HeroList.prototype._read   = function(is, tag, def, bRaw, bString) { return is.readList(tag, true, def, bRaw, bString); };
HeroList.prototype.new     = function() { return new HeroList(this._proto); };
HeroList.prototype.at      = function(index) { return this.value[index]; };
HeroList.prototype.push    = function(value) { this.value.push(value); };
HeroList.prototype.forEach = function(callback) {
    for (var i = 0; i < this.value.length; i++) {
        Iif (callback.call(null, this.value[i], i, this.value) == false) break;
    }
};
HeroList.prototype.toObject = function () {
    var tmp = [];
    for (var i = 0, len = this.value.length; i < len; i++){
        tmp.push(!this.value[i].toObject?this.value[i]:this.value[i].toObject());
    }
 
    return tmp;
};
 
HeroList.prototype.readFromObject = function (json) {
    var vsimple = !this._proto.create && !this._proto._vproto && !this._proto._proto && !this._proto.new;
 
    for (var i = 0, len = json.length; i < len; i++) {
        Eif (vsimple) {
            this.push(json[i]);
        } else {
            var temp = this._proto.new();
            temp.readFromObject(json[i]);
            this.push(temp);
        }
    }
 
    return this;
}
 
HeroList.prototype.__defineGetter__("length", function () { return this.value.length; });
 
Taf.List = function(proto, bValue) {
    return new HeroList(proto, bValue);
};
 
/**
 * TAF-Map实现类
 */
var MultiMap = function(kproto, vproto, bKey, bValue) {
    this._kproto    = kproto;
    this._vproto    = vproto;
    this._bKey = bKey || 0;
    this._bValue = bValue || 0;
    this.keys       = new Object();
    this.value      = new Object();
    this._classname = "map<" + kproto._classname + "," + vproto._classname + ">";
};
 
MultiMap.prototype._write  = function(os, tag, val, bRaw)   { return os.writeMap(tag, val, bRaw);           };
MultiMap.prototype._read   = function(is, tag, def, bRaw)   { return is.readMap(tag, true, def, bRaw);      };
MultiMap.prototype.put     = function(key, value)     { this.insert(key, value);                };
MultiMap.prototype.set     = function(key, value)     { this.insert(key, value);                };
MultiMap.prototype.remove  = function(key)            { delete this.keys[key._genKey()]; delete this.value[key._genKey()]; };
MultiMap.prototype.size    = function()               { return Object.keys(this.keys || {}).length;};
MultiMap.prototype.has     = function(key)            { return this.keys.hasOwnProperty(key._genKey()); };
MultiMap.prototype.insert  = function(key, value)     {
    var keys = Object.keys(this.keys || {});
    for(var i = 0; i < keys.length; i++)
    {
        var anItem = keys[i];
        Iif (key._equal(this.keys[anItem])) {
            this.value[anItem] = value;
            return ;
        }
    }
 
    this.keys[key._genKey()]  = key;
    this.value[key._genKey()] = value;
};
 
MultiMap.prototype.get = function(key) {
    var keys = Object.keys(this.keys || {});
    for(var i = 0; i < keys.length; i++)
    {
        var anItem = keys[i];
        if (key._equal(this.keys[anItem])) {
            return this.value[anItem];
        }
    }
    return undefined;
};
 
MultiMap.prototype.clear   = function() {
    delete this.keys;
    delete this.value;
    this.keys  = new Object();
    this.value = new Object();
}
 
MultiMap.prototype.forEach = function(callback) {
    var keys = Object.keys(this.value || {});
    for(var i = 0; i < keys.length; i++)
    {
        var key = keys[i];
        Iif (callback.call(null, this.keys[key], this.value[key]) == false)
            break;
    }
};
 
MultiMap.prototype.toObject = function () {
    assert(false, "multimap has no toObject function");
}
 
MultiMap.prototype.readFromObject = function () {
    assert(false, "multimap has no toObject readFromObject");
}
 
var HeroMap = function (kproto, vproto, bKey, bValue) {
    this._kproto    = kproto;
    this._vproto    = vproto;
    this._bKey = bKey || 0;
    this._bValue = bValue || 0;
    this.value      = new Object();
    this._classname = "map<" + kproto._classname + "," + vproto._classname + ">";
}
 
HeroMap.prototype._write  = function(os, tag, val)   { return os.writeMap(tag, val);                       }
HeroMap.prototype._read   = function(is, tag, def)   { return is.readMap(tag, true, def);                  }
HeroMap.prototype.new     = function()               { return new HeroMap(this._kproto, this._vproto);     }
HeroMap.prototype.put     = function(key, value)     { this.insert(key, value);                            }
HeroMap.prototype.set     = function(key, value)     { this.insert(key, value);                            }
HeroMap.prototype.remove  = function(key)            { delete this.value[key];                             }
HeroMap.prototype.size    = function()               { return Object.keys(this.value || {}).length;        }
HeroMap.prototype.has     = function(key)            { return this.value.hasOwnProperty(key);              }
HeroMap.prototype.insert  = function(key, value)     { this.value[key] = value;                            }
HeroMap.prototype.get     = function(key)            { return this.value[key];                             }
HeroMap.prototype.clear   = function()               { delete this.value; this.value = new Object();       }
HeroMap.prototype.forEach = function(callback, bKey)       {
    var keys = Object.keys(this.value || {});
    for(var i = 0; i < keys.length; i++)
    {
        var key = keys[i];
        switch (this._kproto){
            case Taf.Int8:
            case Taf.Int16:
            case Taf.Int32:
            case Taf.UInt8:
            case Taf.UInt16:
            case Taf.UInt32:
            case Taf.Float:
            case Taf.Double:
                key=Number(key);
                break;
            case Taf.Int64:
                Iif(!bKey){
                    key=Number(key);
                }
                break;
        }
        Iif (callback.call(null, key, this.value[key]) == false)
            break;
    }
};
 
HeroMap.prototype.toObject = function () {
    var tmp = {};
    var keys = Object.keys(this.value || {});
    for(var i = 0; i < keys.length; i++)
    {
        var key = keys[i];
        tmp[key] = !this.value[key].toObject?this.value[key]:this.value[key].toObject();
    }
 
    return tmp;
};
 
HeroMap.prototype.readFromObject = function (json) {
    var vsimple = !this._vproto.create && !this._vproto._vproto && !this._vproto._proto;
 
    var keys = Object.keys(json || {});
    for(var i = 0; i < keys.length; i++)
    {
        var key = keys[i];
        Eif (vsimple) {
            this.insert(key, json[key]);
        } else {
            var temp = this._vproto.new();
            temp.readFromObject(json[key]);
            this.insert(key, temp);
        }
    }
};
 
Taf.Map = function(kproto, vproto, bKey, bValue) {
    if (kproto.prototype && kproto.prototype._equal) {
        return new MultiMap(kproto, vproto, bKey, bValue);
    }
 
    return new HeroMap(kproto, vproto, bKey, bValue);
}
 
/**
 * 适用于NodeJS支持的Buffer的实现类
 */
var createNodeBuffer = (function () {
    Eif ('allocUnsafe' in Buffer) {
        return function (data) {
            return Buffer.allocUnsafe(data);
        }
    } else {
        return function (data) {
            return new Buffer(data);
        }
    }
}());
 
Taf.BinBuffer = function (buffer) {
    if(!buffer)
    {
        buffer = createNodeBuffer(0);
    }
    this._buffer    = (buffer != undefined && buffer instanceof Buffer)?buffer:null;
    this._length    = (buffer != undefined && buffer instanceof Buffer)?buffer.length:0;
    this._capacity  = this._length;
    this._position  = 0;
}
Taf.BinBuffer._classname = "list<char>";
 
Taf.BinBuffer.prototype.__defineGetter__("length",   function () { return this._length;   });
Taf.BinBuffer.prototype.__defineGetter__("capacity", function () { return this._capacity; });
Taf.BinBuffer.prototype.__defineGetter__("position", function () { return this._position; });
Taf.BinBuffer.prototype.__defineSetter__("position", function (position) { this._position = position; });
 
Taf.BinBuffer._write = function (os, tag, val) {
    return os.writeBytes(tag, val);
}
 
Taf.BinBuffer._read = function (os, tag, def) {
    return os.readBytes(tag, true, def);
}
 
Taf.BinBuffer.new = function () {
    return new Taf.BinBuffer();
}
 
Taf.BinBuffer.from = function (data) {
    var binBuffer = new Taf.BinBuffer();
    binBuffer.writeString(data);
    return binBuffer;
}
 
Taf.BinBuffer.prototype.reset = function () {
    this._length    = 0;
    this._position  = 0;
}
 
Taf.BinBuffer.prototype._allocate = function (byteLength) {
    if (this._capacity > this._position + byteLength) {
        return ;
    }
 
    this._capacity = Math.max(512, (this._position + byteLength) * 2);
    var temp = createNodeBuffer(this._capacity);
    Eif (this._buffer != null) {
        this._buffer.copy(temp, 0, 0, this._position);
        this._buffer = undefined;
    }
 
    this._buffer = temp;
}
 
Taf.BinBuffer.prototype.replace = function (srcBuffer, offset, byteLength) {
    srcBuffer.copy(this._buffer, 0, offset, offset + byteLength);
}
 
Taf.BinBuffer.prototype.writeInt8 = function (value) {
    value = +value;
    this._allocate(1);
    this._buffer.writeInt8(value, this._position);
    this._position += 1;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeUInt8 = function (value) {
    value = +value;
    this._allocate(1);
    this._buffer.writeUInt8(value, this._position);
    this._position += 1;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeInt16 = function (value) {
    value = +value;
    this._allocate(2);
    this._buffer.writeInt16BE(value, this._position);
    this._position += 2;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeUInt16 = function (value) {
    value = +value;
    this._allocate(2);
    this._buffer.writeUInt16BE(value, this._position);
    this._position += 2;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeInt32 = function (value) {
    value = +value;
    this._allocate(4);
    this._buffer.writeInt32BE(value, this._position);
    this._position += 4;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeUInt32 = function (value) {
    value = +value;
    this._allocate(4);
    this._buffer.writeUInt32BE(value, this._position);
    this._position += 4;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeInt64  = function (value, bString) {
    this._allocate(8);
    var H4, L4, val;
    //nodejs v12+ 直接支持bigint写入(value仍然是string表达时才支持高精度)
    Iif(typeof this._buffer.writeBigInt64BE === "function"){
        this._buffer.writeBigInt64BE(BigInt(value), this._position);
    } else {
        if (bString === true || bString === 1) {
            //字符串int64写入,支持int64精度
            val = new Big(value);
            if (val.s === 1) {
                H4 = +val.div(BIT32).round(0,0).toString();
                L4 = +val.mod(BIT32).toString();
            } else {
                H4 = val.div(BIT32).round(0,3);
                L4 = +val.minus((new Big(H4)).times(BIT32)).toString();
                H4 = +H4.plus(BIT32).toString();
            }
        } else {
            //Number写入,支持到2^53-1的精度
            val = +value;
            Eif (val > 0){
                H4 = Math.floor(val/BIT32);
                L4 = val - H4 * BIT32;
            } else {
                H4 = Math.floor(val/BIT32);
                L4 = val - H4 * BIT32;
                H4 += BIT32;
            }
        }
        this._buffer.writeUInt32BE(H4, this._position);
        this._buffer.writeUInt32BE(L4, this._position + 4);
    }
    this._position += 8;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeFloat = function (value) {
    this._allocate(4);
    this._buffer.writeFloatBE(value, this._position);
    this._position += 4;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeDouble = function (value) {
    this._allocate(8);
    this._buffer.writeDoubleBE(value, this._position);
    this._position += 8;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeString = function (value, ByteLength, bRaw) {
    if (bRaw === true || bRaw === 1) {
        this._allocate(ByteLength);
        value.copy(this._buffer, this._position);
        this._position += ByteLength;
        this._length = this._position;
        return ;
    }
 
    var byteLength = ByteLength || Buffer.byteLength(value);
    this._allocate(byteLength);
 
    this._buffer.write(value, this._position, byteLength, "utf8");
 
 
    this._position += byteLength;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeBinBuffer = function (srcBinBuffer) {
    Iif (srcBinBuffer._length == 0 || srcBinBuffer._buffer == null) return ;
 
    this._allocate(srcBinBuffer.length);
    srcBinBuffer._buffer.copy(this._buffer, this._position, 0, srcBinBuffer._length);
    this._position += srcBinBuffer._length;
    this._length = this._position;
}
 
Taf.BinBuffer.prototype.writeNodeBuffer = function (srcBuffer, offset, byteLength) {
    offset      = (offset == undefined)?0:offset;
    byteLength  = (byteLength == undefined)?srcBuffer.length:byteLength;
 
    this._allocate(byteLength);
    srcBuffer.copy(this._buffer, this._position, offset, offset + byteLength);
    this._length   += byteLength;
    this._position += byteLength;
}
 
Taf.BinBuffer.prototype.readInt8 = function () {
    return this._buffer.readInt8(this._position++);
}
 
Taf.BinBuffer.prototype.readInt16 = function () {
    this._position += 2;
    return this._buffer.readInt16BE(this._position - 2);
}
 
Taf.BinBuffer.prototype.readInt32 = function () {
    this._position += 4;
    return this._buffer.readInt32BE(this._position - 4);
}
 
Taf.BinBuffer.prototype.readUInt8 = function () {
    this._position += 1;
    return this._buffer.readUInt8(this._position - 1);
}
 
Taf.BinBuffer.prototype.readUInt16 = function () {
    this._position += 2;
    return this._buffer.readUInt16BE(this._position - 2);
}
 
Taf.BinBuffer.prototype.readUInt32 = function () {
    this._position += 4;
    return this._buffer.readUInt32BE(this._position - 4);
}
 
Taf.BinBuffer.prototype.readInt64 = function(bString) {
    //nodejs v12+ 支持bigint,性能比big.js好
    Iif(typeof this._buffer.readBigInt64BE  === "function"){
        var bigInt =  this._buffer.readBigInt64BE(this._position);
        this._position += 8;
        if(bString === true || bString === 1){
            return bigInt.toString();
        }
        if(bString === 2){
            return bigInt;
        }
        return Number(bigInt);
    }
    var H4 = this._buffer.readUInt32BE(this._position);
    var L4 = this._buffer.readUInt32BE(this._position + 4);
    this._position += 8;
    //字符串格式读出,支持int64精度
    if (bString === true || bString === 1) {
        if (H4 < BIT31) {
            return new Big(H4).times(BIT32).plus(L4).toString();
        } else {
            H4 = BIT32 - 1 - H4;
            return '-' + (new Big(H4)).times(BIT32).plus(BIT32 - L4).toString();
        }
    }
    //Number读出,支持到 2^53 - 1 的精度
    Eif (H4 < BIT31) {
        return H4 * BIT32 + L4;
    } else {
        return -((BIT32 - L4) + BIT32 * (BIT32 - 1 - H4));
    }
}
 
Taf.BinBuffer.prototype.readFloat = function() {
    this._position += 4;
    return this._buffer.readFloatBE(this._position - 4);
}
 
Taf.BinBuffer.prototype.readDouble = function() {
    this._position += 8;
    return this._buffer.readDoubleBE(this._position - 8);
}
 
Taf.BinBuffer.prototype.readString = function (byteLength, bRaw) {
 
    var temp = createNodeBuffer(byteLength);
    var ret;
    if (bRaw === true || bRaw === 1) {
        this._buffer.copy(temp, 0, this._position, this._position + byteLength);
        this._position += byteLength;
        return temp;
    }
 
    this._buffer.copy(temp, 0, this._position, this._position + byteLength);
    this._position += byteLength;
    Iif(temp.length == 1)
    {
        if(temp[0] & 0x80)
        {
            ret = temp.toString("binary", 0, temp.length);
        } else {
            ret = temp.toString("utf8", 0, temp.length);
        }
    } else {
        ret = temp.toString("utf8", 0, temp.length);
    }
 
    return ret;
}
 
Taf.BinBuffer.prototype.readBinBuffer = function (byteLength, bReuse) {
    var temp;
    Iif (bReuse === true) {
        temp = new Taf.BinBuffer();
        temp._buffer    = this._buffer.slice(this._position, this._position + byteLength);
        temp._length    = byteLength;
        temp._capacity  = byteLength;
        temp._position  = 0;
 
        return temp;
    }
 
    temp = new Taf.BinBuffer();
    temp.writeNodeBuffer(this._buffer, this._position, byteLength);
    this._position += byteLength;
    return temp;
}
 
Taf.BinBuffer.prototype.toNodeBuffer = function () {
    var temp = createNodeBuffer(this._length);
    this._buffer.copy(temp, 0, 0, this._length);
    return temp;
}
 
Taf.BinBuffer.prototype.toNodeBufferUnSafe = function () {
    return this._buffer.slice(0, this._length);
}
 
Taf.BinBuffer.prototype.toObject = function () {
    return this.toNodeBuffer();
}
 
Taf.BinBuffer.prototype.readFromObject = Taf.BinBuffer.prototype.writeNodeBuffer;
 
Taf.BinBuffer.prototype.print = function() {
    var str  = "";
    for (var i = 0; i < this._length; i++) {
        str += (this._buffer[i] > 15?"":"0") + this._buffer[i].toString(16) + (((i+1)%16 == 0)?"\n":" ");
    }
 
    console.log(str.toUpperCase());
}
 
/**
 * TAF-JCE输出编解码包裹类
 */
Taf.JceOutputStream = function () {
    this._binBuffer = new Taf.BinBuffer;
}
 
Taf.JceOutputStream.prototype._writeTo = function (tag, type) {
    Eif (tag < 15) {
        this._binBuffer.writeUInt8((tag << 4 & 0xF0) | type);
    } else {
        this._binBuffer.writeUInt16((0xF0 | type) << 8 | tag);
    }
}
 
Taf.JceOutputStream.prototype.setHeaderLength = function (value) {
    var position = this._binBuffer._position === 0?4:this._binBuffer._position;
    var length   = this._binBuffer._position === 0?4:this._binBuffer._length;
 
    this._binBuffer._position = 0;
    this._binBuffer.writeInt32(value);
    this._binBuffer._position = position;
    this._binBuffer._length   = length;
}
 
Taf.JceOutputStream.prototype.writeBoolean = function (tag, value) {
    this.writeInt8(tag, value == true ? 1 : 0);
}
 
Taf.JceOutputStream.prototype.writeInt8 = function (tag, value) {
    value = +value;
    if (value == 0) {
        this._writeTo(tag, Taf.DataHelp.EN_ZERO);
    } else {
        this._writeTo(tag, Taf.DataHelp.EN_INT8);
        this._binBuffer.writeInt8(value);
    }
}
 
Taf.JceOutputStream.prototype.writeInt16 = function (tag, value) {
    value = +value;
    if (value >= -128 && value <= 127) {
        this.writeInt8(tag, value);
    } else {
        this._writeTo(tag, Taf.DataHelp.EN_INT16);
        this._binBuffer.writeInt16(value);
    }
}
 
Taf.JceOutputStream.prototype.writeInt32 = function (tag, value) {
    value = +value;
    if (value >= -32768 && value <= 32767) {
        this.writeInt16(tag, value);
    } else {
        this._writeTo(tag, Taf.DataHelp.EN_INT32);
        this._binBuffer.writeInt32(value);
    }
}
 
Taf.JceOutputStream.prototype.writeInt64 = function (tag, value, bString) {
    var val = Number(value);
    if (val >= -2147483648 && val <= 2147483647) {
        this.writeInt32(tag, val);
        return;
    }
    this._writeTo(tag, Taf.DataHelp.EN_INT64);
    this._binBuffer.writeInt64(value, bString);
}
 
Taf.JceOutputStream.prototype.writeUInt8 = function (tag, value) {
    this.writeInt16(tag, value);
}
 
Taf.JceOutputStream.prototype.writeUInt16 = function (tag, value) {
    this.writeInt32(tag, value);
}
 
Taf.JceOutputStream.prototype.writeUInt32 = function (tag, value) {
    this.writeInt64(tag, value);
}
 
Taf.JceOutputStream.prototype.writeFloat = function (tag, value) {
    Iif (value == 0) {
        this._writeTo(tag, Taf.DataHelp.EN_ZERO);
    } else {
        this._writeTo(tag, Taf.DataHelp.EN_FLOAT);
        this._binBuffer.writeFloat(value);
    }
}
 
Taf.JceOutputStream.prototype.writeDouble = function (tag, value) {
    Iif (value == 0) {
        this._writeTo(tag, Taf.DataHelp.EN_ZERO);
    } else {
        this._writeTo(tag, Taf.DataHelp.EN_DOUBLE);
        this._binBuffer.writeDouble(value);
    }
}
 
Taf.JceOutputStream.prototype.writeStruct = function (tag, value) {
    Iif (value._writeTo == undefined) {
        throw Error("not defined writeTo Function");
    }
 
    this._writeTo(tag, Taf.DataHelp.EN_STRUCTBEGIN);
    value._writeTo(this);
    this._writeTo(0, Taf.DataHelp.EN_STRUCTEND);
}
 
Taf.JceOutputStream.prototype.writeString = function (tag, value, bRaw) {
    var byteLength;
    if (bRaw != undefined && bRaw == true) {
        byteLength = value.length;
        Iif (byteLength > 255) {
            this._writeTo(tag, Taf.DataHelp.EN_STRING4);
            this._binBuffer.writeUInt32(byteLength);
        } else {
            this._writeTo(tag, Taf.DataHelp.EN_STRING1);
            this._binBuffer.writeUInt8(byteLength);
        }
 
        this._binBuffer.writeString(value, byteLength, bRaw);
        return ;
    }
 
    byteLength = Buffer.byteLength(value, "utf8");
    Iif (byteLength > 255) {
        this._writeTo(tag, Taf.DataHelp.EN_STRING4);
        this._binBuffer.writeUInt32(byteLength);
    } else {
        this._writeTo(tag, Taf.DataHelp.EN_STRING1);
        this._binBuffer.writeUInt8(byteLength);
    }
 
    this._binBuffer.writeString(value, byteLength);
}
 
Taf.JceOutputStream.prototype.writeBytes = function (tag, value) {
    this._writeTo(tag, Taf.DataHelp.EN_SIMPLELIST);
    this._writeTo(0, Taf.DataHelp.EN_INT8);
    this.writeInt32(0, value.length);
    this._binBuffer.writeBinBuffer(value);
}
var writeListDeprecateWarnning = util.deprecate(function(){},"bRaw in writeList is deprecated, use List(TafStream.String, bRaw) instead")
Taf.JceOutputStream.prototype.writeList = function (tag, value, bRaw) {
    this._writeTo(tag, Taf.DataHelp.EN_LIST);
    this.writeInt32(0, value.length);
    //3.0.21版本之前通过writeList(xxx, true)来表示字符串转buffer,long转string表示
    //3.0.21版本之后通过构造 List<T, bValue>来表示是否转换,这里以后要废弃掉
    if(bRaw === true ){
        writeListDeprecateWarnning();
    }
    var bValue = value._bValue || bRaw;
 
    for (var i = 0, len = value.value.length; i < len; i++) {
        value._proto._write(this, 0, value.value[i], bValue);
    }
}
 
Taf.JceOutputStream.prototype.writeMap  = function (tag, value) {
    this._writeTo(tag, Taf.DataHelp.EN_MAP);
    this.writeInt32(0, value.size());
 
    var self = this;
    var bKey = value._bKey, bValue = value._bValue;
    if(value._kproto == Taf.String){
        bKey = false;
    }
    value.forEach(function (key, val){
        value._kproto._write(self, 0, key, bKey);
        value._vproto._write(self, 1, val, bValue);
    }, bKey);
}
 
Taf.JceOutputStream.prototype.getBinBuffer = function() {
    return this._binBuffer;
}
 
/**
 * TAF-JCE输入编解码包裹类
 */
Taf.JceInputStream = function (binBuffer) {
    this._binBuffer = binBuffer;
    this._binBuffer._position = 0;
}
 
Taf.JceInputStream.prototype.setBinBuffer = function (binBuffer) {
    this._binBuffer = binBuffer;
    this._binBuffer._position = 0;
}
 
Taf.JceInputStream.prototype._readFrom = function () {
    var temp = this._binBuffer.readUInt8();
    var tag  = (temp & 0xF0) >> 4;
    var type = (temp & 0x0F);
 
    Iif (tag >= 15) tag = this._binBuffer.readUInt8();
    return {tag:tag, type:type};
}
 
Taf.JceInputStream.prototype._peekFrom = function () {
    var pos  = this._binBuffer._position;
    var head = this._readFrom();
    this._binBuffer.position = pos;
 
    return {tag:head.tag, type:head.type, size:(head.tag >= 15) ? 2 : 1};
}
 
Taf.JceInputStream.prototype._skipField = function (type) {
    var Length,size,head,i;
    switch (type) {
        case Taf.DataHelp.EN_INT8        : this._binBuffer._position += 1; break;
        case Taf.DataHelp.EN_INT16       : this._binBuffer._position += 2; break;
        case Taf.DataHelp.EN_INT32       : this._binBuffer._position += 4; break;
        case Taf.DataHelp.EN_INT64       : this._binBuffer._position += 8; break;
        case Taf.DataHelp.EN_FLOAT       : this._binBuffer._position += 4; break;
        case Taf.DataHelp.EN_DOUBLE      : this._binBuffer._position += 8; break;
        case Taf.DataHelp.EN_STRING1     : {
            Length = this._binBuffer.readUInt8();
            this._binBuffer._position += Length;
            break;
        }
        case Taf.DataHelp.EN_STRING4     : {
            Length = this._binBuffer.readUInt32();
            this._binBuffer._position += Length;
            break;
        }
        case Taf.DataHelp.EN_STRUCTBEGIN : this._skipToStructEnd(); break;
        case Taf.DataHelp.EN_STRUCTEND   :
        case Taf.DataHelp.EN_ZERO        : break;
        case Taf.DataHelp.EN_MAP         : {
            size = this.readInt32(0, true);
 
            for (i = 0; i < size * 2; ++i) {
                head = this._readFrom();
                this._skipField(head.type);
            }
 
            break;
        }
        case Taf.DataHelp.EN_SIMPLELIST : {
            head = this._readFrom();
            if (head.type != Taf.DataHelp.EN_INT8) {
                throw new Taf.JceDecodeInvalidValue("skipField with invalid type, type value: " + type + "," + head.type);
            }
 
            Length = this.readInt32(0, true);
            this._binBuffer._position += Length;
            break;
        }
        case Taf.DataHelp.EN_LIST : {
            size = this.readInt32(0, true);
            for (i = 0; i < size; ++i) {
                head = this._readFrom();
                this._skipField(head.type);
            }
            break;
        }
        default : throw new Taf.JceDecodeInvalidValue("skipField with invalid type, type value: " + type);
    }
}
 
Taf.JceInputStream.prototype._skipToStructEnd = function () {
    for ( ; ; ) {
        var head = this._readFrom();
        this._skipField(head.type);
 
        Eif (head.type == Taf.DataHelp.EN_STRUCTEND) {
            return;
        }
    }
}
Taf.JceInputStream.prototype._skipToTag = function (tag, require) {
    while (this._binBuffer._position < this._binBuffer._length) {
        var head = this._peekFrom();
        //记录tag的位置,struct随读随解码
        Iif(this._tagPosMap){
            this._tagPosMap[head.tag] = this._binBuffer._position;
            this._tagPosMap._current = this._binBuffer._position;
        }
        Eif (tag <= head.tag || head.type == Taf.DataHelp.EN_STRUCTEND) {
            Eif ((head.type === Taf.DataHelp.EN_STRUCTEND)?false:(tag === head.tag)) {
                return true;
            }
            break;
        }
        this._binBuffer._position += head.size;
        this._skipField(head.type);
    }
 
    if (require) throw new Taf.JceDecodeRequireNotExist("require field not exist, tag:" + tag);
    return false;
}
 
Taf.JceInputStream.prototype.readBoolean = function (tag, require, DEFAULT_VALUE) {
    return this.readInt8(tag, require, DEFAULT_VALUE) == 1 ? true : false;
}
 
Taf.JceInputStream.prototype.readInt8 = function (tag, require, DEFAULT_VALUE) {
    Iif (this._skipToTag(tag, require) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    switch (head.type) {
        case Taf.DataHelp.EN_ZERO: return 0;
        case Taf.DataHelp.EN_INT8: return this._binBuffer.readInt8();
    }
 
    throw new Taf.JceDecodeMismatch("read int8 type mismatch, tag:" + tag + ", get type:" + head.type);
}
 
Taf.JceInputStream.prototype.readInt16 = function (tag, require, DEFAULT_VALUE) {
    Iif (this._skipToTag(tag, require) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    switch (head.type) {
        case Taf.DataHelp.EN_ZERO   : return 0;
        case Taf.DataHelp.EN_INT8   : return this._binBuffer.readInt8();
        case Taf.DataHelp.EN_INT16  : return this._binBuffer.readInt16();
    }
 
    throw new Taf.JceDecodeMismatch("read int8 type mismatch, tag:" + tag + ", get type:" + head.type);
}
 
Taf.JceInputStream.prototype.readInt32 = function (tag, requrire, DEFAULT_VALUE) {
    Iif (this._skipToTag(tag, requrire) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    switch (head.type) {
        case Taf.DataHelp.EN_ZERO   : return 0;
        case Taf.DataHelp.EN_INT8   : return this._binBuffer.readInt8();
        case Taf.DataHelp.EN_INT16  : return this._binBuffer.readInt16();
        case Taf.DataHelp.EN_INT32  : return this._binBuffer.readInt32();
    }
 
    throw new Taf.JceDecodeMismatch("read int8 type mismatch, tag:" + tag + ", get type:" + head.type);
}
 
Taf.JceInputStream.prototype.readInt64 = function (tag, require, DEFAULT_VALUE, bString) {
    Iif (this._skipToTag(tag, require) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    switch (head.type) {
        case Taf.DataHelp.EN_ZERO   : return bString ? "0" : 0;
        case Taf.DataHelp.EN_INT8   : return bString ? this._binBuffer.readInt8() + "" : this._binBuffer.readInt8();
        case Taf.DataHelp.EN_INT16  : return bString ? this._binBuffer.readInt16() + "" : this._binBuffer.readInt16();
        case Taf.DataHelp.EN_INT32  : return bString ? this._binBuffer.readInt32() + "" : this._binBuffer.readInt32();
        case Taf.DataHelp.EN_INT64  : return this._binBuffer.readInt64(bString);
    }
    throw new Taf.JceDecodeMismatch("read int64 type mismatch, tag:" + tag + ", get type:" + head.type);
}
 
Taf.JceInputStream.prototype.readFloat = function (tag, require, DEFAULT_VALUE) {
    Iif (this._skipToTag(tag, require) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    switch (head.type) {
        case Taf.DataHelp.EN_ZERO   : return 0;
        case Taf.DataHelp.EN_FLOAT  : return this._binBuffer.readFloat();
    }
 
    throw new Taf.JceDecodeMismatch("read float type mismatch, tag:" + tag + ", get type:" + head.type);
}
 
Taf.JceInputStream.prototype.readDouble = function (tag, require, DEFAULT_VALUE) {
    Iif (this._skipToTag(tag, require) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    switch (head.type) {
        case Taf.DataHelp.EN_ZERO    : return 0;
        case Taf.DataHelp.EN_DOUBLE  : return this._binBuffer.readDouble();
    }
 
    throw new Taf.JceDecodeMismatch("read double type mismatch, tag:" + tag + ", get type:" + head.type);
}
 
Taf.JceInputStream.prototype.readUInt8 = function (tag, require, DEFAULT_VALUE)  {
    return this.readInt16(tag, require, DEFAULT_VALUE);
}
 
Taf.JceInputStream.prototype.readUInt16 = function (tag, require, DEFAULT_VALUE) {
    return this.readInt32(tag, require, DEFAULT_VALUE);
}
 
Taf.JceInputStream.prototype.readUInt32 = function (tag, require, DEFAULT_VALUE) {
    return this.readInt64(tag, require, DEFAULT_VALUE);
}
 
Taf.JceInputStream.prototype.readString = function (tag, require, DEFAULT_VALUE, bRaw) {
    Iif (this._skipToTag(tag, require) == false) { return DEFAULT_VALUE; }
 
    var head = this._readFrom();
    Eif (head.type == Taf.DataHelp.EN_STRING1) {
        return this._binBuffer.readString(this._binBuffer.readUInt8(), bRaw);
    }
 
    if (head.type == Taf.DataHelp.EN_STRING4) {
        return this._binBuffer.readString(this._binBuffer.readUInt32(), bRaw);
    }
 
    throw new Taf.JceDecodeMismatch("read 'string' type mismatch, tag: " + tag + ", get type: " + head.type + ".");
}
 
Taf.JceInputStream.prototype.readStruct = function (tag, require, TYPE_T) {
    Iif (this._skipToTag(tag, require) == false) { return  new TYPE_T(); }
 
    var head = this._readFrom();
    Iif (head.type != Taf.DataHelp.EN_STRUCTBEGIN) {
        throw new Taf.JceDecodeMismatch("read struct type mismatch, tag: " + tag + ", get type:" + head.type);
    }
 
    var temp = TYPE_T._readFrom(this);
    this._skipToStructEnd();
    return temp;
}
 
Taf.JceInputStream.prototype.readBytes  = function(tag, require, TYPE_T, bRaw) {
    Iif (this._skipToTag(tag, require) == false) { return new TYPE_T(); }
 
    var head = this._readFrom();
    Eif (head.type == Taf.DataHelp.EN_SIMPLELIST) {
        var temp = this._readFrom();
        Iif (temp.type != Taf.DataHelp.EN_INT8) {
            throw new Taf.JceDecodeMismatch("type mismatch, tag:" + tag + ",type:" + head.type + "," + temp.type);
        }
 
        var size = this.readInt32(0, true);
        Iif (size < 0) {
            throw new Taf.JceDecodeInvalidValue("invalid size, tag:" + tag + ",type:" + head.type + "," + temp.type);
        }
 
        var bytes = this._binBuffer.readBinBuffer(size, bRaw);
        bytes.position = 0;
        return bytes;
    }
 
    throw new Taf.JceDecodeMismatch("type mismatch, tag:" + tag + ",type:" + head.type);
}
 
var readListDeprecateWarnning = util.deprecate(function(){},"bRaw in readList is deprecated, use List(TafStream.String, bRaw) instead");
Taf.JceInputStream.prototype.readList = function(tag, require, TYPE_T, bRaw) {
    Iif (this._skipToTag(tag, require) == false) { return TYPE_T; }
 
    var head = this._readFrom();
    Iif (head.type != Taf.DataHelp.EN_LIST) {
        throw new Taf.JceDecodeMismatch("read 'vector' type mismatch, tag: " + tag + ", get type: " + head.type);
    }
 
    var size = this.readInt32(0, true);
    Iif (size < 0) {
        throw new Taf.JceDecodeInvalidValue("invalid size, tag: " + tag + ", type: " + head.type + ", size: " + size);
    }
    //3.0.21版本之前通过readList(xxx, true)来表示字符串转buffer,long转string表示
    //3.0.21版本之后通过构造 List<T, bValue>来表示是否转换,这里以后要废弃掉
    if(bRaw === true){
        readListDeprecateWarnning();
    }
 
    var bValue = TYPE_T._bValue || bRaw;
 
    var TEMP = new Taf.List(TYPE_T._proto);
    for (var i = 0; i < size; ++i) {
        TEMP.value.push(TEMP._proto._read(this, 0, TEMP._proto, bValue));
    }
    return TEMP;
}
 
Taf.JceInputStream.prototype.readMap = function(tag, require, TYPE_T) {
    Iif (this._skipToTag(tag, require) == false) { return TYPE_T; }
 
    var head = this._readFrom();
    Iif (head.type != Taf.DataHelp.EN_MAP) {
        throw new Taf.JceDecodeMismatch("read 'map' type mismatch, tag: " + tag + ", get type: " + head.type);
    }
 
    var size = this.readInt32(0, true);
    Iif (size < 0) {
        throw new Taf.JceDecodeMismatch("invalid map, tag: " + tag + ", size: " + size);
    }
 
    var bKey = TYPE_T._bKey, bValue = TYPE_T._bValue;
    if(TYPE_T._kproto == Taf.String){
        bKey = false;
    }
    var TEMP = new Taf.Map(TYPE_T._kproto, TYPE_T._vproto, bKey, bValue);
 
    for (var i = 0; i < size; i++) {
        var key = TEMP._kproto._read(this, 0, TEMP._kproto, bKey);
        var val = TEMP._vproto._read(this, 1, TEMP._vproto, bValue);
        TEMP.insert(key, val);
    }
 
    return TEMP;
}
 
/**
 * WUP包裹类
 */
Taf.UniAttribute = function () {
    this._data = new Taf.Map(Taf.String, Taf.BinBuffer);
    this._mmap = new Taf.Map(Taf.String, Taf.Map(Taf.String, Taf.BinBuffer));
    this._buff = new Taf.JceOutputStream();
    this._temp = new Taf.JceInputStream(new Taf.BinBuffer());
    this._iver = Taf.UniAttribute.WUP_SIMPLE;
 
    this.__defineGetter__("wupVersion", function() { return this._iver; });
    this.__defineSetter__("wupVersion", function(value) { this._iver = value; });
}
 
Taf.UniAttribute.WUP_COMPLEX = 2;
Taf.UniAttribute.WUP_SIMPLE  = 3;
 
Taf.UniAttribute.prototype._getkey = function(name, DEFAULT_VALUE, TYPE_T, FUNC, bValue) {
    var binBuffer;
    Eif (this._iver == Taf.UniAttribute.WUP_SIMPLE) {
        binBuffer = this._data.get(name);
        Iif (binBuffer == undefined && DEFAULT_VALUE == undefined) {
            throw new Taf.WupNotFoundKey("UniAttribute not found key:" + name);
        }
        Iif (binBuffer == undefined && DEFAULT_VALUE != undefined) {
            return DEFAULT_VALUE;
        }
    } else {
        var item = this._mmap.get(name);
        if (item == undefined && DEFAULT_VALUE == undefined) {
            throw new Taf.WupNotFoundKey("UniAttribute not found key:" + name);
        }
        if (item == undefined && DEFAULT_VALUE != undefined) {
            return DEFAULT_VALUE;
        }
 
        binBuffer = item.get(TYPE_T._classname);
        if (binBuffer == undefined) {
            throw new Taf.WupNotFoundKey("UniAttribute type match fail,key:" + name + ",type:" + TYPE_T._classname);
        }
    }
 
    this._temp.setBinBuffer(binBuffer);
    return FUNC.call(this._temp, 0, true, TYPE_T, bValue)
};
 
Taf.UniAttribute.prototype._setkey = function(name, value, TYPE_T, FUNC, bValue) {
    this._buff._binBuffer.reset();
    FUNC.call(this._buff, 0, value, bValue);
 
    Eif (this._iver == Taf.UniAttribute.WUP_SIMPLE) {
        this._data.set(name, new Taf.BinBuffer(this._buff.getBinBuffer().toNodeBuffer()));
    } else {
        var temp = new Taf.Map(Taf.String, Taf.BinBuffer);
        temp.set(TYPE_T._classname, new Taf.BinBuffer(this._buff.getBinBuffer().toNodeBuffer()));
        this._mmap.set(name, temp);
    }
};
 
Taf.UniAttribute.prototype.decode = function(binBuffer) {
    var is = new Taf.JceInputStream(binBuffer);
    Eif (this._iver == Taf.UniAttribute.WUP_SIMPLE) {
        this._data.clear();
        this._data = is.readMap(0, true, Taf.Map(Taf.String, Taf.BinBuffer));
    } else {
        this._mmap.clear();
        this._mmap = is.readMap(0, true, Taf.Map(Taf.String, Taf.Map(Taf.String, Taf.BinBuffer)));
    }
};
 
Taf.UniAttribute.prototype.encode = function() {
    var os = new Taf.JceOutputStream();
    os.writeMap(0, this._iver == Taf.UniAttribute.WUP_SIMPLE?this._data:this._mmap);
    return os.getBinBuffer();
};
 
Taf.UniAttribute.prototype.writeBoolean = function(name, value) { this._setkey(name, value, Taf.Boolean, this._buff.writeBoolean); };
Taf.UniAttribute.prototype.writeInt8    = function(name, value) { this._setkey(name, value, Taf.Int8, this._buff.writeInt8);       };
Taf.UniAttribute.prototype.writeUInt8   = function(name, value) { this._setkey(name, value, Taf.UInt8, this._buff.writeUInt8);     };
Taf.UniAttribute.prototype.writeInt16   = function(name, value) { this._setkey(name, value, Taf.Int16, this._buff.writeInt16);     };
Taf.UniAttribute.prototype.writeUInt16  = function(name, value) { this._setkey(name, value, Taf.UInt16, this._buff.writeUInt16);   };
Taf.UniAttribute.prototype.writeInt32   = function(name, value) { this._setkey(name, value, Taf.Int32, this._buff.writeInt32);     };
Taf.UniAttribute.prototype.writeUInt32  = function(name, value) { this._setkey(name, value, Taf.UInt32, this._buff.writeUInt32);   };
Taf.UniAttribute.prototype.writeInt64   = function(name, value, bValue) { this._setkey(name, value, Taf.Int64, this._buff.writeInt64, bValue);     };
Taf.UniAttribute.prototype.writeFloat   = function(name, value) { this._setkey(name, value, Taf.Float, this._buff.writeFloat);     };
Taf.UniAttribute.prototype.writeDouble  = function(name, value) { this._setkey(name, value, Taf.Double, this._buff.writeDouble);   };
Taf.UniAttribute.prototype.writeBytes   = function(name, value) { this._setkey(name, value, Taf.BinBuffer, this._buff.writeBytes); };
Taf.UniAttribute.prototype.writeString  = function(name, value, bValue) { this._setkey(name, value, Taf.String, this._buff.writeString, bValue);   };
Taf.UniAttribute.prototype.writeStruct  = function(name, value) { this._setkey(name, value, value, this._buff.writeStruct);        };
Taf.UniAttribute.prototype.writeList    = function(name, value) { this._setkey(name, value, value, this._buff.writeList);          };
Taf.UniAttribute.prototype.writeMap     = function(name, value) { this._setkey(name, value, value, this._buff.writeMap);           };
 
Taf.UniAttribute.prototype.readBoolean  = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.Boolean,    this._temp.readBoolean);     };
Taf.UniAttribute.prototype.readInt8     = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.Int8,       this._temp.readInt8);        };
Taf.UniAttribute.prototype.readUInt8    = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.UInt8,      this._temp.readUInt8);       };
Taf.UniAttribute.prototype.readInt16    = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.Int16,      this._temp.readInt16);       };
Taf.UniAttribute.prototype.readUInt16   = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.UInt16,     this._temp.readUInt16);      };
Taf.UniAttribute.prototype.readInt32    = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.Int32,      this._temp.readInt32);       };
Taf.UniAttribute.prototype.readUInt32   = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.UInt32,     this._temp.readUInt32);      };
Taf.UniAttribute.prototype.readInt64    = function(name, DEFAULT_VALUE, bValue) { return this._getkey(name, DEFAULT_VALUE, Taf.Int64,      this._temp.readInt64, bValue);       };
Taf.UniAttribute.prototype.readFloat    = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.Float,      this._temp.readFloat);       };
Taf.UniAttribute.prototype.readDouble   = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.Double,     this._temp.readDouble);      };
Taf.UniAttribute.prototype.readBytes    = function(name, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, Taf.BinBuffer,  this._temp.readBytes);       };
Taf.UniAttribute.prototype.readString   = function(name, DEFAULT_VALUE, bValue) { return this._getkey(name, DEFAULT_VALUE, Taf.String,     this._temp.readString, bValue);      };
Taf.UniAttribute.prototype.readStruct   = function(name, TYPE_T, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, TYPE_T, this._temp.readStruct);      };
Taf.UniAttribute.prototype.readList     = function(name, TYPE_T, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, TYPE_T, this._temp.readList);        };
Taf.UniAttribute.prototype.readMap      = function(name, TYPE_T, DEFAULT_VALUE) { return this._getkey(name, DEFAULT_VALUE, TYPE_T, this._temp.readMap);         };
 
Taf.Wup = function () {
    this._iVersion     = 0;
    this._cPacketType  = 0;
    this._iMessageType = 0;
    this._iRequestId   = 0;
    this._sServantName = '';
    this._sFuncName    = '';
    this._binBuffer    = new Taf.BinBuffer();
    this._iTimeout     = 0;
    this._context      = new Taf.Map(Taf.String, Taf.String);
    this._status       = new Taf.Map(Taf.String, Taf.String);
    this._attribute    = new Taf.UniAttribute();
 
    this.__defineGetter__("servantName", function() { return this._sServantName; });
    this.__defineSetter__("servantName", function(value) { this._sServantName = value; });
    this.__defineGetter__("funcName",    function() { return this._sFuncName; });
    this.__defineSetter__("funcName",    function(value) { this._sFuncName = value; });
    this.__defineGetter__("requestId",   function() { return this._iRequestId; });
    this.__defineSetter__("requestId",   function(value) { this._iRequestId = value; });
    this.__defineGetter__("wupVersion",  function() { return this._attribute.wupVersion; });
    this.__defineSetter__("wupVersion",  function(value) { this._attribute.wupVersion = value; });
};
 
Taf.Wup.WUP_COMPLEX = Taf.UniAttribute.WUP_COMPLEX; //复杂WUP协议
Taf.Wup.WUP_SIMPLE  = Taf.UniAttribute.WUP_SIMPLE;  //精简WUP协议
 
Taf.Wup.prototype._writeTo = function() {
    var os = new Taf.JceOutputStream();
    os._binBuffer.writeInt32(0);
    os.writeInt16  (1,  this._attribute.wupVersion);
    os.writeInt8   (2,  this._cPacketType);
    os.writeInt32  (3,  this._iMessageType);
    os.writeInt32  (4,  this._iRequestId);
    os.writeString (5,  this._sServantName);
    os.writeString (6,  this._sFuncName);
    os.writeBytes  (7,  this._binBuffer);
    os.writeInt32  (8,  this._iTimeout);
    os.writeMap    (9,  this._context);
    os.writeMap    (10, this._status);
 
    var pos = os._binBuffer._position;
    var len = os._binBuffer._length;
 
    os._binBuffer._position = 0;
    os._binBuffer.writeInt32(os._binBuffer._length);
    os._binBuffer._length   = len;
    os._binBuffer._position = pos;
 
    return os.getBinBuffer();
}
 
Taf.Wup.prototype._readFrom = function(is) {
    this._iVersion     = is.readInt16(1, true);
    this._cPacketType  = is.readInt8(2, true);
    this._iMessageType = is.readInt32(3, true);
    this._iRequestId   = is.readInt32(4, true);
    this._sServantName = is.readString(5, true);
    this._sFuncName    = is.readString(6, true);
    this._binBuffer    = is.readBytes(7, true);
    this._iTimeout     = is.readInt32(8, true);
    this._context      = is.readMap(9, false, Taf.Map(Taf.String, Taf.String));
    this._status       = is.readMap(10, false, Taf.Map(Taf.String, Taf.String));
 
    this._attribute.wupVersion = this._iVersion;
}
 
Taf.Wup.prototype.encode = function() {
    this._binBuffer = this._attribute.encode();
    return this._writeTo();
}
 
Taf.Wup.prototype.decode = function (binBuffer) {
    var is  = new Taf.JceInputStream(binBuffer);
    var len = is._binBuffer.readInt32();
    Iif (len < 4) {
        throw Error("packet length too short");
    }
    this._readFrom(is);
    this._attribute.decode(this._binBuffer);
}
 
Taf.Wup.prototype.getTafResultCode = function () {
    var code = this._status.get("STATUS_RESULT_CODE");
    return code === undefined?0:parseInt(code);
}
 
Taf.Wup.prototype.getTafResultDesc = function () {
    var desc = this._status.get("STATUS_RESULT_DESC");
    return desc === undefined?"":desc;
}
 
Taf.Wup.prototype.writeBoolean = function(name, value) { this._attribute.writeBoolean(name, value); }
Taf.Wup.prototype.writeInt8    = function(name, value) { this._attribute.writeInt8(name, value);    }
Taf.Wup.prototype.writeUInt8   = function(name, value) { this._attribute.writeUInt8(name, value);   }
Taf.Wup.prototype.writeInt16   = function(name, value) { this._attribute.writeInt16(name, value);   }
Taf.Wup.prototype.writeUInt16  = function(name, value) { this._attribute.writeUInt16(name, value);  }
Taf.Wup.prototype.writeInt32   = function(name, value) { this._attribute.writeInt32(name, value);   }
Taf.Wup.prototype.writeUInt32  = function(name, value) { this._attribute.writeUInt32(name, value);  }
Taf.Wup.prototype.writeInt64   = function(name, value, bValue) { this._attribute.writeInt64(name, value, bValue);   }
Taf.Wup.prototype.writeFloat   = function(name, value) { this._attribute.writeFloat(name, value);   }
Taf.Wup.prototype.writeDouble  = function(name, value) { this._attribute.writeDouble(name, value);  }
Taf.Wup.prototype.writeBytes   = function(name, value) { this._attribute.writeBytes(name, value);   }
Taf.Wup.prototype.writeString  = function(name, value, bValue) { this._attribute.writeString(name, value, bValue);  }
Taf.Wup.prototype.writeStruct  = function(name, value) { this._attribute.writeStruct(name, value);  }
Taf.Wup.prototype.writeList    = function(name, value) { this._attribute.writeList(name, value);    }
Taf.Wup.prototype.writeMap     = function(name, value) { this._attribute.writeMap(name, value);     }
 
Taf.Wup.prototype.readBoolean  = function(name, DEFAULT_VALUE)         { return this._attribute.readBoolean(name, DEFAULT_VALUE);        }
Taf.Wup.prototype.readInt8     = function(name, DEFAULT_VALUE)         { return this._attribute.readInt8(name, DEFAULT_VALUE);           }
Taf.Wup.prototype.readUInt8    = function(name, DEFAULT_VALUE)         { return this._attribute.readUInt8(name, DEFAULT_VALUE);          }
Taf.Wup.prototype.readInt16    = function(name, DEFAULT_VALUE)         { return this._attribute.readInt16(name, DEFAULT_VALUE);          }
Taf.Wup.prototype.readUInt16   = function(name, DEFAULT_VALUE)         { return this._attribute.readUInt16(name, DEFAULT_VALUE);         }
Taf.Wup.prototype.readInt32    = function(name, DEFAULT_VALUE)         { return this._attribute.readInt32(name, DEFAULT_VALUE);          }
Taf.Wup.prototype.readUInt32   = function(name, DEFAULT_VALUE)         { return this._attribute.readUInt32(name, DEFAULT_VALUE);         }
Taf.Wup.prototype.readInt64    = function(name, DEFAULT_VALUE, bValue)         { return this._attribute.readInt64(name, DEFAULT_VALUE, bValue);          }
Taf.Wup.prototype.readFloat    = function(name, DEFAULT_VALUE)         { return this._attribute.readFloat(name, DEFAULT_VALUE);          }
Taf.Wup.prototype.readDouble   = function(name, DEFAULT_VALUE)         { return this._attribute.readDouble(name, DEFAULT_VALUE);         }
Taf.Wup.prototype.readBytes    = function(name, DEFAULT_VALUE)         { return this._attribute.readBytes(name, DEFAULT_VALUE);          }
Taf.Wup.prototype.readString   = function(name, DEFAULT_VALUE, bValue)         { return this._attribute.readString(name, DEFAULT_VALUE, bValue);         }
Taf.Wup.prototype.readStruct   = function(name, TYPE_T, DEFAULT_VALUE) { return this._attribute.readStruct(name, TYPE_T, DEFAULT_VALUE); }
Taf.Wup.prototype.readList     = function(name, TYPE_T, DEFAULT_VALUE) { return this._attribute.readList(name, TYPE_T, DEFAULT_VALUE);   }
Taf.Wup.prototype.readMap      = function(name, TYPE_T, DEFAULT_VALUE) { return this._attribute.readMap(name, TYPE_T, DEFAULT_VALUE);    }