libisnativec-java-5.3.20100629/0000755000175000017500000000000011412332242015606 5ustar twernertwernerlibisnativec-java-5.3.20100629/doc/0000755000175000017500000000000011412332242016353 5ustar twernertwernerlibisnativec-java-5.3.20100629/doc/3rd_party_licenses/0000755000175000017500000000000011412332242022147 5ustar twernertwernerlibisnativec-java-5.3.20100629/doc/3rd_party_licenses/lgpl-2.1.txt0000644000175000017500000006447211220404462024162 0ustar twernertwerner GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! libisnativec-java-5.3.20100629/doc/3rd_party_licenses/jna_license.txt0000644000175000017500000000026511030721450025164 0ustar twernertwernerThis program uses jna (https://jna.dev.java.net/). jna is governed by the LGPL license, available in this directory. jna source code is available at https://jna.dev.java.net/.libisnativec-java-5.3.20100629/src/0000755000175000017500000000000011412332242016375 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/0000755000175000017500000000000011412332242016765 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/intarsys/0000755000175000017500000000000011412332242020641 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/intarsys/nativec/0000755000175000017500000000000011412332242022272 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/0000755000175000017500000000000011412332242023043 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/CWideString.java0000644000175000017500000000400211222634136026072 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; /** * Wrapper class to indicate use of a "wide" string on native side. *

* Use this to indicate on a native call parameter or return type that you will * deal with the platforms wide string capabilities. * */ final public class CWideString { private final String string; public CWideString(String string) { super(); this.string = string; } public String getString() { return string; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/NativeTools.java0000644000175000017500000001501711411055530026162 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; import de.intarsys.nativec.type.INativeType; import de.intarsys.nativec.type.NativeArray; import de.intarsys.nativec.type.NativeArrayType; import de.intarsys.nativec.type.NativeBuffer; import de.intarsys.nativec.type.NativeBufferType; import de.intarsys.nativec.type.NativeInt; import de.intarsys.nativec.type.NativeLong; import de.intarsys.tools.valueholder.IValueHolder; import de.intarsys.tools.valueholder.ObjectValueHolder; /** * Tool class for for dealing with the native framework. */ public class NativeTools { public static byte[] fromNativeByteArray(long ptr, int count) { INativeHandle handle; INativeType type; NativeBuffer nativeValue; if (ptr == 0) { return null; } handle = NativeInterface.get().createHandle(ptr); type = NativeBufferType.create(count); nativeValue = (NativeBuffer) type.createNative(handle); return nativeValue.getBytes(); } public static long fromNativeCLong(long ptr) { INativeHandle handle; NativeLong nativeValue; handle = NativeInterface.get().createHandle(ptr); nativeValue = (NativeLong) NativeLong.META.createNative(handle); return nativeValue.intValue(); } public static IValueHolder fromNativeCLongHolder(long ptr) { return new ObjectValueHolder(fromNativeCLong(ptr)); } public static int fromNativeInt(long ptr) { INativeHandle handle; NativeInt nativeValue; handle = NativeInterface.get().createHandle(ptr); nativeValue = (NativeInt) NativeInt.META.createNative(handle); return nativeValue.intValue(); } public static int[] fromNativeIntArray(INativeHandle handle, int count) { int[] ints; int offset; ints = new int[count]; offset = NativeInt.META.getByteCount(); for (int index = 0; index < count; index++) { ints[index] = handle.getInt(index * offset); } return ints; } public static int[] fromNativeIntArray(long ptr, int count) { INativeHandle handle; handle = NativeInterface.get().createHandle(ptr); return fromNativeIntArray(handle, count); } public static IValueHolder fromNativeIntHolder(long ptr) { return new ObjectValueHolder(fromNativeInt(ptr)); } public static String fromNativeString(long ptr, int count) { return null; } public static INativeHandle toHandle(long address) { return NativeInterface.get().createHandle(address); } public static void toNativeByteArray(long ptr, byte[] value) { if (value != null) { INativeHandle handle; INativeType type; NativeBuffer buffer; handle = NativeInterface.get().createHandle(ptr); type = NativeBufferType.create(value.length); buffer = (NativeBuffer) type.createNative(handle); buffer.setValue(value); } } public static void toNativeCLong(long ptr, IValueHolder value) { toNativeCLong(ptr, value.get().longValue()); } public static void toNativeCLong(long ptr, long value) { INativeHandle handle; NativeLong nativeValue; handle = NativeInterface.get().createHandle(ptr); nativeValue = (NativeLong) NativeLong.META.createNative(handle); nativeValue.setValue(value); } public static void toNativeCLong(long ptr, int[] value) { if (value != null) { INativeHandle handle; NativeArrayType type; NativeArray array; handle = NativeInterface.get().createHandle(ptr); type = NativeArrayType.create(NativeLong.META, value.length); array = (NativeArray) type.createNative(handle); for (int index = 0; index < value.length; index++) { array.setValue(index, value[index]); } } } public static void toNativeCLong(long ptr, long[] value) { if (value != null) { INativeHandle handle; NativeArrayType type; NativeArray array; handle = NativeInterface.get().createHandle(ptr); type = NativeArrayType.create(NativeLong.META, value.length); array = (NativeArray) type.createNative(handle); for (int index = 0; index < value.length; index++) { array.setValue(index, value[index]); } } } public static void toNativeInt(long ptr, int value) { INativeHandle handle; handle = NativeInterface.get().createHandle(ptr); handle.setSize(NativeInt.META.getByteCount()); handle.setInt(0, value); } public static void toNativeInt(long ptr, int[] value) { if (value != null) { INativeHandle handle; NativeArrayType type; NativeArray array; handle = NativeInterface.get().createHandle(ptr); type = NativeArrayType.create(NativeInt.META, value.length); array = (NativeArray) type.createNative(handle); for (int index = 0; index < value.length; index++) { array.setValue(index, value[index]); } } } public static void toNativeInt(long ptr, IValueHolder value) { toNativeInt(ptr, value.get()); } public static void toNativePointer(long ptr, INativeHandle value) { INativeHandle handle; handle = NativeInterface.get().createHandle(ptr); handle.setNativeHandle(0, value); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/NativeInterface.java0000644000175000017500000000764111060237070026767 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; import java.util.Iterator; import sun.misc.Service; /** * Access the VM singleton for {@link INativeInterface}. *

* To make this work, just do one of the following: *

    *
  • set the class name of your implementation with {@link NativeInterface} *
  • *
  • set a {@link INativeInterface} of your choice in {@link NativeInterface}. *
  • *
  • set system property "de.intarsys.nativec.api.INativeInterface" to the * class name of your implementation.
  • *
  • include a service provider file * "de.intarsys.nativec.api.INativeInterface" in your deployment with the class * name of your implementation.
  • *
* */ public class NativeInterface { private static INativeInterface ACTIVE; private static String NAME; public static INativeHandle NULL; public static final String PROP_NATIVEINTERFACE = "de.intarsys.nativec.api.INativeInterface"; //$NON-NLS-1$ static protected INativeInterface createNativeInterface() { String className = getName(); if (className == null) { className = System.getProperty(PROP_NATIVEINTERFACE); } if (className != null) { try { Class clazz = Class.forName(className); return (INativeInterface) clazz.newInstance(); } catch (Exception e) { throw new NoClassDefFoundError(className); } } return findNativeInterface(); } static protected INativeInterface findNativeInterface() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = NativeInterface.class.getClassLoader(); } Iterator ps = Service.providers(INativeInterface.class, loader); if (ps.hasNext()) { try { return (INativeInterface) ps.next(); } catch (Throwable e) { // ignore and try on } } return null; } synchronized static public INativeInterface get() { if (ACTIVE == null) { set(createNativeInterface()); } return ACTIVE; } synchronized static public String getName() { return NAME; } synchronized static public void set(INativeInterface nativeInterface) { if (nativeInterface == null) { throw new NullPointerException("no native interface available"); } ACTIVE = nativeInterface; NULL = nativeInterface.createHandle(0); } synchronized static public void setName(String name) { NAME = name; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/ICallback.java0000644000175000017500000000335711222634136025531 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; public interface ICallback { public Class[] getParameterTypes(); public Class getReturnType(); public Object invoke(Object[] objects); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/INativeFunction.java0000644000175000017500000000372711052336034026767 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; /** * The representation of a native function. */ public interface INativeFunction { /** * Invoke the native function. * * @param returnType * The expected return type. * @param objects * The arguments to the function * @return The result of executing the function */ public T invoke(Class returnType, Object... objects); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/package.html0000644000175000017500000000242411117515200025325 0ustar twernertwerner This is the main package of the native c component.

Here we define the API's and the VM singleton access to native c.

The component itself was built, as we didn't (once again) find anything around that satisfied our needs. We wanted:

  • Clean, layered design
  • Unrestricted access to all modeled objects and concepts.
  • Easy adaption and integration with other views. This means primarily unrestricted access to pointers. There is no use in a native library if i can't bootstrap with a simple address (in our point of view).
  • Clean design for c data structures (primitive, composites and references)
  • Exchangeable native implementation
  • Platform independence
So we created an API and a data structure component and mapped it to JNA, which has great ideas (but in our opinion is to sealed off and has not well done on the data structure side) and can serve as a good foundation. There are some topics we didn't care about yet - this means they may be working or may not, its simply not tested. JNA brings a lot of basic features...
  • Pointer size was not really an issue
  • We didn't care about byte order
64 bit implementation is under way. libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/INativeLibrary.java0000644000175000017500000000414711222634136026606 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; /** * A native library (DLL or shared library). * */ public interface INativeLibrary { /** * Lookup a {@link INativeFunction} from the library. * * @param name * The function name * @return The {@link INativeFunction} */ public INativeFunction getFunction(String name); /** * Lookup a global in the library. * * @param symbolName * The global name * @return The {@link INativeHandle} to the global. */ public INativeHandle getGlobal(String symbolName); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/INativeCallback.java0000644000175000017500000000320711222156754026677 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; public interface INativeCallback { // no methods } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/CLong.java0000644000175000017500000000475311411055530024722 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; /** * A plain Java object representing a platform long. In contrast to the fixed 8 * byte Java primitive type the platform long may vary in size, depending on * operating system and processor architecture. * */ public class CLong extends Number { private Number value; public CLong(long pValue) { value = pValue; } @Override public double doubleValue() { return value.doubleValue(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof CLong) { return value.equals(((CLong) obj).value); } return false; } @Override public float floatValue() { return value.floatValue(); } @Override public int hashCode() { return value.hashCode(); } @Override public int intValue() { return value.intValue(); } @Override public long longValue() { return value.longValue(); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/INativeHandle.java0000644000175000017500000002135311222723334026372 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; /** * A "handle" to a piece of memory (in c space). *

* The handle combines an address and a memory chunk of a specified size. */ public interface INativeHandle { /** * The start address of the memory chunk * * @return The start address of the memory chunk */ public long getAddress(); /** * Marshal the data at byte offset index from the start of the * memory chunk to a byte. * * @param index * The byte offset from the start of the memory chunk * @return A byte marshaled from the memory chunk */ public byte getByte(int index); /** * Marshal the data at byte offset index from the start of the * memory chunk to a byte array of length count. * * @param index * The byte offset from the start of the memory chunk * @param count * The size of the byte array * @return A byte array marshaled from the memory chunk */ public byte[] getByteArray(int index, int count); /** * Marshal the data at byte offset index from the start of the * memory chunk to a long. Get only the "platform" number of bytes. * * @param index * The byte offset from the start of the memory chunk * @return A long marshaled from the memory chunk */ public long getCLong(int index); /** * Marshal the data at byte offset index from the start of the * memory chunk to an int. * * @param index * The byte offset from the start of the memory chunk * @return An int marshaled from the memory chunk */ public int getInt(int index); /** * Marshal the data at byte offset index from the start of the * memory chunk to a long value (which is always 8 byte). * * @param index * The byte offset from the start of the memory chunk * @return A long marshaled from the memory chunk */ public long getLong(int index); /** * Marshal the data at byte offset index from the start of the * memory chunk to an {@link INativeHandle}. * * @param index * The byte offset from the start of the memory chunk * @return An {@link INativeHandle} marshaled from the memory chunk */ public INativeHandle getNativeHandle(int index); /** * Marshal the data at byte offset index from the start of the * memory chunk to a short. * * @param index * The byte offset from the start of the memory chunk * @return A short marshaled from the memory chunk */ public short getShort(int index); /** * The size for the handle in bytes. *

* You can not access bytes from outside the range defined by getAdddress + * size. * */ public int getSize(); /** * Marshal the data at byte offset index from the start of the * memory chunk to a String. * * @param index * The byte offset from the start of the memory chunk * @return A String marshaled from the memory chunk */ public String getString(int index); /** * Marshal the data at byte offset index from the start of the * memory chunk to a String using the platform wide character conversion. * * @param index * The byte offset from the start of the memory chunk * @return A String marshaled from the memory chunk */ public String getWideString(int index); /** * Create a new {@link INativeHandle}, offset from this by * offset bytes. * * @param offset * The byte offset from the start of the memory chunk * @return A new {@link INativeHandle} pointing to "getAddress() + offset". */ public INativeHandle offset(int offset); /** * Write a byte to the memory at byte offset index from the * start of the memory chunk. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setByte(int index, byte value); /** * Write a byte array to the memory at byte offset index from * the start of the memory chunk. The method will write * valueCount bytes from value starting at * valueOffset. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setByteArray(int index, byte[] value, int valueOffset, int valueCount); /** * Write a long to the memory at byte offset index from the * start of the memory chunk. Write only the "platform" number of bytes. The * caller is responsible for observing the value range. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setCLong(int index, long value); /** * Write an int to the memory at byte offset index from the * start of the memory chunk. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setInt(int index, int value); /** * Write a long to the memory at byte offset index from the * start of the memory chunk. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setLong(int index, long value); /** * Write an {@link INativeHandle} to the memory at byte offset * index from the start of the memory chunk. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setNativeHandle(int index, INativeHandle valueHandle); /** * Write a short to the memory at byte offset index from the * start of the memory chunk. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setShort(int index, short value); /** * Set the valid size for the handle to count bytes. *

* You can not access bytes from outside the range defined by getAdddress + * size. * * @param count * The size of the memory managed by the {@link INativeHandle} */ public void setSize(int count); /** * Write a String to the memory at byte offset indexfrom the * start of the memory chunk. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setString(int index, String value); /** * Write a String to the memory at byte offset indexfrom the * start of the memory chunk using the platform wide character conversion. * * @param index * The byte offset from the start of the memory chunk * @param value * The value to write. */ public void setWideString(int index, String value); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/api/INativeInterface.java0000644000175000017500000000644111411055530027074 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.api; /** * The abstraction of a generic interface to c native code. */ public interface INativeInterface { /** * Add a directory to the search path. * * @param path * The path to be added; */ public void addSearchPath(String path); /** * Allocate c memory and return the respective {@link INativeHandle}. * * @param size * The size in bytes. * @return The new allocated {@link INativeHandle} */ public INativeHandle allocate(int size); public INativeCallback createCallback(ICallback callback); /** * Create an {@link INativeFunction} from a function pointer. *

* There is no special handling for the 0 address! * * @param address * The function pointer. * @return The function object. */ public INativeFunction createFunction(long address); /** * Create a void {@link INativeHandle} to a memory address. *

* There is no special handling for the 0 address! * * @param address * The memory address. * @return The handle to the memory address. */ public INativeHandle createHandle(long address); /** * Load a new {@link INativeLibrary}. * * @param name * The name of the library to load. * @return The new {@link INativeLibrary} */ public INativeLibrary createLibrary(String name); /** * The platform long size. * * @return The platform long size. */ public int longSize(); /** * The platform pointer size. * * @return The platform pointer size. */ public int pointerSize(); /** * The platform wide char size. * * @return The platform wide char size. */ public int wideCharSize(); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/jna/0000755000175000017500000000000011412332242023042 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/intarsys/nativec/jna/JnaNativeLibrary.java0000644000175000017500000000562311054467500027127 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.jna; import java.util.Iterator; import com.sun.jna.Function; import com.sun.jna.NativeLibrary; import com.sun.jna.Pointer; import de.intarsys.nativec.api.INativeFunction; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.INativeLibrary; public class JnaNativeLibrary implements INativeLibrary { final private NativeLibrary library; final private JnaNativeInterface nativeInterface; public JnaNativeLibrary(JnaNativeInterface nativeInterface, String name) { this.nativeInterface = nativeInterface; // this is a kludge - but i can't ask for the searchpaths already // registered for (Iterator it = nativeInterface.getSearchPaths().iterator(); it .hasNext();) { String path = it.next(); NativeLibrary.addSearchPath(name, path); } library = NativeLibrary.getInstance(name); } public INativeFunction getFunction(String name) { Function function = getLibrary().getFunction(name); return new JnaNativeFunction(function); } public INativeHandle getGlobal(String symbolName) { Pointer pointer = getLibrary().getGlobalVariableAddress(symbolName); return new JnaNativeHandle(pointer); } protected NativeLibrary getLibrary() { return library; } protected JnaNativeInterface getNativeInterface() { return nativeInterface; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/jna/JnaNativeCallback.java0000644000175000017500000001110011222634134027176 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.jna; import com.sun.jna.CallbackProxy; import com.sun.jna.NativeLong; import com.sun.jna.Pointer; import com.sun.jna.WString; import de.intarsys.nativec.api.CLong; import de.intarsys.nativec.api.CWideString; import de.intarsys.nativec.api.ICallback; import de.intarsys.nativec.api.INativeCallback; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.type.INativeType; import de.intarsys.nativec.type.NativeObject; import de.intarsys.nativec.type.NativeType; public class JnaNativeCallback implements INativeCallback, CallbackProxy { public static Class translateType(Class type) { // TODO implement more elegant type translation if (CWideString.class.isAssignableFrom(type)) { return WString.class; } else if (CLong.class.isAssignableFrom(type)) { return NativeLong.class; } else if (NativeObject.class.isAssignableFrom(type)) { return Pointer.class; } else if (INativeHandle.class.isAssignableFrom(type)) { return Pointer.class; } return type; } private ICallback callback; public JnaNativeCallback(ICallback pCallback) { callback = pCallback; } public Object callback(Object[] args) { for (int index = 0; index < args.length; index++) { Class parameterType; Object object; parameterType = callback.getParameterTypes()[index]; if (CWideString.class.isAssignableFrom(parameterType)) { WString wstring = (WString) args[index]; object = new CWideString(wstring.toString()); } else if (CLong.class.isAssignableFrom(parameterType)) { NativeLong nativeLong = (NativeLong) args[index]; object = new CLong(nativeLong.longValue()); } else if (NativeObject.class.isAssignableFrom(parameterType)) { Pointer pointer = (Pointer) args[index]; if (pointer == null) { object = null; } else { INativeHandle handle = new JnaNativeHandle(pointer); INativeType type = NativeType.lookup(parameterType); if (type == null) { throw new IllegalArgumentException("no type for '" + parameterType + "'"); } object = type.createNative(handle); } } else if (INativeHandle.class.isAssignableFrom(parameterType)) { Pointer pointer = (Pointer) args[index]; if (pointer == null) { object = null; } else { object = new JnaNativeHandle(pointer); } } else { object = args[index]; } args[index] = object; } // TODO map return value too return callback.invoke(args); } public Class[] getParameterTypes() { Class[] genericParameterTypes; Class[] nativeParameterTypes; genericParameterTypes = callback.getParameterTypes(); nativeParameterTypes = new Class[genericParameterTypes.length]; for (int index = 0; index < genericParameterTypes.length; index++) { nativeParameterTypes[index] = translateType(genericParameterTypes[index]); } return nativeParameterTypes; } public Class getReturnType() { return translateType(callback.getReturnType()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/jna/JnaNativeHandle.java0000644000175000017500000001203711412327714026713 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.jna; import com.sun.jna.JNATools; import com.sun.jna.Native; import com.sun.jna.Pointer; import de.intarsys.nativec.api.INativeHandle; public class JnaNativeHandle implements INativeHandle { final private Pointer pointer; private int size; public JnaNativeHandle(JnaNativeHandle handle, int offset) { this.pointer = new Pointer(handle.getAddress() + offset); this.size = handle.size - offset; } public JnaNativeHandle(long address) { this.pointer = new Pointer(address); } public JnaNativeHandle(Pointer pointer) { this.pointer = pointer; } @Override public boolean equals(Object obj) { if (!(obj instanceof INativeHandle)) { return false; } INativeHandle otherHandle = (INativeHandle) obj; return JNATools.getPeer(pointer) == otherHandle.getAddress() && size == otherHandle.getSize(); } public long getAddress() { return JNATools.getPeer(pointer); } public byte getByte(int index) { return pointer.getByte(index); } public byte[] getByteArray(int index, int count) { return pointer.getByteArray(index, count); } public long getCLong(int index) { if (Native.LONG_SIZE == 4) { return pointer.getInt(index); } return pointer.getLong(index); } public int getInt(int index) { return pointer.getInt(index); } public long getLong(int index) { return pointer.getLong(index); } public INativeHandle getNativeHandle(int index) { Pointer tempPointer = pointer.getPointer(index); return new JnaNativeHandle(tempPointer); } public Pointer getPointer() { return pointer; } public short getShort(int index) { return pointer.getShort(index); } public int getSize() { return size; } public String getString(int index) { // if (buffer == null) { // return null; // } // // todo encoding // buffer.position(offset + index); // int i = 0; // while (buffer.get() != 0) { // i++; // } // buffer.position(offset + index); // byte[] valueBytes = new byte[i]; // buffer.get(valueBytes); // return new String(valueBytes); return getPointer().getString(index); } public String getWideString(int index) { return getPointer().getString(index, true); } @Override public int hashCode() { return pointer.hashCode(); } public INativeHandle offset(int offset) { return new JnaNativeHandle(this, offset); } public void setByte(int index, byte value) { pointer.setByte(index, value); } public void setByteArray(int index, byte[] value, int valueOffset, int valueCount) { pointer.write(index, value, valueOffset, valueCount); } public void setCLong(int index, long value) { if (Native.LONG_SIZE == 4) { pointer.setInt(index, (int) value); return; } pointer.setLong(index, value); } public void setInt(int index, int value) { pointer.setInt(index, value); } public void setLong(int index, long value) { pointer.setLong(index, value); } public void setNativeHandle(int index, INativeHandle handle) { pointer.setPointer(index, new Pointer(handle.getAddress())); } public void setShort(int index, short value) { pointer.setShort(index, value); } public void setSize(int pSize) { this.size = pSize; } public void setString(int index, String value) { // // todo encoding getPointer().setString(index, value); } public void setWideString(int index, String value) { getPointer().setString(index, value, true); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/jna/JnaNativeInterface.java0000644000175000017500000001023511411055526027414 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.jna; import java.util.ArrayList; import java.util.List; import com.sun.jna.FastMemory; import com.sun.jna.Function; import com.sun.jna.Native; import com.sun.jna.Pointer; import de.intarsys.nativec.api.ICallback; import de.intarsys.nativec.api.INativeCallback; import de.intarsys.nativec.api.INativeFunction; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.INativeInterface; import de.intarsys.nativec.api.INativeLibrary; /** * An {@link INativeInterface} implemented using JNA, a LGPL licensed Java * native interface abstraction. *

* In our point of view, JNA has the power of deploying all what we wanted to * have, but is ill designed in some key hot spots - so we worked around and * built on top of our own interfaces. * */ public class JnaNativeInterface implements INativeInterface { static { /* * Ugly hack - multithreaded loading caused a deadlock here as * com.sun.jna.Native requests a native library which later requires a * lock on the class loader, while another piece of code * (Security.addProvider) first locks the class loader and later on * requests the runtime to load a native library.

We try here to * provide the same ordering (which is based on a lot of assumptions...) */ ClassLoader loader = JnaNativeInterface.class.getClassLoader(); synchronized (loader) { try { Class.forName("com.sun.jna.Native", true, loader); } catch (ClassNotFoundException e) { // will fail later anyway } } } private List searchPaths = new ArrayList(); public void addSearchPath(String path) { if (searchPaths.contains(path)) { return; } searchPaths.add(path); } public INativeHandle allocate(int size) { return new JnaNativeHandle(new FastMemory(size)); } public INativeCallback createCallback(ICallback callback) { return new JnaNativeCallback(callback); } public INativeFunction createFunction(long address) { Pointer pointer; Function function; pointer = new Pointer(address); function = Function.getFunction(pointer); return new JnaNativeFunction(function); } public INativeHandle createHandle(long address) { return new JnaNativeHandle(address); } public INativeLibrary createLibrary(String name) { return new JnaNativeLibrary(this, name); } protected List getSearchPaths() { return searchPaths; } public int longSize() { return Native.LONG_SIZE; } public int pointerSize() { return Native.POINTER_SIZE; } public int wideCharSize() { return 2; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/jna/JnaNativeFunction.java0000644000175000017500000001055511222634134027304 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.jna; import com.sun.jna.Function; import com.sun.jna.NativeLong; import com.sun.jna.Pointer; import com.sun.jna.WString; import de.intarsys.nativec.api.CLong; import de.intarsys.nativec.api.INativeFunction; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.CWideString; import de.intarsys.nativec.type.INativeObject; import de.intarsys.nativec.type.INativeType; import de.intarsys.nativec.type.NativeObject; import de.intarsys.nativec.type.NativeType; public class JnaNativeFunction implements INativeFunction { private Function function; public JnaNativeFunction(Function function) { this.function = function; } protected Function getFunction() { return function; } public T invoke(Class returnType, Object... objects) { for (int i = 0; i < objects.length; i++) { Object object = objects[i]; if (object instanceof INativeObject) { JnaNativeHandle handle = (JnaNativeHandle) ((INativeObject) object) .getNativeHandle(); if (handle == null) { objects[i] = null; } else { objects[i] = handle.getPointer(); } } else if (object instanceof INativeHandle) { JnaNativeHandle handle = (JnaNativeHandle) object; objects[i] = handle.getPointer(); } else if (object instanceof CWideString) { objects[i] = new WString(((CWideString) object).getString()); } else if (object instanceof CLong) { objects[i] = new NativeLong(((CLong) object).longValue()); } } // if (Log.isLoggable(Level.FINE)) { // Log.log(Level.FINE, "jna invoke " + function.getName()); // } T result; if (CWideString.class.isAssignableFrom(returnType)) { WString wstring = (WString) function.invoke(WString.class, objects); result = (T) new CWideString(wstring.toString()); } else if (CLong.class.isAssignableFrom(returnType)) { NativeLong nativeLong = (NativeLong) function.invoke( NativeLong.class, objects); result = (T) new CLong(nativeLong.longValue()); } else if (NativeObject.class.isAssignableFrom(returnType)) { Pointer pointer = (Pointer) function.invoke(Pointer.class, objects); if (pointer == null) { result = null; } else { INativeHandle handle = new JnaNativeHandle(pointer); INativeType type = NativeType.lookup(returnType); if (type == null) { throw new IllegalArgumentException("no type for '" + returnType + "'"); } result = (T) type.createNative(handle); } } else if (INativeHandle.class.isAssignableFrom(returnType)) { Pointer pointer = (Pointer) function.invoke(Pointer.class, objects); if (pointer == null) { result = null; } else { result = (T) new JnaNativeHandle(pointer); } } else { result = (T) function.invoke(returnType, objects); } return result; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/0000755000175000017500000000000011412332242023253 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeString.java0000644000175000017500000000651311411055530026541 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for a C single byte null terminated string. */ public class NativeString extends NativeObject { /** The meta class instance */ public static final NativeStringType META = new NativeStringType(); static { NativeType.register(NativeString.class, META); } static public NativeString createFromAddress(long address) { return (NativeString) NativeString.META.createNative(NativeTools .toHandle(address)); } private int size = 0; private NativeStringType type; protected NativeString(NativeStringType pType) { type = pType; allocate(); } protected NativeString(NativeStringType pType, INativeHandle handle) { super(handle); type = pType; if (type.hasByteCount()) { handle.setSize(type.getByteCount()); } } protected NativeString(NativeStringType pType, String value) { type = pType; if (type.getStringSize() == 0) { this.size = value.length() + 1; } allocate(); setValue(value); } public NativeString(String value) { this(META, value); } @Override public int getByteCount() { if (type.hasByteCount()) { return type.getByteCount(); } return size; } @Override public INativeType getNativeType() { return type; } public Object getValue() { return stringValue(); } public void setValue(Object value) { setValue((String) value); } public void setValue(String value) { this.size = value.length() + 1; handle.setString(0, value); } /** * The java object corresponding to this. * * @return The java object corresponding to this. */ public String stringValue() { return handle.getString(0); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeLongLP64Type.java0000644000175000017500000000456011375221172027450 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeLongLP64Type extends NativeNumberType { /** * Utility method: return the given number as another number object with * compatible byte size */ public static Object coerce(Number value) { if (NativeObject.SIZE_PTR == 4) { return value.intValue(); } return value.longValue(); } protected NativeLongLP64Type() { super(); } @Override public INativeObject createNative(INativeHandle handle) { return new NativeLongLP64(handle); } @Override public INativeObject createNative(Object value) { return new NativeLongLP64(((Number) value).longValue()); } @Override public int getByteCount() { return NativeObject.SIZE_PTR; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeShort.java0000644000175000017500000000726711251470606026410 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for a primitive short. * */ public class NativeShort extends NativeNumber { /** The meta class instance */ public static final NativeShortType META = new NativeShortType(); static { NativeType.register(NativeShort.class, META); } static public NativeShort createFromAddress(long address) { return (NativeShort) NativeShort.META.createNative(NativeTools .toHandle(address)); } /** * Create a new wrapper */ public NativeShort() { allocate(); } protected NativeShort(INativeHandle handle) { super(handle); } /** * Create a new wrapper */ public NativeShort(short value) { allocate(); setValue(value); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeNumber#byteValue() */ @Override public byte byteValue() { return (byte) shortValue(); } /* * (non-Javadoc) * * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() */ @Override public INativeType getNativeType() { return META; } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativePrimitive#getValue() */ public Object getValue() { return new Short(shortValue()); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeNumber#intValue() */ @Override public int intValue() { return shortValue(); } @Override public long longValue() { return shortValue(); } public void setValue(Object value) { setValue(((Number) value).shortValue()); } public void setValue(short value) { handle.setShort(0, value); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeNumber#shortValue() */ @Override public short shortValue() { return handle.getShort(0); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { if (getNativeHandle().getAddress() == 0) { return "nope - null pointer"; //$NON-NLS-1$ } return String.valueOf(shortValue()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeAbstractStringType.java0000644000175000017500000000471011363545564031105 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.NativeInterface; /** * A meta class implementation */ public class NativeAbstractStringType extends NativeType { private final int characterSize; private final int stringSize; protected NativeAbstractStringType(int pCharacterSize) { super(); characterSize = pCharacterSize; stringSize = 0; } protected NativeAbstractStringType(int pStringSize, int pCharacterSize) { super(); characterSize = pCharacterSize; stringSize = pStringSize; } @Override public int getByteCount() { if (stringSize == 0) { throw new IllegalStateException(); } return stringSize * characterSize; } public int getPreferredBoundary() { return NativeInterface.get().pointerSize(); } public int getStringSize() { return stringSize; } public boolean hasByteCount() { return stringSize != 0; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeByteType.java0000644000175000017500000000412611222634136027043 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeByteType extends NativeNumberType { protected NativeByteType() { super(); } @Override public INativeObject createNative(INativeHandle handle) { return new NativeByte(handle); } @Override public INativeObject createNative(Object value) { return new NativeByte(((Number) value).byteValue()); } @Override public int getByteCount() { return NativeByte.SIZE_BYTE; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeLong.java0000644000175000017500000000633211251470606026200 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for a primitive long. The size of a long depends on the platform; * on a 64-bit Linux or Mac OS X we have 8 byte longs... */ public class NativeLong extends NativeNumber { /** The meta class instance */ public static final NativeLongType META = new NativeLongType(); static { NativeType.register(NativeLong.class, META); } static public NativeLong createFromAddress(long address) { return (NativeLong) NativeLong.META.createNative(NativeTools .toHandle(address)); } /** * Create a new wrapper */ public NativeLong() { allocate(); } protected NativeLong(INativeHandle handle) { super(handle); } /** * Create a new wrapper */ public NativeLong(long value) { allocate(); setValue(value); } @Override public byte byteValue() { return (byte) intValue(); } @Override public INativeType getNativeType() { return META; } public Object getValue() { return new Long(longValue()); } @Override public int intValue() { return (int) longValue(); } @Override public long longValue() { return handle.getCLong(0); } public void setValue(long value) { handle.setCLong(0, value); } public void setValue(Object value) { setValue(((Number) value).longValue()); } @Override public short shortValue() { return (short) longValue(); } @Override public String toString() { if (getNativeHandle().getAddress() == 0) { return "nope - null pointer"; //$NON-NLS-1$ } return String.valueOf(longValue()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeLongType.java0000644000175000017500000000412611222634136027037 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeLongType extends NativeNumberType { protected NativeLongType() { super(); } @Override public INativeObject createNative(INativeHandle handle) { return new NativeLong(handle); } @Override public INativeObject createNative(Object value) { return new NativeLong(((Number) value).longValue()); } @Override public int getByteCount() { return NativeLong.SIZE_LONG; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeShortType.java0000644000175000017500000000413311222634136027235 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeShortType extends NativeNumberType { protected NativeShortType() { super(); } @Override public NativeObject createNative(INativeHandle handle) { return new NativeShort(handle); } @Override public NativeObject createNative(Object value) { return new NativeShort(((Number) value).shortValue()); } @Override public int getByteCount() { return NativeShort.SIZE_SHORT; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeByte.java0000644000175000017500000000723211251470606026204 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for a primitive byte. * */ public class NativeByte extends NativeNumber { /** The meta class instance */ public static final NativeByteType META = new NativeByteType(); static { NativeType.register(NativeByte.class, META); } static public NativeByte createFromAddress(long address) { return (NativeByte) NativeByte.META.createNative(NativeTools .toHandle(address)); } /** * Create a new wrapper */ public NativeByte() { allocate(); } /** * Create a new wrapper */ public NativeByte(byte value) { allocate(); setValue(value); } protected NativeByte(INativeHandle handle) { super(handle); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeNumber#byteValue() */ @Override public byte byteValue() { return handle.getByte(0); } /* * (non-Javadoc) * * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() */ @Override public INativeType getNativeType() { return META; } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativePrimitive#getValue() */ public Object getValue() { return new Byte(byteValue()); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeNumber#intValue() */ @Override public int intValue() { return byteValue(); } @Override public long longValue() { return byteValue(); } public void setValue(byte value) { handle.setByte(0, value); } public void setValue(Object value) { setValue(((Number) value).byteValue()); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeNumber#shortValue() */ @Override public short shortValue() { return byteValue(); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { if (getNativeHandle().getAddress() == 0) { return "nope - null pointer"; //$NON-NLS-1$ } return String.valueOf(byteValue()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeWideStringType.java0000644000175000017500000000444611411055530030217 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeWideStringType extends NativeAbstractStringType { public static NativeWideStringType create(int size) { return new NativeWideStringType(size); } protected NativeWideStringType() { super(2); } protected NativeWideStringType(int size) { super(size, 2); } public NativeObject createNative() { return new NativeWideString(this); } @Override public NativeObject createNative(INativeHandle handle) { return new NativeWideString(this, handle); } @Override public NativeObject createNative(Object value) { return new NativeWideString(this, (String) value); } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeBuffer.java0000644000175000017500000000662311411055530026506 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import java.nio.ByteBuffer; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; /** * A wrapper for a sequence of bytes. * */ public class NativeBuffer extends NativeObject { /** The meta class instance */ public static final NativeBufferType META = new NativeBufferType(); static { NativeType.register(NativeBuffer.class, META); } private NativeBufferType type; public NativeBuffer(byte[] bytes) { type = new NativeBufferType(bytes.length); handle = NativeInterface.get().allocate(bytes.length); handle.setByteArray(0, bytes, 0, bytes.length); } protected NativeBuffer(INativeHandle handle) { super(handle); } public NativeBuffer(int pSize) { type = new NativeBufferType(pSize); handle = NativeInterface.get().allocate(pSize); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeObject#getByteCount() */ @Override public int getByteCount() { // todo alignment? return type.getByteCount(); } /* * (non-Javadoc) * * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() */ @Override public INativeType getNativeType() { return META; } /** * The number of elements in the NativeBuffer * * @return */ public int getSize() { return type.getByteCount(); } public Object getValue() { return getBytes(); } public void setSize(int size) { type = new NativeBufferType(size); handle.setSize(getByteCount()); } public void setValue(Object value) { if (value instanceof byte[]) { byte[] data = (byte[]) value; setByteArray(0, data, 0, data.length); } else if (value instanceof ByteBuffer) { byte[] data = new byte[getSize()]; ((ByteBuffer) value).get(data); setByteArray(0, data, 0, data.length); } } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeBufferType.java0000644000175000017500000000475611411055530027355 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeBufferType extends NativeType { public static NativeBufferType create(int size) { return new NativeBufferType(size); } final private int bufferSize; protected NativeBufferType() { super(); this.bufferSize = 0; } protected NativeBufferType(int bufferSize) { super(); this.bufferSize = bufferSize; } @Override public INativeObject createNative(INativeHandle handle) { NativeBuffer buffer; buffer = new NativeBuffer(handle); // setSize() creates a new type object // TODO create constructor to use the existing one? buffer.setSize(bufferSize); return buffer; } public int getBufferSize() { return bufferSize; } @Override public int getByteCount() { return bufferSize; } public int getPreferredBoundary() { return 1; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeSimpleType.java0000644000175000017500000000345511411055530027370 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; /** * The meta class implementation */ public class NativeSimpleType extends NativeType { protected NativeSimpleType() { super(); } public int getPreferredBoundary() { return getByteCount(); } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeNumberType.java0000644000175000017500000000335311222634136027371 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; /** * The meta class implementation */ public class NativeNumberType extends NativeSimpleType { protected NativeNumberType() { super(); } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeNumber.java0000644000175000017500000000477411222634136026537 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * An abstract superclass for the implementation of number * {@link NativeSimple}. */ public abstract class NativeNumber extends NativeSimple { /** * Create a wrapper */ protected NativeNumber() { } protected NativeNumber(INativeHandle handle) { super(handle); } /** * This as a java primitive byte value. * * @return This as a java primitive byte value. */ public abstract byte byteValue(); /** * This as a java primitive int value. * * @return This as a java primitive int value. */ public abstract int intValue(); /** * This as a java primitive long value. * * @return This as a java primitive long value. */ public abstract long longValue(); /** * This as a java primitive short value. * * @return This as a java primitive short value. */ public abstract short shortValue(); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/package.html0000644000175000017500000000014011052336040025527 0ustar twernertwerner Here you find the data types and data structures for native c. libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeArrayType.java0000644000175000017500000000574711411055530027223 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The type for a {@link NativeArray} */ public class NativeArrayType extends NativeType { public static NativeArrayType create(INativeType baseType, int size) { return new NativeArrayType(baseType, size); } private final int arraySize; /** * The size of the base type of the array. */ private final int baseSize; /** * The base type of the array. */ private final INativeType baseType; protected NativeArrayType(INativeType baseDeclaration, int arraySize) { super(); this.baseType = baseDeclaration; this.baseSize = baseDeclaration.getByteCount(); this.arraySize = arraySize; } @Override public INativeObject createNative(INativeHandle handle) { NativeArray array = new NativeArray(this, handle); if (arraySize > 0) { array.setSize(arraySize); } return array; } @Override public INativeObject createNative(Object value) { NativeArray array = new NativeArray(this); return array; } public int getArraySize() { return arraySize; } public int getBaseSize() { return baseSize; } public INativeType getBaseType() { return baseType; } @Override public int getByteCount() { // todo take into account alignment / packing? return arraySize * baseSize; } public int getPreferredBoundary() { return baseType.getPreferredBoundary(); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeIntType.java0000644000175000017500000000412011222634134026662 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeIntType extends NativeNumberType { protected NativeIntType() { super(); } @Override public INativeObject createNative(INativeHandle handle) { return new NativeInt(handle); } @Override public INativeObject createNative(Object value) { return new NativeInt(((Number) value).longValue()); } @Override public int getByteCount() { return NativeInt.SIZE_INT; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeType.java0000644000175000017500000000672611411055530026222 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import java.util.HashMap; import java.util.Map; import de.intarsys.nativec.api.INativeHandle; /** * A common superclass for {@link INativeType} implementations */ public abstract class NativeType implements INativeType { /** All known meta classes. */ private static Map, INativeType> metaClasses = new HashMap, INativeType>(); public static synchronized INativeType lookup(Class clazz) { INativeType result = metaClasses.get(clazz); if (result == null) { ClassLoader classLoader; clazz.getClasses(); classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = NativeType.class.getClassLoader(); } try { Class.forName(clazz.getName(), true, classLoader); } catch (ClassNotFoundException ex) { // ignore } result = metaClasses.get(clazz); } return result; } public static synchronized void register(Class clazz, INativeType type) { metaClasses.put(clazz, type); } protected NativeType() { // } protected NativeType(Class instanceClass) { register(instanceClass, this); } /** * Create a Declaration that represents an array of this. * * @return Create a Declaration that represents an array of this. */ public INativeType Array(int size) { return new NativeArrayType(this, size); } public INativeObject createNative(INativeHandle handle) { throw new IllegalStateException("meta constructor missing"); } public INativeObject createNative(Object value) { throw new IllegalStateException("meta constructor missing"); } public int getByteCount() { return 0; } /** * Create a Declaration that represents a reference to this. * * @return Create a Declaration that represents a reference to this. */ public INativeType Ref() { return new NativeReferenceType(this); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeStruct.java0000644000175000017500000001002011225416306026550 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import java.util.Iterator; import de.intarsys.nativec.api.INativeHandle; /** * An abstract superclass for the implementation of structured * {@link NativeObject} instances. These objects are built using named slots * with other {@link INativeObject} instances (as opposed to {@link NativeArray}, * using indexed slots). * */ public abstract class NativeStruct extends NativeObject { /** The meta class instance */ public static final NativeStructType META = new NativeStructType(); static { NativeType.register(NativeGenericStruct.class, META); } protected INativeObject[] values; public NativeStruct() { super(); } public NativeStruct(INativeHandle handle) { super(handle); } @Override public int getByteCount() { return getNativeType().getByteCount(); } /** * The NativeObject at the named slot name. *

* The marshalling is delegated to the StructMember in the * StructDeclaration. * * @param name * The name of the slot in the structure. * * @return The NativeObject at the named slot name. */ public INativeObject getNativeObject(String name) { return getStructType().getNativeObject(this, name); } protected StructMember getStructField(String name) { return getStructType().getField(name); } public NativeStructType getStructType() { return (NativeStructType) getNativeType(); } public Object getValue() { throw new UnsupportedOperationException( "getValue not implemented for NativeStruct"); } public void setValue(Object value) { throw new UnsupportedOperationException( "getValue not implemented for NativeStruct"); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeObject#toNestedString() */ @Override public String toNestedString() { return "Struct " + getClass().getName(); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getName()); sb.append("\n"); for (Iterator it = getStructType().getFields().iterator(); it.hasNext();) { StructMember field = (StructMember) it.next(); sb.append(field.getName()); sb.append("="); try { sb.append(getNativeObject(field.getName()).toString()); } catch (RuntimeException e) { sb.append("**error**"); } sb.append("\n"); } return sb.toString(); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeLongLP64.java0000644000175000017500000000671511375221172026612 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for the Windows type LONG_PTR. This type has the same size as a * pointer but is semantically a simple integer. (same as NativeLong on LP64 * platforms size-wise) */ public class NativeLongLP64 extends NativeNumber { /** The meta class instance */ public static final NativeLongLP64Type META = new NativeLongLP64Type(); static { NativeType.register(NativeLongLP64.class, META); } static public NativeLongLP64 createFromAddress(long address) { return (NativeLongLP64) NativeLongLP64.META.createNative(NativeTools .toHandle(address)); } /** * Create a new wrapper */ public NativeLongLP64() { allocate(); } protected NativeLongLP64(INativeHandle handle) { super(handle); } /** * Create a new wrapper */ public NativeLongLP64(long value) { allocate(); setValue(value); } @Override public byte byteValue() { return (byte) intValue(); } @Override public INativeType getNativeType() { return META; } public Object getValue() { return new Long(longValue()); } @Override public int intValue() { return (int) longValue(); } @Override public long longValue() { if (NativeObject.SIZE_PTR == 4) { return handle.getInt(0); } return handle.getLong(0); } public void setValue(long value) { if (NativeObject.SIZE_PTR == 4) { handle.setInt(0, (int) value); return; } handle.setLong(0, value); } public void setValue(Object value) { setValue(((Number) value).longValue()); } @Override public short shortValue() { return (short) longValue(); } @Override public String toString() { if (getNativeHandle().getAddress() == 0) { return "nope - null pointer"; //$NON-NLS-1$ } return String.valueOf(longValue()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeSimple.java0000644000175000017500000000453311222634136026531 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * An abstract superclass for the implementation of primitive * {@link NativeObject} instances. These {@link NativeObject} instances are not * constructed from other {@link NativeObject} instances but implemented * directly in Java, having a fixed size. * */ public abstract class NativeSimple extends NativeObject { /** * Create a wrapper for a NativePrimitive */ protected NativeSimple() { } protected NativeSimple(INativeHandle handle) { super(handle); handle.setSize(getByteCount()); } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativeObject#getByteCount() */ @Override public int getByteCount() { return getNativeType().getByteCount(); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeReferenceType.java0000644000175000017500000000523211411055530030030 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; /** * A declaration for a slot containing a reference to a native object. */ public class NativeReferenceType extends NativeType { public static NativeReferenceType create(INativeType baseType) { return new NativeReferenceType(baseType); } /** The declaration for the dereference slot */ private final INativeType baseType; /** * Declare a reference to a base type */ protected NativeReferenceType(INativeType baseDeclaration) { super(); this.baseType = baseDeclaration; } @Override public INativeObject createNative(INativeHandle handle) { return new NativeReference(this, handle); } @Override public INativeObject createNative(Object value) { return new NativeReference(this); } public INativeType getBaseType() { return baseType; } @Override public int getByteCount() { return NativeInterface.get().pointerSize(); } public int getPreferredBoundary() { return NativeInterface.get().pointerSize(); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeStringType.java0000644000175000017500000000440611411055530027402 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeStringType extends NativeAbstractStringType { public static NativeStringType create(int size) { return new NativeStringType(size); } protected NativeStringType() { super(1); } protected NativeStringType(int size) { super(size, 1); } public NativeObject createNative() { return new NativeString(this); } @Override public NativeObject createNative(INativeHandle handle) { return new NativeString(this, handle); } @Override public NativeObject createNative(Object value) { return new NativeString(this, (String) value); } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeInt.java0000644000175000017500000000631011251470606026027 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for a primitive int (which is always 4 bytes except on ILP64 * systems which can be treated as non-existent for our purposes). */ public class NativeInt extends NativeNumber { /** The meta class instance */ public static final NativeIntType META = new NativeIntType(); static { NativeType.register(NativeInt.class, META); } static public NativeInt createFromAddress(long address) { return (NativeInt) NativeInt.META.createNative(NativeTools .toHandle(address)); } /** * Create a new wrapper */ public NativeInt() { allocate(); } protected NativeInt(INativeHandle handle) { super(handle); } /** * Create a new wrapper */ public NativeInt(long value) { allocate(); setValue(value); } @Override public byte byteValue() { return (byte) intValue(); } @Override public INativeType getNativeType() { return META; } public Object getValue() { return new Integer(intValue()); } @Override public int intValue() { return handle.getInt(0); } @Override public long longValue() { return intValue(); } public void setValue(int value) { handle.setInt(0, value); } public void setValue(Object value) { setValue(((Number) value).intValue()); } @Override public short shortValue() { return (short) intValue(); } @Override public String toString() { if (getNativeHandle().getAddress() == 0) { return "nope - null pointer"; //$NON-NLS-1$ } return String.valueOf(intValue()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/INativeObject.java0000644000175000017500000000526211222634136026617 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The Java object representation of a c memory construct. *

* The {@link INativeObject} has a reference to c memory (the * {@link INativeHandle} and "marshalling" methods "getValue" and "setValue" to * get and set the Java representation. * */ public interface INativeObject { /** * The bytes that make up the {@link INativeObject}. * * @return The bytes that make up the {@link INativeObject}. */ public byte[] getBytes(); /** * The {@link INativeHandle} to the c memory for the object. * * @return */ public INativeHandle getNativeHandle(); /** * The {@link INativeType} for the object. * * @return */ public INativeType getNativeType(); /** * A Java side representation from the memory. * * @return A Java side representation for the {@link INativeObject}. */ public Object getValue(); /** * Assign (and marshall to memory) the Java side representation. * * @param value * The new Java value. */ public void setValue(Object value); } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/StructMember.java0000644000175000017500000002451411411055530026541 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * A field definition within a {@link NativeStructType}. */ public class StructMember { /** the members declaration */ private final INativeType memberType; /** The members name */ protected final String name; /** the index of the member within the struct */ protected final int index; /** The offset of the declaration within its structure */ private final int offset; /** the definition context of the member */ private final NativeStructType structType; /** * Create a slot with name "name" and the declaration "memberDeclaration" */ protected StructMember(NativeStructType structType, String name, int index, INativeType memberType, int offset) { super(); this.structType = structType; this.name = name; this.index = index; this.memberType = memberType; this.offset = offset; } /** * Performance shortcut to access "byte" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public byte getByte(NativeStruct struct, int index) { return struct.handle.getByte(offset + index); } /** * Performance shortcut to access "byte[]" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public byte[] getByteArray(NativeStruct struct, int index, int count) { return struct.handle.getByteArray(offset + index, count); } /** * Performance shortcut to access "platform sized long" in the struct * member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public long getCLong(NativeStruct struct, int index) { return struct.handle.getCLong(offset + index); } /** * Performance shortcut to access "int" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public int getInt(NativeStruct struct, int index) { return struct.handle.getInt(offset + index); } /** * Performance shortcut to access "long" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public long getLong(NativeStruct struct, int index) { return struct.handle.getLong(offset + index); } /** * The type declaration of the slot. * * @return The type declaration of the slot. */ protected INativeType getMemberType() { return memberType; } /** * The slots name. * * @return The slots name. */ public String getName() { return name; } /** * Performance shortcut to access "INativeHandle" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public INativeHandle getNativeHandle(NativeStruct struct, int index) { return struct.handle.getNativeHandle(offset + index); } synchronized public INativeObject getNativeObject(NativeStruct struct) { if (struct.values == null) { struct.values = new INativeObject[structType.getFieldsSize()]; } INativeObject result = struct.values[index]; if (result == null) { result = memberType.createNative(struct.getNativeHandle().offset( offset)); struct.values[this.index] = result; } return result; } /** * The offset of the slot relative to the StructDeclaration. * * @return The offset of the slot relative to the StructDeclaration. */ protected int getOffset() { return offset; } /** * Performance shortcut to access "short" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public short getShort(NativeStruct struct, int index) { return struct.handle.getShort(offset + index); } /** * Performance shortcut to access "String" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public String getString(NativeStruct struct, int index) { return struct.handle.getString(offset + index); } public Object getValue(NativeStruct struct) { return getNativeObject(struct).getValue(); } /** * Performance shortcut to access "String" (from wide characters) in the * struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public String getWideString(NativeStruct struct, int index) { return struct.handle.getWideString(offset + index); } /** * Performance shortcut to access "byte" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setByte(NativeStruct struct, int index, byte value) { struct.handle.setByte(offset + index, value); } /** * Performance shortcut to access "byte[]" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setByteArray(NativeStruct struct, int index, byte[] value, int valueOffset, int valueCount) { struct.handle.setByteArray(offset + index, value, valueOffset, valueCount); } /** * Performance shortcut to access "platform sized long" in the struct * member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setCLong(NativeStruct struct, int index, long value) { struct.handle.setCLong(offset + index, value); } /** * Performance shortcut to access "int" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setInt(NativeStruct struct, int index, int value) { struct.handle.setInt(offset + index, value); } /** * Performance shortcut to access "long" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setLong(NativeStruct struct, int index, long value) { struct.handle.setLong(offset + index, value); } /** * Performance shortcut to access "INativeHandle" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setNativeHandle(NativeStruct struct, int index, INativeHandle value) { struct.handle.setNativeHandle(offset + index, value); } /** * Performance shortcut to access "short" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setShort(NativeStruct struct, int index, short value) { struct.handle.setShort(offset + index, value); } /** * Performance shortcut to access "String" in the struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setString(NativeStruct struct, int index, String value) { struct.handle.setString(offset + index, value); } public void setValue(NativeStruct struct, Object value) { getNativeObject(struct).setValue(value); } /** * Performance shortcut to access "String" (from wide characters) in the * struct member. * * @param struct * The container struct instance * @param index * The memory offset from the struct member base * @return */ public void setWideString(NativeStruct struct, int index, String value) { struct.handle.setWideString(offset + index, value); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "[" + getName() + "]"; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeStructType.java0000644000175000017500000001164611411055530027424 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import de.intarsys.nativec.api.NativeInterface; /** * The meta class implementation */ public class NativeStructType extends NativeType { /** * The boundary where the struct as a member of another struct would want to * be aligned. */ private int byteBoundary = 1; /** * The offset where theoretically a new member could be placed. Where it * will actually be placed depends on the member itself. */ private int byteOffset = 0; /** The size in bytes of the member collection */ private int byteSize = 0; /** The named members of the structure definition */ private final Map fields = new HashMap(); /** * The number of members in the structure */ private int fieldsSize = 0; /** Where members should be aligned in native memory */ // expecting pointer size to be a reasonable default - have to look that up private int packing = NativeInterface.get().pointerSize(); protected NativeStructType() { super(); } protected NativeStructType(Class instanceClass) { super(instanceClass); } /** * Declare a new member for the struct. * * @param name * The name of the new member slot. * @param declaration * The type declaration for the slot */ public StructMember declare(String name, INativeType declaration) { int size; int boundary; size = declaration.getByteCount(); boundary = Math.min(declaration.getPreferredBoundary(), packing); byteBoundary = Math.max(byteBoundary, boundary); byteOffset = byteOffset - ((byteOffset + boundary - 1) % boundary + 1) + boundary; StructMember field = new StructMember(this, name, fieldsSize++, declaration, byteOffset); fields.put(name, field); byteOffset = byteOffset + size; byteSize = byteOffset; return field; } public int getByteBoundary() { return byteBoundary; } @Override public int getByteCount() { return getByteSize(); } /** * The total size of the StructDeclaration. * * @return The total size of the StructDeclaration. */ public int getByteSize() { return byteSize; } public StructMember getField(String name) { return fields.get(name); } /** * The collection of StructMember instances in declaration order. * * @return The collection of StructMember instances in declaration order. */ public List getFields() { return new ArrayList(fields.values()); } public int getFieldsSize() { return fieldsSize; } public INativeObject getNativeObject(NativeStruct struct, String name) { StructMember member = fields.get(name); return member.getNativeObject(struct); } public int getPacking() { return packing; } public int getPreferredBoundary() { return getByteBoundary(); } public void setPacking(int pPacking) { if (!fields.isEmpty()) { throw new IllegalStateException( "packing must be set before members are declared"); } packing = pPacking; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (StructMember field : getFields()) { sb.append(field.toString()); sb.append("\n"); } return sb.toString(); } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeVoid.java0000644000175000017500000000540711411055530026175 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; import de.intarsys.nativec.api.NativeTools; /** * An object representing "void" (nothing) */ public class NativeVoid extends NativeSimple { /** The meta class instance */ public static final NativeVoidType META = new NativeVoidType(); public static final NativeVoid NULL = (NativeVoid) META .createNative(NativeInterface.NULL); static { NativeType.register(NativeVoid.class, META); } static public NativeVoid createFromAddress(long address) { return (NativeVoid) NativeVoid.META.createNative(NativeTools .toHandle(address)); } /** * Create a new wrapper instance */ public NativeVoid() { allocate(); } protected NativeVoid(INativeHandle handle) { super(handle); } /* * (non-Javadoc) * * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() */ @Override public INativeType getNativeType() { return META; } /* * (non-Javadoc) * * @see de.intarsys.tools.nativec.NativePrimitive#getValue() */ public Object getValue() { return null; } public void setValue(Object value) { } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeVoidType.java0000644000175000017500000000404711222634136027043 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The meta class implementation */ public class NativeVoidType extends NativeSimpleType { protected NativeVoidType() { super(); } @Override public INativeObject createNative(INativeHandle handle) { return new NativeVoid(handle); } @Override public INativeObject createNative(Object value) { return new NativeVoid(); } @Override public int getByteCount() { return 0; } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeArray.java0000644000175000017500000001177611222634136026365 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * An {@link INativeObject} that represents a homogeneous (this means of equal * length and type) sequence of other {@link INativeObject} instances. */ public class NativeArray extends NativeObject { /** The meta class instance */ public static final NativeArrayType META = new NativeArrayType( NativeVoid.META, 0); static { NativeType.register(NativeArray.class, META); } public static NativeArray create(INativeType baseType, int size) { NativeArrayType type = new NativeArrayType(baseType, size); return new NativeArray(type); } private INativeObject[] values; private NativeArrayType type; protected NativeArray(NativeArrayType type) { this.type = type; allocate(); } protected NativeArray(NativeArrayType type, INativeHandle handle) { super(handle); this.type = type; } public INativeType getBaseType() { return type.getBaseType(); } @Override public int getByteCount() { return type.getByteCount(); } /** * The {@link INativeObject} at index in the sequence (the index'th element * of the array). * * @param index * The index of the element to be reported. * * @return The NativeObject at index */ synchronized public INativeObject getNativeObject(int index) { if (values == null) { values = new INativeObject[getSize()]; } INativeObject result = values[index]; if (result == null) { int elementOffset = index * type.getBaseSize(); result = type.getBaseType().createNative( handle.offset(elementOffset)); values[index] = result; } return result; } /* * (non-Javadoc) * * @see de.intarsys.graphic.freetype.NativeObject#getMetaClass() */ @Override public INativeType getNativeType() { return type; } /** * The number of NativeObject instances in the sequence represented by this * (in other terms the array size). * * @return The number of NativeObject instances in the sequence represented * by this */ public int getSize() { return type.getArraySize(); } public Object getValue() { throw new UnsupportedOperationException( "getValue not implemented for NativeArray"); } public Object getValue(int index) { return getNativeObject(index).getValue(); } public void setBaseType(INativeType baseType) { type = NativeArrayType.create(baseType, type.getArraySize()); this.values = null; handle.setSize(getByteCount()); } public void setSize(int size) { type = NativeArrayType.create(type.getBaseType(), size); this.values = null; handle.setSize(getByteCount()); } public void setValue(int index, Object value) { getNativeObject(index).setValue(value); } public void setValue(Object value) { throw new UnsupportedOperationException( "setValue not implemented for NativeArray"); } /* * (non-Javadoc) * * @see de.intarsys.graphic.freetype.NativeObject#toNestedString() */ @Override public String toNestedString() { return "[...]"; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < getSize(); i++) { // NativeObject element = get(i); // sb.append(element.toString()); // sb.append(", "); } sb.append("]"); return sb.toString(); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeObject.java0000644000175000017500000002030411412327676026511 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; /** * An instance of an external (C memory) represented object. The C-object is * represented using the handle (the pointer to the object in C memory) and its * type {@link INativeType}. *

* Supported data types are *

    *
  • primitive types (char, byte, int, short, long, String...)
  • *
  • byte buffer
  • *
  • array types
  • *
  • structures
  • *
  • references
  • *
*

*/ public abstract class NativeObject implements INativeObject { public static final int SIZE_BYTE = 1; public static final int SIZE_INT = 4; public static final int SHIFT_INT = 2; public static final int SIZE_LONGLONG = 8; public static final int SHIFT_LONGLONG = 3; public static final int SIZE_LONG = NativeInterface.get().longSize(); public static final int SHIFT_LONG = SIZE_LONG == 4 ? 2 : 3; public static final int SIZE_PTR = NativeInterface.get().pointerSize(); public static final int SIZE_SHORT = 2; /** DEBUG flag */ public static boolean DEBUG = true; /** * The handle to the memory chunk used by this object. While in fact this is * final, Java language semantics does not allow to declare so! *

* The handle should only be assigned in the constructor, via parameter or * "allocate". */ protected INativeHandle handle; /** * */ protected NativeObject() { // } /** * Create a new NativeObject in C-Memory at pointer "handle". The bytes * belonging to this object may already have been copied from C-Memory and * made available in bytes at location offset. * * @param handle * The pointer in C-memory */ protected NativeObject(INativeHandle handle) { this.handle = handle; } /** * Manage the objects memory in Java. C memory will be valid at least as * long as we hold a reference to the buffer. C memory is undefined (but not * a memory leak) and may be reclaimed at any time after dropping our * pointer to the buffer. */ protected void allocate() { int size = getByteCount(); handle = NativeInterface.get().allocate(size); } /** * This is a special form of the "createNative" signature, implementing a * "type cast" on the same memory location. * * @param declaration * The new base declaration type. * @return The {@link INativeObject} at the same memory location as this, * but of a different type. */ public INativeObject cast(INativeType declaration) { return declaration.createNative(handle); } public INativeObject createReference() { NativeReference ref = NativeReference.create(getNativeType()); ref.setValue(this); return ref; } /** * The byte at index as a native byte. * * @param index * The index of the element to be reported. * @return The element at index as a native byte. */ public byte getByte(int index) { return handle.getByte(index); } /** * The element at index as an array of native byte with dimension count. * This is a lightweight optimization. * * @param index * The index of the element to be reported. * @return The element at index as an array of native byte with dimension * count. */ public byte[] getByteArray(int index, int count) { return handle.getByteArray(index, count); } /** * The number of bytes occupied by this. * * @return The number of bytes occupied by this. */ public abstract int getByteCount(); /** * The bytes copied from C-memory that represent this. * * @return The bytes copied from C-memory that represent this. */ public byte[] getBytes() { return handle.getByteArray(0, getByteCount()); } /** * The element at index as a native long. Only the "platform" number of * bytes are read. * * @param index * The index of the element to be reported. * @return The element at index as a native long. */ public long getCLong(int index) { return handle.getCLong(index); } /** * The element at index as a native int. * * @param index * The index of the element to be reported. * @return The element at index as a native int. */ public int getInt(int index) { return handle.getInt(index); } /** * The C-Pointer where the associated memory is found. * * @return The C-Pointer where the associated memory is found. */ public INativeHandle getNativeHandle() { return handle; } public INativeHandle getNativeHandle(int index) { return handle.getNativeHandle(index); } /** * The meta information and behavior for the NativeObject. *

* There is exactly one meta instance for all NativeObject instances of a * certain type. * * @return The meta information and behavior for the NativeObject. * */ abstract public INativeType getNativeType(); /** * The element at index as a native short. This is a lightweight * optimization. * * @param index * The index of the element to be reported. * @return The element at index as a native short. */ public short getShort(int index) { return handle.getShort(index); } public String getString(int index) { return handle.getString(index); } public String getWideString(int index) { return handle.getWideString(index); } /** * Answer true if this is "null". This means the associated * C-pointer is pointing to 0. * * @return Answer true if this is "null". */ public boolean isNull() { return handle == null || handle.getAddress() == 0; } public void setByte(int index, byte value) { handle.setByte(index, value); } public void setByteArray(int index, byte[] value, int valueOffset, int valueCount) { handle.setByteArray(index, value, valueOffset, valueCount); } public void setCLong(int index, long value) { handle.setCLong(index, value); } public void setInt(int index, int value) { handle.setInt(index, value); } public void setNativeHandle(int index, INativeHandle value) { handle.setNativeHandle(index, value); } public void setShort(int index, short value) { handle.setShort(index, value); } public void setString(int index, String value) { handle.setString(index, value); } public void setWideString(int index, String value) { handle.setWideString(index, value); } /** * A string for debugging purposes. * * @return A string for debugging purposes. */ public String toNestedString() { return toString(); } }libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeGenericStruct.java0000644000175000017500000000442211225416306030056 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The generic struct may change its type at runtime. * */ public class NativeGenericStruct extends NativeStruct { private NativeStructType type; protected NativeGenericStruct(NativeStructType type) { super(); this.type = type; allocate(); } protected NativeGenericStruct(NativeStructType type, INativeHandle handle) { super(handle); this.type = type; } @Override public INativeType getNativeType() { return type; } @Override public NativeStructType getStructType() { return type; } public void setNativeType(NativeStructType type) { this.type = type; this.values = null; } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeReference.java0000644000175000017500000000753011222634136027176 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeInterface; /** * An object representing a reference to another object ("pointer"). */ public class NativeReference extends NativeObject { /** The meta class instance */ public static final NativeReferenceType META = new NativeReferenceType( NativeVoid.META); static { NativeType.register(NativeReference.class, META); } public static NativeReference create(INativeType baseType) { NativeReferenceType type = new NativeReferenceType(baseType); return new NativeReference(type); } private INativeHandle dereferenceHandle; private INativeObject dereferenced; private NativeReferenceType type; protected NativeReference(NativeReferenceType type) { this.type = type; allocate(); } protected NativeReference(NativeReferenceType type, INativeHandle handle) { super(handle); this.type = type; handle.setSize(getByteCount()); } public INativeType getBaseType() { return type.getBaseType(); } @Override public int getByteCount() { return NativeInterface.get().pointerSize(); } @Override public INativeType getNativeType() { return type; } synchronized public Object getValue() { dereferenceHandle = handle.getNativeHandle(0); // check if not yet computed or handle changed if (dereferenced == null || !dereferenced.getNativeHandle().equals(dereferenceHandle)) { dereferenced = type.getBaseType().createNative( dereferenceHandle); } return dereferenced; } public void setBaseType(INativeType baseType) { type = NativeReferenceType.create(baseType); dereferenceHandle = null; dereferenced = null; } synchronized public void setValue(Object value) { if (value instanceof INativeObject) { INativeHandle valueHandle = ((INativeObject) value) .getNativeHandle(); handle.setNativeHandle(0, valueHandle); dereferenceHandle = valueHandle; dereferenced = ((INativeObject) value); } else if (value instanceof INativeHandle) { INativeHandle valueHandle = (INativeHandle) value; handle.setNativeHandle(0, valueHandle); dereferenceHandle = valueHandle; dereferenced = null; } else { throw new IllegalArgumentException(); } } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeStaticStruct.java0000644000175000017500000000365611225416306027741 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * A struct with a statically defined type. * */ public abstract class NativeStaticStruct extends NativeStruct { protected NativeStaticStruct() { allocate(); } protected NativeStaticStruct(INativeHandle handle) { super(handle); handle.setSize(getByteCount()); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/NativeWideString.java0000644000175000017500000000664711411055530027362 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; import de.intarsys.nativec.api.NativeTools; /** * A wrapper for a C wide (double byte) string. */ public class NativeWideString extends NativeObject { /** The meta class instance */ public static final NativeWideStringType META = new NativeWideStringType(); static { NativeType.register(NativeWideString.class, META); } static public NativeWideString createFromAddress(long address) { return (NativeWideString) NativeWideString.META .createNative(NativeTools.toHandle(address)); } private int size = 0; private NativeWideStringType type; protected NativeWideString(NativeWideStringType pType) { type = pType; allocate(); } protected NativeWideString(NativeWideStringType pType, INativeHandle handle) { super(handle); type = pType; if (type.hasByteCount()) { handle.setSize(type.getByteCount()); } } protected NativeWideString(NativeWideStringType pType, String value) { type = pType; if (type.getStringSize() == 0) { size = value.length() + 1; size = size << 1; } allocate(); setValue(value); } public NativeWideString(String value) { this(META, value); } @Override public int getByteCount() { if (type.hasByteCount()) { return type.getByteCount(); } return size; } @Override public INativeType getNativeType() { return type; } public Object getValue() { return stringValue(); } public void setValue(Object value) { setValue((String) value); } public void setValue(String value) { size = value.length() + 1; size = size << 1; handle.setWideString(0, value); } /** * The java object corresponding to this. * * @return The java object corresponding to this. */ public String stringValue() { return handle.getWideString(0); } } libisnativec-java-5.3.20100629/src/de/intarsys/nativec/type/INativeType.java0000644000175000017500000000567611222634134026341 0ustar twernertwerner/* * Copyright (c) 2008, intarsys consulting GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of intarsys nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.intarsys.nativec.type; import de.intarsys.nativec.api.INativeHandle; /** * The type (factory) for {@link INativeObject} instances. *

* A type has declaration style method to create more complex types. * */ public interface INativeType { /** * Create an array type from this. * * @param size * The predefined size for the array. * * @return The derived type. */ public INativeType Array(int size); /** * Create an {@link INativeObject} for this type from the Java object. * * @param value * * @return The new {@link INativeObject} */ public INativeObject createNative(Object value); /** * Create a new {@link INativeObject} from a {@link INativeHandle}. * * @param handle * The handle to memory. * @return The new {@link INativeObject} */ public INativeObject createNative(INativeHandle handle); /** * The boundary where this type as a struct member would want to be aligned. * A structure can override this value with packing. * * @return The preferred alignment boundary. */ public int getPreferredBoundary(); /** * The size of the type in c memory. * * @return The size of the type in c memory. */ public int getByteCount(); /** * Create a reference type to this. * * @return The derived type. */ public INativeType Ref(); } libisnativec-java-5.3.20100629/src/overview.html0000644000175000017500000003073611121423376021150 0ustar twernertwerner This is a generic native code wrapper.

Content

  1. Overview
  2. Manual
  3. License
  4. Service and support

Overview

This is "another" JNI wrapping library, but in some regards quite different.

The real memory / JNI access semantics is delegated to some concrete service implementation. Actually we dont'tprovide one of our own, the default implementation we use is JNA.

The real strength of this library is its high level model of JNI access, allowing more easy and understandable API's. We feel that at this point most of the candidates we reviewed are quite poor or hard to understand.

The design goals were as follows:

  • Provide access to all levels of a native interface. As we can not forsee what different libaries and systems will work together, access is provided from bottom up using the plain address as the least common denominator.
  • Provide clean abstractions
  • Differentiate between a "pointer" or "memory handle" and the data semantics. This allows to work as well using plain pointer arithmetics as object state access.

The high level model provides the following abstractions:

  • Interface
  • The concrete implementation for accessing JNI. This interface is quite small and allows for the adaption of many JNI libraries out there. It should not be to hard to create one of your own.
  • Library
  • The loadable library providing state and behavior.
  • Function
  • The behavior part of the library, accepting parameters and return results.
  • Handle
  • An abstraction of a pointer, without any data specific features. This could be viewed as a plain wrapper around an address, another representation for "void *".
  • Object
  • The data referenced by a handle (or pointer). The object deals with the bytes referenced by the handle. The object defines the semantics of how to interpret the memory referenced by the handle. The handle defines "where" to find data, the object defines "what" data you will find there - it deals only with the "dereferenced" part of the handle. On the C side, the object is always represented by the handle / pointer. This means that an object is always used and forwarded by reference. Do not confuse this with the handling on primitive types. In this code snippet
    int a;
    NativeInt b = new NativeInt();
    ...
    int result = function.invoke(Integer.class, a, b);
    ...
    
    the function is called using the value of "a" and a pointer to "b".

This library currently does not provide any convenience for some aspects of native calls, for example

  • primitive arrays
  • float / double support
  • String encodings
  • callbacks
  • maybe other features we didn't need until now and so are not aware of..
The underlying infrastructure (JNA) may handle such constructs, but as long as the semantics is not defined on this library level, you should expect unportable behavior. We propose, even if not convenient, to express all call semantics using documented features only. Feel free to ask for the inclusion of more library features.

Manual

Common scenarios

Call functions with primitive types

The most simple calls use only primitive types. These are mapped by the "invoke" itself using builtin rules to target the C types.

The C part may look like this:

int intValue;
float floatValue;
...
int result = function(intValue, floatValue);
...

An equivalent call from Java would look like:

int intValue;
float floatValue;
...
int result = function.invoke(Integer.class, intValue, floatValue);
...
...
This seems straightforward....

Call functions with String parameters

The first problems are expected when dealing with strings or char* on the C side. But no problem here:

The C part may look like this:

char* string = "test";
...
int result = function(string);
...

An equivalent call from Java would look like:

String string = "test";
...
int result = function.invoke(Integer.class, string);
...
String semantics are handled internally. Even the platform wide character features can be accessed quite easy - simply wrap the String in a "WideString" to indicate the special treatment on invocation marshalling.
String string = "test";
WideString wideString = new WideString(string);
...
int result = function.invoke(Integer.class, wideString);
...
In much the same way you can request a wide string conversion on the result.
...
WideString result = function.invoke(WideString.class);
String string = result.toString();
...

Call with parameters "by reference" (out parameters)

Other functions return values via "out" parameters, via references to C memory.

The C part may look like this:

int value;

...
int result = function(&value);
if (value == 42) {
	...
}
...

An equivalent call from Java would look like:

NativeInt value = new NativeInt();

int result = function.invoke(Integer.class, value);
if (value.intValue() == 42) {
	...
}
...
The NativeObject is allocated in C memory and forwarded "by reference". This call pattern stays the same regardless of the complexity of the NativeObject - so this is the call for your Java side definition of the C struct:
// allocates memory for the struct in C  
MyStruct value = new MyStruct();

// call function with a pointer to the new struct
int result = function.invoke(Integer.class, value);
...

Wrapping pointers returned from out parameters

With C you find quite often a pointer to a newly create memory chunk returned via an out parameter.
my_struct *value;
...
function(&value);
int a = value->a;
...
This is one of the rare occasions you will deal with the "NativeReference" directly. If we follow exactly the pattern we have used so far we get:
// allocate memory for holding a pointer to a struct
NativeReference ptrValue = new NativeReference(MyStruct.META);
// call the function with the pointer to the pointer 
int result = function.invoke(Integer.class, ptrValue);
// dereference the result...
MyStruct value = (MyStruct)ptrValue.getValue();
Here's a common pattern to manage "transparent" handles that do not designate data structures you should deal with - for sure you can declare a NativeVoid subclass of you own for better readability or to add methods.
// allocate memory for holding a void pointer
NativeReference ptrValue = new NativeReference(NativeVoid.META);
// call the function with the pointer to the pointer 
int result = function.invoke(Integer.class, value);
// dereference the result...
NativeVoid value = (NativeVoid)ptrValue.getValue();

Wrapping a pointer returned as function result

Many functions return handles to newly created objects. Wrapping this result to the INativeObject framework is simply a matter of declaration:
NativeVoid result = function.invoke(NativeVoid.class, ...);
You have just wrapped a "void *" (void pointer)! Notice how you "hide" the C reference or pointer semantics. The less you think about it, the more it is intuitive :-). Using this code will return "null" in case of a "0" address.

NativeVoid is a "stateless" object, declaring to be of size "0". This is often the case with transparent handles to some proprietary / private information. Only the handle itself is used to manipulate state via the associated library functions.

In much the same way you can create a NativeStruct instances, holding public state information and allowing easy access to it.

Assume you have defined a NativeStruct subclass "MyStruct" of any memory layout. The above code sequence changes to

MyStruct structObj = function.invoke(MyStruct.class, ...);
Behind this code is a simple two step process. You can do it manually if the default is not exactly what you want, for example in some cases where an address of "-1" means failure.
  • Create the INativeHandle wrapper. This object represents a memory address and implements some access primitives
  • Use the INativeHandle to build the INativeObject wrapper that is situated at this memory location. This can be any of the INativeObject subclasses, such as a primitive, composite or even void representation.
int address = function.invoke(Integer.class, ...);
if (address == 0) {
	return null;
}
INativeHandle handle = NativeInterface.get().createHandle(address);
return NativeVoid.META.createNative(handle);

The pointer, represented by the INativeHandle is wrapped by a NativeObject which is what is referenced by the pointer.

Primitive arrays and buffers

You can call a native function using primitive arrays the same way as using plain primitives. The basic marshalling will map the values in both direction, so you see C side changes in the Java array directly. Conceptually you should treat such a call as follows: The memory is managed by the library. Upon entry, memory is allocated in C address space, the data is copied. The C function is called with the pointer to the newly allocated memory. After termination of the function, memory is copied back to Java address space, and the newly allocated memory is discarded. Access to the memory in C address space is no longer valid! So, if you want to provide a buffer to C for use even outside the function call this is not the correct way! You must use NativeObject instances, for example NativeBuffer, to force C memory allocation resident in memory (controlled by your application). To optimize the marshalling between Java and C you may use direct "ByteBuffer" objects. Using this objects marshalling MAY be optimized by the underlying implementation to share memory between Java and C. In any case you may use a NativeBuffer and access the memory allocated in heap space from both sides. NativeBuffer can be seen as an explicit equivalent to a DirectBuffer.

License

/*
 * Copyright (c) 2008, intarsys consulting GmbH
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * - Neither the name of intarsys nor the names of its contributors may be used
 *   to endorse or promote products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

Service & Support

If you need further support, feel free to contact us. intarsys consulting GmbH
Bahnhofplatz 8
76137 Karlsruhe
Fon +49 721 38479-0
Fax +49 721 38479-60
info@intarsys.de
www.intarsys.de

For service and support contact

  1. EMail support: support@intarsys.de
libisnativec-java-5.3.20100629/src/com/0000755000175000017500000000000011412332242017153 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/com/sun/0000755000175000017500000000000011412332242017760 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/com/sun/jna/0000755000175000017500000000000011412332242020530 5ustar twernertwernerlibisnativec-java-5.3.20100629/src/com/sun/jna/FastMemory.java0000644000175000017500000000050211337471632023473 0ustar twernertwernerpackage com.sun.jna; public class FastMemory extends Pointer { protected long size; // Size of the malloc'ed space public FastMemory() { super(); } public FastMemory(int size) { super(Memory.malloc(size)); } @Override protected void finalize() { Memory.free(peer); peer = 0; } } libisnativec-java-5.3.20100629/src/com/sun/jna/JNATools.java0000644000175000017500000000023511412327706023035 0ustar twernertwernerpackage com.sun.jna; public class JNATools { public static long getPeer(Pointer p) { if (p == null) { return 0; } return p.peer; } } libisnativec-java-5.3.20100629/resource/0000755000175000017500000000000011412332242017435 5ustar twernertwernerlibisnativec-java-5.3.20100629/resource/META-INF/0000755000175000017500000000000011412332242020575 5ustar twernertwernerlibisnativec-java-5.3.20100629/resource/META-INF/services/0000755000175000017500000000000011412332242022420 5ustar twernertwernerlibisnativec-java-5.3.20100629/resource/META-INF/services/de.intarsys.nativec.api.INativeInterface0000644000175000017500000000013310753101724032170 0ustar twernertwernerde.intarsys.nativec.jna.JnaNativeInterface # The JNA based native interface implementationlibisnativec-java-5.3.20100629/lib/0000755000175000017500000000000011412332242016354 5ustar twernertwernerlibisnativec-java-5.3.20100629/javadoc/0000755000175000017500000000000011412332242017215 5ustar twernertwerner