1. /*
  2. * @(#)StringBufferInputStream.java 1.21 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * This class allows an application to create an input stream in
  10. * which the bytes read are supplied by the contents of a string.
  11. * Applications can also read bytes from a byte array by using a
  12. * <code>ByteArrayInputStream</code>.
  13. * <p>
  14. * Only the low eight bits of each character in the string are used by
  15. * this class.
  16. *
  17. * @author Arthur van Hoff
  18. * @version 1.21, 11/29/01
  19. * @see java.io.ByteArrayInputStream
  20. * @see java.io.StringReader
  21. * @since JDK1.0
  22. * @deprecated This class does not properly convert characters into bytes. As
  23. * of JDK 1.1, the preferred way to create a stream from a
  24. * string is via the <code>StringReader</code> class.
  25. */
  26. public
  27. class StringBufferInputStream extends InputStream {
  28. /**
  29. * The string from which bytes are read.
  30. */
  31. protected String buffer;
  32. /**
  33. * The index of the next character to read from the input stream buffer.
  34. *
  35. * @see java.io.StringBufferInputStream#buffer
  36. */
  37. protected int pos;
  38. /**
  39. * The number of valid characters in the input stream buffer.
  40. *
  41. * @see java.io.StringBufferInputStream#buffer
  42. */
  43. protected int count;
  44. /**
  45. * Creates a string input stream to read data from the specified string.
  46. *
  47. * @param s the underlying input buffer.
  48. */
  49. public StringBufferInputStream(String s) {
  50. this.buffer = s;
  51. count = s.length();
  52. }
  53. /**
  54. * Reads the next byte of data from this input stream. The value
  55. * byte is returned as an <code>int</code> in the range
  56. * <code>0</code> to <code>255</code>. If no byte is available
  57. * because the end of the stream has been reached, the value
  58. * <code>-1</code> is returned.
  59. * <p>
  60. * The <code>read</code> method of
  61. * <code>StringBufferInputStream</code> cannot block. It returns the
  62. * low eight bits of the next character in this input stream's buffer.
  63. *
  64. * @return the next byte of data, or <code>-1</code> if the end of the
  65. * stream is reached.
  66. */
  67. public synchronized int read() {
  68. return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  69. }
  70. /**
  71. * Reads up to <code>len</code> bytes of data from this input stream
  72. * into an array of bytes.
  73. * <p>
  74. * The <code>read</code> method of
  75. * <code>StringBufferInputStream</code> cannot block. It copies the
  76. * low eight bits from the characters in this input stream's buffer into
  77. * the byte array argument.
  78. *
  79. * @param b the buffer into which the data is read.
  80. * @param off the start offset of the data.
  81. * @param len the maximum number of bytes read.
  82. * @return the total number of bytes read into the buffer, or
  83. * <code>-1</code> if there is no more data because the end of
  84. * the stream has been reached.
  85. */
  86. public synchronized int read(byte b[], int off, int len) {
  87. if (b == null) {
  88. throw new NullPointerException();
  89. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  90. ((off + len) > b.length) || ((off + len) < 0)) {
  91. throw new IndexOutOfBoundsException();
  92. }
  93. if (pos >= count) {
  94. return -1;
  95. }
  96. if (pos + len > count) {
  97. len = count - pos;
  98. }
  99. if (len <= 0) {
  100. return 0;
  101. }
  102. String s = buffer;
  103. int cnt = len;
  104. while (--cnt >= 0) {
  105. b[off++] = (byte)s.charAt(pos++);
  106. }
  107. return len;
  108. }
  109. /**
  110. * Skips <code>n</code> bytes of input from this input stream. Fewer
  111. * bytes might be skipped if the end of the input stream is reached.
  112. *
  113. * @param n the number of bytes to be skipped.
  114. * @return the actual number of bytes skipped.
  115. */
  116. public synchronized long skip(long n) {
  117. if (n < 0) {
  118. return 0;
  119. }
  120. if (n > count - pos) {
  121. n = count - pos;
  122. }
  123. pos += n;
  124. return n;
  125. }
  126. /**
  127. * Returns the number of bytes that can be read from the input
  128. * stream without blocking.
  129. *
  130. * @return the value of <code>count - pos</code>, which is the
  131. * number of bytes remaining to be read from the input buffer.
  132. */
  133. public synchronized int available() {
  134. return count - pos;
  135. }
  136. /**
  137. * Resets the input stream to begin reading from the first character
  138. * of this input stream's underlying buffer.
  139. */
  140. public synchronized void reset() {
  141. pos = 0;
  142. }
  143. }