1. /*
  2. * @(#)StringBufferInputStream.java 1.26 04/05/18
  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. * 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.26, 05/18/04
  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. @Deprecated
  27. public
  28. class StringBufferInputStream extends InputStream {
  29. /**
  30. * The string from which bytes are read.
  31. */
  32. protected String buffer;
  33. /**
  34. * The index of the next character to read from the input stream buffer.
  35. *
  36. * @see java.io.StringBufferInputStream#buffer
  37. */
  38. protected int pos;
  39. /**
  40. * The number of valid characters in the input stream buffer.
  41. *
  42. * @see java.io.StringBufferInputStream#buffer
  43. */
  44. protected int count;
  45. /**
  46. * Creates a string input stream to read data from the specified string.
  47. *
  48. * @param s the underlying input buffer.
  49. */
  50. public StringBufferInputStream(String s) {
  51. this.buffer = s;
  52. count = s.length();
  53. }
  54. /**
  55. * Reads the next byte of data from this input stream. The value
  56. * byte is returned as an <code>int</code> in the range
  57. * <code>0</code> to <code>255</code>. If no byte is available
  58. * because the end of the stream has been reached, the value
  59. * <code>-1</code> is returned.
  60. * <p>
  61. * The <code>read</code> method of
  62. * <code>StringBufferInputStream</code> cannot block. It returns the
  63. * low eight bits of the next character in this input stream's buffer.
  64. *
  65. * @return the next byte of data, or <code>-1</code> if the end of the
  66. * stream is reached.
  67. */
  68. public synchronized int read() {
  69. return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  70. }
  71. /**
  72. * Reads up to <code>len</code> bytes of data from this input stream
  73. * into an array of bytes.
  74. * <p>
  75. * The <code>read</code> method of
  76. * <code>StringBufferInputStream</code> cannot block. It copies the
  77. * low eight bits from the characters in this input stream's buffer into
  78. * the byte array argument.
  79. *
  80. * @param b the buffer into which the data is read.
  81. * @param off the start offset of the data.
  82. * @param len the maximum number of bytes read.
  83. * @return the total number of bytes read into the buffer, or
  84. * <code>-1</code> if there is no more data because the end of
  85. * the stream has been reached.
  86. */
  87. public synchronized int read(byte b[], int off, int len) {
  88. if (b == null) {
  89. throw new NullPointerException();
  90. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  91. ((off + len) > b.length) || ((off + len) < 0)) {
  92. throw new IndexOutOfBoundsException();
  93. }
  94. if (pos >= count) {
  95. return -1;
  96. }
  97. if (pos + len > count) {
  98. len = count - pos;
  99. }
  100. if (len <= 0) {
  101. return 0;
  102. }
  103. String s = buffer;
  104. int cnt = len;
  105. while (--cnt >= 0) {
  106. b[off++] = (byte)s.charAt(pos++);
  107. }
  108. return len;
  109. }
  110. /**
  111. * Skips <code>n</code> bytes of input from this input stream. Fewer
  112. * bytes might be skipped if the end of the input stream is reached.
  113. *
  114. * @param n the number of bytes to be skipped.
  115. * @return the actual number of bytes skipped.
  116. */
  117. public synchronized long skip(long n) {
  118. if (n < 0) {
  119. return 0;
  120. }
  121. if (n > count - pos) {
  122. n = count - pos;
  123. }
  124. pos += n;
  125. return n;
  126. }
  127. /**
  128. * Returns the number of bytes that can be read from the input
  129. * stream without blocking.
  130. *
  131. * @return the value of <code>count - pos</code>, which is the
  132. * number of bytes remaining to be read from the input buffer.
  133. */
  134. public synchronized int available() {
  135. return count - pos;
  136. }
  137. /**
  138. * Resets the input stream to begin reading from the first character
  139. * of this input stream's underlying buffer.
  140. */
  141. public synchronized void reset() {
  142. pos = 0;
  143. }
  144. }