Example usage for java.util.prefs Preferences putByteArray

List of usage examples for java.util.prefs Preferences putByteArray

Introduction

In this page you can find the example usage for java.util.prefs Preferences putByteArray.

Prototype

public abstract void putByteArray(String key, byte[] value);

Source Link

Document

Associates a string representing the specified byte array with the specified key in this preference node.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(String.class);

    // Save some values
    prefs.put("myString", "a string"); // String
    prefs.putBoolean("myBoolean", true); // boolean
    prefs.putInt("myInt", 123); // int
    prefs.putLong("myLong", 123L); // long
    prefs.putFloat("myFloat", 12.3F); // float
    prefs.putDouble("myDouble", 12.3); // double
    byte[] bytes = new byte[10];
    prefs.putByteArray("myByteArray", bytes); // byte[]

    // Export the node to a file
    prefs.exportNode(new FileOutputStream("output.xml"));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(String.class);

    // Save some values
    prefs.put("myString", "a string"); // String
    prefs.putBoolean("myBoolean", true); // boolean
    prefs.putInt("myInt", 123); // int
    prefs.putLong("myLong", 123L); // long

    // Save some values in the parent node
    prefs = prefs.parent();/*from  w w  w  .  j  av  a  2s  .com*/
    prefs.putFloat("myFloat", 12.3F); // float
    prefs.putDouble("myDouble", 12.3); // double
    byte[] bytes = new byte[10];
    prefs.putByteArray("myByteArray", bytes); // byte[]

    prefs.exportSubtree(new FileOutputStream("output.xml"));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(Main.class);

    // Preference key name
    final String PREF_NAME = "name_of_preference";

    // Save/*from www  . j  a  v  a2s. com*/
    prefs.put(PREF_NAME, "a string"); // String
    prefs.putBoolean(PREF_NAME, true); // boolean
    prefs.putInt(PREF_NAME, 123); // int
    prefs.putLong(PREF_NAME, 123L); // long
    prefs.putFloat(PREF_NAME, 12.3F); // float
    prefs.putDouble(PREF_NAME, 12.3); // double
    byte[] bytes = new byte[1024];
    prefs.putByteArray(PREF_NAME, bytes); // byte[]

    // Retrieve
    String s = prefs.get(PREF_NAME, "a string"); // String
    boolean b = prefs.getBoolean(PREF_NAME, true); // boolean
    int i = prefs.getInt(PREF_NAME, 123); // int
    long l = prefs.getLong(PREF_NAME, 123L); // long
    float f = prefs.getFloat(PREF_NAME, 12.3F); // float
    double d = prefs.getDouble(PREF_NAME, 12.3); // double
    bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[]
}

From source file:edu.umd.cs.findbugs.gui2.GUISaveState.java

public void save() {
    Preferences p = Preferences.userNodeForPackage(GUISaveState.class);

    p.putInt(TAB_SIZE, tabSize);/*w  w w . j  a va 2  s  .c  om*/

    p.putFloat(FONT_SIZE, fontSize);

    try {
        p.put(STARTERDIRECTORY, starterDirectoryForLoadBugs.getCanonicalPath());
    } catch (IOException e) {
        Debug.println(e);
    }
    int sorterLength = MainFrame.getInstance().getSorter().getColumnCount();
    ArrayList<Sortables> sortables = MainFrame.getInstance().getSorter().getOrder();
    p.putInt(GUISaveState.SORTERTABLELENGTH, sorterLength);

    String[] sorterKeys = GUISaveState.generateSorterKeys(sorterLength);
    for (int x = 0; x < sorterKeys.length; x++) {
        p.put(sorterKeys[x], sortables.get(x).prettyName);
    }

    p.putInt(GUISaveState.PREVCOMMENTSSIZE, previousComments.size());

    for (int x = 0; x < previousComments.size(); x++) {
        String comment = previousComments.get(x);
        p.put(GUISaveState.COMMENTKEYS[x], comment);
    }

    int size = recentFiles.size();
    while (recentFiles.size() > MAXNUMRECENTPROJECTS) {
        recentFiles.remove(0);
    }

    p.putInt(GUISaveState.NUMPROJECTS, Math.min(size, MAXNUMRECENTPROJECTS));
    for (int x = 0; x < Math.min(size, MAXNUMRECENTPROJECTS); x++) {
        File file = recentFiles.get(x);
        p.put(GUISaveState.RECENTPROJECTKEYS[x], file.getAbsolutePath());
    }

    p.putByteArray(DOCKINGLAYOUT, dockingLayout);

    p.put(FRAME_BOUNDS,
            frameBounds.x + "," + frameBounds.y + "," + frameBounds.width + "," + frameBounds.height);
    p.putInt(EXTENDED_WINDOW_STATE, extendedWindowState);

    p.putInt(SPLIT_MAIN, splitMain);
    p.putDouble(SPLIT_SUMMARY_NEW, splitSummary);
    p.putInt(SPLIT_TOP, splitTop);
    p.putInt(SPLIT_TREE_COMMENTS, splitTreeComments);
    p.putInt(PACKAGE_PREFIX_SEGEMENTS, packagePrefixSegments);

    p.put(ENABLED_PLUGINS, StringUtils.join(enabledPlugins, ','));
    p.put(DISABLED_PLUGINS, StringUtils.join(disabledPlugins, ','));
    p.put(CUSTOM_PLUGINS, StringUtils.join(customPlugins, ' '));
}

