Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package oshi.util.platform.mac;
import oshi.jna.platform.mac.CoreFoundation;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author angju
*
*/
public class CfUtil {
public static final CoreFoundation.CFAllocatorRef ALLOCATOR = CoreFoundation.INSTANCE.CFAllocatorGetDefault();
/**
* Cache cfStrings
*/
private static Map cfStringMap = new ConcurrentHashMap();
/**
* Return a CFStringRef representing a string, caching the result
*
* @param key
* The string, usually a registry key
* @return the corresponding CFString
*/
public static CoreFoundation.CFStringRef getCFString(String key) {
return computeIfAbsent(cfStringMap,key);
}
/**
* Enum values used for number type in CFNumberGetValue(). Use ordinal() to
* fetch the corresponding constant.
*/
public enum CFNumberType {
unusedZero, kCFNumberSInt8Type, kCFNumberSInt16Type, kCFNumberSInt32Type, kCFNumberSInt64Type, kCFNumberFloat32Type, kCFNumberFloat64Type, kCFNumberCharType, kCFNumberShortType, kCFNumberIntType, kCFNumberLongType, kCFNumberLongLongType, kCFNumberFloatType, kCFNumberDoubleType, kCFNumberCFIndexType, kCFNumberNSIntegerType, kCFNumberCGFloatType, kCFNumberMaxType
}
/**
* Convert a pointer representing a Core Foundations LongLong into its long
*
* @param p
* The pointer to a 64-bit integer
* @return The corresponding long
*/
public static long cfPointerToLong(Pointer p) {
LongByReference lbr = new LongByReference();
CoreFoundation.INSTANCE.CFNumberGetValue(p, CFNumberType.kCFNumberLongLongType.ordinal(), lbr);
return lbr.getValue();
}
/**
* Convert a pointer representing a Core Foundations LongLong into its long
*
* @param p
* The pointer to an integer
* @return The corresponding int
*/
public static int cfPointerToInt(Pointer p) {
IntByReference ibr = new IntByReference();
CoreFoundation.INSTANCE.CFNumberGetValue(p, CFNumberType.kCFNumberIntType.ordinal(), ibr);
return ibr.getValue();
}
/**
* Convert a pointer representing a Core Foundations Boolean into its
* boolean
*
* @param p
* The pointer to a boolean
* @return The corresponding boolean
*/
public static boolean cfPointerToBoolean(Pointer p) {
return CoreFoundation.INSTANCE.CFBooleanGetValue(p);
}
/**
* Convert a pointer representing a Core Foundations String into its string
*
* @param p
* The pointer to a CFString
* @return The corresponding string
*/
public static String cfPointerToString(Pointer p) {
if (p == null) {
return "null";
}
long length = CoreFoundation.INSTANCE.CFStringGetLength(p);
long maxSize = CoreFoundation.INSTANCE.CFStringGetMaximumSizeForEncoding(length, CoreFoundation.UTF_8);
if (maxSize == 0) {
maxSize = 1;
}
Pointer buf = new Memory(maxSize);
CoreFoundation.INSTANCE.CFStringGetCString(p, buf, maxSize, CoreFoundation.UTF_8);
return buf.getString(0);
}
/**
* Releases a CF reference. Mandatory when an object is owned (using
* 'create' or 'copy' methods).
*
* @param ref
* The reference to release
*/
public static void release(PointerType ref) {
if (ref != null) {
CoreFoundation.INSTANCE.CFRelease(ref);
}
}
private static CoreFoundation.CFStringRef computeIfAbsent(Map map, String key){
CoreFoundation.CFStringRef result = map.get(key);
if (result == null){
result = CoreFoundation.CFStringRef.toCFString(key);
map.put(key, result);
}
return result;
}
}