OSDN Git Service

* All files: Updated copyright information.
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / convert / UnicodeToBytes.java
1 /* Copyright (C) 1999, 2000  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package gnu.gcj.convert; 
10  
11 public abstract class UnicodeToBytes
12 {
13   /** Buffer to emit bytes to.
14    * The locations buf[count] ... buf[buf.length-1] are available. */
15   public byte[] buf;
16   public int count;
17
18   static Class defaultEncodingClass;
19
20   static synchronized void getDefaultEncodingClass()
21   {
22     // Test (defaultEncodingClass == null) again in case of race condition.
23     if (defaultEncodingClass == null)
24       {
25         String encoding = System.getProperty("file.encoding");
26         String className = "gnu.gcj.convert.Output_"+encoding;
27         try
28           {
29             defaultEncodingClass = Class.forName(className);
30           }
31         catch (ClassNotFoundException ex)
32           {
33             throw new NoClassDefFoundError("missing default encoding "
34                                            + encoding + " (class "
35                                            + className + " not found)");
36             
37           }
38       }
39   }
40
41   public abstract String getName();
42
43   public static UnicodeToBytes getDefaultEncoder()
44   {
45     try
46       {
47         if (defaultEncodingClass == null)
48           getDefaultEncodingClass();
49         return (UnicodeToBytes) defaultEncodingClass.newInstance();
50       }
51     catch (Throwable ex)
52       {
53         return new Output_8859_1();
54       }
55   }
56
57   /** Get a char-stream->byte-stream converter given an encoding name. */
58   public static UnicodeToBytes getEncoder (String encoding)
59     throws java.io.UnsupportedEncodingException
60   {
61     String className = "gnu.gcj.convert.Output_"+encoding;
62     Class encodingClass;
63     try 
64       { 
65         encodingClass = Class.forName(className); 
66         return (UnicodeToBytes) encodingClass.newInstance();
67       } 
68     catch (Throwable ex) 
69       { 
70         try
71           {
72             return new Output_iconv (encoding);
73           }
74         catch (Throwable _)
75           {
76             // Put the original exception in the throwable.
77             throw new java.io.UnsupportedEncodingException(encoding + " ("
78                                                            + ex + ')');
79           }
80       }
81   }
82
83   public final void setOutput(byte[] buffer, int count)
84   {
85     this.buf = buffer;
86     this.count = count;
87   }
88
89   /** Convert chars to bytes.
90     * Converted bytes are written to buf, starting at count.
91     * @param inbuffer source of characters to convert
92     * @param inpos index of initial character in inbuffer to convert
93     * @param inlength number of characters to convert
94     * @return number of chars converted
95     * Also, this.count is increment by the number of bytes converted.
96     */
97   public abstract int write (char[] inbuffer, int inpos, int inlength);
98
99   /** Convert chars to bytes.
100     * Converted bytes are written to buf, starting at count.
101     * @param str source of characters to convert
102     * @param inpos index of initial character in str to convert
103     * @param inlength number of characters to convert
104     * @param work if non-null, a buffer than can be used
105     * @return number of chars converted
106     * Also, this.count is increment by the number of bytes converted.
107     */
108   public int write (String str, int inpos, int inlength, char[] work)
109   {
110     if (work == null)
111       work = new char[inlength];
112     int srcEnd = inpos + (inlength > work.length ? work.length : inlength);
113     str.getChars(inpos, srcEnd, work, 0);
114     return write(work, inpos, inlength);
115   }
116 }