Source Code Cross Referenced for EncodingUtil.java in  » Database-Client » SQL-Workbench » workbench » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » SQL Workbench » workbench.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.