BytesToUnicode.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (C) 1999, 2000, 2001, 2005 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.gcj.convert;
  7. import java.nio.charset.Charset;
  8. public abstract class BytesToUnicode extends IOConverter
  9. {
  10. /** Buffer to read bytes from.
  11. * The characters inbuffer[inpos] ... inbuffer[inlength-1] are available. */
  12. public byte[] inbuffer;
  13. /** Starting index in buffer to read bytes from. */
  14. public int inpos;
  15. /** End of valid bytes in buffer. */
  16. public int inlength;
  17. // The name of the default encoding.
  18. static String defaultEncoding;
  19. /* These keep a small cache of decoders for reuse. The array holds
  20. the actual decoders. The currCachePos is the next value we are
  21. going to replace in the cache. We don't just throw the data away
  22. if the cache is full, because if the cache filled up with stuff
  23. we don't need then the cache would be worthless. We instead
  24. circulate through the cache the implement kind of an LRU
  25. algorithm. */
  26. private static final int CACHE_SIZE = 4; // A power of 2 for speed
  27. private static BytesToUnicode[] decoderCache
  28. = new BytesToUnicode[CACHE_SIZE];
  29. private static int currCachePos = 0;
  30. public abstract String getName();
  31. public static BytesToUnicode getDefaultDecoder()
  32. {
  33. try
  34. {
  35. synchronized (BytesToUnicode.class)
  36. {
  37. if (defaultEncoding == null)
  38. {
  39. String encoding
  40. = canonicalize (System.getProperty("file.encoding",
  41. "8859_1"));
  42. String className = "gnu.gcj.convert.Input_" + encoding;
  43. try
  44. {
  45. Class defaultDecodingClass = Class.forName(className);
  46. defaultEncoding = encoding;
  47. }
  48. catch (ClassNotFoundException ex)
  49. {
  50. throw new NoClassDefFoundError("missing default encoding "
  51. + encoding + " (class "
  52. + className
  53. + " not found)");
  54. }
  55. }
  56. }
  57. return getDecoder (defaultEncoding);
  58. }
  59. catch (Throwable ex)
  60. {
  61. return new Input_8859_1();
  62. }
  63. }
  64. /** Get a byte-stream->char-stream converter given an encoding name. */
  65. public static BytesToUnicode getDecoder (String encoding)
  66. throws java.io.UnsupportedEncodingException
  67. {
  68. /* First hunt in our cache to see if we have a decoder that is
  69. already allocated. */
  70. String canonicalEncoding = canonicalize(encoding);
  71. synchronized (BytesToUnicode.class)
  72. {
  73. int i;
  74. for (i = 0; i < decoderCache.length; ++i)
  75. {
  76. if (decoderCache[i] != null
  77. && canonicalEncoding.equals(decoderCache[i].getName ()))
  78. {
  79. BytesToUnicode rv = decoderCache[i];
  80. decoderCache[i] = null;
  81. return rv;
  82. }
  83. }
  84. }
  85. // It's not in the cache, so now we have to do real work.
  86. String className = "gnu.gcj.convert.Input_" + canonicalEncoding;
  87. Class decodingClass;
  88. try
  89. {
  90. decodingClass = Class.forName(className);
  91. return (BytesToUnicode) decodingClass.newInstance();
  92. }
  93. catch (Throwable ex)
  94. {
  95. try
  96. {
  97. // We pass the original name to iconv and let it handle
  98. // its own aliasing. Note that we intentionally prefer
  99. // iconv over nio.
  100. return new Input_iconv (encoding);
  101. }
  102. catch (Throwable _)
  103. {
  104. // Ignore, and try the next method.
  105. }
  106. try
  107. {
  108. return new BytesToCharsetAdaptor(Charset.forName(encoding));
  109. }
  110. catch (Throwable _)
  111. {
  112. throw new java.io.UnsupportedEncodingException(encoding
  113. + " (" + ex + ')');
  114. }
  115. }
  116. }
  117. /** Make input bytes available to the conversion.
  118. * @param buffer source of input bytes
  119. * @param pos index of first available byte
  120. * @param length one more than index of last available byte
  121. */
  122. public final void setInput(byte[] buffer, int pos, int length)
  123. {
  124. inbuffer = buffer;
  125. inpos = pos;
  126. inlength = length;
  127. }
  128. /** Convert bytes to chars.
  129. * Input bytes are taken from this.inbuffer. The available input
  130. * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
  131. * @param outbuffer buffer for the converted character
  132. * @param outpos position in buffer to start putting converted characters
  133. * @param count the maximum number of characters to convert
  134. * @return number of chars placed in outbuffer.
  135. * Also, this.inpos is incremented by the number of bytes consumed.
  136. *
  137. * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
  138. * while the output upper bound is outbuffer[outpos+count-1]. The
  139. * justification is that inlength is like the count field of a
  140. * BufferedInputStream, while the count parameter is like the
  141. * length parameter of a read request.) The count parameter is
  142. * also defined to be <= outbuffer.length - outpos (per the specification
  143. * of the length parameter for a read request).
  144. */
  145. public abstract int read (char[] outbuffer, int outpos, int count);
  146. /** Indicate that the converter is resuable.
  147. * This class keeps track of converters on a per-encoding basis.
  148. * When done with an encoder you may call this method to indicate
  149. * that it can be reused later.
  150. */
  151. public void done ()
  152. {
  153. synchronized (BytesToUnicode.class)
  154. {
  155. this.inbuffer = null;
  156. this.inpos = 0;
  157. this.inlength = 0;
  158. decoderCache[currCachePos] = this;
  159. currCachePos = (currCachePos + 1) % CACHE_SIZE;
  160. }
  161. }
  162. }