Java Properties Save savePropertiesToEncodedString(Properties props, String comment)

Here you can find the source of savePropertiesToEncodedString(Properties props, String comment)

Description

save Properties To Encoded String

License

Open Source License

Parameter

Parameter Description
props a parameter
comment a parameter

Exception

Parameter Description
IOException an exception

Return

the encoded string

Declaration

public static String savePropertiesToEncodedString(Properties props, String comment) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  ww w . ja  v  a2  s  .co m*/
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.util.Properties;

public class Main {
    private static final String ENCODED_CHAR_PERCENT = "%25";
    private static final String ENCODED_CHAR_CARRIAGE_RETURN = "%0d";
    private static final String ENCODED_CHAR_TAB = "%09";
    private static final String ENCODED_CHAR_NEWLINE = "%0a";
    private static final String ENCODED_CHAR_SPACE = "%20";
    private static final String ENCODED_CHAR_COLON = "%3a";
    private static final String ENCODED_CHAR_EQUALS = "%3d";

    /**
     * @param props
     * @param comment
     * @return the encoded string
     * @throws IOException
     */
    public static String savePropertiesToEncodedString(Properties props, String comment) throws IOException {
        if (props == null)
            return null;
        return encodeName(savePropertiesToString(props, comment));
    }

    /**
     * @param theName
     * @return the encoded name
     */
    public static String encodeName(String theName) {
        int theSize = theName.length();
        StringBuffer encoded = new StringBuffer(theSize);
        char ch;

        for (int ii = 0; ii < theSize; ii++) {
            ch = theName.charAt(ii);
            switch (ch) {
            // these are the set of illegal characters in a Property name
            case '=': // %3d
                encoded.append(ENCODED_CHAR_EQUALS);
                break;
            case ':': // %3a
                encoded.append(ENCODED_CHAR_COLON);
                break;
            case ' ': // %20
                encoded.append(ENCODED_CHAR_SPACE);
                break;
            case '\n': // %0a
                encoded.append(ENCODED_CHAR_NEWLINE);
                break;
            case '\t': // %09
                encoded.append(ENCODED_CHAR_TAB);
                break;
            case '\r': // %0d
                encoded.append(ENCODED_CHAR_CARRIAGE_RETURN);
                break;
            case '%': // %25
                // added because its our encoding flag
                encoded.append(ENCODED_CHAR_PERCENT);
                break;
            default:
                encoded.append(ch);
                break;
            }
        }

        return encoded.toString();
    }

    /**
     * @param props
     * @param comment
     * @return the string
     * @throws IOException
     */
    public static String savePropertiesToString(Properties props, String comment) throws IOException {
        if (props == null)
            return null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        props.store(out, comment);
        String tmpString = out.toString();
        out = null;
        return tmpString;
    }
}

Related

  1. saveProperties(Properties properties, String propertiesFileName, String header)
  2. saveProperties(Properties props, File location)
  3. saveProperties(String fileName, Properties properties)
  4. saveProperties(String fileName, Properties properties, String title)
  5. saveProperties(String propName, Properties props)
  6. savePropertiesToString(Properties props, String comment)
  7. saveProperty(String key, String value)
  8. saveProperty(String key, String value)
  9. saveProperty(String key, String value)