Merge from mainline
[official-gcc.git] / libjava / gnu / gcj / convert / UnicodeToBytes.java
blob8522bec117ac3dd15c204a65589a9722e2cc3a78
1 /* Copyright (C) 1999, 2000, 2001, 2003, 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 UnicodeToBytes extends IOConverter
15 /** Buffer to emit bytes to.
16 * The locations buf[count] ... buf[buf.length-1] are available. */
17 public byte[] buf;
18 public int count;
20 // The name of the default encoding.
21 static String defaultEncoding;
23 /* These keep a small cache of encoders for reuse. The array holds
24 the actual encoders. The currCachePos is the next value we are
25 going to replace in the cache. We don't just throw the data away
26 if the cache is full, because if the cache filled up with stuff we
27 don't need then the cache would be worthless. We instead
28 circulate through the cache the implement kind of an LRU
29 algorithm. */
30 private static final int CACHE_SIZE = 4; // A power of 2 for speed
31 private static UnicodeToBytes[] encoderCache
32 = new UnicodeToBytes[CACHE_SIZE];
33 private static int currCachePos = 0;
35 public abstract String getName();
37 public static UnicodeToBytes getDefaultEncoder()
39 try
41 synchronized (UnicodeToBytes.class)
43 if (defaultEncoding == null)
45 String encoding
46 = canonicalize (System.getProperty("file.encoding",
47 "8859_1"));
48 String className = "gnu.gcj.convert.Output_" + encoding;
49 try
51 Class defaultEncodingClass = Class.forName(className);
52 defaultEncoding = encoding;
54 catch (ClassNotFoundException ex)
56 throw new NoClassDefFoundError("missing default encoding "
57 + encoding + " (class "
58 + className
59 + " not found)");
64 return getEncoder (defaultEncoding);
66 catch (Throwable ex)
68 return new Output_8859_1();
72 /** Get a char-stream->byte-stream converter given an encoding name. */
73 public static UnicodeToBytes getEncoder (String encoding)
74 throws java.io.UnsupportedEncodingException
76 /* First hunt in our cache to see if we have a encoder that is
77 already allocated. */
78 String canonicalEncoding = canonicalize(encoding);
79 synchronized (UnicodeToBytes.class)
81 int i;
82 for (i = 0; i < encoderCache.length; ++i)
84 if (encoderCache[i] != null
85 && canonicalEncoding.equals(encoderCache[i].getName ()))
87 UnicodeToBytes rv = encoderCache[i];
88 encoderCache[i] = null;
89 return rv;
94 String className = "gnu.gcj.convert.Output_" + canonicalEncoding;
95 Class encodingClass;
96 try
98 encodingClass = Class.forName(className);
99 return (UnicodeToBytes) encodingClass.newInstance();
101 catch (Throwable ex)
105 // We pass the original name to iconv and let it handle
106 // its own aliasing. Note that we intentionally prefer
107 // iconv over nio.
108 return new Output_iconv (encoding);
110 catch (Throwable _)
112 // Ignore, and try the next method.
116 // Try using finding java.nio.charset.Charset and using
117 // the adaptor. Use the original name as Charsets have
118 // their own canonical names.
119 return new CharsetToBytesAdaptor(Charset.forName(encoding));
121 catch (Throwable _)
123 // Put the original exception in the throwable.
124 throw new java.io.UnsupportedEncodingException(encoding + " ("
125 + ex + ')');
130 public final void setOutput(byte[] buffer, int count)
132 this.buf = buffer;
133 this.count = count;
136 /** Convert chars to bytes.
137 * Converted bytes are written to buf, starting at count.
138 * @param inbuffer source of characters to convert
139 * @param inpos index of initial character in inbuffer to convert
140 * @param inlength number of characters to convert
141 * @return number of chars converted
142 * Also, this.count is increment by the number of bytes converted.
144 public abstract int write (char[] inbuffer, int inpos, int inlength);
146 /** Convert chars to bytes.
147 * Converted bytes are written to buf, starting at count.
148 * @param str source of characters to convert
149 * @param inpos index of initial character in str to convert
150 * @param inlength number of characters to convert
151 * @param work if non-null, a buffer than can be used
152 * @return number of chars converted
153 * Also, this.count is increment by the number of bytes converted.
155 public int write (String str, int inpos, int inlength, char[] work)
157 if (work == null)
158 work = new char[inlength];
159 int srcEnd = inpos + (inlength > work.length ? work.length : inlength);
160 str.getChars(inpos, srcEnd, work, 0);
161 return write(work, 0, srcEnd - inpos);
165 * Returns true when the converter has consumed some bytes that are
166 * not yet converted to characters because further continuation
167 * bytes are needed. Defaults to false, should be overridden by
168 * decoders that internally store some bytes.
170 public boolean havePendingBytes()
172 return false;
175 /** Indicate that the converter is resuable.
176 * This class keeps track of converters on a per-encoding basis.
177 * When done with an encoder you may call this method to indicate
178 * that it can be reused later.
180 public void done ()
182 synchronized (UnicodeToBytes.class)
184 this.buf = null;
185 this.count = 0;
187 encoderCache[currCachePos] = this;
188 currCachePos = (currCachePos + 1) % CACHE_SIZE;