View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util.platform.mac;
6   
7   import com.sun.jna.Pointer;
8   import com.sun.jna.platform.mac.CoreFoundation.CFStringRef;
9   
10  import oshi.annotation.concurrent.ThreadSafe;
11  import oshi.util.Constants;
12  
13  /**
14   * CF String retrieving
15   */
16  @ThreadSafe
17  public final class CFUtil {
18  
19      private CFUtil() {
20      }
21  
22      /**
23       * /** Convert a pointer to a CFString into a String.
24       *
25       * @param result Pointer to the CFString
26       * @return a CFString or "unknown" if it has no value
27       */
28      public static String cfPointerToString(Pointer result) {
29          return cfPointerToString(result, true);
30      }
31  
32      /**
33       * Convert a pointer to a CFString into a String.
34       *
35       * @param result        Pointer to the CFString
36       * @param returnUnknown Whether to return the "unknown" string
37       * @return a CFString including a possible empty one if {@code returnUnknown} is false, or "unknown" if it is true
38       */
39      public static String cfPointerToString(Pointer result, boolean returnUnknown) {
40          String s = "";
41          if (result != null) {
42              CFStringRef cfs = new CFStringRef(result);
43              s = cfs.stringValue();
44          }
45          if (returnUnknown && s.isEmpty()) {
46              return Constants.UNKNOWN;
47          }
48          return s;
49      }
50  
51  }