1:
8:
9: package ;
10:
11: import ;
12:
13: public abstract class UnicodeToBytes extends IOConverter
14: {
15:
17: public byte[] buf;
18: public int count;
19:
20:
21: static String defaultEncoding;
22:
23:
30: private static final int CACHE_SIZE = 4;
31: private static UnicodeToBytes[] encoderCache
32: = new UnicodeToBytes[CACHE_SIZE];
33: private static int currCachePos = 0;
34:
35: public abstract String getName();
36:
37: public static UnicodeToBytes getDefaultEncoder()
38: {
39: try
40: {
41: synchronized (UnicodeToBytes.class)
42: {
43: if (defaultEncoding == null)
44: {
45: String encoding
46: = canonicalize (System.getProperty("file.encoding",
47: "8859_1"));
48: String className = "gnu.gcj.convert.Output_" + encoding;
49: try
50: {
51: Class defaultEncodingClass = Class.forName(className);
52: defaultEncoding = encoding;
53: }
54: catch (ClassNotFoundException ex)
55: {
56: throw new NoClassDefFoundError("missing default encoding "
57: + encoding + " (class "
58: + className
59: + " not found)");
60: }
61: }
62: }
63:
64: return getEncoder (defaultEncoding);
65: }
66: catch (Throwable ex)
67: {
68: return new Output_8859_1();
69: }
70: }
71:
72:
73: public static UnicodeToBytes getEncoder (String encoding)
74: throws java.io.UnsupportedEncodingException
75: {
76:
78: String canonicalEncoding = canonicalize(encoding);
79: synchronized (UnicodeToBytes.class)
80: {
81: int i;
82: for (i = 0; i < encoderCache.length; ++i)
83: {
84: if (encoderCache[i] != null
85: && canonicalEncoding.equals(encoderCache[i].getName ()))
86: {
87: UnicodeToBytes rv = encoderCache[i];
88: encoderCache[i] = null;
89: return rv;
90: }
91: }
92: }
93:
94: String className = "gnu.gcj.convert.Output_" + canonicalEncoding;
95: Class encodingClass;
96: try
97: {
98: encodingClass = Class.forName(className);
99: return (UnicodeToBytes) encodingClass.newInstance();
100: }
101: catch (Throwable ex)
102: {
103: try
104: {
105:
106:
107:
108: return new Output_iconv (encoding);
109: }
110: catch (Throwable _)
111: {
112:
113: }
114: try
115: {
116:
117:
118:
119: return new CharsetToBytesAdaptor(Charset.forName(encoding));
120: }
121: catch (Throwable _)
122: {
123:
124: throw new java.io.UnsupportedEncodingException(encoding + " ("
125: + ex + ')');
126: }
127: }
128: }
129:
130: public final void setOutput(byte[] buffer, int count)
131: {
132: this.buf = buffer;
133: this.count = count;
134: }
135:
136:
144: public abstract int write (char[] inbuffer, int inpos, int inlength);
145:
146:
155: public int write (String str, int inpos, int inlength, char[] work)
156: {
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);
162: }
163:
164:
170: public boolean havePendingBytes()
171: {
172: return false;
173: }
174:
175:
180: public void setFinished()
181: {
182: }
183:
184:
189: public void done ()
190: {
191: synchronized (UnicodeToBytes.class)
192: {
193: this.buf = null;
194: this.count = 0;
195:
196: encoderCache[currCachePos] = this;
197: currCachePos = (currCachePos + 1) % CACHE_SIZE;
198: }
199: }
200: }