Source Code Cross Referenced for Helper.java in  » 6.0-JDK-Modules » j2me » com » sun » cdc » i18n » 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 » 6.0 JDK Modules » j2me » com.sun.cdc.i18n 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)Helper.java	1.21 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package com.sun.cdc.i18n;
029:
030:        import java.io.*;
031:
032:        /**
033:         * This class general helper functions for the J2SE environment.
034:         * <p>
035:         * <em>No application code should reference this class directly.</em>
036:         *
037:         * @version 1.0 12/15/99
038:         */
039:        public class Helper {
040:
041:            /**
042:             * The name of the default character encoding
043:             */
044:            private static String defaultEncoding;
045:
046:            /**
047:             * Default path to the J2ME classes
048:             */
049:            private static String defaultMEPath;
050:
051:            /**
052:             * True if we are running on a J2ME system
053:             */
054:            private static boolean j2me = false;
055:
056:            /**
057:             * Class initializer
058:             */
059:            static {
060:                /* Get the default encoding name */
061:                defaultEncoding = System
062:                        .getProperty("microedition.encodingClass");
063:                if (defaultEncoding == null) {
064:                    defaultEncoding = "ISO_LATIN_1";
065:                }
066:
067:                /* Work out if we are running on a J2ME system */
068:                if (System.getProperty("microedition.configuration") != null) {
069:                    j2me = true;
070:                } else {
071:                    defaultEncoding = null;
072:                }
073:
074:                /* Get the default encoding name */
075:                defaultMEPath = System.getProperty("microedition.i18npath");
076:                if (defaultMEPath == null) {
077:                    defaultMEPath = "com.sun.cdc.i18n.j2me";
078:                }
079:
080:                //System.out.println("j2me="+j2me);
081:                //System.out.println("defaultEncoding="+defaultEncoding);
082:            }
083:
084:            /*------------------------------------------------------------------------------*/
085:            /*                               Character encoding                             */
086:            /*------------------------------------------------------------------------------*/
087:
088:            /**
089:             * Get a reader for an InputStream
090:             *
091:             * @param  is              The input stream the reader is for
092:             * @return                 A new reader for the stream
093:             */
094:            public static Reader getStreamReader(InputStream is) {
095:                try {
096:                    return getStreamReader(is, defaultEncoding);
097:                } catch (UnsupportedEncodingException x) {
098:                    throw new RuntimeException("Missing default encoding "
099:                            + defaultEncoding);
100:                }
101:            }
102:
103:            /**
104:             * Get a reader for an InputStream
105:             *
106:             * @param  is              The input stream the reader is for
107:             * @param  name            The name of the decoder
108:             * @return                 A new reader for the stream
109:             * @exception UnsupportedEncodingException  If the encoding is not known
110:             */
111:            public static Reader getStreamReader(InputStream is, String name)
112:                    throws UnsupportedEncodingException {
113:
114:                /* Test for null arguments */
115:                if (is == null || name == null) {
116:                    throw new NullPointerException();
117:                }
118:
119:                /* Get the reader from the encoding */
120:                StreamReader fr = getStreamReaderPrim(name);
121:
122:                /* Open the connection and return*/
123:                return fr.open(is, name);
124:            }
125:
126:            /**
127:             * Get a reader for an InputStream
128:             *
129:             * @param  is              The input stream the reader is for
130:             * @param  name            The name of the decoder
131:             * @return                 A new reader for the stream
132:             * @exception UnsupportedEncodingException  If the encoding is not known
133:             */
134:            private static StreamReader getStreamReaderPrim(String name)
135:                    throws UnsupportedEncodingException {
136:
137:                if (name == null) {
138:                    throw new NullPointerException();
139:                }
140:
141:                try {
142:                    String className;
143:
144:                    /* Get the reader class name */
145:                    if (j2me) {
146:                        className = defaultMEPath + '.' + name + "_Reader";
147:                    } else {
148:                        className = "com.sun.cdc.i18n.j2me.Default_Reader";
149:                    }
150:
151:                    /* Using the decoder names lookup a class to implement the reader */
152:                    Class clazz = Class.forName(className);
153:
154:                    /* Return a new instance */
155:                    return (StreamReader) clazz.newInstance();
156:
157:                } catch (ClassNotFoundException x) {
158:                    throw new UnsupportedEncodingException("Encoding " + name
159:                            + " not found");
160:                } catch (InstantiationException x) {
161:                    throw new RuntimeException("InstantiationException "
162:                            + x.getMessage());
163:                } catch (IllegalAccessException x) {
164:                    throw new RuntimeException("IllegalAccessException "
165:                            + x.getMessage());
166:                } catch (ClassCastException x) {
167:                    throw new RuntimeException("ClassCastException "
168:                            + x.getMessage());
169:                }
170:            }
171:
172:            /**
173:             * Get a writer for an OutputStream
174:             *
175:             * @param  os              The output stream the reader is for
176:             * @return                 A new writer for the stream
177:             */
178:            public static Writer getStreamWriter(OutputStream os) {
179:                try {
180:                    return getStreamWriter(os, defaultEncoding);
181:                } catch (UnsupportedEncodingException x) {
182:                    throw new RuntimeException("Missing default encoding "
183:                            + defaultEncoding);
184:                }
185:            }
186:
187:            /**
188:             * Get a writer for an OutputStream
189:             *
190:             * @param  os              The output stream the reader is for
191:             * @param  name            The name of the decoder
192:             * @return                 A new writer for the stream
193:             * @exception UnsupportedEncodingException  If the encoding is not known
194:             */
195:            public static Writer getStreamWriter(OutputStream os, String name)
196:                    throws UnsupportedEncodingException {
197:
198:                /* Test for null arguments */
199:                if (os == null || name == null) {
200:                    throw new NullPointerException();
201:                }
202:
203:                /* Get the writer from the encoding */
204:                StreamWriter sw = getStreamWriterPrim(name);
205:
206:                /* Open it on the output stream and return */
207:                return sw.open(os, name);
208:            }
209:
210:            /**
211:             * Get a writer for an OutputStream
212:             *
213:             * @param  os              The output stream the reader is for
214:             * @param  name            The name of the decoder
215:             * @return                 A new writer for the stream
216:             * @exception UnsupportedEncodingException  If the encoding is not known
217:             */
218:            private static StreamWriter getStreamWriterPrim(String name)
219:                    throws UnsupportedEncodingException {
220:
221:                if (name == null) {
222:                    throw new NullPointerException();
223:                }
224:
225:                try {
226:                    String className;
227:
228:                    /* Get the writer class name */
229:                    if (j2me) {
230:                        className = defaultMEPath + '.' + name + "_Writer";
231:                    } else {
232:                        className = "com.sun.cdc.i18n.j2me.Default_Writer";
233:                    }
234:
235:                    /* Using the decoder names lookup a class to implement the writer */
236:                    Class clazz = Class.forName(className);
237:
238:                    /* Construct a new instance */
239:                    return (StreamWriter) clazz.newInstance();
240:
241:                } catch (ClassNotFoundException x) {
242:                    throw new UnsupportedEncodingException("Encoding " + name
243:                            + " not found");
244:                } catch (InstantiationException x) {
245:                    throw new RuntimeException("InstantiationException "
246:                            + x.getMessage());
247:                } catch (IllegalAccessException x) {
248:                    throw new RuntimeException("IllegalAccessException "
249:                            + x.getMessage());
250:                } catch (ClassCastException x) {
251:                    throw new RuntimeException("ClassCastException "
252:                            + x.getMessage());
253:                }
254:            }
255:
256:            /**
257:             * Convert a byte array to a char array
258:             *
259:             * @param  buffer          The byte array buffer
260:             * @param  offset          The offset
261:             * @param  length          The length
262:             * @return                 A new char array
263:             */
264:            public static char[] byteToCharArray(byte[] buffer, int offset,
265:                    int length) {
266:                try {
267:                    return byteToCharArray(buffer, offset, length,
268:                            defaultEncoding);
269:                } catch (UnsupportedEncodingException x) {
270:                    throw new RuntimeException("Missing default encoding "
271:                            + defaultEncoding);
272:                }
273:            }
274:
275:            /**
276:             * Convert a char array to a byte array
277:             *
278:             * @param  buffer          The char array buffer
279:             * @param  offset          The offset
280:             * @param  length          The length
281:             * @return                 A new byte array
282:             */
283:            public static byte[] charToByteArray(char[] buffer, int offset,
284:                    int length) {
285:                try {
286:                    return charToByteArray(buffer, offset, length,
287:                            defaultEncoding);
288:                } catch (UnsupportedEncodingException x) {
289:                    throw new RuntimeException("Missing default encoding "
290:                            + defaultEncoding);
291:                }
292:            }
293:
294:            /*
295:             * Cached variables for byteToCharArray
296:             */
297:            private static String lastReaderEncoding;
298:            private static StreamReader lastReader;
299:
300:            /**
301:             * Convert a byte array to a char array
302:             *
303:             * @param  buffer          The byte array buffer
304:             * @param  offset          The offset
305:             * @param  length          The length
306:             * @param  enc             The character encoding
307:             * @return                 A new char array
308:             * @exception UnsupportedEncodingException  If the encoding is not known
309:             */
310:            public static synchronized char[] byteToCharArray(byte[] buffer,
311:                    int offset, int length, String enc)
312:                    throws UnsupportedEncodingException {
313:
314:                /* If we don't have a cached reader then make one */
315:                if (lastReaderEncoding == null
316:                        || !lastReaderEncoding.equals(enc)) {
317:                    lastReader = getStreamReaderPrim(enc);
318:                    lastReaderEncoding = enc;
319:                }
320:
321:                /* Ask the reader for the size the output will be */
322:                int size = lastReader.sizeOf(buffer, offset, length);
323:
324:                /* Allocate a buffer of that size */
325:                char[] outbuf = new char[size];
326:
327:                /* Open the reader on a ByteArrayInputStream */
328:                lastReader.open(
329:                        new ByteArrayInputStream(buffer, offset, length), enc);
330:
331:                try {
332:                    /* Read the input */
333:                    lastReader.read(outbuf, 0, size);
334:                    /* Close the reader */
335:                    lastReader.close();
336:                } catch (IOException x) {
337:                    throw new RuntimeException("IOException reading reader "
338:                            + x.getMessage());
339:                }
340:
341:                /* And return the buffer */
342:                return outbuf;
343:            }
344:
345:            /*
346:             * Cached variables for charToByteArray
347:             */
348:            private static String lastWriterEncoding;
349:            private static StreamWriter lastWriter;
350:
351:            /**
352:             * Convert a char array to a byte array
353:             *
354:             * @param  buffer          The char array buffer
355:             * @param  offset          The offset
356:             * @param  length          The length
357:             * @param  enc             The character encoding
358:             * @return                 A new byte array
359:             * @exception UnsupportedEncodingException  If the encoding is not known
360:             */
361:            public static synchronized byte[] charToByteArray(char[] buffer,
362:                    int offset, int length, String enc)
363:                    throws UnsupportedEncodingException {
364:
365:                /* If we don't have a cached writer then make one */
366:                if (lastWriterEncoding == null
367:                        || !lastWriterEncoding.equals(enc)) {
368:                    lastWriter = getStreamWriterPrim(enc);
369:                    lastWriterEncoding = enc;
370:                }
371:
372:                /* Ask the writeer for the size the output will be */
373:                int size = lastWriter.sizeOf(buffer, offset, length);
374:
375:                /* Get the output stream */
376:                ByteArrayOutputStream os = new ByteArrayOutputStream(size);
377:
378:                /* Open the writer */
379:                lastWriter.open(os, enc);
380:
381:                try {
382:                    /* Convert */
383:                    lastWriter.write(buffer, offset, length);
384:                    /* Close the writer */
385:                    lastWriter.close();
386:                } catch (IOException x) {
387:                    throw new RuntimeException("IOException writing writer "
388:                            + x.getMessage());
389:                }
390:
391:                /* Close the output stream */
392:                try {
393:                    os.close();
394:                } catch (IOException x) {
395:                }
396:                ;
397:
398:                /* Return the array */
399:                return os.toByteArray();
400:            }
401:
402:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.