1. /*
  2. * @(#)StringReader.java 1.24 04/02/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * A character stream whose source is a string.
  10. *
  11. * @version 1.24, 04/02/19
  12. * @author Mark Reinhold
  13. * @since JDK1.1
  14. */
  15. public class StringReader extends Reader {
  16. private String str;
  17. private int length;
  18. private int next = 0;
  19. private int mark = 0;
  20. /**
  21. * Create a new string reader.
  22. *
  23. * @param s String providing the character stream.
  24. */
  25. public StringReader(String s) {
  26. this.str = s;
  27. this.length = s.length();
  28. }
  29. /** Check to make sure that the stream has not been closed */
  30. private void ensureOpen() throws IOException {
  31. if (str == null)
  32. throw new IOException("Stream closed");
  33. }
  34. /**
  35. * Read a single character.
  36. *
  37. * @return The character read, or -1 if the end of the stream has been
  38. * reached
  39. *
  40. * @exception IOException If an I/O error occurs
  41. */
  42. public int read() throws IOException {
  43. synchronized (lock) {
  44. ensureOpen();
  45. if (next >= length)
  46. return -1;
  47. return str.charAt(next++);
  48. }
  49. }
  50. /**
  51. * Read characters into a portion of an array.
  52. *
  53. * @param cbuf Destination buffer
  54. * @param off Offset at which to start writing characters
  55. * @param len Maximum number of characters to read
  56. *
  57. * @return The number of characters read, or -1 if the end of the
  58. * stream has been reached
  59. *
  60. * @exception IOException If an I/O error occurs
  61. */
  62. public int read(char cbuf[], int off, int len) throws IOException {
  63. synchronized (lock) {
  64. ensureOpen();
  65. if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  66. ((off + len) > cbuf.length) || ((off + len) < 0)) {
  67. throw new IndexOutOfBoundsException();
  68. } else if (len == 0) {
  69. return 0;
  70. }
  71. if (next >= length)
  72. return -1;
  73. int n = Math.min(length - next, len);
  74. str.getChars(next, next + n, cbuf, off);
  75. next += n;
  76. return n;
  77. }
  78. }
  79. /**
  80. * Skips the specified number of characters in the stream. Returns
  81. * the number of characters that were skipped.
  82. *
  83. * <p>The <code>ns</code> parameter may be negative, even though the
  84. * <code>skip</code> method of the {@link Reader} superclass throws
  85. * an exception in this case. Negative values of <code>ns</code> cause the
  86. * stream to skip backwards. Negative return values indicate a skip
  87. * backwards. It is not possible to skip backwards past the beginning of
  88. * the string.
  89. *
  90. * <p>If the entire string has been read or skipped, then this method has
  91. * no effect and always returns 0.
  92. *
  93. * @exception IOException If an I/O error occurs
  94. */
  95. public long skip(long ns) throws IOException {
  96. synchronized (lock) {
  97. ensureOpen();
  98. if (next >= length)
  99. return 0;
  100. // Bound skip by beginning and end of the source
  101. long n = Math.min(length - next, ns);
  102. n = Math.max(-next, n);
  103. next += n;
  104. return n;
  105. }
  106. }
  107. /**
  108. * Tell whether this stream is ready to be read.
  109. *
  110. * @return True if the next read() is guaranteed not to block for input
  111. *
  112. * @exception IOException If the stream is closed
  113. */
  114. public boolean ready() throws IOException {
  115. synchronized (lock) {
  116. ensureOpen();
  117. return true;
  118. }
  119. }
  120. /**
  121. * Tell whether this stream supports the mark() operation, which it does.
  122. */
  123. public boolean markSupported() {
  124. return true;
  125. }
  126. /**
  127. * Mark the present position in the stream. Subsequent calls to reset()
  128. * will reposition the stream to this point.
  129. *
  130. * @param readAheadLimit Limit on the number of characters that may be
  131. * read while still preserving the mark. Because
  132. * the stream's input comes from a string, there
  133. * is no actual limit, so this argument must not
  134. * be negative, but is otherwise ignored.
  135. *
  136. * @exception IllegalArgumentException If readAheadLimit is < 0
  137. * @exception IOException If an I/O error occurs
  138. */
  139. public void mark(int readAheadLimit) throws IOException {
  140. if (readAheadLimit < 0){
  141. throw new IllegalArgumentException("Read-ahead limit < 0");
  142. }
  143. synchronized (lock) {
  144. ensureOpen();
  145. mark = next;
  146. }
  147. }
  148. /**
  149. * Reset the stream to the most recent mark, or to the beginning of the
  150. * string if it has never been marked.
  151. *
  152. * @exception IOException If an I/O error occurs
  153. */
  154. public void reset() throws IOException {
  155. synchronized (lock) {
  156. ensureOpen();
  157. next = mark;
  158. }
  159. }
  160. /**
  161. * Close the stream.
  162. */
  163. public void close() {
  164. str = null;
  165. }
  166. }