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