Frames | No Frames |
1: /* EncodingHelper.java -- Useful character encoding methods. 2: Copyright (C) 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.nio.charset; 40: 41: import java.util.HashMap; 42: import java.nio.charset.IllegalCharsetNameException; 43: import java.nio.charset.UnsupportedCharsetException; 44: import java.nio.charset.Charset; 45: import java.io.UnsupportedEncodingException; 46: 47: /** 48: * This class provides some useful utility methods 49: * for charset encoding for the java.lang and java.io methods. 50: * 51: * @author Sven de Marothy 52: */ 53: public class EncodingHelper 54: { 55: 56: /** 57: * Contains the mapping from java.io canonical names 58: * to java.nio canonical names. 59: */ 60: private static final HashMap<String,String> canonicalNames; 61: 62: static { 63: canonicalNames = new HashMap<String,String>(); 64: canonicalNames.put("US-ASCII", "ASCII"); 65: canonicalNames.put("windows-1250", "Cp1250"); 66: canonicalNames.put("windows-1251", "Cp1251"); 67: canonicalNames.put("windows-1252", "Cp1252"); 68: canonicalNames.put("windows-1253", "Cp1253"); 69: canonicalNames.put("windows-1254", "Cp1254"); 70: canonicalNames.put("windows-1257", "Cp1257"); 71: canonicalNames.put("ISO-8859-1", "ISO8859_1"); 72: canonicalNames.put("ISO-8859-2", "ISO8859_2"); 73: canonicalNames.put("ISO-8859-4", "ISO8859_4"); 74: canonicalNames.put("ISO-8859-5", "ISO8859_5"); 75: canonicalNames.put("ISO-8859-7", "ISO8859_7"); 76: canonicalNames.put("ISO-8859-9", "ISO8859_9"); 77: canonicalNames.put("ISO-8859-13", "ISO8859_13"); 78: canonicalNames.put("ISO-8859-15", "ISO8859_15"); 79: canonicalNames.put("KOI8-R", "KOI8_R"); 80: canonicalNames.put("UTF-8", "UTF8"); 81: canonicalNames.put("UTF-16BE", "UnicodeBigUnmarked"); 82: canonicalNames.put("UTF-16LE", "UnicodeLittleUnmarked"); 83: canonicalNames.put("windows-1255", "Cp1255"); 84: canonicalNames.put("windows-1256", "Cp1256"); 85: canonicalNames.put("windows-1258", "Cp1258"); 86: canonicalNames.put("ISO-8859-3", "ISO8859_3"); 87: canonicalNames.put("ISO-8859-6", "ISO8859_6"); 88: canonicalNames.put("ISO-8859-8", "ISO8859_8"); 89: } 90: 91: /** 92: * Returns the name of the default encoding, 93: * falls back on defaults to Latin-1 if there's a problem. 94: */ 95: public static String getDefaultEncoding() 96: { 97: try 98: { 99: return System.getProperty("file.encoding"); 100: } catch(SecurityException e) { 101: } catch(IllegalArgumentException e) { 102: } 103: // XXX - Throw an error here? For now, default to the 'safe' encoding. 104: return "8859_1"; 105: } 106: 107: /** 108: * Returns the java.io canonical name of a charset given with the 109: * java.nio canonical name. If the charset does not have a java.io 110: * canonical name, the input string is returned. 111: */ 112: public static String getOldCanonical(String newCanonical) 113: { 114: String oldCanonical = (String) canonicalNames.get(newCanonical); 115: return (oldCanonical != null)?oldCanonical : newCanonical; 116: } 117: 118: public static boolean isISOLatin1(String s) 119: { 120: if(s.equals("ISO-8859-1") || 121: s.equals("8859_1") || 122: s.equals("ISO_8859-1") || 123: s.equals("latin1") || 124: s.equals("ISO8859_1") || 125: s.equals("ISO_8859_1")) 126: return true; 127: return false; 128: } 129: 130: /** 131: * Gets a charset, throwing the java.io exception and not 132: * the java.nio exception if an error occurs. 133: */ 134: public static Charset getCharset(String name) 135: throws UnsupportedEncodingException 136: { 137: try 138: { 139: return Charset.forName(name); 140: } 141: catch(IllegalCharsetNameException e) 142: { 143: throw new UnsupportedEncodingException("Charset "+name+" not found."); 144: } 145: catch(UnsupportedCharsetException e) 146: { 147: throw new UnsupportedEncodingException("Charset "+name+" not found."); 148: } 149: } 150: 151: /** 152: * Returns the default charset without throwing any exceptions. The default 153: * charset is UTF8. 154: * 155: * @return the default charset 156: */ 157: public static Charset getDefaultCharset() 158: { 159: return new UTF_8(); 160: } 161: }