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
|
/****************************************************************************
* NCSA HDF *
* National Computational Science Alliance *
* University of Illinois at Urbana-Champaign *
* 605 E. Springfield, Champaign IL 61820 *
* *
* Center for Information Sciences and Databases, ETH Zurich, Switzerland *
* *
* For conditions of distribution and use, see the accompanying *
* COPYING file. *
* *
****************************************************************************/
package ch.systemsx.cisd.base.convert;
import java.nio.ByteBuffer;
/**
* This class encapsulates native methods to deal with arrays of numbers, converting from numbers to
* bytes and bytes to numbers.
* <p>
* These routines are used by class <b>HDFArray</b> to pass data to and from the HDF5 library.
* <p>
* Methods copyXxxToByte() convert a Java array of primitive numbers (int, short, ...) to a Java
* array of bytes. Methods copyByteToXxx() convert from a Java array of bytes into a Java array of
* primitive numbers (int, short, ...)
* <p>
* Variant interfaces convert only a sub-array.
*/
public class NativeData
{
private static final boolean useNativeLib = false;
/** Size of a <code>short</code> value in <code>byte</code>s. */
public final static int SHORT_SIZE = 2;
/** Size of a <code>char</code> value in <code>byte</code>s. */
public final static int CHAR_SIZE = 2;
/** Size of an <code>int</code> value in <code>byte</code>s. */
public final static int INT_SIZE = 4;
/** Size of a <code>long</code> value in <code>byte</code>s. */
public final static int LONG_SIZE = 8;
/** Size of a <code>float</code> value in <code>byte</code>s. */
public final static int FLOAT_SIZE = 4;
/** Size of a <code>double</code> value in <code>byte</code>s. */
public final static int DOUBLE_SIZE = 8;
/** Byte Order enumeration. */
// Implementation note: the ordinal of the entries needs to be understood by the native methods
public enum ByteOrder
{
/** <code>byte[]</code> is in native byte order (that is: don't change byte order) */
NATIVE(java.nio.ByteOrder.nativeOrder()),
/** <code>byte[]</code> is in little endian byte order */
LITTLE_ENDIAN(java.nio.ByteOrder.LITTLE_ENDIAN),
/** <code>byte[]</code> is in big endian byte order */
BIG_ENDIAN(java.nio.ByteOrder.BIG_ENDIAN);
private final java.nio.ByteOrder nioByteOrder;
ByteOrder(java.nio.ByteOrder nioByteOrder)
{
this.nioByteOrder = nioByteOrder;
}
java.nio.ByteOrder getNioByteOrder()
{
return nioByteOrder;
}
static ByteOrder getNativeByteOrder()
{
return NATIVE.nioByteOrder.equals(LITTLE_ENDIAN.nioByteOrder) ? LITTLE_ENDIAN
: BIG_ENDIAN;
}
}
/**
* Returns <code>true</code> if this platform is a little-endian platform and <code>false</code>
* , if it is a big-endian platform.
*/
private static native boolean isLittleEndian();
/**
* Copies a range from an array of <code>int</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>int</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>int</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>int</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyIntToByte(int[] inData, int inStart, byte[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>byte</code> into an array of <code>int</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>int</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>int</code> to
* start
* @param len The number of <code>int</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyByteToInt(byte[] inData, int inStart, int[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>long</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>long</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>long</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>long</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyLongToByte(long[] inData, int inStart, byte[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>byte</code> into an array of <code>long</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>long</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>long</code> to
* start
* @param len The number of <code>long</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyByteToLong(byte[] inData, int inStart, long[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>short</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>short</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>short</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>short</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyShortToByte(short[] inData, int inStart, byte[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>byte</code> into an array of <code>short</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>short</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>short</code> to
* start
* @param len The number of <code>short</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyByteToShort(byte[] inData, int inStart, short[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>char</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>char</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>char</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>char</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyCharToByte(char[] inData, int inStart, byte[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>byte</code> into an array of <code>char</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>char</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>char</code> to
* start
* @param len The number of <code>char</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyByteToChar(byte[] inData, int inStart, char[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>float</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>float</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>float</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>float</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyFloatToByte(float[] inData, int inStart, byte[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>byte</code> into an array of <code>float</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>float</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>float</code> to
* start
* @param len The number of <code>float</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyByteToFloat(byte[] inData, int inStart, float[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>double</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>double</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>double</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>double</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyDoubleToByte(double[] inData, int inStart, byte[] outData,
int outStart, int len, int byteOrder);
/**
* Copies a range from an array of <code>byte</code> into an array of <code>double</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>double</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>double</code> to
* start
* @param len The number of <code>double</code> to copy
* @param byteOrder The ordinal of {@link ByteOrder}, encoding what byte order the
* <var>outData</var> should be in.
*/
private static native void copyByteToDouble(byte[] inData, int inStart, double[] outData,
int outStart, int len, int byteOrder);
//
// Public
//
/** Call to ensure that the native library is loaded. */
public static void ensureNativeLibIsLoaded()
{
}
/**
* Returns <code>true</code>, if this class uses the native library and <code>false</code>
* otherwise.
*/
public static boolean isUseNativeLib()
{
return useNativeLib;
}
/**
* Returns the native byte order of the host running this JRE.
*/
public static ByteOrder getNativeByteOrder()
{
return ByteOrder.getNativeByteOrder();
}
/**
* Changes the byte order of the bytes constituting <var>s</var>.
*/
public static short changeByteOrder(short s)
{
return (short) ((s << 8) | ((s >> 8) & 0xff));
}
/**
* Changes the byte order of the bytes constituting <var>c</var>.
*/
public static char changeByteOrder(char c)
{
return (char) ((c << 8) | ((c >> 8) & 0xff));
}
/**
* Changes the byte order of the bytes constituting <var>i</var>.
*/
public static int changeByteOrder(int i)
{
return ((changeByteOrder((short) i) << 16) | (changeByteOrder((short) (i >> 16)) & 0xffff));
}
/**
* Changes the byte order of the bytes constituting <var>f</var>.
*/
public static float changeByteOrder(float f)
{
return Float.intBitsToFloat(changeByteOrder(Float.floatToRawIntBits(f)));
}
/**
* Changes the byte order of the bytes constituting <var>l</var>.
*/
public static long changeByteOrder(long l)
{
return (((long) changeByteOrder((int) (l)) << 32) | (changeByteOrder((int) (l >> 32)) & 0xffffffffL));
}
/**
* Changes the byte order of the bytes constituting <var>d</var>.
*/
public static double changeByteOrder(double d)
{
return Double.longBitsToDouble(changeByteOrder(Double.doubleToRawLongBits(d)));
}
/**
* Copies a range from an array of <code>int</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>int</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>int</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>int</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyIntToByte(int[] inData, int inStart, byte[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyIntToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(outData, outStart, len * INT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asIntBuffer().put(inData, inStart, len);
}
}
/**
* Copies a range from an array of <code>byte</code> into an array of <code>int</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>int</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>int</code> to
* start
* @param len The number of <code>int</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyByteToInt(byte[] inData, int inStart, int[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyByteToInt(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(inData, inStart, len * INT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asIntBuffer().get(outData, outStart, len);
}
}
/**
* Copies a range from an array of <code>long</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>long</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>long</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>long</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyLongToByte(long[] inData, int inStart, byte[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyLongToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(outData, outStart, len * LONG_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asLongBuffer().put(inData, inStart, len);
}
}
/**
* Copies a range from an array of <code>byte</code> into an array of <code>long</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>long</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>long</code> to
* start
* @param len The number of <code>long</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyByteToLong(byte[] inData, int inStart, long[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyByteToLong(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(inData, inStart, len * LONG_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asLongBuffer().get(outData, outStart, len);
}
}
/**
* Copies a range from an array of <code>short</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>short</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>short</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>short</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyShortToByte(short[] inData, int inStart, byte[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyShortToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(outData, outStart, len * SHORT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asShortBuffer().put(inData, inStart, len);
}
}
/**
* Copies a range from an array of <code>char</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>char</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>char</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>char</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyCharToByte(char[] inData, int inStart, byte[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyCharToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(outData, outStart, len * SHORT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asCharBuffer().put(inData, inStart, len);
}
}
/**
* Copies a range from an array of <code>byte</code> into an array of <code>short</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>short</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>short</code> to
* start
* @param len The number of <code>short</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyByteToShort(byte[] inData, int inStart, short[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyByteToShort(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(inData, inStart, len * SHORT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asShortBuffer().get(outData, outStart, len);
}
}
/**
* Copies a range from an array of <code>byte</code> into an array of <code>char</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>short</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>short</code> to
* start
* @param len The number of <code>short</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyByteToChar(byte[] inData, int inStart, char[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyByteToChar(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(inData, inStart, len * CHAR_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asCharBuffer().get(outData, outStart, len);
}
}
/**
* Copies a range from an array of <code>float</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>float</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>float</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>float</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyFloatToByte(float[] inData, int inStart, byte[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyFloatToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(outData, outStart, len * FLOAT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asFloatBuffer().put(inData, inStart, len);
}
}
/**
* Copies a range from an array of <code>byte</code> into an array of <code>float</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>float</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>float</code> to
* start
* @param len The number of <code>float</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyByteToFloat(byte[] inData, int inStart, float[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyByteToFloat(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(inData, inStart, len * FLOAT_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asFloatBuffer().get(outData, outStart, len);
}
}
/**
* Copies a range from an array of <code>double</code> into an array of <code>byte</code>.
*
* @param inData The input array of <code>double</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>double</code> to
* start
* @param outData The output array of <code>byte</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>byte</code> to
* start
* @param len The number of <code>double</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyDoubleToByte(double[] inData, int inStart, byte[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyDoubleToByte(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(outData, outStart, len * DOUBLE_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asDoubleBuffer().put(inData, inStart, len);
}
}
/**
* Copies a range from an array of <code>byte</code> into an array of <code>double</code>.
*
* @param inData The input array of <code>byte</code> values.
* @param inStart The position in the input array <code>inData</code> of <code>byte</code> to
* start
* @param outData The output array of <code>double</code> values.
* @param outStart The start in the output array <code>byteData</code> of <code>double</code> to
* start
* @param len The number of <code>double</code> to copy
* @param byteOrder The {@link ByteOrder}, encoding what byte order the <var>outData</var>
* should be in.
*/
public static void copyByteToDouble(byte[] inData, int inStart, double[] outData, int outStart,
int len, ByteOrder byteOrder)
{
if (useNativeLib)
{
copyByteToDouble(inData, inStart, outData, outStart, len, byteOrder.ordinal());
} else
{
final ByteBuffer bb = ByteBuffer.wrap(inData, inStart, len * DOUBLE_SIZE);
bb.order(byteOrder.getNioByteOrder());
bb.asDoubleBuffer().get(outData, outStart, len);
}
}
/**
* Converts a <code>byte[]</code> array into a <code>char[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @param start The position in the <var>byteArr</var> to start the conversion.
* @param len The number of <code>short</code> values to convert.
* @return The <code>char[]</code> array.
*/
public static char[] byteToChar(byte[] byteArr, ByteOrder byteOrder, int start, int len)
{
final char[] array = new char[len];
copyByteToChar(byteArr, start, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into a <code>char[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @return The <code>char[]</code> array.
*/
public static char[] byteToChar(byte[] byteArr, ByteOrder byteOrder)
{
if (byteArr.length % CHAR_SIZE != 0)
{
throw new IllegalArgumentException("Length of byteArr does not match size of data type");
}
final int len = byteArr.length / SHORT_SIZE;
final char[] array = new char[len];
copyByteToChar(byteArr, 0, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into a <code>short[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @param start The position in the <var>byteArr</var> to start the conversion.
* @param len The number of <code>short</code> values to convert.
* @return The <code>short[]</code> array.
*/
public static short[] byteToShort(byte[] byteArr, ByteOrder byteOrder, int start, int len)
{
final short[] array = new short[len];
copyByteToShort(byteArr, start, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into a <code>short[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @return The <code>short[]</code> array.
*/
public static short[] byteToShort(byte[] byteArr, ByteOrder byteOrder)
{
if (byteArr.length % SHORT_SIZE != 0)
{
throw new IllegalArgumentException("Length of byteArr does not match size of data type");
}
final int len = byteArr.length / SHORT_SIZE;
final short[] array = new short[len];
copyByteToShort(byteArr, 0, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>short[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @param start The position in <var>data</var> to start the conversion.
* @param len The number of <code>short</code> values to convert.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] shortToByte(short[] data, ByteOrder byteOrder, int start, int len)
{
final byte[] byteArr = new byte[SHORT_SIZE * len];
copyShortToByte(data, start, byteArr, 0, len, byteOrder);
return byteArr;
}
/**
* Converts a <code>short[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] shortToByte(short[] data, ByteOrder byteOrder)
{
final byte[] byteArr = new byte[SHORT_SIZE * data.length];
copyShortToByte(data, 0, byteArr, 0, data.length, byteOrder);
return byteArr;
}
/**
* Converts a <code>char[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @param start The position in <var>data</var> to start the conversion.
* @param len The number of <code>char</code> values to convert.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] charToByte(char[] data, ByteOrder byteOrder, int start, int len)
{
final byte[] byteArr = new byte[CHAR_SIZE * len];
copyCharToByte(data, start, byteArr, 0, len, byteOrder);
return byteArr;
}
/**
* Converts a <code>char[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] charToByte(char[] data, ByteOrder byteOrder)
{
final byte[] byteArr = new byte[CHAR_SIZE * data.length];
copyCharToByte(data, 0, byteArr, 0, data.length, byteOrder);
return byteArr;
}
/**
* Converts a <code>byte[]</code> array into an <code>int[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @param start The position in the <var>byteArr</var> to start the conversion.
* @param len The number of <code>int</code> values to convert.
* @return The <code>int[]</code> array.
*/
public static int[] byteToInt(byte[] byteArr, ByteOrder byteOrder, int start, int len)
{
final int[] array = new int[len];
copyByteToInt(byteArr, start, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into an <code>int[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @return The <code>int[]</code> array.
*/
public static int[] byteToInt(byte[] byteArr, ByteOrder byteOrder)
{
if (byteArr.length % INT_SIZE != 0)
{
throw new IllegalArgumentException("Length of byteArr does not match size of data type");
}
final int len = byteArr.length / INT_SIZE;
final int[] array = new int[len];
copyByteToInt(byteArr, 0, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>int[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @param start The position in <var>data</var> to start the conversion.
* @param len The number of <code>int</code> values to convert.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] intToByte(int[] data, ByteOrder byteOrder, int start, int len)
{
final byte[] byteArr = new byte[INT_SIZE * len];
copyIntToByte(data, start, byteArr, 0, len, byteOrder);
return byteArr;
}
/**
* Converts a <code>int[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] intToByte(int[] data, ByteOrder byteOrder)
{
final byte[] byteArr = new byte[INT_SIZE * data.length];
copyIntToByte(data, 0, byteArr, 0, data.length, byteOrder);
return byteArr;
}
/**
* Converts a <code>byte[]</code> array into a <code>long[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @param start The position in the <var>byteArr</var> to start the conversion.
* @param len The number of <code>long</code> values to convert.
* @return The <code>long[]</code> array.
*/
public static long[] byteToLong(byte[] byteArr, ByteOrder byteOrder, int start, int len)
{
final long[] array = new long[len];
copyByteToLong(byteArr, start, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into a <code>long[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @return The <code>long[]</code> array.
*/
public static long[] byteToLong(byte[] byteArr, ByteOrder byteOrder)
{
if (byteArr.length % LONG_SIZE != 0)
{
throw new IllegalArgumentException("Length of byteArr does not match size of data type");
}
final int len = byteArr.length / LONG_SIZE;
final long[] array = new long[len];
copyByteToLong(byteArr, 0, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>long[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @param start The position in <var>data</var> to start the conversion.
* @param len The number of <code>long</code> values to convert.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] longToByte(long[] data, ByteOrder byteOrder, int start, int len)
{
final byte[] byteArr = new byte[LONG_SIZE * len];
copyLongToByte(data, start, byteArr, 0, len, byteOrder);
return byteArr;
}
/**
* Converts a <code>long[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] longToByte(long[] data, ByteOrder byteOrder)
{
final byte[] byteArr = new byte[LONG_SIZE * data.length];
copyLongToByte(data, 0, byteArr, 0, data.length, byteOrder);
return byteArr;
}
/**
* Converts a <code>byte[]</code> array into a <code>float[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @param start The position in the <var>byteArr</var> to start the conversion.
* @param len The number of <code>float</code> values to convert.
* @return The <code>float[]</code> array.
*/
public static float[] byteToFloat(byte[] byteArr, ByteOrder byteOrder, int start, int len)
{
final float[] array = new float[len];
copyByteToFloat(byteArr, start, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into a <code>float[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @return The <code>float[]</code> array.
*/
public static float[] byteToFloat(byte[] byteArr, ByteOrder byteOrder)
{
if (byteArr.length % FLOAT_SIZE != 0)
{
throw new IllegalArgumentException("Length of byteArr does not match size of data type");
}
final int len = byteArr.length / FLOAT_SIZE;
final float[] array = new float[len];
copyByteToFloat(byteArr, 0, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>float[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @param start The position in <var>data</var> to start the conversion.
* @param len The number of <code>float</code> values to convert.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] floatToByte(float[] data, ByteOrder byteOrder, int start, int len)
{
final byte[] byteArr = new byte[FLOAT_SIZE * len];
copyFloatToByte(data, start, byteArr, 0, len, byteOrder);
return byteArr;
}
/**
* Converts a <code>float[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] floatToByte(float[] data, ByteOrder byteOrder)
{
final byte[] byteArr = new byte[FLOAT_SIZE * data.length];
copyFloatToByte(data, 0, byteArr, 0, data.length, byteOrder);
return byteArr;
}
/**
* Converts a <code>byte[]</code> array into a <code>double[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @param start The position in the <var>byteArr</var> to start the conversion.
* @param len The number of <code>double</code> values to convert.
* @return The <code>double[]</code> array.
*/
public static double[] byteToDouble(byte[] byteArr, ByteOrder byteOrder, int start, int len)
{
final double[] array = new double[len];
copyByteToDouble(byteArr, start, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>byte[]</code> array into a <code>double[]</code> array.
*
* @param byteArr The <code>byte[]</code> to convert.
* @param byteOrder The byte order of <var>byteArr</var>.
* @return The <code>double[]</code> array.
*/
public static double[] byteToDouble(byte[] byteArr, ByteOrder byteOrder)
{
if (byteArr.length % DOUBLE_SIZE != 0)
{
throw new IllegalArgumentException("Length of byteArr does not match size of data type");
}
final int len = byteArr.length / DOUBLE_SIZE;
final double[] array = new double[len];
copyByteToDouble(byteArr, 0, array, 0, len, byteOrder);
return array;
}
/**
* Converts a <code>double[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @param start The position in <var>data</var> to start the conversion.
* @param len The number of <code>double</code> values to convert.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] doubleToByte(double[] data, ByteOrder byteOrder, int start, int len)
{
final byte[] byteArr = new byte[DOUBLE_SIZE * len];
copyDoubleToByte(data, start, byteArr, 0, len, byteOrder);
return byteArr;
}
/**
* Converts a <code>double[]</code> array to a <code>byte[]</code> array.
*
* @param data The array to convert.
* @param byteOrder The byte order of the returned <code>byte[]</code>.
* @return The converted <code>byte[]</code> array.
*/
public static byte[] doubleToByte(double[] data, ByteOrder byteOrder)
{
final byte[] byteArr = new byte[DOUBLE_SIZE * data.length];
copyDoubleToByte(data, 0, byteArr, 0, data.length, byteOrder);
return byteArr;
}
}
|