001: /*
002: * EncodingUtil.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.io.BufferedReader;
015: import java.io.BufferedWriter;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.OutputStream;
023: import java.io.OutputStreamWriter;
024: import java.io.Reader;
025: import java.io.UnsupportedEncodingException;
026: import java.io.Writer;
027: import java.nio.charset.Charset;
028: import java.util.Iterator;
029: import java.util.Map;
030: import javax.swing.JComponent;
031: import workbench.log.LogMgr;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034:
035: /**
036: * Utility class to handle encoding related stuff
037: *
038: * @author support@sql-workbench.net
039: */
040: public class EncodingUtil {
041: private static String[] charsets;
042:
043: /**
044: * Create a BufferedReader for the given file and encoding
045: * The buffer size is set to 64K
046: */
047: public static Reader createReader(File f, String encoding)
048: throws IOException, UnsupportedEncodingException {
049: InputStream inStream = new FileInputStream(f);
050: return createReader(inStream, encoding);
051: }
052:
053: public static Reader createReader(InputStream in, String encoding)
054: throws IOException, UnsupportedEncodingException {
055: Reader r = null;
056: if (encoding != null) {
057: try {
058: String enc = cleanupEncoding(encoding);
059:
060: if (enc.toLowerCase().startsWith("utf")) {
061: r = new UnicodeReader(in, enc);
062: } else {
063: r = new InputStreamReader(in, enc);
064: }
065: } catch (UnsupportedEncodingException e) {
066: throw e;
067: }
068: } else {
069: r = new InputStreamReader(in);
070: }
071: return r;
072: }
073:
074: /**
075: * Create a BufferedReader for the given file and encoding.
076: * If no encoding is given, then a regular FileReader without
077: * a specific encoding is used.
078: * The default buffer size is 16kb
079: */
080: public static BufferedReader createBufferedReader(File f,
081: String encoding) throws IOException {
082: return createBufferedReader(f, encoding, 16 * 1024);
083: }
084:
085: /**
086: * Create a BufferedReader for the given file, encoding and buffer size.
087: * If no encoding is given, then a regular FileReader without
088: * a specific encoding is used.
089: */
090: public static BufferedReader createBufferedReader(File f,
091: String encoding, int buffSize) throws IOException {
092: Reader r = createReader(f, encoding);
093: return new BufferedReader(r, buffSize);
094: }
095:
096: /**
097: * Allow some common other names for encodings (e.g. UTF for UTF-8)
098: */
099: public static String cleanupEncoding(String input) {
100: if (input == null)
101: return null;
102: if ("utf".equalsIgnoreCase(input))
103: return "UTF-8";
104: if ("utf8".equalsIgnoreCase(input))
105: return "UTF-8";
106: if ("utf-8".equalsIgnoreCase(input))
107: return "UTF-8";
108: return input;
109: }
110:
111: /**
112: * Returns the system's default encoding.
113: */
114: public static String getDefaultEncoding() {
115: String enc = Settings.getInstance().getDefaultFileEncoding();
116: return enc;
117: }
118:
119: /**
120: * Return all available encodings.
121: */
122: public synchronized static String[] getEncodings() {
123: if (charsets == null) {
124: Map sets = java.nio.charset.Charset.availableCharsets();
125: Iterator names = sets.keySet().iterator();
126: charsets = new String[sets.size()];
127: int i = 0;
128: while (names.hasNext()) {
129: charsets[i] = (String) names.next();
130: i++;
131: }
132: }
133: return charsets;
134: }
135:
136: /**
137: * Test if the given encoding is supported. Before this is
138: * tested, cleanupEncoding() is called to allow for some
139: * common "abbreviations"
140: * @see #cleanupEncoding(String)
141: */
142: public static boolean isEncodingSupported(String encoding) {
143: String enc = cleanupEncoding(encoding);
144: try {
145: Charset.forName(enc);
146: return true;
147: } catch (Throwable e) {
148: return false;
149: }
150: }
151:
152: public static Writer createWriter(File outfile, String encoding,
153: boolean append) throws IOException {
154: return createWriter(new FileOutputStream(outfile, append),
155: encoding);
156: }
157:
158: public static Writer createWriter(OutputStream stream,
159: String encoding) throws IOException {
160: Writer pw = null;
161: final int buffSize = 64 * 1024;
162: if (encoding != null) {
163: try {
164: OutputStreamWriter out = new OutputStreamWriter(stream,
165: cleanupEncoding(encoding));
166: pw = new BufferedWriter(out, buffSize);
167: } catch (UnsupportedEncodingException e) {
168: // Fall back to default encoding
169: pw = new BufferedWriter(new OutputStreamWriter(stream),
170: buffSize);
171: String msg = ResourceMgr.getString("ErrWrongEncoding");
172: msg = StringUtil.replace(msg, "%encoding%", encoding);
173: LogMgr.logError("EncodingUtil.createWriter()", msg, e);
174: }
175: }
176: return pw;
177: }
178:
179: public static JComponent createEncodingPanel() {
180: try {
181: return (JComponent) Class.forName(
182: "workbench.gui.components.EncodingPanel")
183: .newInstance();
184: } catch (Exception e) {
185: return null;
186: }
187: }
188: }
|