ByteArrayInputStream.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* ByteArrayInputStream.java -- Read an array 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. /**
  33. * This class permits an array of bytes to be read as an input stream.
  34. *
  35. * @author Warren Levy (warrenl@cygnus.com)
  36. * @author Aaron M. Renn (arenn@urbanophile.com)
  37. */
  38. public class ByteArrayInputStream extends InputStream
  39. {
  40. /**
  41. * The array that contains the data supplied during read operations
  42. */
  43. protected byte[] buf;
  44. /**
  45. * The array index of the next byte to be read from the buffer
  46. * <code>buf</code>
  47. */
  48. protected int pos;
  49. /**
  50. * The currently marked position in the stream. This defaults to 0, so a
  51. * reset operation on the stream resets it to read from array index 0 in
  52. * the buffer - even if the stream was initially created with an offset
  53. * greater than 0
  54. */
  55. protected int mark;
  56. /**
  57. * This indicates the maximum number of bytes that can be read from this
  58. * stream. It is the array index of the position after the last valid
  59. * byte in the buffer <code>buf</code>
  60. */
  61. protected int count;
  62. /**
  63. * Create a new ByteArrayInputStream that will read bytes from the passed
  64. * in byte array. This stream will read from the beginning to the end
  65. * of the array. It is identical to calling an overloaded constructor
  66. * as <code>ByteArrayInputStream(buf, 0, buf.length)</code>.
  67. * <p>
  68. * Note that this array is not copied. If its contents are changed
  69. * while this stream is being read, those changes will be reflected in the
  70. * bytes supplied to the reader. Please use caution in changing the
  71. * contents of the buffer while this stream is open.
  72. *
  73. * @param buffer The byte array buffer this stream will read from.
  74. */
  75. public ByteArrayInputStream(byte[] buffer)
  76. {
  77. this(buffer, 0, buffer.length);
  78. }
  79. /**
  80. * Create a new ByteArrayInputStream that will read bytes from the
  81. * passed in byte array. This stream will read from position
  82. * <code>offset</code> in the array for a length of
  83. * <code>length</code> bytes past <code>offset</code>. If the
  84. * stream is reset to a position before <code>offset</code> then
  85. * more than <code>length</code> bytes can be read from the stream.
  86. * The <code>length</code> value should be viewed as the array index
  87. * one greater than the last position in the buffer to read.
  88. * <p>
  89. * Note that this array is not copied. If its contents are changed
  90. * while this stream is being read, those changes will be reflected in the
  91. * bytes supplied to the reader. Please use caution in changing the
  92. * contents of the buffer while this stream is open.
  93. *
  94. * @param buffer The byte array buffer this stream will read from.
  95. * @param offset The index into the buffer to start reading bytes from
  96. * @param length The number of bytes to read from the buffer
  97. */
  98. public ByteArrayInputStream(byte[] buffer, int offset, int length)
  99. {
  100. if (offset < 0 || length < 0 || offset > buffer.length)
  101. throw new IllegalArgumentException();
  102. buf = buffer;
  103. count = offset + length;
  104. if (count > buf.length)
  105. count = buf.length;
  106. pos = offset;
  107. mark = pos;
  108. }
  109. /**
  110. * This method returns the number of bytes available to be read from this
  111. * stream. The value returned will be equal to <code>count - pos</code>.
  112. *
  113. * @return The number of bytes that can be read from this stream
  114. * before blocking, which is all of them
  115. */
  116. public synchronized int available()
  117. {
  118. return count - pos;
  119. }
  120. /**
  121. * This method sets the mark position in this stream to the current
  122. * position. Note that the <code>readlimit</code> parameter in this
  123. * method does nothing as this stream is always capable of
  124. * remembering all the bytes int it.
  125. * <p>
  126. * Note that in this class the mark position is set by default to
  127. * position 0 in the stream. This is in constrast to some other
  128. * stream types where there is no default mark position.
  129. *
  130. * @param readLimit The number of bytes this stream must remember.
  131. * This parameter is ignored.
  132. */
  133. public synchronized void mark(int readLimit)
  134. {
  135. // readLimit is ignored per Java Class Lib. book, p.220.
  136. mark = pos;
  137. }
  138. /**
  139. * This method overrides the <code>markSupported</code> method in
  140. * <code>InputStream</code> in order to return <code>true</code> -
  141. * indicating that this stream class supports mark/reset
  142. * functionality.
  143. *
  144. * @return <code>true</code> to indicate that this class supports
  145. * mark/reset.
  146. */
  147. public boolean markSupported()
  148. {
  149. return true;
  150. }
  151. /**
  152. * This method reads one byte from the stream. The <code>pos</code>
  153. * counter is advanced to the next byte to be read. The byte read is
  154. * returned as an int in the range of 0-255. If the stream position
  155. * is already at the end of the buffer, no byte is read and a -1 is
  156. * returned in order to indicate the end of the stream.
  157. *
  158. * @return The byte read, or -1 if end of stream
  159. */
  160. public synchronized int read()
  161. {
  162. if (pos < count)
  163. return ((int) buf[pos++]) & 0xFF;
  164. return -1;
  165. }
  166. /**
  167. * This method reads bytes from the stream and stores them into a
  168. * caller supplied buffer. It starts storing the data at index
  169. * <code>offset</code> into the buffer and attempts to read
  170. * <code>len</code> bytes. This method can return before reading
  171. * the number of bytes requested if the end of the stream is
  172. * encountered first. The actual number of bytes read is returned.
  173. * If no bytes can be read because the stream is already at the end
  174. * of stream position, a -1 is returned.
  175. * <p>
  176. * This method does not block.
  177. *
  178. * @param buffer The array into which the bytes read should be stored.
  179. * @param offset The offset into the array to start storing bytes
  180. * @param length The requested number of bytes to read
  181. *
  182. * @return The actual number of bytes read, or -1 if end of stream.
  183. */
  184. public synchronized int read(byte[] buffer, int offset, int length)
  185. {
  186. if (pos >= count)
  187. return -1;
  188. int numBytes = Math.min(count - pos, length);
  189. System.arraycopy(buf, pos, buffer, offset, numBytes);
  190. pos += numBytes;
  191. return numBytes;
  192. }
  193. /**
  194. * This method sets the read position in the stream to the mark
  195. * point by setting the <code>pos</code> variable equal to the
  196. * <code>mark</code> variable. Since a mark can be set anywhere in
  197. * the array, the mark/reset methods int this class can be used to
  198. * provide random search capabilities for this type of stream.
  199. */
  200. public synchronized void reset()
  201. {
  202. pos = mark;
  203. }
  204. /**
  205. * This method attempts to skip the requested number of bytes in the
  206. * input stream. It does this by advancing the <code>pos</code>
  207. * value by the specified number of bytes. It this would exceed the
  208. * length of the buffer, then only enough bytes are skipped to
  209. * position the stream at the end of the buffer. The actual number
  210. * of bytes skipped is returned.
  211. *
  212. * @param num The requested number of bytes to skip
  213. *
  214. * @return The actual number of bytes skipped.
  215. */
  216. public synchronized long skip(long num)
  217. {
  218. // Even though the var numBytes is a long, in reality it can never
  219. // be larger than an int since the result of subtracting 2 positive
  220. // ints will always fit in an int. Since we have to return a long
  221. // anyway, numBytes might as well just be a long.
  222. long numBytes = Math.min((long) (count - pos), num < 0 ? 0L : num);
  223. pos += numBytes;
  224. return numBytes;
  225. }
  226. }