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: }
|