From 36c7c503dc6563b3227c26b070d394818c6d5c74 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Sat, 16 May 2009 16:39:35 +0000 Subject: [PATCH] add: class NativeData (support for converting native data with different endiness) SVN: 11059 --- base/.classpath | 1 + base/build/build.xml | 54 +- base/dist/COPYING_HDF | 88 +++ base/source/c/COPYING | 88 +++ base/source/c/compile_linux_amd64.sh | 3 + base/source/c/compile_linux_i386.sh | 3 + base/source/c/compile_macosx_i386.sh | 3 + base/source/c/compile_macosx_x86_64.sh | 3 + base/source/c/compile_solaris_32.sh | 3 - base/source/c/compile_solaris_64.sh | 3 - base/source/c/compile_solaris_amd64.sh | 6 + base/source/c/compile_solaris_sparc.sh | 6 + base/source/c/compile_solaris_sparcv9.sh | 6 + base/source/c/compile_solaris_x86.sh | 6 + base/source/c/compile_windows_i386.mak | 176 ++++++ base/source/c/compile_windows_x64.mak | 176 ++++++ base/source/c/copyByteDouble.c | 42 ++ base/source/c/copyByteFloat.c | 42 ++ base/source/c/copyByteInt.c | 42 ++ base/source/c/copyByteLong.c | 42 ++ base/source/c/copyByteShort.c | 42 ++ base/source/c/copyByteTarget.ctempl | 240 ++++++++ base/source/c/copyCommon.c | 200 +++++++ base/source/c/unix.c | 15 + .../cisd/base/convert/NativeData.java | 563 ++++++++++++++++++ .../cisd/base/convert/NativeDataTests.java | 327 ++++++++++ 26 files changed, 2167 insertions(+), 13 deletions(-) create mode 100644 base/dist/COPYING_HDF create mode 100644 base/source/c/COPYING delete mode 100755 base/source/c/compile_solaris_32.sh delete mode 100755 base/source/c/compile_solaris_64.sh create mode 100755 base/source/c/compile_solaris_amd64.sh create mode 100755 base/source/c/compile_solaris_sparc.sh create mode 100755 base/source/c/compile_solaris_sparcv9.sh create mode 100755 base/source/c/compile_solaris_x86.sh create mode 100644 base/source/c/compile_windows_i386.mak create mode 100644 base/source/c/compile_windows_x64.mak create mode 100644 base/source/c/copyByteDouble.c create mode 100644 base/source/c/copyByteFloat.c create mode 100644 base/source/c/copyByteInt.c create mode 100644 base/source/c/copyByteLong.c create mode 100644 base/source/c/copyByteShort.c create mode 100644 base/source/c/copyByteTarget.ctempl create mode 100755 base/source/c/copyCommon.c create mode 100644 base/source/java/ch/systemsx/cisd/base/convert/NativeData.java create mode 100644 base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java diff --git a/base/.classpath b/base/.classpath index 8bb9ea4a3eb..dbaf269a69a 100644 --- a/base/.classpath +++ b/base/.classpath @@ -8,5 +8,6 @@ <classpathentry kind="lib" path="/libraries/commons-io/commons-io.jar" sourcepath="/libraries/commons-io/src.zip"/> <classpathentry kind="lib" path="/libraries/commons-lang/commons-lang.jar" sourcepath="/libraries/commons-lang/src.zip"/> <classpathentry kind="lib" path="/libraries/unix"/> + <classpathentry kind="lib" path="/libraries/nativedata"/> <classpathentry kind="output" path="targets/classes"/> </classpath> diff --git a/base/build/build.xml b/base/build/build.xml index 9b45f31ac0b..d712a1c1b90 100644 --- a/base/build/build.xml +++ b/base/build/build.xml @@ -15,7 +15,8 @@ <property name="src.zip.file" value="${dist}/cisd-base-src.zip" /> <property name="javadoc.zip.file" value="${dist}/cisd-base-javadoc.zip" /> <property name="original.dist" value="../${ant.project.name}/dist" /> - <property name="nativesrc" value="${lib}/unix/native" /> + <property name="nativesrc1" value="${lib}/unix/native" /> + <property name="nativesrc2" value="${lib}/nativedata/native" /> <property name="nativeroot" value="${targets}/ant" /> <property name="native" value="${nativeroot}/native" /> @@ -30,19 +31,27 @@ <build-info revision="revision.number" version="version.number" clean="clean.flag" /> <echo file="${build.info.file}">${version.number}:${revision.number}:${clean.flag}</echo> <copy todir="${native}"> - <fileset dir="${nativesrc}"> + <fileset dir="${nativesrc1}"> <include name="**/libunix.so" /> <include name="**/libunix.jnilib" /> </fileset> </copy> + <copy todir="${native}"> + <fileset dir="${nativesrc2}"> + <include name="**/libnativedata.so" /> + <include name="**/libnativedata.jnilib" /> + <include name="**/nativedata.dll" /> + </fileset> + </copy> <recursive-jar destfile="${jar.file}"> <fileset dir="${classes}"> <include name="**/*.class" /> <include name="${build.info.filename}" /> </fileset> <fileset dir="${nativeroot}"> - <include name="**/libunix.so" /> - <include name="**/libunix.jnilib" /> + <include name="native/**/*.so" /> + <include name="native/**/*.jnilib" /> + <include name="native/**/*.dll" /> </fileset> <manifest> <attribute name="Main-Class" @@ -58,13 +67,34 @@ <mkdir dir="${dist.common.lib}" /> <build-info revision="revision.number" version="version.number" clean="clean.flag" /> <echo file="${build.info.file}">${version.number}:${revision.number}:${clean.flag}</echo> + <copy todir="${native}"> + <fileset dir="${nativesrc1}"> + <include name="**/libunix.so" /> + <include name="**/libunix.jnilib" /> + </fileset> + </copy> + <copy todir="${native}"> + <fileset dir="${nativesrc2}"> + <include name="**/libnativedata.so" /> + <include name="**/libnativedata.jnilib" /> + <include name="**/nativedata.dll" /> + </fileset> + </copy> <recursive-jar destfile="${jar.test.file}"> <fileset dir="${classes}"> - <include name="ch/systemsx/cisd/base/tests/**/*.class" /> + <include name="**/*.class" /> <include name="${build.info.filename}" /> </fileset> + <fileset dir="${nativeroot}"> + <include name="native/**/*.so" /> + <include name="native/**/*.jnilib" /> + <include name="native/**/*.dll" /> + </fileset> + <zipfileset src="${lib}/testng/testng-jdk15.jar" /> + <zipfileset src="${lib}/commons-lang/commons-lang.jar" /> + <zipfileset src="${lib}/commons-io/commons-io.jar" /> <manifest> - <attribute name="Main-Class" value="ch.systemsx.cisd.base.unix.UnixTests" /> + <attribute name="Main-Class" value="ch.systemsx.cisd.base.convert.NativeDataTests" /> <attribute name="Version" value="${version.number}" /> <attribute name="Build-Number" value="${version.number} (r${revision.number},${clean.flag})" /> @@ -77,11 +107,18 @@ <build-info revision="revision.number" version="version.number" clean="clean.flag" /> <echo file="${build.info.file}">${version.number}:${revision.number}:${clean.flag}</echo> <copy todir="${native}"> - <fileset dir="${nativesrc}"> + <fileset dir="${nativesrc1}"> <include name="**/libunix.so" /> <include name="**/libunix.jnilib" /> </fileset> </copy> + <copy todir="${native}"> + <fileset dir="${nativesrc2}"> + <include name="**/libnativedata.so" /> + <include name="**/libnativedata.jnilib" /> + <include name="**/nativedata.dll" /> + </fileset> + </copy> <recursive-jar destfile="${jar.test.complete.file}"> <fileset dir="${classes}"> <include name="**/*.class" /> @@ -90,6 +127,9 @@ <fileset dir="${nativeroot}"> <include name="**/libunix.so" /> <include name="**/libunix.jnilib" /> + <include name="**/libnativedata.so" /> + <include name="**/libnativedata.jnilib" /> + <include name="**/nativedata.dll" /> </fileset> <zipfileset src="${lib}/testng/testng-jdk15.jar" /> <zipfileset src="${lib}/commons-lang/commons-lang.jar" /> diff --git a/base/dist/COPYING_HDF b/base/dist/COPYING_HDF new file mode 100644 index 00000000000..7ca600955f3 --- /dev/null +++ b/base/dist/COPYING_HDF @@ -0,0 +1,88 @@ +Copyright Notice and Statement for NCSA Hierarchical Data Format (HDF) +Java Software Library and Utilities + +NCSA Hierarchical Data Format (HDF) Software Library and Utilities +Copyright 1988-2004, the Board of Trustees of the University of Illinois. +Copyright 2007-2009, Center for Information Sciences and Databases, ETH Zurich, Switzerland. +All rights reserved. + +Contributors to the library: National Center for Supercomputing +Applications (NCSA) at the University of Illinois, Lawrence +Livermore Nationall Laboratory (LLNL), Sandia National Laboratories (SNL), +Los Alamos National Laboratory (LANL). Fortner Software, Unidata +Program Center (netCDF), The Independent JPEG Group (JPEG), +Jean-loup Gailly and Mark Adler (gzip), and Digital Equipment +Corporation (DEC). Macintosh support contributed by Gregory L. Guerin. +Center for Information Sciences and Databases, ETH Zurich, Switzerland + +The package 'glguerin': +Copyright 1998, 1999 by Gregory L. Guerin. +Redistribute or reuse only as described below. +These files are from the MacBinary Toolkit for Java: + <http://www.amug.org/~glguerin/sw/#macbinary> +and are redistributed by NCSA with permission of the author. + +This work was supported in part by a Cooperative Agreement with +NASA under NASA grant NAG 5-2040 and NAG NCC5-599. + +Redistribution and use in source and binary forms, with or without +modification, are permitted for any purpose (including commercial purposes) +provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or materials provided with the distribution. + +3. In addition, redistributions of modified forms of the source or binary + code must carry prominent notices stating that the original code was + changed and the date of the change. + +4. All publications or advertising materials mentioning features or use of + this software must acknowledge that it was developed by the National + Center for Supercomputing Applications at the University of Illinois, and + credit the Contributors. + +5. Neither the name of the University nor the names of the Contributors may + be used to endorse or promote products derived from this software without + specific prior written permission from the University or the Contributors. + +6. THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND THE CONTRIBUTORS "AS IS" + WITH NO WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. In no event + shall the University or the Contributors be liable for any damages + suffered by the users arising out of the use of this software, even if + advised of the possibility of such damage. + +-------------------------------------------------------------------------- +Portions of HDF5 were developed with support from the University of +California, Lawrence Livermore National Laboratory (UC LLNL). +The following statement applies to those portions of the product +and must be retained in any redistribution of source code, binaries, +documentation, and/or accompanying materials: + + This work was partially produced at the University of California, + Lawrence Livermore National Laboratory (UC LLNL) under contract no. + W-7405-ENG-48 (Contract 48) between the U.S. Department of Energy + (DOE) and The Regents of the University of California (University) + for the operation of UC LLNL. + + DISCLAIMER: + This work was prepared as an account of work sponsored by an agency + of the United States Government. Neither the United States + Government nor the University of California nor any of their + employees, makes any warranty, express or implied, or assumes any + liability or responsibility for the accuracy, completeness, or + usefulness of any information, apparatus, product, or process + disclosed, or represents that its use would not infringe privately- + owned rights. Reference herein to any specific commercial products, + process, or service by trade name, trademark, manufacturer, or + otherwise, does not necessarily constitute or imply its endorsement, + recommendation, or favoring by the United States Government or the + University of California. The views and opinions of authors + expressed herein do not necessarily state or reflect those of the + United States Government or the University of California, and shall + not be used for advertising or product endorsement purposes. +-------------------------------------------------------------------------- + diff --git a/base/source/c/COPYING b/base/source/c/COPYING new file mode 100644 index 00000000000..7ca600955f3 --- /dev/null +++ b/base/source/c/COPYING @@ -0,0 +1,88 @@ +Copyright Notice and Statement for NCSA Hierarchical Data Format (HDF) +Java Software Library and Utilities + +NCSA Hierarchical Data Format (HDF) Software Library and Utilities +Copyright 1988-2004, the Board of Trustees of the University of Illinois. +Copyright 2007-2009, Center for Information Sciences and Databases, ETH Zurich, Switzerland. +All rights reserved. + +Contributors to the library: National Center for Supercomputing +Applications (NCSA) at the University of Illinois, Lawrence +Livermore Nationall Laboratory (LLNL), Sandia National Laboratories (SNL), +Los Alamos National Laboratory (LANL). Fortner Software, Unidata +Program Center (netCDF), The Independent JPEG Group (JPEG), +Jean-loup Gailly and Mark Adler (gzip), and Digital Equipment +Corporation (DEC). Macintosh support contributed by Gregory L. Guerin. +Center for Information Sciences and Databases, ETH Zurich, Switzerland + +The package 'glguerin': +Copyright 1998, 1999 by Gregory L. Guerin. +Redistribute or reuse only as described below. +These files are from the MacBinary Toolkit for Java: + <http://www.amug.org/~glguerin/sw/#macbinary> +and are redistributed by NCSA with permission of the author. + +This work was supported in part by a Cooperative Agreement with +NASA under NASA grant NAG 5-2040 and NAG NCC5-599. + +Redistribution and use in source and binary forms, with or without +modification, are permitted for any purpose (including commercial purposes) +provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or materials provided with the distribution. + +3. In addition, redistributions of modified forms of the source or binary + code must carry prominent notices stating that the original code was + changed and the date of the change. + +4. All publications or advertising materials mentioning features or use of + this software must acknowledge that it was developed by the National + Center for Supercomputing Applications at the University of Illinois, and + credit the Contributors. + +5. Neither the name of the University nor the names of the Contributors may + be used to endorse or promote products derived from this software without + specific prior written permission from the University or the Contributors. + +6. THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND THE CONTRIBUTORS "AS IS" + WITH NO WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. In no event + shall the University or the Contributors be liable for any damages + suffered by the users arising out of the use of this software, even if + advised of the possibility of such damage. + +-------------------------------------------------------------------------- +Portions of HDF5 were developed with support from the University of +California, Lawrence Livermore National Laboratory (UC LLNL). +The following statement applies to those portions of the product +and must be retained in any redistribution of source code, binaries, +documentation, and/or accompanying materials: + + This work was partially produced at the University of California, + Lawrence Livermore National Laboratory (UC LLNL) under contract no. + W-7405-ENG-48 (Contract 48) between the U.S. Department of Energy + (DOE) and The Regents of the University of California (University) + for the operation of UC LLNL. + + DISCLAIMER: + This work was prepared as an account of work sponsored by an agency + of the United States Government. Neither the United States + Government nor the University of California nor any of their + employees, makes any warranty, express or implied, or assumes any + liability or responsibility for the accuracy, completeness, or + usefulness of any information, apparatus, product, or process + disclosed, or represents that its use would not infringe privately- + owned rights. Reference herein to any specific commercial products, + process, or service by trade name, trademark, manufacturer, or + otherwise, does not necessarily constitute or imply its endorsement, + recommendation, or favoring by the United States Government or the + University of California. The views and opinions of authors + expressed herein do not necessarily state or reflect those of the + United States Government or the University of California, and shall + not be used for advertising or product endorsement purposes. +-------------------------------------------------------------------------- + diff --git a/base/source/c/compile_linux_amd64.sh b/base/source/c/compile_linux_amd64.sh index ac521aacfbc..6262ef9928e 100755 --- a/base/source/c/compile_linux_amd64.sh +++ b/base/source/c/compile_linux_amd64.sh @@ -1,3 +1,6 @@ #! /bin/bash gcc -shared -O3 -fPIC unix.c -I/usr/java/jdk5/include -I/usr/java/jdk5/include/linux -o libunix.so + +# MACHINE_BYTE_ORDER=1 corresponds to 'little endian' +gcc -shared -O3 -fPIC -DMACHINE_BYTE_ORDER=1 copy*.c -I/usr/java/jdk5/include -I/usr/java/jdk5/include/linux -o libnativedata.so diff --git a/base/source/c/compile_linux_i386.sh b/base/source/c/compile_linux_i386.sh index 94b3a9bb3c0..1d0a40440d2 100755 --- a/base/source/c/compile_linux_i386.sh +++ b/base/source/c/compile_linux_i386.sh @@ -1,3 +1,6 @@ #! /bin/bash gcc -shared -O3 unix.c -I/usr/java/jdk5/include -I/usr/java/jdk5/include/linux -o libunix.so + +# MACHINE_BYTE_ORDER=1 corresponds to 'little endian' +gcc -shared -O3 -fPIC -DMACHINE_BYTE_ORDER=1 copy*.c -I/usr/java/jdk5/include -I/usr/java/jdk5/include/linux -o libnativedata.so diff --git a/base/source/c/compile_macosx_i386.sh b/base/source/c/compile_macosx_i386.sh index 578cc2340fe..b4273d54de6 100755 --- a/base/source/c/compile_macosx_i386.sh +++ b/base/source/c/compile_macosx_i386.sh @@ -1,3 +1,6 @@ #! /bin/bash gcc -bundle -O3 -D_POSIX_C_SOURCE unix.c -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers -o libunix.jnilib + +# MACHINE_BYTE_ORDER=1 corresponds to 'little endian' +gcc -bundle -O3 -DMACHINE_BYTE_ORDER=1 copy*.c -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers -o libnativedata.jnilib diff --git a/base/source/c/compile_macosx_x86_64.sh b/base/source/c/compile_macosx_x86_64.sh index d55bbdbebb7..01fbb1688a3 100755 --- a/base/source/c/compile_macosx_x86_64.sh +++ b/base/source/c/compile_macosx_x86_64.sh @@ -1,3 +1,6 @@ #! /bin/bash gcc -m64 -dynamiclib -O3 -D_POSIX_C_SOURCE unix.c -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers -o libunix.jnilib + +# MACHINE_BYTE_ORDER=1 corresponds to 'little endian' +gcc -m64 -dynamiclib -O3 -DMACHINE_BYTE_ORDER=1 copy*.c -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers -o libnativedata.jnilib diff --git a/base/source/c/compile_solaris_32.sh b/base/source/c/compile_solaris_32.sh deleted file mode 100755 index fbb97583603..00000000000 --- a/base/source/c/compile_solaris_32.sh +++ /dev/null @@ -1,3 +0,0 @@ -#! /bin/bash - -cc -G -KPIC -fast -I/usr/java/include -I/usr/java/include/solaris unix.c -o libunix.so diff --git a/base/source/c/compile_solaris_64.sh b/base/source/c/compile_solaris_64.sh deleted file mode 100755 index 57960788404..00000000000 --- a/base/source/c/compile_solaris_64.sh +++ /dev/null @@ -1,3 +0,0 @@ -#! /bin/bash - -cc -G -KPIC -fast -m64 -I/usr/java/include -I/usr/java/include/solaris unix.c -o libunix.so diff --git a/base/source/c/compile_solaris_amd64.sh b/base/source/c/compile_solaris_amd64.sh new file mode 100755 index 00000000000..16c3ac9bfae --- /dev/null +++ b/base/source/c/compile_solaris_amd64.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +cc -G -KPIC -fast -m64 -I/usr/java/include -I/usr/java/include/solaris unix.c -o libunix.so + +# MACHINE_BYTE_ORDER=1 corresponds to 'little endian' +cc -G -KPIC -fast -m64 -DMACHINE_BYTE_ORDER=1 copy*.c -I/usr/java/include -I/usr/java/include/solaris -o libnativedata.so diff --git a/base/source/c/compile_solaris_sparc.sh b/base/source/c/compile_solaris_sparc.sh new file mode 100755 index 00000000000..7efb3be3e71 --- /dev/null +++ b/base/source/c/compile_solaris_sparc.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +cc -G -KPIC -fast -I/usr/java/include -I/usr/java/include/solaris unix.c -o libunix.so + +# MACHINE_BYTE_ORDER=2 corresponds to 'big endian' +cc -G -KPIC -fast -DMACHINE_BYTE_ORDER=2 copy*.c -I/usr/java/include -I/usr/java/include/solaris -o libnativedata.so diff --git a/base/source/c/compile_solaris_sparcv9.sh b/base/source/c/compile_solaris_sparcv9.sh new file mode 100755 index 00000000000..37852b48b73 --- /dev/null +++ b/base/source/c/compile_solaris_sparcv9.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +cc -G -KPIC -fast -m64 -I/usr/java/include -I/usr/java/include/solaris unix.c -o libunix.so + +# MACHINE_BYTE_ORDER=2 corresponds to 'big endian' +cc -G -KPIC -fast -m64 -DMACHINE_BYTE_ORDER=2 copy*.c -I/usr/java/include -I/usr/java/include/solaris -o libnativedata.so diff --git a/base/source/c/compile_solaris_x86.sh b/base/source/c/compile_solaris_x86.sh new file mode 100755 index 00000000000..34b1192eced --- /dev/null +++ b/base/source/c/compile_solaris_x86.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +cc -G -KPIC -fast -I/usr/java/include -I/usr/java/include/solaris unix.c -o libunix.so + +# MACHINE_BYTE_ORDER=1 corresponds to 'little endian' +cc -G -KPIC -fast -DMACHINE_BYTE_ORDER=1 copy*.c -I/usr/java/include -I/usr/java/include/solaris -o libnativedata.so diff --git a/base/source/c/compile_windows_i386.mak b/base/source/c/compile_windows_i386.mak new file mode 100644 index 00000000000..43a64307748 --- /dev/null +++ b/base/source/c/compile_windows_i386.mak @@ -0,0 +1,176 @@ +#============================================================================ +# +# Makefile to compile the 'nativedata' native library +# Usage: nmake /f compile_windows_i386.mak +# +#============================================================================ + +# Visual C++ directory, for example +VCPPDIR=C:\Program Files\Microsoft Visual Studio 8\VC + +# Directory where JDK is installed (We require JDK 1.5 or above), for example +JAVADIR=C:\Program Files\Java\jdk1.5.0_15 + +# Common parent directory +PARENTDIR=C:\nativeData + +# Directory of the HDF Java Products, for example +SRCDIR=$(PARENTDIR)\c\ + +#=========================================================================== +# Do not make any change below this line unless you know what you do +#=========================================================================== +PATH=$(PATH);$(VCPPDIR)\BIN +SRCDIR=$(SRCDIR) + +VALID_PATH_SET=YES +#------------------------------------------------------- +# Test if all path is valid + +!IF EXISTS("$(VCPPDIR)") +!ELSE +!MESSAGE ERROR: Visual C++ directory $(VCPPDIR) does not exist +VALID_PATH_SET=NO +!ENDIF + +!IF EXISTS("$(JAVADIR)") +!ELSE +!MESSAGE ERROR: JDK directory $(JAVADIR) does not exist +VALID_PATH_SET=NO +!ENDIF + +!IF EXISTS("$(SRCDIR)") +!ELSE +!MESSAGE ERROR: C source directory $(SRCDIR) does not exist +VALID_PATH_SET=NO +!ENDIF + +#------------------------------------------------------- + + +!IF "$(VALID_PATH_SET)" == "YES" + +!IF "$(OS)" == "Windows_NT" +NULL= +!ELSE +NULL=nul +!ENDIF + +INTDIR=.\nativedata\Release +OUTDIR=$(SRCDIR)\lib\win + +INCLUDES = \ + "$(JAVADIR)\include\jni.h" \ + "$(JAVADIR)\include\win32\jni_md.h" + + +ALL : "$(OUTDIR)\nativedata.dll" + +"$(INTDIR)" : + if not exist "$(INTDIR)/$(NULL)" mkdir "$(INTDIR)" + +"$(OUTDIR)" : + if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)" + +CPP=cl.exe +CPP_PROJ=/nologo /W3 /EHsc /O2 /I "$(JAVADIR)\include" /I "$(JAVADIR)\include\win32" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "MACHINE_BYTE_ORDER=1" /Fp"$(INTDIR)\nativedata.pch" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c + +.c{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.c{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +MTL=midl.exe +MTL_PROJ=/nologo /D "NDEBUG" /mktyplib203 /win32 +RSC=rc.exe +BSC32=bscmake.exe +BSC32_FLAGS=/nologo /o"$(INTDIR)\nativedata.bsc" +BSC32_SBRS= \ + +LINK=link.exe +LINK_FLAGS=/nologo /dll /nodefaultlib:msvcrt /incremental:no /pdb:"$(INTDIR)\nativedata.pdb" /machine:I386 /out:"$(OUTDIR)\nativedata.dll" /implib:"$(INTDIR)\nativedata.lib" +LINK_OBJS= \ + "$(INTDIR)\copyCommon.obj" \ + "$(INTDIR)\copyByteDouble.obj" \ + "$(INTDIR)\copyByteFloat.obj" \ + "$(INTDIR)\copyByteInt.obj" \ + "$(INTDIR)\copyByteLong.obj" \ + "$(INTDIR)\copyByteShort.obj" + +"$(OUTDIR)\nativedata.dll" : "$(OUTDIR)" $(DEF_FILE) $(LINK_OBJS) + $(LINK) @<< + $(LINK_FLAGS) $(LINK_OBJS) +<< + + +SOURCE=$(SRCDIR)\copyCommon.c + +"$(INTDIR)\copyCommon.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + + +SOURCE=$(SRCDIR)\copyByteDouble.c + +"$(INTDIR)\copyByteDouble.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteFloat.c + +"$(INTDIR)\copyByteFloat.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteInt.c + +"$(INTDIR)\copyByteInt.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteLong.c + +"$(INTDIR)\copyByteLong.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteShort.c + +"$(INTDIR)\copyByteShort.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + + + +CLEAN : + -@erase "$(INTDIR)\copyCommon.obj" + -@erase "$(INTDIR)\copyByteDouble.obj" + -@erase "$(INTDIR)\copyByteFloat.obj" + -@erase "$(INTDIR)\copyByteInt.obj" + -@erase "$(INTDIR)\copyByteLong.obj" + -@erase "$(INTDIR)\copyByteShort.obj" + -@erase "$(INTDIR)\vc80.idb" + -@erase "$(INTDIR)\nativedata.exp" + -@erase "$(INTDIR)\nativedata.lib" + -@erase "$(OUTDIR)\nativedata.dll" + +!ENDIF diff --git a/base/source/c/compile_windows_x64.mak b/base/source/c/compile_windows_x64.mak new file mode 100644 index 00000000000..aebd972d566 --- /dev/null +++ b/base/source/c/compile_windows_x64.mak @@ -0,0 +1,176 @@ +#============================================================================ +# +# Makefile to compile the 'nativedata' native library +# Usage: nmake /f compile_windows_i386.mak +# +#============================================================================ + +# Visual C++ directory, for example +VCPPDIR=C:\Program Files\Microsoft Visual Studio 8\VC + +# Directory where JDK is installed (We require JDK 1.5 or above), for example +JAVADIR=C:\Program Files\Java\jdk1.5.0_15 + +# Common parent directory +PARENTDIR=C:\nativeData + +# Directory of the HDF Java Products, for example +SRCDIR=$(PARENTDIR)\c\ + +#=========================================================================== +# Do not make any change below this line unless you know what you do +#=========================================================================== +PATH=$(PATH);$(VCPPDIR)\BIN +SRCDIR=$(SRCDIR) + +VALID_PATH_SET=YES +#------------------------------------------------------- +# Test if all path is valid + +!IF EXISTS("$(VCPPDIR)") +!ELSE +!MESSAGE ERROR: Visual C++ directory $(VCPPDIR) does not exist +VALID_PATH_SET=NO +!ENDIF + +!IF EXISTS("$(JAVADIR)") +!ELSE +!MESSAGE ERROR: JDK directory $(JAVADIR) does not exist +VALID_PATH_SET=NO +!ENDIF + +!IF EXISTS("$(SRCDIR)") +!ELSE +!MESSAGE ERROR: C source directory $(SRCDIR) does not exist +VALID_PATH_SET=NO +!ENDIF + +#------------------------------------------------------- + + +!IF "$(VALID_PATH_SET)" == "YES" + +!IF "$(OS)" == "Windows_NT" +NULL= +!ELSE +NULL=nul +!ENDIF + +INTDIR=.\nativedata\Release +OUTDIR=$(SRCDIR)\lib\win + +INCLUDES = \ + "$(JAVADIR)\include\jni.h" \ + "$(JAVADIR)\include\win32\jni_md.h" + + +ALL : "$(OUTDIR)\nativedata.dll" + +"$(INTDIR)" : + if not exist "$(INTDIR)/$(NULL)" mkdir "$(INTDIR)" + +"$(OUTDIR)" : + if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)" + +CPP=cl.exe +CPP_PROJ=/nologo /W3 /EHsc /O2 /I "$(JAVADIR)\include" /I "$(JAVADIR)\include\win32" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "MACHINE_BYTE_ORDER=1" /Fp"$(INTDIR)\nativedata.pch" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c + +.c{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.c{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +MTL=midl.exe +MTL_PROJ=/nologo /D "NDEBUG" /mktyplib203 /win32 +RSC=rc.exe +BSC32=bscmake.exe +BSC32_FLAGS=/nologo /o"$(INTDIR)\nativedata.bsc" +BSC32_SBRS= \ + +LINK=link.exe +LINK_FLAGS=/nologo /dll /nodefaultlib:msvcrt /incremental:no /pdb:"$(INTDIR)\nativedata.pdb" /machine:x64 /out:"$(OUTDIR)\nativedata.dll" /implib:"$(INTDIR)\nativedata.lib" +LINK_OBJS= \ + "$(INTDIR)\copyCommon.obj" \ + "$(INTDIR)\copyByteDouble.obj" \ + "$(INTDIR)\copyByteFloat.obj" \ + "$(INTDIR)\copyByteInt.obj" \ + "$(INTDIR)\copyByteLong.obj" \ + "$(INTDIR)\copyByteShort.obj" + +"$(OUTDIR)\nativedata.dll" : "$(OUTDIR)" $(DEF_FILE) $(LINK_OBJS) + $(LINK) @<< + $(LINK_FLAGS) $(LINK_OBJS) +<< + + +SOURCE=$(SRCDIR)\copyCommon.c + +"$(INTDIR)\copyCommon.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + + +SOURCE=$(SRCDIR)\copyByteDouble.c + +"$(INTDIR)\copyByteDouble.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteFloat.c + +"$(INTDIR)\copyByteFloat.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteInt.c + +"$(INTDIR)\copyByteInt.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteLong.c + +"$(INTDIR)\copyByteLong.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + +SOURCE=$(SRCDIR)\copyByteShort.c + +"$(INTDIR)\copyByteShort.obj" : $(SOURCE) $(INCLUDES) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + + + +CLEAN : + -@erase "$(INTDIR)\copyCommon.obj" + -@erase "$(INTDIR)\copyByteDouble.obj" + -@erase "$(INTDIR)\copyByteFloat.obj" + -@erase "$(INTDIR)\copyByteInt.obj" + -@erase "$(INTDIR)\copyByteLong.obj" + -@erase "$(INTDIR)\copyByteShort.obj" + -@erase "$(INTDIR)\vc80.idb" + -@erase "$(INTDIR)\nativedata.exp" + -@erase "$(INTDIR)\nativedata.lib" + -@erase "$(OUTDIR)\nativedata.dll" + +!ENDIF diff --git a/base/source/c/copyByteDouble.c b/base/source/c/copyByteDouble.c new file mode 100644 index 00000000000..734d7399ba7 --- /dev/null +++ b/base/source/c/copyByteDouble.c @@ -0,0 +1,42 @@ +/**************************************************************************** + * 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. * + * * + ****************************************************************************/ + +/* + * This module contains the implementation of all the native methods + * used for number conversion. This is represented by the Java + * class HDFNativeData. + * + * These routines convert one dimensional arrays of bytes into + * one-D arrays of other types (int, double, etc) and vice versa. + * + * These routines are called from the Java parts of the Java-C + * interface. + * + * ***Important notes: + * + * 1. These routines are designed to be portable--they use the + * C compiler to do the required native data manipulation. + * 2. These routines copy the data at least once -- a serious + * but unavoidable performance hit. + */ + +#define TARGET jdouble +#define TARGET_ARRAY jdoubleArray +#define METHODNAMETB "copyDoubleToByte" +#define FUNCTIONNAMETB Java_ch_systemsx_cisd_base_convert_NativeData_copyDoubleToByte___3DI_3BIII +#define METHODNAMEBT "copyByteToDouble" +#define FUNCTIONNAMEBT Java_ch_systemsx_cisd_base_convert_NativeData_copyByteToDouble___3BI_3DIII +#define COPY_FUNC GetDoubleArrayRegion +#define CHANGE_BYTE_ORDER CHANGE_BYTE_ORDER_8 + +#include "copyByteTarget.ctempl" diff --git a/base/source/c/copyByteFloat.c b/base/source/c/copyByteFloat.c new file mode 100644 index 00000000000..797a818a67b --- /dev/null +++ b/base/source/c/copyByteFloat.c @@ -0,0 +1,42 @@ +/**************************************************************************** + * 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. * + * * + ****************************************************************************/ + +/* + * This module contains the implementation of all the native methods + * used for number conversion. This is represented by the Java + * class HDFNativeData. + * + * These routines convert one dimensional arrays of bytes into + * one-D arrays of other types (int, float, etc) and vice versa. + * + * These routines are called from the Java parts of the Java-C + * interface. + * + * ***Important notes: + * + * 1. These routines are designed to be portable--they use the + * C compiler to do the required native data manipulation. + * 2. These routines copy the data at least once -- a serious + * but unavoidable performance hit. + */ + +#define TARGET jfloat +#define TARGET_ARRAY jfloatArray +#define METHODNAMETB "copyFloatToByte" +#define FUNCTIONNAMETB Java_ch_systemsx_cisd_base_convert_NativeData_copyFloatToByte___3FI_3BIII +#define METHODNAMEBT "copyByteToFloat" +#define FUNCTIONNAMEBT Java_ch_systemsx_cisd_base_convert_NativeData_copyByteToFloat___3BI_3FIII +#define COPY_FUNC GetFloatArrayRegion +#define CHANGE_BYTE_ORDER CHANGE_BYTE_ORDER_4 + +#include "copyByteTarget.ctempl" diff --git a/base/source/c/copyByteInt.c b/base/source/c/copyByteInt.c new file mode 100644 index 00000000000..422754671c6 --- /dev/null +++ b/base/source/c/copyByteInt.c @@ -0,0 +1,42 @@ +/**************************************************************************** + * 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. * + * * + ****************************************************************************/ + +/* + * This module contains the implementation of all the native methods + * used for number conversion. This is represented by the Java + * class HDFNativeData. + * + * These routines convert one dimensional arrays of bytes into + * one-D arrays of other types (int, float, etc) and vice versa. + * + * These routines are called from the Java parts of the Java-C + * interface. + * + * ***Important notes: + * + * 1. These routines are designed to be portable--they use the + * C compiler to do the required native data manipulation. + * 2. These routines copy the data at least once -- a serious + * but unavoidable performance hit. + */ + +#define TARGET jint +#define TARGET_ARRAY jintArray +#define METHODNAMETB "copyIntToByte" +#define FUNCTIONNAMETB Java_ch_systemsx_cisd_base_convert_NativeData_copyIntToByte___3II_3BIII +#define METHODNAMEBT "copyByteToInt" +#define FUNCTIONNAMEBT Java_ch_systemsx_cisd_base_convert_NativeData_copyByteToInt___3BI_3IIII +#define COPY_FUNC GetIntArrayRegion +#define CHANGE_BYTE_ORDER CHANGE_BYTE_ORDER_4 + +#include "copyByteTarget.ctempl" diff --git a/base/source/c/copyByteLong.c b/base/source/c/copyByteLong.c new file mode 100644 index 00000000000..c28352f2e6d --- /dev/null +++ b/base/source/c/copyByteLong.c @@ -0,0 +1,42 @@ +/**************************************************************************** + * 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. * + * * + ****************************************************************************/ + +/* + * This module contains the implementation of all the native methods + * used for number conversion. This is represented by the Java + * class HDFNativeData. + * + * These routines convert one dimensional arrays of bytes into + * one-D arrays of other types (int, float, etc) and vice versa. + * + * These routines are called from the Java parts of the Java-C + * interface. + * + * ***Important notes: + * + * 1. These routines are designed to be portable--they use the + * C compiler to do the required native data manipulation. + * 2. These routines copy the data at least once -- a serious + * but unavoidable performance hit. + */ + +#define TARGET jlong +#define TARGET_ARRAY jlongArray +#define METHODNAMETB "copyLongToByte" +#define FUNCTIONNAMETB Java_ch_systemsx_cisd_base_convert_NativeData_copyLongToByte___3JI_3BIII +#define METHODNAMEBT "copyByteToLong" +#define FUNCTIONNAMEBT Java_ch_systemsx_cisd_base_convert_NativeData_copyByteToLong___3BI_3JIII +#define COPY_FUNC GetLongArrayRegion +#define CHANGE_BYTE_ORDER CHANGE_BYTE_ORDER_8 + +#include "copyByteTarget.ctempl" diff --git a/base/source/c/copyByteShort.c b/base/source/c/copyByteShort.c new file mode 100644 index 00000000000..5081ab73991 --- /dev/null +++ b/base/source/c/copyByteShort.c @@ -0,0 +1,42 @@ +/**************************************************************************** + * 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. * + * * + ****************************************************************************/ + +/* + * This module contains the implementation of all the native methods + * used for number conversion. This is represented by the Java + * class HDFNativeData. + * + * These routines convert one dimensional arrays of bytes into + * one-D arrays of other types (int, float, etc) and vice versa. + * + * These routines are called from the Java parts of the Java-C + * interface. + * + * ***Important notes: + * + * 1. These routines are designed to be portable--they use the + * C compiler to do the required native data manipulation. + * 2. These routines copy the data at least once -- a serious + * but unavoidable performance hit. + */ + +#define TARGET jshort +#define TARGET_ARRAY jshortArray +#define METHODNAMETB "copyShortToByte" +#define FUNCTIONNAMETB Java_ch_systemsx_cisd_base_convert_NativeData_copyShortToByte___3SI_3BIII +#define METHODNAMEBT "copyByteToShort" +#define FUNCTIONNAMEBT Java_ch_systemsx_cisd_base_convert_NativeData_copyByteToShort___3BI_3SIII +#define COPY_FUNC GetShortArrayRegion +#define CHANGE_BYTE_ORDER CHANGE_BYTE_ORDER_2 + +#include "copyByteTarget.ctempl" diff --git a/base/source/c/copyByteTarget.ctempl b/base/source/c/copyByteTarget.ctempl new file mode 100644 index 00000000000..f982126e91d --- /dev/null +++ b/base/source/c/copyByteTarget.ctempl @@ -0,0 +1,240 @@ +/**************************************************************************** + * 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. * + * * + ****************************************************************************/ + +/* + * This module contains the implementation of all the native methods + * used for number conversion. This is represented by the Java + * class HDFNativeData. + * + * These routines convert one dimensional arrays of bytes into + * one-D arrays of other types (int, float, etc) and vice versa. + * + * These routines are called from the Java parts of the Java-C + * interface. + * + * ***Important notes: + * + * 1. These routines are designed to be portable--they use the + * C compiler to do the required native data manipulation. + * 2. These routines copy the data at least once -- a serious + * but unavoidable performance hit. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <jni.h> + +extern jboolean h5JNIFatalError( JNIEnv *env, char *functName); +extern jboolean h5nullArgument( JNIEnv *env, char *functName); +extern jboolean h5badArgument( JNIEnv *env, char *functName); + +/* Change byte order for data type of length 2. */ +#define CHANGE_BYTE_ORDER_2(ARRAY) {jbyte _tmp; _tmp=ARRAY[0]; ARRAY[0]=ARRAY[1]; ARRAY[1]=_tmp;} +/* Change byte order for data type of length 4. */ +#define CHANGE_BYTE_ORDER_4(ARRAY) {jbyte _tmp; _tmp=ARRAY[0]; ARRAY[0]=ARRAY[3]; ARRAY[3]=_tmp; _tmp=ARRAY[1]; ARRAY[1]=ARRAY[2]; ARRAY[2]=_tmp;} +/* Change byte order for data type of length 8. */ +#define CHANGE_BYTE_ORDER_8(ARRAY) {jbyte _tmp; _tmp=ARRAY[0]; ARRAY[0]=ARRAY[7]; ARRAY[7]=_tmp; _tmp=ARRAY[1]; ARRAY[1]=ARRAY[6]; ARRAY[6]=_tmp; _tmp=ARRAY[2]; ARRAY[2]=ARRAY[5]; ARRAY[5]=_tmp; _tmp=ARRAY[3]; ARRAY[3]=ARRAY[4]; ARRAY[4]=_tmp;} + +#define INDATA_IS_NULL_ERR_TB METHODNAMETB ": inData is NULL" +#define OUTDATA_IS_NULL_ERR_TB METHODNAMETB ": outData is NULL" +#define OOB_IN_ERR_TB METHODNAMETB ": inStart or len is out of bounds" +#define OOB_OUT_ERR_TB METHODNAMETB ": outStart or len is out of bounds" +#define PINNING_OUT_ERR_TB METHODNAMETB ": pinning outArray failed" + +#define INDATA_IS_NULL_ERR_BT METHODNAMEBT ": inData is NULL" +#define OUTDATA_IS_NULL_ERR_BT METHODNAMEBT ": outData is NULL" +#define OOB_IN_ERR_BT METHODNAMEBT ": inStart or len is out of bounds" +#define OOB_OUT_ERR_BT METHODNAMEBT ": outStart or len is out of bounds" +#define PINNING_OUT_ERR_BT METHODNAMEBT ": pinning outArray failed" + +/* + * public static native void copy<TARGET_CAPT>oByte(<TARGET>[] inData, int inStart, + * byte[] outData, int outStart, int len, int byteOrder); + */ +JNIEXPORT void JNICALL FUNCTIONNAMETB +(JNIEnv *env, + jclass clss, + TARGET_ARRAY inData, /* IN: array of TARGET */ + jint inStart, + jbyteArray outData, /* OUT: array of byte */ + jint outStart, + jint len, + jint byteOrder +) +{ + jsize inSize, outSize; + jint lenInBytes; + jbyte *outArray; + jboolean isCopy; + + if (inData == NULL) { + h5nullArgument(env, INDATA_IS_NULL_ERR_BT); + return; + } + + if (outData == NULL) { + h5nullArgument(env, OUTDATA_IS_NULL_ERR_TB); + return; + } + + lenInBytes = len * sizeof(TARGET); + +#ifdef __cplusplus + inSize = env->GetArrayLength(inData); +#else + inSize = (*env)->GetArrayLength(env, inData); +#endif + if ((inStart < 0) || (inStart + len > inSize)) { + h5badArgument(env, OOB_IN_ERR_TB); + return; + } + +#ifdef __cplusplus + outSize = env->GetArrayLength(outData); +#else + outSize = (*env)->GetArrayLength(env, outData); +#endif + if ((outStart < 0) || (outStart + lenInBytes > outSize)) { + h5badArgument(env, OOB_OUT_ERR_TB); + return; + } + +#ifdef __cplusplus + outArray = env->GetPrimitiveArrayCritical(outData, &isCopy); +#else + outArray = (*env)->GetPrimitiveArrayCritical(env, outData, &isCopy); +#endif + if (outArray == NULL) { + h5JNIFatalError(env, PINNING_OUT_ERR_TB); + return; + } + +#ifdef __cplusplus + env->COPY_FUNC(inData, inStart, len, (TARGET*) (outArray + outStart)); +#else + (*env)->COPY_FUNC(env, inData, inStart, len, (TARGET*) (outArray + outStart)); +#endif + + if (byteOrder > 0 && byteOrder != MACHINE_BYTE_ORDER) + { + jbyte *buf = outArray; + int nelmts; + for(nelmts=len; nelmts >= 0; --nelmts) + { + CHANGE_BYTE_ORDER(buf); + buf += sizeof(TARGET); + } + } + +#ifdef __cplusplus + env->ReleasePrimitiveArrayCritical(outData, outArray, 0); +#else + (*env)->ReleasePrimitiveArrayCritical(env, outData,outArray, 0); +#endif + + return; +} + +/* + * public static native void copyByteTo<TARGET_CAP>(byte[] inData, int inStart, + * TARGET[] outData, int outStart, int len, int byteOrder); + */ +JNIEXPORT void JNICALL FUNCTIONNAMEBT +(JNIEnv *env, + jclass clss, + jbyteArray inData, /* IN: array of byte */ + jint inStart, + TARGET_ARRAY outData, /* OUT: array of TAGET */ + jint outStart, + jint len, + jint byteOrder + ) +{ + jsize inSize, outSize; + jint lenInBytes; + TARGET *outArray; + jboolean isCopy; + + if (inData == NULL) { + h5nullArgument(env, INDATA_IS_NULL_ERR_BT); + return; + } + + if (outData == NULL) { + h5nullArgument(env, OUTDATA_IS_NULL_ERR_BT); + return; + } + + lenInBytes = len * sizeof(TARGET); + +#ifdef __cplusplus + inSize = env->GetArrayLength(inData); +#else + inSize = (*env)->GetArrayLength(env, inData); +#endif + if ((inStart < 0) || (inStart + lenInBytes > inSize)) { + h5badArgument(env, OOB_IN_ERR_BT); + return; + } + +#ifdef __cplusplus + outSize = env->GetArrayLength(outData); +#else + outSize = (*env)->GetArrayLength(env, outData); +#endif + if ((outStart < 0) || (outStart + len > outSize)) { + h5badArgument(env, OOB_OUT_ERR_BT); + return; + } + +#ifdef __cplusplus + outArray = env->GetPrimitiveArrayCritical(outData, &isCopy); +#else + outArray = (*env)->GetPrimitiveArrayCritical(env, outData, &isCopy); +#endif + if (outArray == NULL) { + h5JNIFatalError(env, PINNING_OUT_ERR_BT); + return; + } + +#ifdef __cplusplus + env->GetByteArrayRegion(inData, inStart, lenInBytes, outArray + outStart); +#else + (*env)->GetByteArrayRegion(env, inData, inStart, lenInBytes, (jbyte*) (outArray + outStart)); +#endif + + if (byteOrder > 0 && byteOrder != MACHINE_BYTE_ORDER) + { + jbyte *buf = (jbyte*) outArray; + int nelmts; + for(nelmts=len; nelmts >= 0; --nelmts) + { + CHANGE_BYTE_ORDER(buf); + buf += sizeof(TARGET); + } + } + +#ifdef __cplusplus + env->ReleasePrimitiveArrayCritical(outData, outArray, 0); +#else + (*env)->ReleasePrimitiveArrayCritical(env, outData, outArray, 0); +#endif + + return; +} + +#ifdef __cplusplus +} +#endif diff --git a/base/source/c/copyCommon.c b/base/source/c/copyCommon.c new file mode 100755 index 00000000000..64fa05a76ee --- /dev/null +++ b/base/source/c/copyCommon.c @@ -0,0 +1,200 @@ +/**************************************************************************** + * NCSA HDF * + * National Comptational Science Alliance * + * University of Illinois at Urbana-Champaign * + * 605 E. Springfield, Champaign IL 61820 * + * * + * For conditions of distribution and use, see the accompanying * + * COPYING file. * + * * + ****************************************************************************/ + +/* + * This is a utility program used by native code to generate Java exceptions. + * + */ +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdio.h> +#include "jni.h" + +/* + * A fatal error in a JNI call + * Create and throw an 'InternalError' + * + * Note: This routine never returns from the 'throw', + * and the Java native method immediately raises the + * exception. + */ +jboolean h5JNIFatalError( JNIEnv *env, char *functName) +{ + jmethodID jm; + jclass jc; + char * args[2]; + jobject ex; + jstring str; + int rval; + +#ifdef __cplusplus + jc = env->FindClass("java/lang/InternalError"); +#else + jc = (*env)->FindClass(env, "java/lang/InternalError"); +#endif + if (jc == NULL) { + return JNI_FALSE; + } +#ifdef __cplusplus + jm = env->GetMethodID(jc, "<init>", "(Ljava/lang/String;)V"); +#else + jm = (*env)->GetMethodID(env, jc, "<init>", "(Ljava/lang/String;)V"); +#endif + if (jm == NULL) { + return JNI_FALSE; + } + +#ifdef __cplusplus + str = env->NewStringUTF(functName); +#else + str = (*env)->NewStringUTF(env,functName); +#endif + args[0] = (char *)str; + args[1] = 0; +#ifdef __cplusplus + ex = env->NewObjectA ( jc, jm, (jvalue *)args ); + + rval = env->Throw( (jthrowable) ex ); +#else + ex = (*env)->NewObjectA ( env, jc, jm, (jvalue *)args ); + + rval = (*env)->Throw(env, ex ); +#endif + if (rval < 0) { + fprintf(stderr, "FATAL ERROR: JNIFatal: Throw failed\n"); + return JNI_FALSE; + } + + return JNI_TRUE; +} + +/* + * A NULL argument in an HDF5 call + * Create and throw an 'NullPointerException' + * + * Note: This routine never returns from the 'throw', + * and the Java native method immediately raises the + * exception. + */ +jboolean h5nullArgument( JNIEnv *env, char *functName) +{ + jmethodID jm; + jclass jc; + char * args[2]; + jobject ex; + jstring str; + int rval; + +#ifdef __cplusplus + jc = env->FindClass("java/lang/NullPointerException"); +#else + jc = (*env)->FindClass(env, "java/lang/NullPointerException"); +#endif + if (jc == NULL) { + return JNI_FALSE; + } +#ifdef __cplusplus + jm = env->GetMethodID(jc, "<init>", "(Ljava/lang/String;)V"); +#else + jm = (*env)->GetMethodID(env, jc, "<init>", "(Ljava/lang/String;)V"); +#endif + if (jm == NULL) { + return JNI_FALSE; + } + +#ifdef __cplusplus + str = env->NewStringUTF(functName); +#else + str = (*env)->NewStringUTF(env,functName); +#endif + args[0] = (char *)str; + args[1] = 0; +#ifdef __cplusplus + ex = env->NewObjectA ( jc, jm, (jvalue *)args ); + + rval = env->Throw((jthrowable) ex ); +#else + ex = (*env)->NewObjectA ( env, jc, jm, (jvalue *)args ); + + rval = (*env)->Throw(env, ex ); +#endif + + if (rval < 0) { + fprintf(stderr, "FATAL ERROR: NullPointer: Throw failed\n"); + return JNI_FALSE; + } + + return JNI_TRUE; +} + +/* + * A bad argument in an HDF5 call + * Create and throw an 'IllegalArgumentException' + * + * Note: This routine never returns from the 'throw', + * and the Java native method immediately raises the + * exception. + */ +jboolean h5badArgument( JNIEnv *env, char *functName) +{ + jmethodID jm; + jclass jc; + char * args[2]; + jobject ex; + jstring str; + int rval; + +#ifdef __cplusplus + jc = env->FindClass("java/lang/IllegalArgumentException"); +#else + jc = (*env)->FindClass(env, "java/lang/IllegalArgumentException"); +#endif + if (jc == NULL) { + return JNI_FALSE; + } +#ifdef __cplusplus + jm = env->GetMethodID(jc, "<init>", "(Ljava/lang/String;)V"); +#else + jm = (*env)->GetMethodID(env, jc, "<init>", "(Ljava/lang/String;)V"); +#endif + if (jm == NULL) { + return JNI_FALSE; + } + +#ifdef __cplusplus + str = env->NewStringUTF(functName); +#else + str = (*env)->NewStringUTF(env,functName); +#endif + args[0] = (char *)str; + args[1] = 0; +#ifdef __cplusplus + ex = env->NewObjectA ( jc, jm, (jvalue *)args ); + + rval = env->Throw((jthrowable) ex ); +#else + ex = (*env)->NewObjectA ( env, jc, jm, (jvalue *)args ); + + rval = (*env)->Throw(env, ex ); +#endif + if (rval < 0) { + fprintf(stderr, "FATAL ERROR: BadArgument: Throw failed\n"); + return JNI_FALSE; + } + + return JNI_TRUE; +} + +#ifdef __cplusplus +} +#endif diff --git a/base/source/c/unix.c b/base/source/c/unix.c index ca7b79a0bc8..f4f5e636911 100644 --- a/base/source/c/unix.c +++ b/base/source/c/unix.c @@ -1,3 +1,18 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include <unistd.h> #include <errno.h> #include <string.h> diff --git a/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java b/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java new file mode 100644 index 00000000000..bfe67feffb8 --- /dev/null +++ b/base/source/java/ch/systemsx/cisd/base/convert/NativeData.java @@ -0,0 +1,563 @@ +/**************************************************************************** + * NCSA HDF * + * National Comptational 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 ch.systemsx.cisd.base.utilities.NativeLibraryUtilities; + +/** + * 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 HDF-5 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 a section of an array. + */ +public class NativeData +{ + + static + { + if (NativeLibraryUtilities.loadNativeLibrary("nativedata") == false) + { + System.err.println("No suitable 'nativedata' library found for this platform."); + System.exit(1); + } + } + + /** Size of a <code>short</code> value in <code>byte</code>s. */ + public final static int SHORT_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, + /** <code>byte[]</code> is in little endian byte order */ + LITTLE_ENDIAN, + /** <code>byte[]</code> is in big endian byte order */ + BIG_ENDIAN, + } + + /** + * 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. + */ + 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. + */ + 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. + */ + 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. + */ + 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. + */ + 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. + */ + static native void copyByteToShort(byte[] inData, int inStart, short[] 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. + */ + 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. + */ + 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. + */ + 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. + */ + 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() + { + } + + /** + * 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + 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.ordinal()); + return byteArr; + } + +} diff --git a/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java b/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java new file mode 100644 index 00000000000..b8c296d232b --- /dev/null +++ b/base/sourceTest/java/ch/systemsx/cisd/base/convert/NativeDataTests.java @@ -0,0 +1,327 @@ +/* + * Copyright 2009 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.base.convert; + +import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.assertFalse; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.base.convert.NativeData.ByteOrder; +import ch.systemsx.cisd.base.utilities.BuildAndEnvironmentInfo; + +/** + * Test cases for {@link NativeData}. + * + * @author Bernd Rinn + */ +public class NativeDataTests +{ + + @SuppressWarnings("unused") + @DataProvider(name = "getOfs") + private Object[][] getOfs() + { + return new Object[][] + { + { 0, 0 }, + { 0, 1 }, + { 0, 2 }, + { 0, 3 }, + { 1, 0 }, + { 1, 1 }, + { 1, 2 }, + { 1, 3 }, + { 2, 0 }, + { 2, 1 }, + { 2, 2 }, + { 2, 3 }, + { 3, 0 }, + { 3, 1 }, + { 3, 2 }, + { 3, 3 }, }; + } + + @Test(dataProvider = "getOfs") + public void testIntToByteToInt(int sourceOfs, int targetOfs) + { + final int sizeOfTarget = 4; + final int[] orignalArr = new int[] + { -1, 17, 100000, -1000000 }; + final int[] iarr = new int[sourceOfs + orignalArr.length]; + System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); + final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; + NativeData.copyIntToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + final int[] iarr2 = new int[(barr.length - targetOfs) / sizeOfTarget]; + NativeData.copyByteToInt(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + assertTrue(Arrays.equals(iarr, iarr2)); + } + + @Test(dataProvider = "getOfs") + public void testLongToByteToLong(int sourceOfs, int targetOfs) + { + final int sizeOfTarget = 8; + final long[] orignalArr = new long[] + { -1, 17, 100000, -1000000 }; + final long[] iarr = new long[sourceOfs + orignalArr.length]; + System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); + final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; + NativeData.copyLongToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + final long[] iarr2 = new long[(barr.length - targetOfs) / sizeOfTarget]; + NativeData.copyByteToLong(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + assertTrue(Arrays.equals(iarr, iarr2)); + } + + @Test(dataProvider = "getOfs") + public void testShortToByteToShort(int sourceOfs, int targetOfs) + { + final int sizeOfTarget = 8; + final short[] orignalArr = new short[] + { -1, 17, 20000, (short) -50000 }; + final short[] iarr = new short[sourceOfs + orignalArr.length]; + System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); + final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; + NativeData.copyShortToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + final short[] iarr2 = new short[(barr.length - targetOfs) / sizeOfTarget]; + NativeData.copyByteToShort(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + assertTrue(Arrays.equals(iarr, iarr2)); + } + + @Test(dataProvider = "getOfs") + public void testFloatToByteToFloat(int sourceOfs, int targetOfs) + { + final int sizeOfTarget = 8; + final float[] orignalArr = new float[] + { -1, 17, 3.14159f, -1e6f }; + final float[] iarr = new float[sourceOfs + orignalArr.length]; + System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); + final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; + NativeData.copyFloatToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + final float[] iarr2 = new float[(barr.length - targetOfs) / sizeOfTarget]; + NativeData.copyByteToFloat(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + assertTrue(Arrays.equals(iarr, iarr2)); + } + + @Test(dataProvider = "getOfs") + public void testDoubleToByteToDouble(int sourceOfs, int targetOfs) + { + final int sizeOfTarget = 8; + final double[] orignalArr = new double[] + { -1, 17, 3.14159, -1e42 }; + final double[] iarr = new double[sourceOfs + orignalArr.length]; + System.arraycopy(orignalArr, 0, iarr, sourceOfs, orignalArr.length); + final byte[] barr = new byte[iarr.length * sizeOfTarget + targetOfs]; + NativeData.copyDoubleToByte(iarr, sourceOfs, barr, targetOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + final double[] iarr2 = new double[(barr.length - targetOfs) / sizeOfTarget]; + NativeData.copyByteToDouble(barr, targetOfs, iarr2, sourceOfs, orignalArr.length, + NativeData.ByteOrder.NATIVE.ordinal()); + assertTrue(Arrays.equals(iarr, iarr2)); + } + + @Test + public void testShortEndianConversion() + { + + final short[] values = new short[] + { 1, 2, 4, 8, 16, 256, 512 }; + final short[] convertedValuesExpected = new short[] + { 1 << 8, 1 << 9, 1 << 10, 1 << 11, 1 << 12, 1, 2 }; + final short[] convertedValuesFound = + NativeData.byteToShort(NativeData.shortToByte(values, ByteOrder.BIG_ENDIAN), + ByteOrder.LITTLE_ENDIAN); + assertTrue(Arrays.equals(convertedValuesExpected, convertedValuesFound)); + } + + @Test + public void testIntEndianConversion() + { + + final int[] values = new int[] + { 1, 2, 4, 8, 16, 256, 1 << 16 }; + final int[] convertedValuesExpected = new int[] + { 1 << 24, 1 << 25, 1 << 26, 1 << 27, 1 << 28, 1 << 16, 256 }; + final int[] convertedValuesFound = + NativeData.byteToInt(NativeData.intToByte(values, ByteOrder.BIG_ENDIAN), + ByteOrder.LITTLE_ENDIAN); + assertTrue(Arrays.equals(convertedValuesExpected, convertedValuesFound)); + } + + @Test + public void testLongEndianConversion() + { + + final long[] values = new long[] + { 1, 2, 4, 8, 16, 256, 1L << 16, 1L << 24 }; + final long[] convertedValuesExpected = new long[] + { 1L << 56, 1L << 57, 1L << 58, 1L << 59, 1L << 60, 1L << 48, 1L << 40, 1L << 32 }; + final long[] convertedValuesFound = + NativeData.byteToLong(NativeData.longToByte(values, ByteOrder.BIG_ENDIAN), + ByteOrder.LITTLE_ENDIAN); + assertTrue(Arrays.equals(convertedValuesExpected, convertedValuesFound)); + } + + @Test + public void testFloatLittleEndianRoundtrip() + { + + final float[] values = new float[] + { 1.1f, 2.2f, 3.3f, 1e-25f, 1e25f }; + final float[] convertedValuesFound = + NativeData.byteToFloat(NativeData.floatToByte(values, ByteOrder.LITTLE_ENDIAN), + ByteOrder.LITTLE_ENDIAN); + assertTrue(Arrays.equals(values, convertedValuesFound)); + } + + @Test + public void testFloatBigEndianRoundtrip() + { + + final float[] values = new float[] + { 1.1f, 2.2f, 3.3f, 1e-25f, 1e25f }; + final float[] convertedValuesFound = + NativeData.byteToFloat(NativeData.floatToByte(values, ByteOrder.BIG_ENDIAN), + ByteOrder.BIG_ENDIAN); + assertTrue(Arrays.equals(values, convertedValuesFound)); + } + + @Test + public void testDoubleLittleEndianRoundtrip() + { + + final double[] values = new double[] + { 1.1f, 2.2f, 3.3f, 1e-25f, 1e25f }; + final double[] convertedValuesFound = + NativeData.byteToDouble(NativeData.doubleToByte(values, ByteOrder.LITTLE_ENDIAN), + ByteOrder.LITTLE_ENDIAN); + assertTrue(Arrays.equals(values, convertedValuesFound)); + } + + @Test + public void testDoubleBigEndianRoundtrip() + { + + final double[] values = new double[] + { 1.1, 2.2, 3.3, 1e-25, 1e25 }; + final double[] convertedValuesFound = + NativeData.byteToDouble(NativeData.doubleToByte(values, ByteOrder.BIG_ENDIAN), + ByteOrder.BIG_ENDIAN); + assertTrue(Arrays.equals(values, convertedValuesFound)); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testNPE() + { + NativeData.copyByteToLong(null, 0, null, 0, 0, 0); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testIAE() + { + NativeData.copyByteToLong(new byte[] {}, -1, new long[] {}, 0, 0, 0); + } + + @Test + public void testPlatformEndiness() + { + final double[] values = new double[] + { 1.1, 2.2, 3.3, 1e-200, 1e200 }; + final double[] valuesLE = + NativeData.byteToDouble(NativeData.doubleToByte(values, ByteOrder.LITTLE_ENDIAN), + ByteOrder.NATIVE); + final double[] valuesBE = + NativeData.byteToDouble(NativeData.doubleToByte(values, ByteOrder.BIG_ENDIAN), + ByteOrder.NATIVE); + if (Arrays.equals(values, valuesLE)) + { + System.out.println("Platform is little endian."); + assertFalse(Arrays.equals(values, valuesBE)); + } + if (Arrays.equals(values, valuesBE)) + { + System.out.println("Platform is big endian."); + assertFalse(Arrays.equals(values, valuesLE)); + } + } + + private void afterClass() + { + } + + private void setUp() + { + } + + public static void main(String[] args) throws Throwable + { + System.out.println(BuildAndEnvironmentInfo.INSTANCE); + System.out.println(); + NativeData.ensureNativeLibIsLoaded(); + final NativeDataTests test = new NativeDataTests(); + try + { + for (Method m : NativeDataTests.class.getMethods()) + { + final Test testAnnotation = m.getAnnotation(Test.class); + if (testAnnotation == null || m.getParameterTypes().length > 0) + { + continue; + } + System.out.println("Running " + m.getName()); + test.setUp(); + try + { + m.invoke(test); + } catch (InvocationTargetException wrapperThrowable) + { + final Throwable th = wrapperThrowable.getCause(); + boolean exceptionFound = false; + for (Class<?> expectedExClazz : testAnnotation.expectedExceptions()) + { + if (expectedExClazz == th.getClass()) + { + exceptionFound = true; + break; + } + } + if (exceptionFound == false) + { + throw th; + } + } + } + System.out.println("Tests OK!"); + } finally + { + test.afterClass(); + } + } + +} -- GitLab