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 HashMap canonicalNames; 61: 62: static { 63: canonicalNames = new HashMap(); 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: String encoding; 98: try 99: { 100: return System.getProperty("file.encoding"); 101: } catch(SecurityException e) { 102: } catch(IllegalArgumentException e) { 103: } 104: // XXX - Throw an error here? For now, default to the 'safe' encoding. 105: return "8859_1"; 106: } 107: 108: /** 109: * Returns the java.io canonical name of a charset given with the 110: * java.nio canonical name. If the charset does not have a java.io 111: * canonical name, the input string is returned. 112: */ 113: public static String getOldCanonical(String newCanonical) 114: { 115: String oldCanonical = (String) canonicalNames.get(newCanonical); 116: return (oldCanonical != null)?oldCanonical : newCanonical; 117: } 118: 119: public static boolean isISOLatin1(String s) 120: { 121: if(s.equals("ISO-8859-1") || 122: s.equals("8859_1") || 123: s.equals("ISO_8859-1") || 124: s.equals("latin1") || 125: s.equals("ISO8859_1") || 126: s.equals("ISO_8859_1")) 127: return true; 128: return false; 129: } 130: 131: /** 132: * Gets a charset, throwing the java.io exception and not 133: * the java.nio exception if an error occurs. 134: */ 135: public static Charset getCharset(String name) 136: throws UnsupportedEncodingException 137: { 138: try 139: { 140: return Charset.forName(name); 141: } 142: catch(IllegalCharsetNameException e) 143: { 144: throw new UnsupportedEncodingException("Charset "+name+" not found."); 145: } 146: catch(UnsupportedCharsetException e) 147: { 148: throw new UnsupportedEncodingException("Charset "+name+" not found."); 149: } 150: } 151: 152: /** 153: * Returns the default charset without throwing any exceptions. The default 154: * charset is UTF8. 155: * 156: * @return the default charset 157: */ 158: public static Charset getDefaultCharset() 159: { 160: return new UTF_8(); 161: } 162: } 163: 164: