Java Preference Get getCLOB(Preferences prefs, String key, String def)

Here you can find the source of getCLOB(Preferences prefs, String key, String def)

Description

Returns the character large object (CLOB) associated with the specified key in the given preference node.

License

Open Source License

Parameter

Parameter Description
prefs the preference node to store data under
key the key whose value is to be returned
def the value to be returned in the event that this preference node has no value associated with <code>key</code>.

Declaration

public static String getCLOB(Preferences prefs, String key, String def) 

Method Source Code

//package com.java2s;
// modify it under the terms of the GNU Lesser General Public License

import java.util.prefs.Preferences;

public class Main {
    static final String CHECKSUM_PREFIX = "PrefsUtil$CLOBValue@";

    /** Returns the character large object (CLOB) associated with the specified
     * key in the given preference node.
     * //  w w w. j a va2 s .  c  o  m
     * This method can be safely called even if a non-CLOB value was stored for
     * the given key;  in that case, the non-CLOB value will be returned.
     * 
     * Returns the specified default if there is no value associated with the
     * key, if the backing store is inaccessible, or if a previously stored
     * CLOB has been corrupted by external writes to this preference node.
     * 
     * @param prefs the preference node to store data under
     * @param key the key whose value is to be returned
     * @param def the value to be returned in the event that this preference
     *    node has no value associated with <code>key</code>.
     */
    public static String getCLOB(Preferences prefs, String key, String def) {
        String checkVal = prefs.get(key, null);
        if (checkVal == null)
            return def;
        else if (!isCheckVal(checkVal))
            return checkVal;

        Preferences p = getClobStorage(prefs, key);
        StringBuffer buf = new StringBuffer();
        int num = 0;
        while (true) {
            String val = p.get(Integer.toString(num++), null);
            if (val == null)
                break;
            else
                buf.append(val);
        }

        String result = buf.toString();
        if (!checkVal.equals(getCheckVal(result)))
            return def;

        return result;
    }

    private static boolean isCheckVal(String checkVal) {
        return checkVal.startsWith(CHECKSUM_PREFIX);
    }

    private static Preferences getClobStorage(Preferences prefs, String key) {
        return prefs.node(key + "_clob");
    }

    private static String getCheckVal(String val) {
        return CHECKSUM_PREFIX + val.hashCode();
    }
}

Related

  1. get(String key, String defaultValue)
  2. getAltNode(final String name)
  3. getBoolean(final String key, final boolean defaultValue)
  4. getClobStorage(Preferences prefs, String key)
  5. getDefault()
  6. getFloat(String key, Float def)
  7. getInt(final Class preferencesClass, final String preferenceName)