StringBufferInputStream.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* StringBufferInputStream.java -- Read an String as a stream
  2. Copyright (C) 1998, 1999, 2001, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  33. * "The Java Language Specification", ISBN 0-201-63451-1
  34. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  35. * Status: Believed complete and correct. Deprecated in JDK 1.1.
  36. */
  37. /**
  38. * This class permits a <code>String</code> to be read as an input stream.
  39. * The low eight bits of each character in the <code>String</code> are the
  40. * bytes that are returned. The high eight bits of each character are
  41. * discarded.
  42. * <p>
  43. * The mark/reset functionality in this class behaves differently than
  44. * normal. The <code>mark()</code> method is always ignored and the
  45. * <code>reset()</code> method always resets in stream to start reading from
  46. * position 0 in the String. Note that since this method does not override
  47. * <code>markSupported()</code> in <code>InputStream</code>, calling that
  48. * method will return <code>false</code>.
  49. * <p>
  50. * Note that this class is deprecated because it does not properly handle
  51. * 16-bit Java characters. It is provided for backwards compatibility only
  52. * and should not be used for new development. The <code>StringReader</code>
  53. * class should be used instead.
  54. *
  55. * @deprecated
  56. *
  57. * @author Aaron M. Renn (arenn@urbanophile.com)
  58. * @author Warren Levy (warrenl@cygnus.com)
  59. */
  60. public class StringBufferInputStream extends InputStream
  61. {
  62. /** The String which is the input to this stream. */
  63. protected String buffer;
  64. /** Position of the next byte in buffer to be read. */
  65. protected int pos = 0;
  66. /** The length of the String buffer. */
  67. protected int count;
  68. /**
  69. * Create a new <code>StringBufferInputStream</code> that will read bytes
  70. * from the passed in <code>String</code>. This stream will read from the
  71. * beginning to the end of the <code>String</code>.
  72. *
  73. * @param s The <code>String</code> this stream will read from.
  74. */
  75. public StringBufferInputStream(String s)
  76. {
  77. buffer = s;
  78. count = s.length();
  79. }
  80. /**
  81. * This method returns the number of bytes available to be read from this
  82. * stream. The value returned will be equal to <code>count - pos</code>.
  83. *
  84. * @return The number of bytes that can be read from this stream before
  85. * blocking, which is all of them
  86. */
  87. public int available()
  88. {
  89. return count - pos;
  90. }
  91. /**
  92. * This method reads one byte from the stream. The <code>pos</code> counter
  93. * is advanced to the next byte to be read. The byte read is returned as
  94. * an int in the range of 0-255. If the stream position is already at the
  95. * end of the buffer, no byte is read and a -1 is returned in order to
  96. * indicate the end of the stream.
  97. *
  98. * @return The byte read, or -1 if end of stream
  99. */
  100. public int read()
  101. {
  102. if (pos >= count)
  103. return -1; // EOF
  104. return ((int) buffer.charAt(pos++)) & 0xFF;
  105. }
  106. /**
  107. * This method reads bytes from the stream and stores them into a caller
  108. * supplied buffer. It starts storing the data at index <code>offset</code>
  109. * into the buffer and attempts to read <code>len</code> bytes. This method
  110. * can return before reading the number of bytes requested if the end of the
  111. * stream is encountered first. The actual number of bytes read is
  112. * returned. If no bytes can be read because the stream is already at
  113. * the end of stream position, a -1 is returned.
  114. * <p>
  115. * This method does not block.
  116. *
  117. * @param b The array into which the bytes read should be stored.
  118. * @param off The offset into the array to start storing bytes
  119. * @param len The requested number of bytes to read
  120. *
  121. * @return The actual number of bytes read, or -1 if end of stream.
  122. */
  123. public int read(byte[] b, int off, int len)
  124. {
  125. if (off < 0 || len < 0 || off + len > b.length)
  126. throw new ArrayIndexOutOfBoundsException();
  127. if (pos >= count)
  128. return -1; // EOF
  129. int numRead = Math.min(len, count - pos);
  130. if (numRead < 0)
  131. return 0;
  132. buffer.getBytes(pos, pos + numRead, b, off);
  133. pos += numRead;
  134. return numRead;
  135. }
  136. /**
  137. * This method sets the read position in the stream to the beginning
  138. * setting the <code>pos</code> variable equal to 0. Note that this differs
  139. * from the common implementation of the <code>reset()</code> method.
  140. */
  141. public void reset()
  142. {
  143. pos = 0;
  144. }
  145. /**
  146. * This method attempts to skip the requested number of bytes in the
  147. * input stream. It does this by advancing the <code>pos</code> value by the
  148. * specified number of bytes. It this would exceed the length of the
  149. * buffer, then only enough bytes are skipped to position the stream at
  150. * the end of the buffer. The actual number of bytes skipped is returned.
  151. *
  152. * @param n The requested number of bytes to skip
  153. *
  154. * @return The actual number of bytes skipped.
  155. */
  156. public long skip(long n)
  157. {
  158. if (n < 0)
  159. return 0L;
  160. long actualSkip = Math.min(n, count - pos);
  161. pos += actualSkip;
  162. return actualSkip;
  163. }
  164. }