Java Code Examples for java.util.prefs.Preferences#putByteArray()

The following examples show how to use java.util.prefs.Preferences#putByteArray() . 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: FallbackProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Void call() throws Exception { // encryption changing
    LOG.fine("encryption changing");
    Map<String,char[]> saved = new HashMap<String,char[]>();
    Preferences prefs = prefs();
    for (String k : prefs.keys()) {
        if (k.endsWith(DESCRIPTION)) {
            continue;
        }
        byte[] ciphertext = prefs.getByteArray(k, null);
        if (ciphertext == null) {
            continue;
        }
        saved.put(k, encryption.decrypt(ciphertext));
    }
    LOG.log(Level.FINE, "reencrypting keys: {0}", saved.keySet());
    encryption.encryptionChanged();
    for (Map.Entry<String,char[]> entry : saved.entrySet()) {
        prefs.putByteArray(entry.getKey(), encryption.encrypt(entry.getValue()));
    }
    LOG.fine("encryption changing finished");
    return null;
}
 
Example 2
Source File: RecentFiles.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static void store(List<HistoryItem> history) {
    Preferences _prefs = getPrefs();
    for (int i = 0; i < history.size(); i++) {
        HistoryItem hi = history.get(i);
        if ((hi.id != i) && (hi.id >= history.size())) {
            _prefs.remove(PROP_URL_PREFIX + hi.id);
            _prefs.remove(PROP_ICON_PREFIX + hi.id);
        }
        hi.id = i;
        _prefs.put(PROP_URL_PREFIX + i, hi.getPath());
        if (hi.getIconBytes() == null) {
            _prefs.remove(PROP_ICON_PREFIX + i);
        } else {
            _prefs.putByteArray(PROP_ICON_PREFIX + i, hi.getIconBytes());
        }
    }
    LOG.log(Level.FINE, "Stored");
}
 
Example 3
Source File: FileSystemFontProvider.java    From PdfBox-Android with Apache License 2.0 6 votes vote down vote up
private void saveCache()
{
    // Get the preferences database for this package.
    Preferences prefs = Preferences.userNodeForPackage(FileSystemFontProvider.class);

    // To save, write the object to a byte array.
    try
    {
        for (FSFontInfo fontInfo : fontInfoList)
        {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
            // write it to the stream
            objectOut.writeObject(fontInfo);
            prefs.putByteArray(fontInfo.file.getAbsolutePath(), byteOut.toByteArray());
        }
    }
    catch (IOException e)
    {
        Log.e("PdfBox-Android", "Could not write to font cache", e);
    }
    Log.w("PdfBox-Android",
        "Finished building font cache, found " + fontInfoList.size() + " fonts");
}
 
Example 4
Source File: TestXMLStore.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setup() throws IOException {
  storeFile = tempFolder.newFile().getAbsolutePath();
  XMLStore store = XMLStore.createFromFile(storeFile, null);
  PreferencesExt prefs = store.getPreferences();
  prefs.putDouble("testD", 3.14157);
  prefs.putFloat("testF", 1.23456F);
  prefs.putLong("testL", 12345678900L);
  prefs.putInt("testI", 123456789);
  prefs.put("testS", "youdBeDeadbyNow");
  prefs.putBoolean("testB", true);

  byte[] barr = new byte[3];
  barr[0] = 1;
  barr[1] = 2;
  barr[2] = 3;
  prefs.putByteArray("testBA", barr);

  Preferences subnode = prefs.node("SemperUbi");
  subnode.putDouble("testD", 3.14158);
  subnode.putFloat("testF", 1.23457F);
  subnode.putLong("testL", 12345678901L);
  subnode.putInt("testI", 123456780);
  subnode.put("testS", "youdBeLivebyNow");
  subnode.putBoolean("testB", false);

  byte[] barr2 = new byte[3];
  barr2[0] = 2;
  barr2[1] = 3;
  barr2[2] = 4;
  subnode.putByteArray("testBA", barr2);

  store.save();
}
 
Example 5
Source File: FallbackProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean _save(String key, char[] password, String description) {
    Preferences prefs = prefs();
    try {
        prefs.putByteArray(key, encryption.encrypt(password));
    } catch (Exception x) {
        LOG.log(Level.FINE, "failed to encrypt password for " + key, x);
        return false;
    }
    if (description != null) {
        // Preferences interface gives no access to *.properties comments, so:
        prefs.put(key + DESCRIPTION, description);
    }
    return true;
}
 
Example 6
Source File: MasterPasswordEncryption.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override boolean enabled() {
    if (Boolean.getBoolean("netbeans.keyring.no.master")) {
        LOG.fine("master password encryption disabled");
        return false;
    }
    if (GraphicsEnvironment.isHeadless()) {
        LOG.fine("disabling master password encryption in headless mode");
        return false;
    }
    try {
        KEY_FACTORY = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
        encrypt = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        decrypt = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        Preferences prefs = NbPreferences.forModule(Keyring.class);
        Utils.goMinusR(prefs);
        String saltKey = "salt"; // NOI18N
        byte[] salt = prefs.getByteArray(saltKey, null);
        if (salt == null) {
            salt = new byte[36];
            new SecureRandom().nextBytes(salt);
            prefs.putByteArray(saltKey, salt);
        }
        PARAM_SPEC = new PBEParameterSpec(salt, 20);
        LOG.warning("Falling back to master password encryption; " +
                "add -J-Dorg.netbeans.modules.keyring.level=0 to netbeans.conf to see why native keyrings could not be loaded");
        return true;
    } catch (Exception x) {
        LOG.log(Level.INFO, "Cannot initialize security using " + ENCRYPTION_ALGORITHM, x);
        return false;
    }
}
 