From source file:de.scoopgmbh.copper.monitoring.client.context.ApplicationContext.java

public ApplicationContext() {
    mainStackPane = new StackPane();
    mainPane = new BorderPane();
    mainPane.setId("background");//important for css
    mainStackPane.getChildren().add(mainPane);
    messageProvider = new MessageProvider(ResourceBundle.getBundle("de.scoopgmbh.copper.gui.message"));

    final Preferences prefs = Preferences.userRoot().node("de.scoopgmbh.coppermonitor");

    SettingsModel defaultSettings = new SettingsModel();
    AuditralColorMapping newItem = new AuditralColorMapping();
    newItem.color.setValue(Color.rgb(255, 128, 128));
    newItem.loglevelRegEx.setValue("1");
    defaultSettings.auditralColorMappings.add(newItem);
    byte[] defaultModelbytes;
    ByteArrayOutputStream os = null;
    try {//  ww  w.java 2 s  .  co m
        os = new ByteArrayOutputStream();
        ObjectOutputStream o = new ObjectOutputStream(os);
        o.writeObject(defaultSettings);
        defaultModelbytes = os.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    settingsModelSingleton = defaultSettings;
    ByteArrayInputStream is = null;
    try {
        is = new ByteArrayInputStream(prefs.getByteArray(SETTINGS_KEY, defaultModelbytes));
        ObjectInputStream o = new ObjectInputStream(is);
        Object object = o.readObject();
        if (object instanceof SettingsModel) {
            settingsModelSingleton = (SettingsModel) object;
        }
    } catch (Exception e) {
        logger.error("", e);
        getIssueReporterSingleton()
                .reportWarning("Can't load settings from (Preferences: " + prefs + ") use defaults instead", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            ByteArrayOutputStream os = null;
            try {
                os = new ByteArrayOutputStream();
                ObjectOutputStream o = new ObjectOutputStream(os);
                o.writeObject(settingsModelSingleton);
                prefs.putByteArray(SETTINGS_KEY, os.toByteArray());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }));
}

From source file:org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.java

private void saveCache() {
    // Get the preferences database for this package.
    Preferences prefs = Preferences.userNodeForPackage(FileSystemFontProvider.class);

    // To save, write the object to a byte array.
    try {//w w w.  j a v a  2 s .c om
        for (FSFontInfo fontInfo : fontInfoList) {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
            // write it to the stream
            objectOut.writeObject(fontInfo);
            prefs.putByteArray(fontInfo.file.getAbsolutePath(), byteOut.toByteArray());
        }
    } catch (IOException e) {
        LOG.error("Could not write to font cache", e);
    }
    LOG.warn("Finished building font cache, found " + fontInfoList.size() + " fonts");
}

From source file:org.netbeans.jcode.util.PreferenceUtils.java

public static <T> void set(Preferences pref, T t) {
    pref.putByteArray(t.getClass().getName(), PreferenceUtils.serialize((Serializable) t));
}