Class Registry


  • public class Registry
    extends java.lang.Object
    This class can be used to create, delete, read, write and list registry values. Currently only Windows and Palm OS are supported.

    Note that these methods don't work at Java desktop.

    Here's a full sample for Windows:
       ListBox lb;
       private void log(String s)
       {
          lb.add(s);
          lb.selectLast();
       }
    
       public void initUI()
       {
          ByteArrayStream bas = new ByteArrayStream(10);
          DataStreamLE ds = new DataStreamLE(bas);
    
          lb = new ListBox();
          lb.enableHorizontalScroll();
          add(lb, LEFT,TOP,FILL,FILL);
          try
          {
             log("adding int 0x12345678");
             Registry.set(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","IntKey",0x12345678);
             log("added. now retrieving...");
             int ires = Registry.getInt(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","IntKey");
             log("int retrieved: "+Convert.unsigned2hex(ires,8));
    
             log("adding string \"Bárbara\"");
             Registry.set(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","StringKey","B�rbara");
             log("added. now retrieving...");
             String sres = Registry.getString(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","StringKey");
             log("string retrieved: "+sres);
    
             double dvalue = 1234.56789;
             log("adding blob double value: "+dvalue);
             ds.writeDouble(dvalue);
             Registry.set(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","BlobKey",bas.toByteArray());
             log("added. now retrieving...");
             byte[] bres = Registry.getBlob(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","BlobKey");
             bas.setBuffer(bres);
             double dres = ds.readDouble();
             log("double retrieved: "+dres);
    
             log("deleting string value");
             Registry.delete(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","StringKey");
             log("deleted. trying to retrieve...");
             try
             {
                sres = Registry.getString(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","StringKey");
                log("Failure! delete didn't work");
             }
             catch (ElementNotFoundException enfe)
             {
                log("Success: the deleted key was not found (as expected)");
             }
    
             log("deleting the whole key");
             Registry.delete(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test");
             log("deleted. trying to retrieve...");
             try
             {
                sres = Registry.getString(Registry.HKEY_CURRENT_USER, "Software\\TotalCross\\appSettings\\Test","IntKey");
                log("Failure! delete didn't work");
             }
             catch (ElementNotFoundException enfe)
             {
                log("Success: the deleted key was not found (as expected)");
             }
             log("Tests finished.");
          }
          catch (Exception e)
          {
             MessageBox.showException(e, true);
          }
       }
     
    And here's a full sample for Palm OS.
       ListBox lb;
       private void log(String s)
       {
          lb.add(s);
          lb.selectLast();
       }
    
       public void initUI()
       {
          ByteArrayStream bas = new ByteArrayStream(10);
          DataStreamLE ds = new DataStreamLE(bas);
    
          lb = new ListBox();
          lb.enableHorizontalScroll();
          add(lb, LEFT,TOP,FILL,FILL);
          try
          {
             // retrieves the ROM version from the Feature manager
             int ret = Registry.getInt(Registry.FEATURE, "psys", "1");
             log("rom version: "+Convert.unsigned2hex(ret,8));
    
             log("adding int 0x12345678");
             Registry.set(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"1",0x12345678);
             log("added. now retrieving...");
             int ires = Registry.getInt(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"1");
             log("int retrieved: "+Convert.unsigned2hex(ires,8));
    
             log("adding string \"Bárbara\"");
             Registry.set(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"2","B�rbara");
             log("added. now retrieving...");
             String sres = Registry.getString(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"2");
             log("string retrieved: "+sres);
    
             double dvalue = 1234.56789;
             log("adding blob double value: "+dvalue);
             ds.writeDouble(dvalue);
             Registry.set(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"3",bas.toByteArray());
             log("added. now retrieving...");
             byte[] bres = Registry.getBlob(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"3");
             bas.setBuffer(bres);
             double dres = ds.readDouble();
             log("double retrieved: "+dres);
    
             log("deleting string value");
             Registry.delete(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"2");
             log("deleted. trying to retrieve...");
             try
             {
                sres = Registry.getString(Registry.UNSAVED_PREFERENCES, Settings.applicationId,"2");
                log("Failure! delete didn't work");
             }
             catch (ElementNotFoundException enfe)
             {
                log("Success: the deleted key was not found (as expected)");
             }
    
             log("Tests finished.");
          }
          catch (Exception e)
          {
             MessageBox.showException(e, true);
          }
       }
     
    Since:
    TotalCross 1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int FEATURE
      PalmOS-specific value to be used in the hk parameter.
      static int HKEY_CLASSES_ROOT
      Windows-specific value to be used in the hk parameter.
      static int HKEY_CURRENT_USER
      Windows-specific value to be used in the hk parameter.
      static int HKEY_LOCAL_MACHINE
      Windows-specific value to be used in the hk parameter.
      static int HKEY_USERS
      Windows-specific value to be used in the hk parameter.
      static int SAVED_PREFERENCES
      PalmOS-specific value to be used in the hk parameter.
      static int UNSAVED_PREFERENCES
      PalmOS-specific value to be used in the hk parameter.
    • Constructor Summary

      Constructors 
      Constructor Description
      Registry()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean delete​(int hk, java.lang.String key)
      Deletes a key with all its values.
      static boolean delete​(int hk, java.lang.String key, java.lang.String value)
      Deletes a specific value.
      static byte[] getBlob​(int hk, java.lang.String key, java.lang.String value)
      Returns a String at the given location.
      static int getInt​(int hk, java.lang.String key, java.lang.String value)
      Returns an integer at the given location.
      static java.lang.String getString​(int hk, java.lang.String key, java.lang.String value)
      Returns a String at the given location.
      static java.lang.String[] list​(int hk, java.lang.String key)
      Lists all children keys of the given key.
      static void set​(int hk, java.lang.String key, java.lang.String value, byte[] data)
      Sets a byte array (blob) value at the given location.
      static void set​(int hk, java.lang.String key, java.lang.String value, int data)
      Sets an integer value at the given location.
      static void set​(int hk, java.lang.String key, java.lang.String value, java.lang.String data)
      Sets a String value at the given location.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HKEY_CLASSES_ROOT

        public static final int HKEY_CLASSES_ROOT
        Windows-specific value to be used in the hk parameter.
        See Also:
        Constant Field Values
      • HKEY_CURRENT_USER

        public static final int HKEY_CURRENT_USER
        Windows-specific value to be used in the hk parameter.
        See Also:
        Constant Field Values
      • HKEY_LOCAL_MACHINE

        public static final int HKEY_LOCAL_MACHINE
        Windows-specific value to be used in the hk parameter.
        See Also:
        Constant Field Values
      • HKEY_USERS

        public static final int HKEY_USERS
        Windows-specific value to be used in the hk parameter.
        See Also:
        Constant Field Values
      • FEATURE

        public static final int FEATURE
        PalmOS-specific value to be used in the hk parameter. Note that the Features database is read-only, and trying to write to it will throw an exception.
        See Also:
        Constant Field Values
      • SAVED_PREFERENCES

        public static final int SAVED_PREFERENCES
        PalmOS-specific value to be used in the hk parameter.
        See Also:
        Constant Field Values
      • UNSAVED_PREFERENCES

        public static final int UNSAVED_PREFERENCES
        PalmOS-specific value to be used in the hk parameter.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Registry

        public Registry()
    • Method Detail

      • getInt

        public static int getInt​(int hk,
                                 java.lang.String key,
                                 java.lang.String value)
                          throws ElementNotFoundException,
                                 java.lang.Exception
        Returns an integer at the given location.
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. In Palm OS, can be FEATURE, SAVED_PREFERENCES or UNSAVED_PREFERENCES.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test". In Palm OS, its the application id (E.G.: Settings.applicationId).
        value - In Windows, is the value to be retrieved (E.G.: "IntData"). In Palm OS, is an integer representing the number of the preference (from 0 to 32767).
        Throws:
        ElementNotFoundException - if the hk, key or value was not found.
        java.lang.Exception - if there was an error when retrieving the data.
      • getString

        public static java.lang.String getString​(int hk,
                                                 java.lang.String key,
                                                 java.lang.String value)
                                          throws ElementNotFoundException,
                                                 java.lang.Exception
        Returns a String at the given location. In Palm OS you cannot retrieve a string FEATURE.
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. In Palm OS, can be FEATURE, SAVED_PREFERENCES or UNSAVED_PREFERENCES.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test". In Palm OS, its the application id (E.G.: Settings.applicationId).
        value - In Windows, is the value to be retrieved (E.G.: "IntData"). In Palm OS, is an integer representing the number of the preference (from 0 to 32767).
        Throws:
        ElementNotFoundException - if the hk, key or value was not found.
        java.lang.Exception - if there was an error when retrieving the data.
      • getBlob

        public static byte[] getBlob​(int hk,
                                     java.lang.String key,
                                     java.lang.String value)
                              throws ElementNotFoundException,
                                     java.lang.Exception
        Returns a String at the given location. In Palm OS you cannot retrieve a string FEATURE. This can be used to read long and double types, just create a ByteArrayStream, attach a DataStreamLE to it and call readDouble/readLong.
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. In Palm OS, can be FEATURE, SAVED_PREFERENCES or UNSAVED_PREFERENCES.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test". In Palm OS, its the application id (E.G.: Settings.applicationId).
        value - In Windows, is the value to be retrieved (E.G.: "IntData"). In Palm OS, is an integer representing the number of the preference (from 0 to 32767).
        Throws:
        ElementNotFoundException - if the hk, key or value was not found.
        java.lang.Exception - if there was an error when retrieving the data.
      • set

        public static void set​(int hk,
                               java.lang.String key,
                               java.lang.String value,
                               int data)
                        throws java.lang.Exception
        Sets an integer value at the given location. If the key or value doesn't exists, it is created.
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. In Palm OS, can be FEATURE, SAVED_PREFERENCES or UNSAVED_PREFERENCES.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test". In Palm OS, its the application id (E.G.: Settings.applicationId).
        value - In Windows, is the value to be retrieved (E.G.: "IntData"). In Palm OS, is an integer representing the number of the preference (from 0 to 32767).
        data - The data to be written.
        Throws:
        java.lang.Exception - if there was an error when setting the data.
      • set

        public static void set​(int hk,
                               java.lang.String key,
                               java.lang.String value,
                               java.lang.String data)
                        throws java.lang.Exception
        Sets a String value at the given location. If the key or value doesn't exists, it is created. Note that the String is created as ASCII, not as UNICODE. To store a UNICODE string, use a byte array (blob).
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. In Palm OS, can be FEATURE, SAVED_PREFERENCES or UNSAVED_PREFERENCES.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test". In Palm OS, its the application id (E.G.: Settings.applicationId).
        value - In Windows, is the value to be retrieved (E.G.: "IntData"). In Palm OS, is an integer representing the number of the preference (from 0 to 32767).
        data - The data to be written. In Palm OS, its limited to 64KB.
        Throws:
        java.lang.Exception - if there was an error when setting the data.
      • set

        public static void set​(int hk,
                               java.lang.String key,
                               java.lang.String value,
                               byte[] data)
                        throws java.lang.Exception
        Sets a byte array (blob) value at the given location. If the key or value doesn't exists, it is created.
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. In Palm OS, can be FEATURE, SAVED_PREFERENCES or UNSAVED_PREFERENCES.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test". In Palm OS, its the application id (E.G.: Settings.applicationId).
        value - In Windows, is the value to be retrieved (E.G.: "IntData"). In Palm OS, is an integer representing the number of the preference (from 0 to 32767).
        data - The data to be written. In Palm OS, its limited to 64KB.
        Throws:
        java.lang.Exception - if there was an error when setting the data.
      • delete

        public static boolean delete​(int hk,
                                     java.lang.String key,
                                     java.lang.String value)
        Deletes a specific value.
      • delete

        public static boolean delete​(int hk,
                                     java.lang.String key)
        Deletes a key with all its values. Does not work on Palm OS, which requires that you pass the value number too.
      • list

        public static java.lang.String[] list​(int hk,
                                              java.lang.String key)
        Lists all children keys of the given key. Works only in Windows platforms. Note that only keys are listed, and not values nor data.
        Parameters:
        hk - In Windows, can be HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS.
        key - In Windows, its the full path to the key (E.G.: "Software\\TotalCross\\appSettings\\Test".