Java Code Examples for java.util.prefs.Preferences#MAX_VALUE_LENGTH

The following examples show how to use java.util.prefs.Preferences#MAX_VALUE_LENGTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreferencesHandler.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public static void setPreference( String preferenceKey, String[] valuesArray ) {
    if (preferencesDb != null) {
        preferencesDb.setPreference(preferenceKey, valuesArray);
        return;
    }
    Preferences preferences = Preferences.userRoot().node(PREFS_NODE_NAME);
    if (valuesArray != null) {
        int maxLength = Preferences.MAX_VALUE_LENGTH;
        String arrayToString = Stream.of(valuesArray).collect(Collectors.joining(PREF_STRING_SEPARATORS));

        // remove from last if it is too large
        int remIndex = valuesArray.length - 1;
        while( arrayToString.length() > maxLength ) {
            valuesArray[remIndex--] = "";
            arrayToString = Stream.of(valuesArray).collect(Collectors.joining(PREF_STRING_SEPARATORS));
        }

        preferences.put(preferenceKey, arrayToString);
    } else {
        preferences.remove(preferenceKey);
    }
}
 
Example 2
Source File: SaveGame.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
     * Saves a savable in a system-dependent way. Note that only small amounts of data can be saved.
     * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
     * @param dataName A unique name for this savegame, e.g. "save_001"
     * @param data The Savable to save
     */
    public static void saveGame(String gamePath, String dataName, Savable data) {
        Preferences prefs = Preferences.userRoot().node(gamePath);
        BinaryExporter ex = BinaryExporter.getInstance();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            GZIPOutputStream zos = new GZIPOutputStream(out);
            ex.save(data, zos);
            zos.close();
        } catch (IOException ex1) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
            ex1.printStackTrace();
        }
        UUEncoder enc = new UUEncoder();
        String dataString = enc.encodeBuffer(out.toByteArray());
//        System.out.println(dataString);
        if (dataString.length() > Preferences.MAX_VALUE_LENGTH) {
            throw new IllegalStateException("SaveGame dataset too large");
        }
        prefs.put(dataName, dataString);
    }
 
Example 3
Source File: DemoUtils.java    From java-ocr-api with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue) {
    if (newValidValue == null) { 
        return;
    }

    DefaultComboBoxModel comboModel = (DefaultComboBoxModel) combo.getModel();

    int existingIndex = comboModel.getIndexOf(newValidValue);
    if (existingIndex >= 0) { 
        comboModel.removeElementAt(existingIndex);
    }
    comboModel.insertElementAt(newValidValue, 0);
    combo.setSelectedIndex(0);

    StringBuilder entries = new StringBuilder();
    int size = Math.min(comboModel.getSize(), 20); 
    for (int i = 0; i < size; i++) {
        entries.append(comboModel.getElementAt(i));
        if (i != size - 1) { 
            entries.append(DELIMITER);
        }
    }

    while (entries.length() > Preferences.MAX_VALUE_LENGTH) {
        int lastIndex = entries.lastIndexOf(DELIMITER);
        if (lastIndex == -1) {
            break;
        } else {
            entries.delete(lastIndex, entries.length());
        }
    }

    prefs.put(prefKey, entries.toString());
    try {
        prefs.flush();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: Configuration.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * @param wikipedia Wikipedia.
 * @param property Property name.
 * @param subProperty Sub property name.
 * @param value Property value.
 */
public void setSubProperties(
    EnumWikipedia wikipedia,
    String property, String subProperty,
    Properties value) {
  try {
    if (getPreferences(wikipedia) != null) {
      // First, remove the old properties
      if (getPreferences(wikipedia).nodeExists(property)) {
        removeNode(getPreferences(wikipedia), property + "/" + subProperty);
      }

      // Create the new ones
      if (value != null) {
        Preferences node = getPreferences(wikipedia).node(property + "/" + subProperty);
        for (Entry<Object, Object> p : value.entrySet()) {
          String key = p.getKey().toString();
          String keyValue = p.getValue().toString();
          if ((key != null) && (key.length() <= Preferences.MAX_KEY_LENGTH) &&
              (keyValue != null) && (keyValue.length() <= Preferences.MAX_VALUE_LENGTH)) {
            node.put(key, keyValue);
          }
        }
      }
    }
  } catch (BackingStoreException e) {
    //
  }
}