Java Preference Put putCLOB(Preferences prefs, String key, String clob)

Here you can find the source of putCLOB(Preferences prefs, String key, String clob)

Description

Store a character large object (CLOB) under the given key in the given preference node.

License

Open Source License

Parameter

Parameter Description
prefs the preference node to store data under
key the key to associate data with
clob the CLOB value to store

Declaration

public static void putCLOB(Preferences prefs, String key, String clob) 

Method Source Code


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

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

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

    /**//  w  ww.  j  a va 2s. c  o m
     * Store a character large object (CLOB) under the given key in the given
     * preference node.
     * 
     * @param prefs the preference node to store data under
     * @param key the key to associate data with
     * @param clob the CLOB value to store
     */
    public static void putCLOB(Preferences prefs, String key, String clob) {
        if (clob == null)
            throw new NullPointerException("CLOB value cannot be null");

        // Degenerate case: if the value to store is short enough to fit under
        // a single preference key, just store it normally.
        if (clob.length() < Preferences.MAX_VALUE_LENGTH && !isCheckVal(clob)) {
            removeCLOB(prefs, key);
            prefs.put(key, clob);
            return;
        }

        // First, write the checksum value.  Then, if anything below fails, the
        // worst case scenario is that we will be able to detect the corrupt
        // data and revert to a future default.
        prefs.put(key, getCheckVal(clob));

        // Create a child node specifically for "clob storage" for this key
        Preferences p = getClobStorage(prefs, key);
        try {
            p.clear();
        } catch (BackingStoreException bse) {
            // it would be nice to clear all data underneath the clob storage
            // node since it might reduce our resource utilization.  But even
            // if this fails, the code below ought to cover our bases.
        }

        // Split the CLOB into smaller pieces, and put them under our clob
        // storage node with the numerically ascending keys.
        int num = 0;
        while (true) {
            int len = clob.length();
            int pieceLen = Math.min(len, Preferences.MAX_VALUE_LENGTH - 1);
            p.put(Integer.toString(num++), clob.substring(0, pieceLen));
            if (len == pieceLen)
                break;
            clob = clob.substring(pieceLen);
        }

        // Delete the key that immediately follows our numerically ascending
        // keys.  This way, if the clear() call above failed, we will still
        // be able to detect the end of the stored data.
        p.remove(Integer.toString(num));
    }

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

    /**
     * Removes the character large object (CLOB) associated with the specified
     * key in the given preference node, if any.
     * 
     * @param prefs the preferences node where data is stored
     * @param key the key the CLOB was associated with
     */
    public static void removeCLOB(Preferences prefs, String key) {
        prefs.remove(key);
        try {
            getClobStorage(prefs, key).removeNode();
        } catch (BackingStoreException bse) {
        }
    }

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

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

Related

  1. put(final String key, final boolean value)
  2. putBooleanDontOverwrite(Preferences prefs, String key, boolean value)
  3. putList(String path, ArrayList list)
  4. putPrefsBoolean(Preferences prefs, String key, boolean value, boolean def)
  5. putPrefsStrings(Preferences prefs, String key, String[] strings)
  6. setLocale(final Locale l)