📄 abstractpreferences.java
字号:
return null; // encoding exception } } int remaining = endchar == -1 ? c.length : endchar; int i = 0; while (remaining > 0) { // Four input chars (6 bits) are decoded as three bytes as // 000000 001111 111122 222222 byte b0 = (byte) (c[i] << 2); if (remaining >= 2) { b0 += (c[i+1] & 0x30) >> 4; } bs.write(b0); if (remaining >= 3) { byte b1 = (byte) ((c[i+1] & 0x0F) << 4); b1 += (byte) ((c[i+2] & 0x3C) >> 2); bs.write(b1); } if (remaining >= 4) { byte b2 = (byte) ((c[i+2] & 0x03) << 6); b2 += c[i+3]; bs.write(b2); } i += 4; remaining -= 4; } return bs.toByteArray(); } /** * Convenience method for getting the given entry as a double. * When the string representation of the requested entry can be decoded * with <code>Double.parseDouble()</code> then that double is returned, * otherwise the given default double 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 double getDouble(String key, double defaultVal) { 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 immediatly visible in this VM, but may not be * immediatly 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); // XXX - fire events } } /** * 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, String.valueOf(value)); // XXX - Use when using 1.4 compatible Boolean // 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 immediatly visible in this VM, but may not be * immediatly 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -