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
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. */
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
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()
41 synchronized (UnicodeToBytes
.class)
43 if (defaultEncoding
== null)
46 = canonicalize (System
.getProperty("file.encoding",
48 String className
= "gnu.gcj.convert.Output_" + encoding
;
51 Class defaultEncodingClass
= Class
.forName(className
);
52 defaultEncoding
= encoding
;
54 catch (ClassNotFoundException ex
)
56 throw new NoClassDefFoundError("missing default encoding "
57 + encoding
+ " (class "
64 return getEncoder (defaultEncoding
);
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
78 String canonicalEncoding
= canonicalize(encoding
);
79 synchronized (UnicodeToBytes
.class)
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;
94 String className
= "gnu.gcj.convert.Output_" + canonicalEncoding
;
98 encodingClass
= Class
.forName(className
);
99 return (UnicodeToBytes
) encodingClass
.newInstance();
105 // We pass the original name to iconv and let it handle
106 // its own aliasing. Note that we intentionally prefer
108 return new Output_iconv (encoding
);
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
));
123 // Put the original exception in the throwable.
124 throw new java
.io
.UnsupportedEncodingException(encoding
+ " ("
130 public final void setOutput(byte[] buffer
, int 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
)
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()
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.
182 synchronized (UnicodeToBytes
.class)
187 encoderCache
[currCachePos
] = this;
188 currCachePos
= (currCachePos
+ 1) % CACHE_SIZE
;