UnicodeToBytes.java [plain text]
package gnu.gcj.convert;
public abstract class UnicodeToBytes extends IOConverter
{
public byte[] buf;
public int count;
static String defaultEncoding;
private static final int CACHE_SIZE = 4; private static UnicodeToBytes[] encoderCache
= new UnicodeToBytes[CACHE_SIZE];
private static int currCachePos = 0;
public abstract String getName();
public static UnicodeToBytes getDefaultEncoder()
{
try
{
synchronized (UnicodeToBytes.class)
{
if (defaultEncoding == null)
{
String encoding
= canonicalize (System.getProperty("file.encoding",
"8859_1"));
String className = "gnu.gcj.convert.Output_" + encoding;
try
{
Class defaultEncodingClass = Class.forName(className);
defaultEncoding = encoding;
}
catch (ClassNotFoundException ex)
{
throw new NoClassDefFoundError("missing default encoding "
+ encoding + " (class "
+ className
+ " not found)");
}
}
}
return getEncoder (defaultEncoding);
}
catch (Throwable ex)
{
return new Output_8859_1();
}
}
public static UnicodeToBytes getEncoder (String encoding)
throws java.io.UnsupportedEncodingException
{
synchronized (UnicodeToBytes.class)
{
int i;
for (i = 0; i < encoderCache.length; ++i)
{
if (encoderCache[i] != null
&& encoding.equals(encoderCache[i].getName ()))
{
UnicodeToBytes rv = encoderCache[i];
encoderCache[i] = null;
return rv;
}
}
}
String className = "gnu.gcj.convert.Output_" + canonicalize (encoding);
Class encodingClass;
try
{
encodingClass = Class.forName(className);
return (UnicodeToBytes) encodingClass.newInstance();
}
catch (Throwable ex)
{
try
{
return new Output_iconv (encoding);
}
catch (Throwable _)
{
throw new java.io.UnsupportedEncodingException(encoding + " ("
+ ex + ')');
}
}
}
public final void setOutput(byte[] buffer, int count)
{
this.buf = buffer;
this.count = count;
}
public abstract int write (char[] inbuffer, int inpos, int inlength);
public int write (String str, int inpos, int inlength, char[] work)
{
if (work == null)
work = new char[inlength];
int srcEnd = inpos + (inlength > work.length ? work.length : inlength);
str.getChars(inpos, srcEnd, work, 0);
return write(work, 0, srcEnd - inpos);
}
public void done ()
{
synchronized (UnicodeToBytes.class)
{
this.buf = null;
this.count = 0;
encoderCache[currCachePos] = this;
currCachePos = (currCachePos + 1) % CACHE_SIZE;
}
}
}