UnicodeReader.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* UnicodeReader.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.stream;
  32. import java.io.IOException;
  33. import java.io.Reader;
  34. /**
  35. * A reader that converts UTF-16 characters to Unicode code points.
  36. *
  37. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  38. */
  39. public class UnicodeReader
  40. {
  41. final Reader in;
  42. UnicodeReader(Reader in)
  43. {
  44. this.in = in;
  45. }
  46. public void mark(int limit)
  47. throws IOException
  48. {
  49. in.mark(limit * 2);
  50. }
  51. public void reset()
  52. throws IOException
  53. {
  54. in.reset();
  55. }
  56. public int read()
  57. throws IOException
  58. {
  59. int ret = in.read();
  60. if (ret == -1)
  61. return ret;
  62. if (ret >= 0xd800 && ret < 0xdc00)
  63. {
  64. // Unicode surrogate?
  65. int low = in.read();
  66. if (low >= 0xdc00 && low < 0xe000)
  67. ret = Character.toCodePoint((char) ret, (char) low);
  68. else
  69. throw new IOException("unpaired surrogate: U+" +
  70. Integer.toHexString(ret));
  71. }
  72. else if (ret >= 0xdc00 && ret < 0xe000)
  73. throw new IOException("unpaired surrogate: U+" +
  74. Integer.toHexString(ret));
  75. return ret;
  76. }
  77. public int read(int[] buf, int off, int len)
  78. throws IOException
  79. {
  80. if (len == 0)
  81. return 0;
  82. char[] b2 = new char[len];
  83. int ret = in.read(b2, 0, len);
  84. if (ret <= 0)
  85. return ret;
  86. int l = ret - 1;
  87. int i = 0, j = off;
  88. for (; i < l; i++)
  89. {
  90. char c = b2[i];
  91. if (c >= 0xd800 && c < 0xdc00)
  92. {
  93. // Unicode surrogate?
  94. char d = b2[i + 1];
  95. if (d >= 0xdc00 && d < 0xe000)
  96. {
  97. buf[j++] = Character.toCodePoint(c, d);
  98. i++;
  99. continue;
  100. }
  101. else
  102. throw new IOException("unpaired surrogate: U+" +
  103. Integer.toHexString(c));
  104. }
  105. else if (c >= 0xdc00 && c < 0xe000)
  106. throw new IOException("unpaired surrogate: U+" +
  107. Integer.toHexString(c));
  108. buf[j++] = (int) c;
  109. }
  110. if (i == l)
  111. {
  112. // last char
  113. char c = b2[l];
  114. if (c >= 0xd800 && c < 0xdc00)
  115. {
  116. int low = in.read();
  117. if (low >= 0xdc00 && low < 0xe000)
  118. {
  119. buf[j++] = Character.toCodePoint(c, (char) low);
  120. return j;
  121. }
  122. else
  123. throw new IOException("unpaired surrogate: U+" +
  124. Integer.toHexString(c));
  125. }
  126. else if (c >= 0xdc00 && c < 0xe000)
  127. throw new IOException("unpaired surrogate: U+" +
  128. Integer.toHexString(c));
  129. buf[j++] = (int) c;
  130. }
  131. return j;
  132. }
  133. public void close()
  134. throws IOException
  135. {
  136. in.close();
  137. }
  138. /**
  139. * Returns the specified UTF-16 char array as an array of Unicode code
  140. * points.
  141. */
  142. public static int[] toCodePointArray(String text)
  143. throws IOException
  144. {
  145. char[] b2 = text.toCharArray();
  146. int[] buf = new int[b2.length];
  147. if (b2.length > 0)
  148. {
  149. int l = b2.length - 1;
  150. int i = 0, j = 0;
  151. for (; i < l; i++)
  152. {
  153. char c = b2[i];
  154. if (c >= 0xd800 && c < 0xdc00)
  155. {
  156. // Unicode surrogate?
  157. char d = b2[i + 1];
  158. if (d >= 0xdc00 && d < 0xe000)
  159. {
  160. buf[j++] = Character.toCodePoint(c, d);
  161. i++;
  162. continue;
  163. }
  164. else
  165. throw new IOException("unpaired surrogate: U+" +
  166. Integer.toHexString(c));
  167. }
  168. else if (c >= 0xdc00 && c < 0xe000)
  169. throw new IOException("unpaired surrogate: U+" +
  170. Integer.toHexString(c));
  171. buf[j++] = (int) c;
  172. }
  173. if (i == l)
  174. {
  175. // last char
  176. buf[j++] = (int) b2[l];
  177. if (j < buf.length)
  178. {
  179. int[] buf2 = new int[j];
  180. System.arraycopy(buf, 0, buf2, 0, j);
  181. buf = buf2;
  182. }
  183. }
  184. }
  185. return buf;
  186. }
  187. }