OSDN Git Service

6e4d15dab9f69c0c37b62fea3884929171354c34
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / convert / BytesToUnicode.java
1 /* Copyright (C) 1999  Cygnus Solutions
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 BytesToUnicode
12 {
13   /** Buffer to read bytes from.
14    * The characters inbuffer[inpos] ... inbuffer[inlength-1] are available. */
15   public byte[] inbuffer;
16   /** Starting index in buffer to read bytes from. */
17   public int inpos;
18   /** End of valid bytes in buffer. */
19   public int inlength;
20
21   static Class defaultDecodingClass;
22
23   static synchronized void getDefaultDecodingClass()
24   {
25     // Test (defaultDecodingClass == null) again in case of race condition.
26     if (defaultDecodingClass == null)
27       {
28         String encoding = System.getProperty("file.encoding");
29         String className = "gnu.gcj.convert.Input_"+encoding;
30         try
31           {
32             defaultDecodingClass = Class.forName(className);
33           }
34         catch (ClassNotFoundException ex)
35           {
36             throw new NoClassDefFoundError("missing default encoding "
37                                            + encoding + " (class "
38                                            + className + " not found)");
39           }
40       }
41   }
42
43   public abstract String getName();
44
45   public static BytesToUnicode getDefaultDecoder()
46   {
47     try
48       {
49         if (defaultDecodingClass == null)
50           getDefaultDecodingClass();
51         return (BytesToUnicode) defaultDecodingClass.newInstance();
52       }
53     catch (Throwable ex)
54       {
55         return new Input_8859_1();
56       }
57   }
58
59   /** Get a byte-stream->char-stream converter given an encoding name. */
60   public static BytesToUnicode getDecoder (String encoding)
61     throws java.io.UnsupportedEncodingException
62   {
63     String className = "gnu.gcj.convert.Input_"+encoding;
64     Class decodingClass;
65     try 
66       { 
67         decodingClass = Class.forName(className); 
68         return (BytesToUnicode) decodingClass.newInstance();
69       } 
70     catch (Throwable ex) 
71       { 
72         throw new java.io.UnsupportedEncodingException(encoding
73                                                        + " (" + ex + ')');
74       }
75   }
76
77   /** Make input bytes available to the conversion.
78    * @param buffer source of input bytes
79    * @param pos index of first available byte
80    * @param length one more than index of last available byte
81    */
82   public final void setInput(byte[] buffer, int pos, int length)
83   {
84     inbuffer = buffer;
85     inpos = pos;
86     inlength = length;
87   }
88
89   /** Convert bytes to chars.
90    * Input bytes are taken from this.inbuffer.  The available input
91    * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
92    * @param outbuffer buffer for the converted character
93    * @param outpos position in buffer to start putting converted characters
94    * @param count the maximum number of characters to convert
95    * @return number of chars placed in outbuffer.
96    * Also, this.inpos is incremented by the number of bytes consumed.
97    *
98    * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
99    * while the output upper bound is outbuffer[outpos+count-1].  The
100    * justification is that inlength is like the count field of a
101    * BufferedInputStream, while the count parameter is like the
102    * length parameter of a read request.)  The count parameter is
103    * also defined to be <= outbuffer.length - outpos (per the specification
104    * of the length parameter for a read request).
105    */
106   public abstract int read (char[] outbuffer, int outpos, int count);
107 }