Here you can find the source of encode(String text, String encoding)
public static String encode(String text, String encoding)
//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"); } } }