ReaderInputStream.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* ReaderInputStream.java --
  2. Copyright (C) 1999, 2000, 2001, 2004 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.dom.ls;
  32. import java.io.InputStream;
  33. import java.io.IOException;
  34. import java.io.Reader;
  35. /**
  36. * Character stream wrapper.
  37. *
  38. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  39. * @author <a href='mailto:mark@klomp.org'>Mark Wielaard</a>
  40. */
  41. public class ReaderInputStream
  42. extends InputStream
  43. {
  44. private Reader reader;
  45. private String encoding;
  46. // Holds extra spillover data if necessary
  47. private byte extra[];
  48. private int pos;
  49. private byte extra_marked[];
  50. private int pos_marked;
  51. public ReaderInputStream(Reader reader)
  52. {
  53. this.reader = reader;
  54. this.encoding = "UTF-8";
  55. }
  56. void setEncoding(String encoding)
  57. {
  58. this.encoding = encoding;
  59. }
  60. public int read()
  61. throws IOException
  62. {
  63. if (extra != null)
  64. {
  65. int result = extra[pos];
  66. pos++;
  67. if (pos >= extra.length)
  68. {
  69. extra = null;
  70. }
  71. return result;
  72. }
  73. return reader.read();
  74. }
  75. public int read(byte[] b)
  76. throws IOException
  77. {
  78. return read(b, 0, b.length);
  79. }
  80. public int read(byte[] b, int off, int len)
  81. throws IOException
  82. {
  83. if (len == 0)
  84. {
  85. return 0;
  86. }
  87. if (extra != null)
  88. {
  89. int available = extra.length - pos;
  90. int l = available < len ? available : len;
  91. System.arraycopy(extra, 0, b, off, l);
  92. pos += l;
  93. if (pos >= extra.length)
  94. {
  95. extra = null;
  96. }
  97. return l;
  98. }
  99. char[] c = new char[len];
  100. int l = reader.read(c, 0, len);
  101. if (l == -1)
  102. {
  103. return -1;
  104. }
  105. String s = new String(c, 0, l);
  106. byte[] d = s.getBytes(encoding);
  107. int available = d.length;
  108. int more = d.length - len;
  109. if (more > 0)
  110. {
  111. extra = new byte[more];
  112. pos = 0;
  113. System.arraycopy(d, len, extra, 0, more);
  114. available -= more;
  115. }
  116. System.arraycopy(d, 0, b, off, available);
  117. return available;
  118. }
  119. public void close()
  120. throws IOException
  121. {
  122. reader.close();
  123. }
  124. public boolean markSupported()
  125. {
  126. return reader.markSupported();
  127. }
  128. public void mark(int limit)
  129. {
  130. if (extra != null)
  131. {
  132. extra_marked = new byte[extra.length];
  133. System.arraycopy(extra, 0, extra_marked, 0, extra.length);
  134. pos_marked = pos;
  135. }
  136. else
  137. {
  138. extra_marked = null;
  139. }
  140. try
  141. {
  142. // Note that this might be a bit more than asked for.
  143. // Because we might also have the extra_marked bytes.
  144. // That is fine (and necessary for reset() to work).
  145. reader.mark(limit);
  146. }
  147. catch (IOException ioe)
  148. {
  149. throw new RuntimeException(ioe);
  150. }
  151. }
  152. public void reset()
  153. throws IOException
  154. {
  155. extra = extra_marked;
  156. pos = pos_marked;
  157. extra_marked = null;
  158. reader.reset();
  159. }
  160. public long skip(long n)
  161. throws IOException
  162. {
  163. long done = 0;
  164. if (extra != null)
  165. {
  166. int available = extra.length - pos;
  167. done = available < n ? available : n;
  168. pos += done;
  169. if (pos >= extra.length)
  170. {
  171. extra = null;
  172. }
  173. }
  174. n -= done;
  175. if (n > 0)
  176. {
  177. return reader.skip(n) + done;
  178. }
  179. else
  180. {
  181. return done;
  182. }
  183. }
  184. /**
  185. * Returns conservative number of bytes available without blocking.
  186. * Actual number of bytes that can be read without blocking might
  187. * be (much) bigger.
  188. */
  189. public int available()
  190. throws IOException
  191. {
  192. if (extra != null)
  193. {
  194. return pos - extra.length;
  195. }
  196. return reader.ready() ? 1 : 0;
  197. }
  198. public String toString()
  199. {
  200. return getClass().getName() + "[" + reader + ", " + encoding + "]";
  201. }
  202. }