Example 7
Source File: ProjectTrust.java    From netbeans with Apache License 2.0 5 votes vote down vote up
ProjectTrust(Preferences prefs) {
    byte[] buf = prefs.getByteArray(KEY_SALT, null);
    if (buf == null) {
        buf = new byte[16];
        new Random().nextBytes(buf);
        prefs.putByteArray(KEY_SALT, buf);
    }
    salt = buf;
    projectTrust = prefs.node(NODE_PROJECT);
    key = new SecretKeySpec(salt, HMAC_SHA256);
}
 
Example 8
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testBase64() {
    Preferences orig = Preferences.userRoot().node(getName());
    assertNull("Original contains value", orig.get("key-1", null));
    Preferences test = ProxyPreferencesImpl.getProxyPreferences(this, orig);
    test.putByteArray("key-1", "however you like it".getBytes());
    assertEquals("Wrong value", "however you like it", new String(test.getByteArray("key-1", null)));
}
 
Example 9
Source File: FormattingCustomizerPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void deepCopy(Preferences from, Preferences to) throws BackingStoreException {
    for(String kid : from.childrenNames()) {
        Preferences fromKid = from.node(kid);
        Preferences toKid = to.node(kid);
        deepCopy(fromKid, toKid);
    }
    for(String key : from.keys()) {
        String value = from.get(key, null);
        if (value == null) continue;

        Class type = guessType(value);
        if (Integer.class == type) {
            to.putInt(key, from.getInt(key, -1));
        } else if (Long.class == type) {
            to.putLong(key, from.getLong(key, -1L));
        } else if (Float.class == type) {
            to.putFloat(key, from.getFloat(key, -1f));
        } else if (Double.class == type) {
            to.putDouble(key, from.getDouble(key, -1D));
        } else if (Boolean.class == type) {
            to.putBoolean(key, from.getBoolean(key, false));
        } else if (String.class == type) {
            to.put(key, value);
        } else /* byte [] */ {
            to.putByteArray(key, from.getByteArray(key, new byte [0]));
        }
    }
}
 
Example 10
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testBase64() {
    Preferences orig = Preferences.userRoot().node(getName());
    assertNull("Original contains value", orig.get("key-1", null));
    Preferences test = ProxyPreferencesImpl.getProxyPreferences(this, orig);
    test.putByteArray("key-1", "however you like it".getBytes());
    assertEquals("Wrong value", "however you like it", new String(test.getByteArray("key-1", null)));
}
 
Example 11
Source File: ProxyPreferencesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testBase64() {
    Preferences orig = Preferences.userRoot().node(getName());
    assertNull("Original contains value", orig.get("key-1", null));
    Preferences test = ProxyPreferences.getProxyPreferences(this, orig);
    test.putByteArray("key-1", "however you like it".getBytes());
    assertEquals("Wrong value", "however you like it", new String(test.getByteArray("key-1", null)));
}
 
Example 12
Source File: PreferencesHandler.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static void setPreference( String preferenceKey, byte[] value ) {
    if (preferencesDb != null) {
        preferencesDb.setPreference(preferenceKey, value);
        return;
    }
    Preferences preferences = Preferences.userRoot().node(PREFS_NODE_NAME);
    if (value != null) {
        preferences.putByteArray(preferenceKey, value);
    } else {
        preferences.remove(preferenceKey);
    }
}
 
Example 13
Source File: ReporterPreferencesManager.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public void storeBoundsForWindow(Class cls, Rectangle rect) {
    if (userName == null) {
        throw new IllegalStateException(NO_USERNAME_MSG);
    }
    Preferences prefs = Preferences.userNodeForPackage(cls).node(userName);
    prefs.putByteArray(getKey(cls, BOUNDS_KEY), object2Bytes(rect));
}
 
Example 14
Source File: HuffmanTree.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compute a frequency table from an InputStream and save a compacted representation of this
 * table in the system preferences. (To be used later by @see #useStandardTable(String) )
 * 
 * @param name
 *            name to give the table.
 * @param in
 *            InputStream
 * @return true on success
 * @throws Exception
 */
public static boolean CreateStandardTableFromStream(String name, InputStream in)
	throws Exception{
	int[] tbl = new int[TABLESIZE];
	while (in.available() != 0) {
		int c = in.read();
		tbl[c]++;
	}
	byte[] dest = HuffmanTree.compactTable(tbl);
	Preferences pref = Preferences.userNodeForPackage(Huff.class);
	Preferences node = pref.node("StandardTables");
	node.putByteArray(name, dest);
	pref.flush();
	return true;
}
 
Example 15
Source File: DefaultCredentialManager.java    From triplea with GNU General Public License v3.0 4 votes vote down vote up
private static void saveMasterPassword(
    final Preferences preferences, final char[] masterPassword) {
  preferences.putByteArray(PREFERENCE_KEY_MASTER_PASSWORD, encodeCharsToBytes(masterPassword));
}