1. /*
  2. * @(#)ByteArrayInputStream.java 1.44 03/12/19
  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. * A <code>ByteArrayInputStream</code> contains
  10. * an internal buffer that contains bytes that
  11. * may be read from the stream. An internal
  12. * counter keeps track of the next byte to
  13. * be supplied by the <code>read</code> method.
  14. * <p>
  15. * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
  16. * this class can be called after the stream has been closed without
  17. * generating an <tt>IOException</tt>.
  18. *
  19. * @author Arthur van Hoff
  20. * @version 1.44, 12/19/03
  21. * @see java.io.StringBufferInputStream
  22. * @since JDK1.0
  23. */
  24. public
  25. class ByteArrayInputStream extends InputStream {
  26. /**
  27. * An array of bytes that was provided
  28. * by the creator of the stream. Elements <code>buf[0]</code>
  29. * through <code>buf[count-1]</code> are the
  30. * only bytes that can ever be read from the
  31. * stream; element <code>buf[pos]</code> is
  32. * the next byte to be read.
  33. */
  34. protected byte buf[];
  35. /**
  36. * The index of the next character to read from the input stream buffer.
  37. * This value should always be nonnegative
  38. * and not larger than the value of <code>count</code>.
  39. * The next byte to be read from the input stream buffer
  40. * will be <code>buf[pos]</code>.
  41. */
  42. protected int pos;
  43. /**
  44. * The currently marked position in the stream.
  45. * ByteArrayInputStream objects are marked at position zero by
  46. * default when constructed. They may be marked at another
  47. * position within the buffer by the <code>mark()</code> method.
  48. * The current buffer position is set to this point by the
  49. * <code>reset()</code> method.
  50. * <p>
  51. * If no mark has been set, then the value of mark is the offset
  52. * passed to the constructor (or 0 if the offset was not supplied).
  53. *
  54. * @since JDK1.1
  55. */
  56. protected int mark = 0;
  57. /**
  58. * The index one greater than the last valid character in the input
  59. * stream buffer.
  60. * This value should always be nonnegative
  61. * and not larger than the length of <code>buf</code>.
  62. * It is one greater than the position of
  63. * the last byte within <code>buf</code> that
  64. * can ever be read from the input stream buffer.
  65. */
  66. protected int count;
  67. /**
  68. * Creates a <code>ByteArrayInputStream</code>
  69. * so that it uses <code>buf</code> as its
  70. * buffer array.
  71. * The buffer array is not copied.
  72. * The initial value of <code>pos</code>
  73. * is <code>0</code> and the initial value
  74. * of <code>count</code> is the length of
  75. * <code>buf</code>.
  76. *
  77. * @param buf the input buffer.
  78. */
  79. public ByteArrayInputStream(byte buf[]) {
  80. this.buf = buf;
  81. this.pos = 0;
  82. this.count = buf.length;
  83. }
  84. /**
  85. * Creates <code>ByteArrayInputStream</code>
  86. * that uses <code>buf</code> as its
  87. * buffer array. The initial value of <code>pos</code>
  88. * is <code>offset</code> and the initial value
  89. * of <code>count</code> is the minimum of <code>offset+length</code>
  90. * and <code>buf.length</code>.
  91. * The buffer array is not copied. The buffer's mark is
  92. * set to the specified offset.
  93. *
  94. * @param buf the input buffer.
  95. * @param offset the offset in the buffer of the first byte to read.
  96. * @param length the maximum number of bytes to read from the buffer.
  97. */
  98. public ByteArrayInputStream(byte buf[], int offset, int length) {
  99. this.buf = buf;
  100. this.pos = offset;
  101. this.count = Math.min(offset + length, buf.length);
  102. this.mark = offset;
  103. }
  104. /**
  105. * Reads the next byte of data from this input stream. The value
  106. * byte is returned as an <code>int</code> in the range
  107. * <code>0</code> to <code>255</code>. If no byte is available
  108. * because the end of the stream has been reached, the value
  109. * <code>-1</code> is returned.
  110. * <p>
  111. * This <code>read</code> method
  112. * cannot block.
  113. *
  114. * @return the next byte of data, or <code>-1</code> if the end of the
  115. * stream has been reached.
  116. */
  117. public synchronized int read() {
  118. return (pos < count) ? (buf[pos++] & 0xff) : -1;
  119. }
  120. /**
  121. * Reads up to <code>len</code> bytes of data into an array of bytes
  122. * from this input stream.
  123. * If <code>pos</code> equals <code>count</code>,
  124. * then <code>-1</code> is returned to indicate
  125. * end of file. Otherwise, the number <code>k</code>
  126. * of bytes read is equal to the smaller of
  127. * <code>len</code> and <code>count-pos</code>.
  128. * If <code>k</code> is positive, then bytes
  129. * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
  130. * are copied into <code>b[off]</code> through
  131. * <code>b[off+k-1]</code> in the manner performed
  132. * by <code>System.arraycopy</code>. The
  133. * value <code>k</code> is added into <code>pos</code>
  134. * and <code>k</code> is returned.
  135. * <p>
  136. * This <code>read</code> method cannot block.
  137. *
  138. * @param b the buffer into which the data is read.
  139. * @param off the start offset of the data.
  140. * @param len the maximum number of bytes read.
  141. * @return the total number of bytes read into the buffer, or
  142. * <code>-1</code> if there is no more data because the end of
  143. * the stream has been reached.
  144. */
  145. public synchronized int read(byte b[], int off, int len) {
  146. if (b == null) {
  147. throw new NullPointerException();
  148. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  149. ((off + len) > b.length) || ((off + len) < 0)) {
  150. throw new IndexOutOfBoundsException();
  151. }
  152. if (pos >= count) {
  153. return -1;
  154. }
  155. if (pos + len > count) {
  156. len = count - pos;
  157. }
  158. if (len <= 0) {
  159. return 0;
  160. }
  161. System.arraycopy(buf, pos, b, off, len);
  162. pos += len;
  163. return len;
  164. }
  165. /**
  166. * Skips <code>n</code> bytes of input from this input stream. Fewer
  167. * bytes might be skipped if the end of the input stream is reached.
  168. * The actual number <code>k</code>
  169. * of bytes to be skipped is equal to the smaller
  170. * of <code>n</code> and <code>count-pos</code>.
  171. * The value <code>k</code> is added into <code>pos</code>
  172. * and <code>k</code> is returned.
  173. *
  174. * @param n the number of bytes to be skipped.
  175. * @return the actual number of bytes skipped.
  176. */
  177. public synchronized long skip(long n) {
  178. if (pos + n > count) {
  179. n = count - pos;
  180. }
  181. if (n < 0) {
  182. return 0;
  183. }
  184. pos += n;
  185. return n;
  186. }
  187. /**
  188. * Returns the number of bytes that can be read from this input
  189. * stream without blocking.
  190. * The value returned is
  191. * <code>count - pos</code>,
  192. * which is the number of bytes remaining to be read from the input buffer.
  193. *
  194. * @return the number of bytes that can be read from the input stream
  195. * without blocking.
  196. */
  197. public synchronized int available() {
  198. return count - pos;
  199. }
  200. /**
  201. * Tests if this <code>InputStream</code> supports mark/reset. The
  202. * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
  203. * always returns <code>true</code>.
  204. *
  205. * @since JDK1.1
  206. */
  207. public boolean markSupported() {
  208. return true;
  209. }
  210. /**
  211. * Set the current marked position in the stream.
  212. * ByteArrayInputStream objects are marked at position zero by
  213. * default when constructed. They may be marked at another
  214. * position within the buffer by this method.
  215. * <p>
  216. * If no mark has been set, then the value of the mark is the
  217. * offset passed to the constructor (or 0 if the offset was not
  218. * supplied).
  219. *
  220. * <p> Note: The <code>readAheadLimit</code> for this class
  221. * has no meaning.
  222. *
  223. * @since JDK1.1
  224. */
  225. public void mark(int readAheadLimit) {
  226. mark = pos;
  227. }
  228. /**
  229. * Resets the buffer to the marked position. The marked position
  230. * is 0 unless another position was marked or an offset was specified
  231. * in the constructor.
  232. */
  233. public synchronized void reset() {
  234. pos = mark;
  235. }
  236. /**
  237. * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
  238. * this class can be called after the stream has been closed without
  239. * generating an <tt>IOException</tt>.
  240. * <p>
  241. */
  242. public void close() throws IOException {
  243. }
  244. }