FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / gcj / convert / BytesToUnicode.java
blobe7d042baab3a9bb8ba40a8a5ac1d2c35b845d579
1 /* Copyright (C) 1999, 2000, 2001 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.gcj.convert;
11 public abstract class BytesToUnicode extends IOConverter
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;
21 // The name of the default encoding.
22 static String defaultEncoding;
24 /* These keep a small cache of decoders for reuse. The array holds
25 the actual decoders. The currCachePos is the next value we are
26 going to replace in the cache. We don't just throw the data away
27 if the cache is full, because if the cache filled up with stuff
28 we don't need then the cache would be worthless. We instead
29 circulate through the cache the implement kind of an LRU
30 algorithm. */
31 private static final int CACHE_SIZE = 4; // A power of 2 for speed
32 private static BytesToUnicode[] decoderCache
33 = new BytesToUnicode[CACHE_SIZE];
34 private static int currCachePos = 0;
36 public abstract String getName();
38 public static BytesToUnicode getDefaultDecoder()
40 try
42 synchronized (BytesToUnicode.class)
44 if (defaultEncoding == null)
46 String encoding
47 = canonicalize (System.getProperty("file.encoding",
48 "8859_1"));
49 String className = "gnu.gcj.convert.Input_" + encoding;
50 try
52 Class defaultDecodingClass = Class.forName(className);
53 defaultEncoding = encoding;
55 catch (ClassNotFoundException ex)
57 throw new NoClassDefFoundError("missing default encoding "
58 + encoding + " (class "
59 + className
60 + " not found)");
64 return getDecoder (defaultEncoding);
66 catch (Throwable ex)
68 return new Input_8859_1();
72 /** Get a byte-stream->char-stream converter given an encoding name. */
73 public static BytesToUnicode getDecoder (String encoding)
74 throws java.io.UnsupportedEncodingException
76 /* First hunt in our cache to see if we have a decoder that is
77 already allocated. */
78 synchronized (BytesToUnicode.class)
80 int i;
81 for (i = 0; i < decoderCache.length; ++i)
83 if (decoderCache[i] != null
84 && encoding.equals(decoderCache[i].getName ()))
86 BytesToUnicode rv = decoderCache[i];
87 decoderCache[i] = null;
88 return rv;
93 // It's not in the cache, so now we have to do real work.
94 String className = "gnu.gcj.convert.Input_" + canonicalize (encoding);
95 Class decodingClass;
96 try
98 decodingClass = Class.forName(className);
99 return (BytesToUnicode) decodingClass.newInstance();
101 catch (Throwable ex)
105 // We pass the original name to iconv and let it handle
106 // its own aliasing.
107 return new Input_iconv (encoding);
109 catch (Throwable _)
111 throw new java.io.UnsupportedEncodingException(encoding
112 + " (" + ex + ')');
117 /** Make input bytes available to the conversion.
118 * @param buffer source of input bytes
119 * @param pos index of first available byte
120 * @param length one more than index of last available byte
122 public final void setInput(byte[] buffer, int pos, int length)
124 inbuffer = buffer;
125 inpos = pos;
126 inlength = length;
129 /** Convert bytes to chars.
130 * Input bytes are taken from this.inbuffer. The available input
131 * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
132 * @param outbuffer buffer for the converted character
133 * @param outpos position in buffer to start putting converted characters
134 * @param count the maximum number of characters to convert
135 * @return number of chars placed in outbuffer.
136 * Also, this.inpos is incremented by the number of bytes consumed.
138 * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
139 * while the output upper bound is outbuffer[outpos+count-1]. The
140 * justification is that inlength is like the count field of a
141 * BufferedInputStream, while the count parameter is like the
142 * length parameter of a read request.) The count parameter is
143 * also defined to be <= outbuffer.length - outpos (per the specification
144 * of the length parameter for a read request).
146 public abstract int read (char[] outbuffer, int outpos, int count);
148 /** Indicate that the converter is resuable.
149 * This class keeps track of converters on a per-encoding basis.
150 * When done with an encoder you may call this method to indicate
151 * that it can be reused later.
153 public void done ()
155 synchronized (BytesToUnicode.class)
157 this.inbuffer = null;
158 this.inpos = 0;
159 this.inlength = 0;
161 decoderCache[currCachePos] = this;
162 currCachePos = (currCachePos + 1) % CACHE_SIZE;