Dead
[official-gcc.git] / gomp-20050608-branch / libjava / gnu / gcj / convert / BytesToUnicode.java
blobe3afe3ee97bdb6ddda36f0c0c2acb7dd94fb4851
1 /* Copyright (C) 1999, 2000, 2001, 2005 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 import java.nio.charset.Charset;
13 public abstract class BytesToUnicode extends IOConverter
15 /** Buffer to read bytes from.
16 * The characters inbuffer[inpos] ... inbuffer[inlength-1] are available. */
17 public byte[] inbuffer;
18 /** Starting index in buffer to read bytes from. */
19 public int inpos;
20 /** End of valid bytes in buffer. */
21 public int inlength;
23 // The name of the default encoding.
24 static String defaultEncoding;
26 /* These keep a small cache of decoders for reuse. The array holds
27 the actual decoders. The currCachePos is the next value we are
28 going to replace in the cache. We don't just throw the data away
29 if the cache is full, because if the cache filled up with stuff
30 we don't need then the cache would be worthless. We instead
31 circulate through the cache the implement kind of an LRU
32 algorithm. */
33 private static final int CACHE_SIZE = 4; // A power of 2 for speed
34 private static BytesToUnicode[] decoderCache
35 = new BytesToUnicode[CACHE_SIZE];
36 private static int currCachePos = 0;
38 public abstract String getName();
40 public static BytesToUnicode getDefaultDecoder()
42 try
44 synchronized (BytesToUnicode.class)
46 if (defaultEncoding == null)
48 String encoding
49 = canonicalize (System.getProperty("file.encoding",
50 "8859_1"));
51 String className = "gnu.gcj.convert.Input_" + encoding;
52 try
54 Class defaultDecodingClass = Class.forName(className);
55 defaultEncoding = encoding;
57 catch (ClassNotFoundException ex)
59 throw new NoClassDefFoundError("missing default encoding "
60 + encoding + " (class "
61 + className
62 + " not found)");
66 return getDecoder (defaultEncoding);
68 catch (Throwable ex)
70 return new Input_8859_1();
74 /** Get a byte-stream->char-stream converter given an encoding name. */
75 public static BytesToUnicode getDecoder (String encoding)
76 throws java.io.UnsupportedEncodingException
78 /* First hunt in our cache to see if we have a decoder that is
79 already allocated. */
80 String canonicalEncoding = canonicalize(encoding);
81 synchronized (BytesToUnicode.class)
83 int i;
84 for (i = 0; i < decoderCache.length; ++i)
86 if (decoderCache[i] != null
87 && canonicalEncoding.equals(decoderCache[i].getName ()))
89 BytesToUnicode rv = decoderCache[i];
90 decoderCache[i] = null;
91 return rv;
96 // It's not in the cache, so now we have to do real work.
97 String className = "gnu.gcj.convert.Input_" + canonicalEncoding;
98 Class decodingClass;
99 try
101 decodingClass = Class.forName(className);
102 return (BytesToUnicode) decodingClass.newInstance();
104 catch (Throwable ex)
108 // We pass the original name to iconv and let it handle
109 // its own aliasing. Note that we intentionally prefer
110 // iconv over nio.
111 return new Input_iconv (encoding);
113 catch (Throwable _)
115 // Ignore, and try the next method.
119 return new BytesToCharsetAdaptor(Charset.forName(encoding));
121 catch (Throwable _)
123 throw new java.io.UnsupportedEncodingException(encoding
124 + " (" + ex + ')');
129 /** Make input bytes available to the conversion.
130 * @param buffer source of input bytes
131 * @param pos index of first available byte
132 * @param length one more than index of last available byte
134 public final void setInput(byte[] buffer, int pos, int length)
136 inbuffer = buffer;
137 inpos = pos;
138 inlength = length;
141 /** Convert bytes to chars.
142 * Input bytes are taken from this.inbuffer. The available input
143 * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
144 * @param outbuffer buffer for the converted character
145 * @param outpos position in buffer to start putting converted characters
146 * @param count the maximum number of characters to convert
147 * @return number of chars placed in outbuffer.
148 * Also, this.inpos is incremented by the number of bytes consumed.
150 * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
151 * while the output upper bound is outbuffer[outpos+count-1]. The
152 * justification is that inlength is like the count field of a
153 * BufferedInputStream, while the count parameter is like the
154 * length parameter of a read request.) The count parameter is
155 * also defined to be <= outbuffer.length - outpos (per the specification
156 * of the length parameter for a read request).
158 public abstract int read (char[] outbuffer, int outpos, int count);
160 /** Indicate that the converter is resuable.
161 * This class keeps track of converters on a per-encoding basis.
162 * When done with an encoder you may call this method to indicate
163 * that it can be reused later.
165 public void done ()
167 synchronized (BytesToUnicode.class)
169 this.inbuffer = null;
170 this.inpos = 0;
171 this.inlength = 0;
173 decoderCache[currCachePos] = this;
174 currCachePos = (currCachePos + 1) % CACHE_SIZE;