Java String Encode encode(String text, String encoding)

Here you can find the source of encode(String text, String encoding)

Description

encode

License

Open Source License

Declaration

public static String encode(String text, String encoding) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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://  w  w  w.  ja v a2  s.  com
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {
    public static String encode(String text, String encoding) {
        if (true)
            return text;
        try {
            byte[] bs = text.getBytes(System.getProperty("file.encoding"));
            ByteArrayInputStream is = new ByteArrayInputStream(bs);
            InputStreamReader r = new InputStreamReader(is, encoding);
            char[] cs = new char[bs.length];
            int l = r.read(cs, 0, cs.length);
            return new String(cs, 0, l);
        } catch (IOException e) {
            if ("UTF-8".equals(encoding))
                return text;
            return encode(text, "UTF-8");
        }
    }
}

Related

  1. encode(String src, String charset)
  2. encode(String str)
  3. encode(String str, int start, int end, Writer writer)
  4. encode(String text, String charset)
  5. encode(String text, String encoding)
  6. encode(String val, String encoding)
  7. encode(String value, String encoding)
  8. encodeBase64(String str)
  9. encodeConvenience(String str)