abstractpreferences.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,385 行 · 第 1/4 页

JAVA
1,385
字号
        String value = get(key, null);        if (value != null) {            try {                return Double.parseDouble(value);            } catch (NumberFormatException nfe) { /* ignore */ }        }        return defaultVal;    }    /**     * Convenience method for getting the given entry as a float.     * When the string representation of the requested entry can be decoded     * with <code>Float.parseFloat()</code> then that float is returned,     * otherwise the given default float value is returned.     *     * @exception IllegalArgumentException if key is larger then 80 characters     * @exception IllegalStateException if this node has been removed     * @exception NullPointerException if key is null     */    public float getFloat(String key, float defaultVal) {        String value = get(key, null);        if (value != null) {            try {                return Float.parseFloat(value);            } catch (NumberFormatException nfe) { /* ignore */ }        }        return defaultVal;    }    /**     * Convenience method for getting the given entry as an integer.     * When the string representation of the requested entry can be decoded     * with <code>Integer.parseInt()</code> then that integer is returned,     * otherwise the given default integer value is returned.     *     * @exception IllegalArgumentException if key is larger then 80 characters     * @exception IllegalStateException if this node has been removed     * @exception NullPointerException if key is null     */    public int getInt(String key, int defaultVal) {        String value = get(key, null);        if (value != null) {            try {                return Integer.parseInt(value);            } catch (NumberFormatException nfe) { /* ignore */ }        }        return defaultVal;    }    /**     * Convenience method for getting the given entry as a long.     * When the string representation of the requested entry can be decoded     * with <code>Long.parseLong()</code> then that long is returned,     * otherwise the given default long value is returned.     *     * @exception IllegalArgumentException if key is larger then 80 characters     * @exception IllegalStateException if this node has been removed     * @exception NullPointerException if key is null     */    public long getLong(String key, long defaultVal) {        String value = get(key, null);        if (value != null) {            try {                return Long.parseLong(value);            } catch (NumberFormatException nfe) { /* ignore */ }        }        return defaultVal;    }    /**     * Sets the value of the given preferences entry for this node.     * Key and value cannot be null, the key cannot exceed 80 characters     * and the value cannot exceed 8192 characters.     * <p>     * The result will be immediately visible in this VM, but may not be     * immediately written to the backing store.     * <p>     * Checks that key and value are valid, locks this node, and checks that     * the node has not been removed. Then it calls <code>putSpi()</code>.     *     * @exception NullPointerException if either key or value are null     * @exception IllegalArgumentException if either key or value are to large     * @exception IllegalStateException when this node has been removed     */    public void put(String key, String value) {        if (key.length() > MAX_KEY_LENGTH            || value.length() > MAX_VALUE_LENGTH)            throw new IllegalArgumentException("key ("                                               + key.length() + ")"                                               + " or value ("                                               + value.length() + ")"                                               + " to large");        synchronized(lock) {            if (isRemoved())                throw new IllegalStateException("Node removed");            putSpi(key, value);            if (preferenceListeners != null)              fire(new PreferenceChangeEvent(this, key, value));        }                }    /**     * Convenience method for setting the given entry as a boolean.     * The boolean is converted with <code>Boolean.toString(value)</code>     * and then stored in the preference entry as that string.     *     * @exception NullPointerException if key is null     * @exception IllegalArgumentException if the key length is to large     * @exception IllegalStateException when this node has been removed     */    public void putBoolean(String key, boolean value) {        put(key, Boolean.toString(value));    }    /**     * Convenience method for setting the given entry as an array of bytes.     * The byte array is converted to a Base64 encoded string     * and then stored in the preference entry as that string.     * <p>     * Note that a byte array encoded as a Base64 string will be about 1.3     * times larger then the original length of the byte array, which means     * that the byte array may not be larger about 6 KB.     *     * @exception NullPointerException if either key or value are null     * @exception IllegalArgumentException if either key or value are to large     * @exception IllegalStateException when this node has been removed     */    public void putByteArray(String key, byte[] value) {        put(key, encode64(value));    }    /**     * Helper method for encoding an array of bytes as a Base64 String.     */    private static String encode64(byte[] b) {        StringBuffer sb = new StringBuffer((b.length/3)*4);        int i = 0;        int remaining = b.length;        char c[] = new char[4];        while (remaining > 0) {            // Three input bytes are encoded as four chars (6 bits) as            // 00000011 11112222 22333333            c[0] = (char) ((b[i] & 0xFC) >> 2);            c[1] = (char) ((b[i] & 0x03) << 4);            if (remaining >= 2) {                c[1] += (char) ((b[i+1] & 0xF0) >> 4);                c[2] = (char) ((b[i+1] & 0x0F) << 2);                if (remaining >= 3) {                    c[2] += (char) ((b[i+2] & 0xC0) >> 6);                    c[3] = (char) (b[i+2] & 0x3F);                } else {                    c[3] = 64;                }            } else {                c[2] = 64;                c[3] = 64;            }            // Convert to base64 chars            for(int j = 0; j < 4; j++) {                if (c[j] < 26) {                    c[j] += 'A';                } else if (c[j] < 52) {                    c[j] = (char) (c[j] - 26 + 'a');                } else if (c[j] < 62) {                    c[j] = (char) (c[j] - 52 + '0');                } else if (c[j] == 62) {                    c[j] = '+';                } else if (c[j] == 63) {                    c[j] = '/';                } else {                    c[j] = '=';                }            }            sb.append(c);            i += 3;            remaining -= 3;        }        return sb.toString();    }    /**     * Convenience method for setting the given entry as a double.     * The double is converted with <code>Double.toString(double)</code>     * and then stored in the preference entry as that string.     *     * @exception NullPointerException if the key is null     * @exception IllegalArgumentException if the key length is to large     * @exception IllegalStateException when this node has been removed     */    public void putDouble(String key, double value) {        put(key, Double.toString(value));    }    /**     * Convenience method for setting the given entry as a float.     * The float is converted with <code>Float.toString(float)</code>     * and then stored in the preference entry as that string.     *     * @exception NullPointerException if the key is null     * @exception IllegalArgumentException if the key length is to large     * @exception IllegalStateException when this node has been removed     */    public void putFloat(String key, float value) {        put(key, Float.toString(value));    }    /**     * Convenience method for setting the given entry as an integer.     * The integer is converted with <code>Integer.toString(int)</code>     * and then stored in the preference entry as that string.     *     * @exception NullPointerException if the key is null     * @exception IllegalArgumentException if the key length is to large     * @exception IllegalStateException when this node has been removed     */    public void putInt(String key, int value) {        put(key, Integer.toString(value));    }    /**     * Convenience method for setting the given entry as a long.     * The long is converted with <code>Long.toString(long)</code>     * and then stored in the preference entry as that string.     *     * @exception NullPointerException if the key is null     * @exception IllegalArgumentException if the key length is to large     * @exception IllegalStateException when this node has been removed     */    public void putLong(String key, long value) {        put(key, Long.toString(value));    }    /**     * Removes the preferences entry from this preferences node.     * <p>          * The result will be immediately visible in this VM, but may not be     * immediately written to the backing store.     * <p>     * This implementation checks that the key is not larger then 80     * characters, gets the lock of this node, checks that the node has     * not been removed and calls <code>removeSpi</code> with the given key.     *     * @exception NullPointerException if the key is null     * @exception IllegalArgumentException if the key length is to large     * @exception IllegalStateException when this node has been removed     */    public void remove(String key) {        if (key.length() > MAX_KEY_LENGTH)            throw new IllegalArgumentException(key);        synchronized(lock) {            if (isRemoved())                throw new IllegalStateException("Node removed");            removeSpi(key);            if (preferenceListeners != null)              fire(new PreferenceChangeEvent(this, key, null));        }    }    /**     * Removes all entries from this preferences node. May need access to the     * backing store to get and clear all entries.     * <p>     * The result will be immediately visible in this VM, but may not be     * immediatly written to the backing store.     * <p>     * This implementation locks this node, checks that the node has not been     * removed and calls <code>keys()</code> to get a complete array of keys     * for this node. For every key found <code>removeSpi()</code> is called.     *     * @exception BackingStoreException when the backing store cannot be     *            reached     * @exception IllegalStateException if this node has been removed     */    public void clear() throws BackingStoreException {        synchronized(lock) {            if (isRemoved())                throw new IllegalStateException("Node Removed");            String[] keys = keys();            for (int i = 0; i < keys.length; i++) {                removeSpi(keys[i]);            }        }    }    /**     * Writes all preference changes on this and any subnode that have not     * yet been written to the backing store. This has no effect on the     * preference entries in this VM, but it makes sure that all changes     * are visible to other programs (other VMs might need to call the     * <code>sync()</code> method to actually see the changes to the backing     * store.     * <p>     * Locks this node, calls the <code>flushSpi()</code> method, gets all     * the (cached - already existing in this VM) subnodes and then calls     * <code>flushSpi()</code> on every subnode with this node unlocked and     * only that particular subnode locked.     *     * @exception BackingStoreException when the backing store cannot be     *            reached     */    public void flush() throws BackingStoreException {        flushNode(false);    }    /**     * Writes and reads all preference changes to and from this and any     * subnodes. This makes sure that all local changes are written to the     * backing store and that all changes to the backing store are visible     * in this preference node (and all subnodes).     * <p>     * Checks that this node is not removed, locks this node, calls the     * <code>syncSpi()</code> method, gets all the subnodes and then calls     * <code>syncSpi()</code> on every subnode with this node unlocked and     * only that particular subnode locked.     *     * @exception BackingStoreException when the backing store cannot be     *            reached     * @exception IllegalStateException if this node has been removed     */    public void sync() throws BackingStoreException {        flushNode(true);    }        /**     * Private helper method that locks this node and calls either     * <code>flushSpi()</code> if <code>sync</code> is false, or

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?