001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Sergey V. Kuksenko
019: * @version $Revision$
020: */package org.apache.harmony.awt.nativebridge;
021:
022: import org.apache.harmony.misc.accessors.AccessorFactory;
023: import org.apache.harmony.misc.accessors.MemoryAccessor;
024:
025: public class NativeBridge {
026:
027: private NativeBridge() {
028: }
029:
030: private static final NativeBridge instance = new NativeBridge();
031: private static final MemoryAccessor memAccess = AccessorFactory
032: .getMemoryAccessor();
033: public static final int ptrSize = memAccess.getPointerSize();
034: public static final boolean is64 = (ptrSize == 8);
035:
036: /**
037: * Factory method returns the only instance of the NativeBridge
038: */
039: public static NativeBridge getInstance() {
040: return instance;
041: }
042:
043: /**
044: * Int8Pointer factory methods
045: */
046:
047: /**
048: * Creates pointer to 1-byte data type with given size.
049: * @param size amount of memory in 1-bytes units that should be allocated.
050: * @param direct <code>true</code> for native
051: * memory usage, <code>false</code> - for java array usage
052: */
053: public Int8Pointer createInt8Pointer(int size, boolean direct) {
054: return new Int8Pointer(size, direct);
055: }
056:
057: /**
058: * Creates modified UTF8 encoded copy of String object in native heap or in java array.
059: * depending on <code>direct</code> parameter.
060: * @param str - original string
061: * @param direct <code>true</code> to create string in native
062: * memory, <code>false</code> - using java array
063: */
064: public Int8Pointer createStringUTF(String str, boolean direct) {
065: return createInt8Pointer(str, direct);
066: }
067:
068: /**
069: * Creates modified UTF8 encoded copy of String object in native heap or in java array.
070: * depending on <code>direct</code> parameter.
071: * @param str - original string
072: * @param direct - <code>true</code> to create string in native
073: * memory, <code>false</code> - using java array
074: */
075: public Int8Pointer createInt8Pointer(String str, boolean direct) {
076: Int8Pointer p = new Int8Pointer(str.length() * 3 + 2, direct);
077: p.setStringUTF(str);
078: return p;
079: }
080:
081: /**
082: * Creates pointer to 1-byte data type.
083: * @param addr - native memory address
084: */
085: public Int8Pointer createInt8Pointer(long addr) {
086: if (addr == 0) {
087: return null;
088: }
089: return new Int8Pointer(addr);
090: }
091:
092: /**
093: * Creates pointer to 1-byte data type using some void pointer object.
094: * @param elementPointer pointer object that should be wrapped
095: */
096: public Int8Pointer createInt8Pointer(VoidPointer elementPointer) {
097: return new Int8Pointer(elementPointer);
098: }
099:
100: /**
101: * Int16Pointer factory methods
102: */
103: /**
104: * Creates pointer to 2-byte data type with given size.
105: * @param size amount of memory in 2-bytes units that should be allocated.
106: * @param direct <code>true</code> for native
107: * memory usage, <code>false</code> - for java array usage
108: */
109: public Int16Pointer createInt16Pointer(int size, boolean direct) {
110: return new Int16Pointer(size, direct);
111: }
112:
113: /**
114: * Creates pointer to 2-byte data type using some void pointer object.
115: * @param elementPointer pointer object that should be wrapped
116: */
117: public Int16Pointer createInt16Pointer(VoidPointer elementPointer) {
118: return new Int16Pointer(elementPointer);
119: }
120:
121: /**
122: * Creates pointer to 2-byte data type.
123: * @param addr - native memory address
124: */
125: public Int16Pointer createInt16Pointer(long addr) {
126: if (addr == 0) {
127: return null;
128: }
129: return new Int16Pointer(addr);
130: }
131:
132: /**
133: * Creates UTF16 (Unicode) copy of String object.
134: * @param str - original string
135: * @param direct - <code>true</code> to create string in native
136: * memory, <code>false</code> - using java array
137: */
138: public Int16Pointer createInt16Pointer(String str, boolean direct) {
139: Int16Pointer s = this .createInt16Pointer(str.length() + 2,
140: direct);
141: s.setString(str);
142: return s;
143: }
144:
145: /**
146: * Creates UTF16 (Unicode) copy of String object.
147: * @param str - original string
148: * @param direct - <code>true</code> to create string in native
149: * memory, <code>false</code> - using java array
150: */
151: public Int16Pointer createString(String str, boolean direct) {
152: return createInt16Pointer(str, direct);
153: }
154:
155: /**
156: * Int32Pointer factory methods
157: */
158: /**
159: * Creates pointer to 4-byte data type with given size.
160: * @param size amount of memory in 4-bytes units that should be allocated.
161: * @param direct <code>true</code> for native
162: * memory usage, <code>false</code> - for java array usage
163: */
164: public Int32Pointer createInt32Pointer(int size, boolean direct) {
165: return new Int32Pointer(size, direct);
166: }
167:
168: /**
169: * Creates pointer to 4-byte data type using some void pointer object.
170: * @param elementPointer pointer object that should be wrapped
171: */
172: public Int32Pointer createInt32Pointer(VoidPointer elementPointer) {
173: return new Int32Pointer(elementPointer);
174: }
175:
176: /**
177: * Creates pointer to 4-byte data type.
178: * @param addr - native memory address
179: */
180: public Int32Pointer createInt32Pointer(long addr) {
181: if (addr == 0) {
182: return null;
183: }
184: return new Int32Pointer(addr);
185: }
186:
187: /**
188: * Int64Pointer factory methods
189: */
190: /**
191: * Creates pointer to 8-byte data type with given size.
192: * @param size amount of memory in 8-bytes units that should be allocated.
193: * @param direct <code>true</code> for native
194: * memory usage, <code>false</code> - for java array usage
195: */
196: public Int64Pointer createInt64Pointer(int size, boolean direct) {
197: return new Int64Pointer(size, direct);
198: }
199:
200: /**
201: * Creates pointer to 8-byte data type using some void pointer object.
202: * @param elementPointer pointer object that should be wrapped
203: */
204: public Int64Pointer createInt64Pointer(VoidPointer elementPointer) {
205: return new Int64Pointer(elementPointer);
206: }
207:
208: /**
209: * Creates pointer to 8-byte data type.
210: * @param addr - native memory address
211: */
212: public Int64Pointer createInt64Pointer(long addr) {
213: if (addr == 0) {
214: return null;
215: }
216: return new Int64Pointer(addr);
217: }
218:
219: /**
220: * FloatPointer factory methods
221: */
222: /**
223: * Creates pointer to float data type with given size.
224: * @param size amount of memory in 4-bytes units that should be allocated.
225: * @param direct <code>true</code> for native
226: * memory usage, <code>false</code> - for java array usage
227: */
228: public FloatPointer createFloatPointer(int size, boolean direct) {
229: return new FloatPointer(size, direct);
230: }
231:
232: /**
233: * Creates pointer to float data type using some void pointer object.
234: * @param elementPointer pointer object that should be wrapped
235: */
236: public FloatPointer createFloatPointer(VoidPointer elementPointer) {
237: return new FloatPointer(elementPointer);
238: }
239:
240: /**
241: * Creates pointer to float data type.
242: * @param addr - native memory address
243: */
244: public FloatPointer createFloatPointer(long addr) {
245: if (addr == 0) {
246: return null;
247: }
248: return new FloatPointer(addr);
249: }
250:
251: /**
252: * FloatPointer factory methods
253: */
254: public DoublePointer createDoublePointer(int size, boolean direct) {
255: return new DoublePointer(size, direct);
256: }
257:
258: /**
259: * Creates pointer to double data type using some void pointer object.
260: * @param elementPointer pointer object that should be wrapped
261: */
262: public DoublePointer createDoublePointer(VoidPointer elementPointer) {
263: return new DoublePointer(elementPointer);
264: }
265:
266: /**
267: * Creates pointer to double data type.
268: * @param addr - native memory address
269: */
270: public DoublePointer createDoublePointer(long addr) {
271: if (addr == 0) {
272: return null;
273: }
274: return new DoublePointer(addr);
275: }
276:
277: /**
278: * PointerPointer factory methods
279: */
280: public PointerPointer createPointerPointer(int size, boolean direct) {
281: return new PointerPointer(size, direct);
282: }
283:
284: public PointerPointer createPointerPointer(long ptrPtr) {
285: return new PointerPointer(ptrPtr);
286: }
287:
288: /**
289: * Creates pointer to <type>pointer using void pointer object.
290: * @param p pointer object that should be wrapped
291: * @param direct <code>true</code> for native memory usage
292: */
293: public PointerPointer createPointerPointer(VoidPointer p,
294: boolean direct) {
295: return new PointerPointer(p, direct);
296: }
297:
298: /**
299: * CLongPointer factory methods
300: */
301: /**
302: * Creates pointer C-lang long data type using some void pointer object.
303: * @param elementPointer pointer object that should be wrapped
304: */
305: public CLongPointer createCLongPointer(VoidPointer elementPointer) {
306: return new CLongPointer(elementPointer);
307: }
308:
309: /**
310: * Creates pointer to C-lang long data type with given size.
311: * @param size amount of memory in 1-bytes units that should be allocated.
312: * @param direct <code>true</code> for native
313: * memory usage, <code>false</code> - for java array usage
314: */
315: public CLongPointer createCLongPointer(int size, boolean direct) {
316: return new CLongPointer(size, direct);
317: }
318:
319: /**
320: * Creates pointer to C-lang long data type.
321: * @param addr - native memory address
322: */
323: public CLongPointer createCLongPointer(long addr) {
324: if (addr == 0) {
325: return null;
326: }
327: return new CLongPointer(addr);
328: }
329: }
|