StringReader.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* StringReader.java -- permits a String to be read as a character input stream
  2. Copyright (C) 1998, 1999, 2000, 2003 Free Software Foundation
  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 java.io;
  32. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  33. * "The Java Language Specification", ISBN 0-201-63451-1
  34. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  35. * Status: Believed complete and correct
  36. */
  37. /**
  38. * This class permits a <code>String</code> to be read as a character
  39. * input stream.
  40. * <p>
  41. * The mark/reset functionality in this class behaves differently than
  42. * normal. If no mark has been set, then calling the <code>reset()</code>
  43. * method rewinds the read pointer to the beginning of the <code>String</code>.
  44. *
  45. * @author Aaron M. Renn (arenn@urbanophile.com)
  46. * @author Warren Levy (warrenl@cygnus.com)
  47. * @date October 19, 1998.
  48. */
  49. public class StringReader extends Reader
  50. {
  51. /* A String provided by the creator of the stream. */
  52. private String buf;
  53. /* Position of the next char in buf to be read. */
  54. private int pos;
  55. /* The currently marked position in the stream. */
  56. private int markedPos;
  57. /* The index in buf one greater than the last valid character. */
  58. private int count;
  59. /**
  60. * Create a new <code>StringReader</code> that will read chars from the
  61. * passed in <code>String</code>. This stream will read from the beginning
  62. * to the end of the <code>String</code>.
  63. *
  64. * @param buffer The <code>String</code> this stream will read from.
  65. */
  66. public StringReader(String buffer)
  67. {
  68. super();
  69. buf = buffer;
  70. count = buffer.length();
  71. markedPos = pos = 0;
  72. }
  73. public void close()
  74. {
  75. synchronized (lock)
  76. {
  77. buf = null;
  78. }
  79. }
  80. public void mark(int readAheadLimit) throws IOException
  81. {
  82. synchronized (lock)
  83. {
  84. if (buf == null)
  85. throw new IOException("Stream closed");
  86. // readAheadLimit is ignored per Java Class Lib. book, p. 1692.
  87. markedPos = pos;
  88. }
  89. }
  90. public boolean markSupported()
  91. {
  92. return true;
  93. }
  94. public int read() throws IOException
  95. {
  96. synchronized (lock)
  97. {
  98. if (buf == null)
  99. throw new IOException("Stream closed");
  100. if (pos < count)
  101. return ((int) buf.charAt(pos++)) & 0xFFFF;
  102. return -1;
  103. }
  104. }
  105. public int read(char[] b, int off, int len) throws IOException
  106. {
  107. synchronized (lock)
  108. {
  109. if (buf == null)
  110. throw new IOException("Stream closed");
  111. /* Don't need to check pos value, arraycopy will check it. */
  112. if (off < 0 || len < 0 || off + len > b.length)
  113. throw new ArrayIndexOutOfBoundsException();
  114. if (pos >= count)
  115. return -1;
  116. int lastChar = Math.min(count, pos + len);
  117. buf.getChars(pos, lastChar, b, off);
  118. int numChars = lastChar - pos;
  119. pos = lastChar;
  120. return numChars;
  121. }
  122. }
  123. /**
  124. * This method determines if the stream is ready to be read. This class
  125. * is always ready to read and so always returns <code>true</code>, unless
  126. * close() has previously been called in which case an IOException is
  127. * thrown.
  128. *
  129. * @return <code>true</code> to indicate that this object is ready to be read.
  130. * @exception IOException If the stream is closed.
  131. */
  132. public boolean ready() throws IOException
  133. {
  134. if (buf == null)
  135. throw new IOException("Stream closed");
  136. return true;
  137. }
  138. /**
  139. * Sets the read position in the stream to the previously
  140. * marked position or to 0 (i.e., the beginning of the stream) if the mark
  141. * has not already been set.
  142. */
  143. public void reset() throws IOException
  144. {
  145. synchronized (lock)
  146. {
  147. if (buf == null)
  148. throw new IOException("Stream closed");
  149. pos = markedPos;
  150. }
  151. }
  152. /**
  153. * This method attempts to skip the requested number of chars in the
  154. * input stream. It does this by advancing the <code>pos</code> value by
  155. * the specified number of chars. It this would exceed the length of the
  156. * buffer, then only enough chars are skipped to position the stream at
  157. * the end of the buffer. The actual number of chars skipped is returned.
  158. *
  159. * @param n The requested number of chars to skip
  160. *
  161. * @return The actual number of chars skipped.
  162. */
  163. public long skip(long n) throws IOException
  164. {
  165. synchronized (lock)
  166. {
  167. if (buf == null)
  168. throw new IOException("Stream closed");
  169. // Even though the var numChars is a long, in reality it can never
  170. // be larger than an int since the result of subtracting 2 positive
  171. // ints will always fit in an int. Since we have to return a long
  172. // anyway, numChars might as well just be a long.
  173. long numChars = Math.min((long) (count - pos), n < 0 ? 0L : n);
  174. pos += numChars;
  175. return numChars;
  176. }
  177. }
  178. }