Java String Encode encode(String str, int start, int end, Writer writer)

Here you can find the source of encode(String str, int start, int end, Writer writer)

Description

encode

License

Open Source License

Parameter

Parameter Description
str a parameter
start a parameter
end a parameter
writer a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void encode(String str, int start, int end, Writer writer) 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://www  .  j a  v a2 s .c  o  m
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

import java.io.IOException;
import java.io.Writer;

import java.util.Hashtable;

public class Main {
    private static Hashtable _table;
    private static Hashtable _reverse;

    /**
     * @param str
     * @param start
     * @param end
     * @param writer
     * @throws IOException
     */
    public static void encode(String str, int start, int end, Writer writer) throws IOException {
        for (int i = start; i < end; i++) {
            char ch = str.charAt(i);
            String special = getSpecial(ch);
            if (special != null) {
                writer.write(special);
            } else {
                if ((ch & 0xff) != 0) {
                    writer.write("&#"); //$NON-NLS-1$
                    writer.write(Integer.toString(ch));
                    writer.write(";"); //$NON-NLS-1$
                } else {
                    writer.write(ch);
                }
            }
        }
    }

    /**
     * @param str
     * @param result
     * @return the encoded string buffer
     */
    public static StringBuffer encode(String str, StringBuffer result) {
        return encode(str, 0, str.length(), result);
    }

    /**
     * @param str
     * @param start
     * @param end
     * @param result
     * @return the encoded string buffer
     */
    public static StringBuffer encode(String str, int start, int end, StringBuffer result) {
        for (int i = start; i < end; i++) {
            char ch = str.charAt(i);
            String special = getSpecial(ch);
            if (special != null) {
                result.append(special);
            } else {
                if ((ch & 0xff00) != 0) {
                    result.append("&#"); //$NON-NLS-1$
                    result.append(Integer.toString(ch));
                    result.append(";"); //$NON-NLS-1$
                } else {
                    result.append(ch);
                }
            }
        }
        return result;
    }

    /**
     * @param ch 
     * @return if not in the special list
     */
    public static String getSpecial(int ch) {
        return (String) _table.get(Integer.valueOf(ch));
    }

    /**
     * @param str
     * @return the code value corresponding to the string or null
     * if string is unknown
     */
    public static int getSpecial(String str) {
        Integer result = (Integer) _reverse.get(str);
        if (result == null) {
            return -1;
        }
        return result.intValue();
    }
}

Related

  1. encode(String s, String encoding)
  2. encode(String source, String charsetFrom, String charsetTo)
  3. encode(String src)
  4. encode(String src, String charset)
  5. encode(String str)
  6. encode(String text, String charset)
  7. encode(String text, String encoding)
  8. encode(String text, String encoding)
  9. encode(String val, String encoding)