1. /*
  2. * @(#)StringReader.java 1.17 03/01/23
  3. *
  4. * Copyright 2003 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.17, 03/01/23
  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. * Skip characters.
  81. *
  82. * @exception IOException If an I/O error occurs
  83. */
  84. public long skip(long ns) throws IOException {
  85. synchronized (lock) {
  86. ensureOpen();
  87. if (next >= length)
  88. return 0;
  89. long n = Math.min(length - next, ns);
  90. next += n;
  91. return n;
  92. }
  93. }
  94. /**
  95. * Tell whether this stream is ready to be read.
  96. *
  97. * @return True if the next read() is guaranteed not to block for input
  98. *
  99. * @exception IOException If the stream is closed
  100. */
  101. public boolean ready() throws IOException {
  102. synchronized (lock) {
  103. ensureOpen();
  104. return true;
  105. }
  106. }
  107. /**
  108. * Tell whether this stream supports the mark() operation, which it does.
  109. */
  110. public boolean markSupported() {
  111. return true;
  112. }
  113. /**
  114. * Mark the present position in the stream. Subsequent calls to reset()
  115. * will reposition the stream to this point.
  116. *
  117. * @param readAheadLimit Limit on the number of characters that may be
  118. * read while still preserving the mark. Because
  119. * the stream's input comes from a string, there
  120. * is no actual limit, so this argument must not
  121. * be negative, but is otherwise ignored.
  122. *
  123. * @exception IllegalArgumentException If readAheadLimit is < 0
  124. * @exception IOException If an I/O error occurs
  125. */
  126. public void mark(int readAheadLimit) throws IOException {
  127. if (readAheadLimit < 0){
  128. throw new IllegalArgumentException("Read-ahead limit < 0");
  129. }
  130. synchronized (lock) {
  131. ensureOpen();
  132. mark = next;
  133. }
  134. }
  135. /**
  136. * Reset the stream to the most recent mark, or to the beginning of the
  137. * string if it has never been marked.
  138. *
  139. * @exception IOException If an I/O error occurs
  140. */
  141. public void reset() throws IOException {
  142. synchronized (lock) {
  143. ensureOpen();
  144. next = mark;
  145. }
  146. }
  147. /**
  148. * Close the stream.
  149. */
  150. public void close() {
  151. str = null;
  152. }
  153. }