Here you can find the source of putCLOB(Preferences prefs, String key, String clob)
Parameter | Description |
---|---|
prefs | the preference node to store data under |
key | the key to associate data with |
clob | the CLOB value to store |
public static void putCLOB(Preferences prefs, String key, String clob)
//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"); } }