1. /*
  2. * @(#)ImageInputStream.java 1.47 04/05/13
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio.stream;
  8. import java.io.DataInput;
  9. import java.io.EOFException;
  10. import java.io.IOException;
  11. import java.io.UTFDataFormatException;
  12. import java.nio.ByteOrder;
  13. /**
  14. * A seekable input stream interface for use by
  15. * <code>ImageReader</code>s. Various input sources, such as
  16. * <code>InputStream</code>s and <code>File</code>s,
  17. * as well as future fast I/O sources may be "wrapped" by a suitable
  18. * implementation of this interface for use by the Image I/O API.
  19. *
  20. * @see ImageInputStreamImpl
  21. * @see FileImageInputStream
  22. * @see FileCacheImageInputStream
  23. * @see MemoryCacheImageInputStream
  24. *
  25. * @version 0.5
  26. */
  27. public interface ImageInputStream extends DataInput {
  28. /**
  29. * Sets the desired byte order for future reads of data values
  30. * from this stream. For example, the sequence of bytes '0x01
  31. * 0x02 0x03 0x04' if read as a 4-byte integer would have the
  32. * value '0x01020304' using network byte order and the value
  33. * '0x04030201' under the reverse byte order.
  34. *
  35. * <p> The enumeration class <code>java.nio.ByteOrder</code> is
  36. * used to specify the byte order. A value of
  37. * <code>ByteOrder.BIG_ENDIAN</code> specifies so-called
  38. * big-endian or network byte order, in which the high-order byte
  39. * comes first. Motorola and Sparc processors store data in this
  40. * format, while Intel processors store data in the reverse
  41. * <code>ByteOrder.LITTLE_ENDIAN</code> order.
  42. *
  43. * <p> The byte order has no effect on the results returned from
  44. * the <code>readBits</code> method (or the value written by
  45. * <code>ImageOutputStream.writeBits</code>).
  46. *
  47. * @param byteOrder one of <code>ByteOrder.BIG_ENDIAN</code> or
  48. * <code>java.nio.ByteOrder.LITTLE_ENDIAN</code>, indicating whether
  49. * network byte order or its reverse will be used for future
  50. * reads.
  51. *
  52. * @see java.nio.ByteOrder
  53. * @see #getByteOrder
  54. * @see #readBits(int)
  55. */
  56. void setByteOrder(ByteOrder byteOrder);
  57. /**
  58. * Returns the byte order with which data values will be read from
  59. * this stream as an instance of the
  60. * <code>java.nio.ByteOrder</code> enumeration.
  61. *
  62. * @return one of <code>ByteOrder.BIG_ENDIAN</code> or
  63. * <code>ByteOrder.LITTLE_ENDIAN</code>, indicating which byte
  64. * order is being used.
  65. *
  66. * @see java.nio.ByteOrder
  67. * @see #setByteOrder
  68. */
  69. ByteOrder getByteOrder();
  70. /**
  71. * Reads a single byte from the stream and returns it as an
  72. * integer between 0 and 255. If the end of the stream is
  73. * reached, -1 is returned.
  74. *
  75. * <p> The bit offset within the stream is reset to zero before
  76. * the read occurs.
  77. *
  78. * @return a byte value from the stream, as an int, or -1 to
  79. * indicate EOF.
  80. *
  81. * @exception IOException if an I/O error occurs.
  82. */
  83. int read() throws IOException;
  84. /**
  85. * Reads up to <code>b.length</code> bytes from the stream, and
  86. * stores them into <code>b</code> starting at index 0. The
  87. * number of bytes read is returned. If no bytes can be read
  88. * because the end of the stream has been reached, -1 is returned.
  89. *
  90. * <p> The bit offset within the stream is reset to zero before
  91. * the read occurs.
  92. *
  93. * @param b an array of bytes to be written to.
  94. *
  95. * @return the number of bytes actually read, or <code>-1</code>
  96. * to indicate EOF.
  97. *
  98. * @exception NullPointerException if <code>b</code> is
  99. * <code>null</code>.
  100. *
  101. * @exception IOException if an I/O error occurs.
  102. */
  103. int read(byte[] b) throws IOException;
  104. /**
  105. * Reads up to <code>len</code> bytes from the stream, and stores
  106. * them into <code>b</code> starting at index <code>off</code>.
  107. * The number of bytes read is returned. If no bytes can be read
  108. * because the end of the stream has been reached, <code>-1</code>
  109. * is returned.
  110. *
  111. * <p> The bit offset within the stream is reset to zero before
  112. * the read occurs.
  113. *
  114. * @param b an array of bytes to be written to.
  115. * @param off the starting position within <code>b</code> to write to.
  116. * @param len the maximum number of <code>byte</code>s to read.
  117. *
  118. * @return the number of bytes actually read, or <code>-1</code>
  119. * to indicate EOF.
  120. *
  121. * @exception NullPointerException if <code>b</code> is
  122. * <code>null</code>.
  123. * @exception IndexOutOfBoundsException if <code>off</code> is
  124. * negative, <code>len</code> is negative, or <code>off +
  125. * len</code> is greater than <code>b.length</code>.
  126. * @exception IOException if an I/O error occurs.
  127. */
  128. int read(byte[] b, int off, int len) throws IOException;
  129. /**
  130. * Reads up to <code>len</code> bytes from the stream, and
  131. * modifies the supplied <code>IIOByteBuffer</code> to indicate
  132. * the byte array, offset, and length where the data may be found.
  133. * The caller should not attempt to modify the data found in the
  134. * <code>IIOByteBuffer</code>.
  135. *
  136. * <p> The bit offset within the stream is reset to zero before
  137. * the read occurs.
  138. *
  139. * @param buf an IIOByteBuffer object to be modified.
  140. * @param len the maximum number of <code>byte</code>s to read.
  141. *
  142. * @exception IndexOutOfBoundsException if <code>len</code> is
  143. * negative.
  144. * @exception NullPointerException if <code>buf</code> is
  145. * <code>null</code>.
  146. *
  147. * @exception IOException if an I/O error occurs.
  148. */
  149. void readBytes(IIOByteBuffer buf, int len) throws IOException;
  150. /**
  151. * Reads a byte from the stream and returns a <code>boolean</code>
  152. * value of <code>true</code> if it is nonzero, <code>false</code>
  153. * if it is zero.
  154. *
  155. * <p> The bit offset within the stream is reset to zero before
  156. * the read occurs.
  157. *
  158. * @return a boolean value from the stream.
  159. *
  160. * @exception EOFException if the end of the stream is reached.
  161. * @exception IOException if an I/O error occurs.
  162. */
  163. boolean readBoolean() throws IOException;
  164. /**
  165. * Reads a byte from the stream and returns it as a
  166. * <code>byte</code> value. Byte values between <code>0x00</code>
  167. * and <code>0x7f</code> represent integer values between
  168. * <code>0</code> and <code>127</code>. Values between
  169. * <code>0x80</code> and <code>0xff</code> represent negative
  170. * values from <code>-128</code> to <code>/1</code>.
  171. *
  172. * <p> The bit offset within the stream is reset to zero before
  173. * the read occurs.
  174. *
  175. * @return a signed byte value from the stream.
  176. *
  177. * @exception EOFException if the end of the stream is reached.
  178. * @exception IOException if an I/O error occurs.
  179. */
  180. byte readByte() throws IOException;
  181. /**
  182. * Reads a byte from the stream, and (conceptually) converts it to
  183. * an int, masks it with <code>0xff</code> in order to strip off
  184. * any sign-extension bits, and returns it as a <code>byte</code>
  185. * value.
  186. *
  187. * <p> Thus, byte values between <code>0x00</code> and
  188. * <code>0x7f</code> are simply returned as integer values between
  189. * <code>0</code> and <code>127</code>. Values between
  190. * <code>0x80</code> and <code>0xff</code>, which normally
  191. * represent negative <code>byte</code>values, will be mapped into
  192. * positive integers between <code>128</code> and
  193. * <code>255</code>.
  194. *
  195. * <p> The bit offset within the stream is reset to zero before
  196. * the read occurs.
  197. *
  198. * @return an unsigned byte value from the stream.
  199. *
  200. * @exception EOFException if the end of the stream is reached.
  201. * @exception IOException if an I/O error occurs.
  202. */
  203. int readUnsignedByte() throws IOException;
  204. /**
  205. * Reads two bytes from the stream, and (conceptually)
  206. * concatenates them according to the current byte order, and
  207. * returns the result as a <code>short</code> value.
  208. *
  209. * <p> The bit offset within the stream is reset to zero before
  210. * the read occurs.
  211. *
  212. * @return a signed short value from the stream.
  213. *
  214. * @exception EOFException if the stream reaches the end before
  215. * reading all the bytes.
  216. * @exception IOException if an I/O error occurs.
  217. *
  218. * @see #getByteOrder
  219. */
  220. short readShort() throws IOException;
  221. /**
  222. * Reads two bytes from the stream, and (conceptually)
  223. * concatenates them according to the current byte order, converts
  224. * the resulting value to an <code>int</code>, masks it with
  225. * <code>0xffff</code> in order to strip off any sign-extension
  226. * buts, and returns the result as an unsigned <code>int</code>
  227. * value.
  228. *
  229. * <p> The bit offset within the stream is reset to zero before
  230. * the read occurs.
  231. *
  232. * @return an unsigned short value from the stream, as an int.
  233. *
  234. * @exception EOFException if the stream reaches the end before
  235. * reading all the bytes.
  236. * @exception IOException if an I/O error occurs.
  237. *
  238. * @see #getByteOrder
  239. */
  240. int readUnsignedShort() throws IOException;
  241. /**
  242. * Equivalent to <code>readUnsignedShort</code>, except that the
  243. * result is returned using the <code>char</code> datatype.
  244. *
  245. * <p> The bit offset within the stream is reset to zero before
  246. * the read occurs.
  247. *
  248. * @return an unsigned char value from the stream.
  249. *
  250. * @exception EOFException if the stream reaches the end before
  251. * reading all the bytes.
  252. * @exception IOException if an I/O error occurs.
  253. *
  254. * @see #readUnsignedShort
  255. */
  256. char readChar() throws IOException;
  257. /**
  258. * Reads 4 bytes from the stream, and (conceptually) concatenates
  259. * them according to the current byte order and returns the result
  260. * as an <code>int</code>.
  261. *
  262. * <p> The bit offset within the stream is ignored and treated as
  263. * though it were zero.
  264. *
  265. * @return a signed int value from the stream.
  266. *
  267. * @exception EOFException if the stream reaches the end before
  268. * reading all the bytes.
  269. * @exception IOException if an I/O error occurs.
  270. *
  271. * @see #getByteOrder
  272. */
  273. int readInt() throws IOException;
  274. /**
  275. * Reads 4 bytes from the stream, and (conceptually) concatenates
  276. * them according to the current byte order, converts the result
  277. * to a long, masks it with <code>0xffffffffL</code> in order to
  278. * strip off any sign-extension bits, and returns the result as an
  279. * unsigned <code>long</code> value.
  280. *
  281. * <p> The bit offset within the stream is reset to zero before
  282. * the read occurs.
  283. *
  284. * @return an unsigned int value from the stream, as a long.
  285. *
  286. * @exception EOFException if the stream reaches the end before
  287. * reading all the bytes.
  288. * @exception IOException if an I/O error occurs.
  289. *
  290. * @see #getByteOrder
  291. */
  292. long readUnsignedInt() throws IOException;
  293. /**
  294. * Reads 8 bytes from the stream, and (conceptually) concatenates
  295. * them according to the current byte order and returns the result
  296. * as a <code>long</code>.
  297. *
  298. * <p> The bit offset within the stream is reset to zero before
  299. * the read occurs.
  300. *
  301. * @return a signed long value from the stream.
  302. *
  303. * @exception EOFException if the stream reaches the end before
  304. * reading all the bytes.
  305. * @exception IOException if an I/O error occurs.
  306. *
  307. * @see #getByteOrder
  308. */
  309. long readLong() throws IOException;
  310. /**
  311. * Reads 4 bytes from the stream, and (conceptually) concatenates
  312. * them according to the current byte order and returns the result
  313. * as a <code>float</code>.
  314. *
  315. * <p> The bit offset within the stream is reset to zero before
  316. * the read occurs.
  317. *
  318. * @return a float value from the stream.
  319. *
  320. * @exception EOFException if the stream reaches the end before
  321. * reading all the bytes.
  322. * @exception IOException if an I/O error occurs.
  323. *
  324. * @see #getByteOrder
  325. */
  326. float readFloat() throws IOException;
  327. /**
  328. * Reads 8 bytes from the stream, and (conceptually) concatenates
  329. * them according to the current byte order and returns the result
  330. * as a <code>double</code>.
  331. *
  332. * <p> The bit offset within the stream is reset to zero before
  333. * the read occurs.
  334. *
  335. * @return a double value from the stream.
  336. *
  337. * @exception EOFException if the stream reaches the end before
  338. * reading all the bytes.
  339. * @exception IOException if an I/O error occurs.
  340. *
  341. * @see #getByteOrder
  342. */
  343. double readDouble() throws IOException;
  344. /**
  345. * Reads the next line of text from the input stream. It reads
  346. * successive bytes, converting each byte separately into a
  347. * character, until it encounters a line terminator or end of
  348. * file; the characters read are then returned as a
  349. * <code>String</code>. Note that because this method processes
  350. * bytes, it does not support input of the full Unicode character
  351. * set.
  352. *
  353. * <p> If end of file is encountered before even one byte can be
  354. * read, then <code>null</code> is returned. Otherwise, each byte
  355. * that is read is converted to type <code>char</code> by
  356. * zero-extension. If the character <code>'\n'</code> is
  357. * encountered, it is discarded and reading ceases. If the
  358. * character <code>'\r'</code> is encountered, it is discarded
  359. * and, if the following byte converts to the character
  360. * <code>'\n'</code>, then that is discarded also; reading then
  361. * ceases. If end of file is encountered before either of the
  362. * characters <code>'\n'</code> and <code>'\r'</code> is
  363. * encountered, reading ceases. Once reading has ceased, a
  364. * <code>String</code> is returned that contains all the
  365. * characters read and not discarded, taken in order. Note that
  366. * every character in this string will have a value less than
  367. * <code>\u0100</code>, that is, <code>(char)256</code>.
  368. *
  369. * <p> The bit offset within the stream is reset to zero before
  370. * the read occurs.
  371. *
  372. * @return a String containing a line of text from the stream.
  373. *
  374. * @exception IOException if an I/O error occurs.
  375. */
  376. String readLine() throws IOException;
  377. /**
  378. * Reads in a string that has been encoded using a
  379. * <a href="../../../java/io/DataInput.html#modified-utf-8">modified
  380. * UTF-8</a>
  381. * format. The general contract of <code>readUTF</code> is that
  382. * it reads a representation of a Unicode character string encoded
  383. * in modified UTF-8 format; this string of characters is
  384. * then returned as a <code>String</code>.
  385. *
  386. * <p> First, two bytes are read and used to construct an unsigned
  387. * 16-bit integer in the manner of the
  388. * <code>readUnsignedShort</code> method, using network byte order
  389. * (regardless of the current byte order setting). This integer
  390. * value is called the <i>UTF length</i> and specifies the number
  391. * of additional bytes to be read. These bytes are then converted
  392. * to characters by considering them in groups. The length of each
  393. * group is computed from the value of the first byte of the
  394. * group. The byte following a group, if any, is the first byte of
  395. * the next group.
  396. *
  397. * <p> If the first byte of a group matches the bit pattern
  398. * <code>0xxxxxxx</code> (where <code>x</code> means "may be
  399. * <code>0</code> or <code>1</code>"), then the group consists of
  400. * just that byte. The byte is zero-extended to form a character.
  401. *
  402. * <p> If the first byte of a group matches the bit pattern
  403. * <code>110xxxxx</code>, then the group consists of that byte
  404. * <code>a</code> and a second byte <code>b</code>. If there is no
  405. * byte <code>b</code> (because byte <code>a</code> was the last
  406. * of the bytes to be read), or if byte <code>b</code> does not
  407. * match the bit pattern <code>10xxxxxx</code>, then a
  408. * <code>UTFDataFormatException</code> is thrown. Otherwise, the
  409. * group is converted to the character:
  410. *
  411. * <p> <pre><code>
  412. * (char)(((a& 0x1F) << 6) | (b & 0x3F))
  413. * </code></pre>
  414. *
  415. * If the first byte of a group matches the bit pattern
  416. * <code>1110xxxx</code>, then the group consists of that byte
  417. * <code>a</code> and two more bytes <code>b</code> and
  418. * <code>c</code>. If there is no byte <code>c</code> (because
  419. * byte <code>a</code> was one of the last two of the bytes to be
  420. * read), or either byte <code>b</code> or byte <code>c</code>
  421. * does not match the bit pattern <code>10xxxxxx</code>, then a
  422. * <code>UTFDataFormatException</code> is thrown. Otherwise, the
  423. * group is converted to the character:
  424. *
  425. * <p> <pre><code>
  426. * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
  427. * </code></pre>
  428. *
  429. * If the first byte of a group matches the pattern
  430. * <code>1111xxxx</code> or the pattern <code>10xxxxxx</code>,
  431. * then a <code>UTFDataFormatException</code> is thrown.
  432. *
  433. * <p> If end of file is encountered at any time during this
  434. * entire process, then an <code>EOFException</code> is thrown.
  435. *
  436. * <p> After every group has been converted to a character by this
  437. * process, the characters are gathered, in the same order in
  438. * which their corresponding groups were read from the input
  439. * stream, to form a <code>String</code>, which is returned.
  440. *
  441. * <p> The current byte order setting is ignored.
  442. *
  443. * <p> The bit offset within the stream is reset to zero before
  444. * the read occurs.
  445. *
  446. * <p><strong>Note:</strong> This method should not be used in
  447. * the implementation of image formats that use standard UTF-8,
  448. * because the modified UTF-8 used here is incompatible with
  449. * standard UTF-8.
  450. *
  451. * @return a String read from the stream.
  452. *
  453. * @exception EOFException if this stream reaches the end
  454. * before reading all the bytes.
  455. * @exception UTFDataFormatException if the bytes do not represent a
  456. * valid modified UTF-8 encoding of a string.
  457. * @exception IOException if an I/O error occurs.
  458. */
  459. String readUTF() throws IOException;
  460. /**
  461. * Reads <code>len</code> bytes from the stream, and stores them
  462. * into <code>b</code> starting at index <code>off</code>.
  463. * If the end of the stream is reached, an <code>EOFException</code>
  464. * will be thrown.
  465. *
  466. * <p> The bit offset within the stream is reset to zero before
  467. * the read occurs.
  468. *
  469. * @param b an array of bytes to be written to.
  470. * @param off the starting position within <code>b</code> to write to.
  471. * @param len the maximum number of <code>byte</code>s to read.
  472. *
  473. * @exception IndexOutOfBoundsException if <code>off</code> is
  474. * negative, <code>len</code> is negative, or <code>off +
  475. * len</code> is greater than <code>b.length</code>.
  476. * @exception NullPointerException if <code>b</code> is
  477. * <code>null</code>.
  478. * @exception EOFException if the stream reaches the end before
  479. * reading all the bytes.
  480. * @exception IOException if an I/O error occurs.
  481. */
  482. void readFully(byte[] b, int off, int len) throws IOException;
  483. /**
  484. * Reads <code>b.length</code> bytes from the stream, and stores them
  485. * into <code>b</code> starting at index <code>0</code>.
  486. * If the end of the stream is reached, an <code>EOFException</code>
  487. * will be thrown.
  488. *
  489. * <p> The bit offset within the stream is reset to zero before
  490. * the read occurs.
  491. *
  492. * @param b an array of <code>byte</code>s.
  493. *
  494. * @exception NullPointerException if <code>b</code> is
  495. * <code>null</code>.
  496. * @exception EOFException if the stream reaches the end before
  497. * reading all the bytes.
  498. * @exception IOException if an I/O error occurs.
  499. */
  500. void readFully(byte[] b) throws IOException;
  501. /**
  502. * Reads <code>len</code> shorts (signed 16-bit integers) from the
  503. * stream according to the current byte order, and
  504. * stores them into <code>s</code> starting at index
  505. * <code>off</code>. If the end of the stream is reached, an
  506. * <code>EOFException</code> will be thrown.
  507. *
  508. * <p> The bit offset within the stream is reset to zero before
  509. * the read occurs.
  510. *
  511. * @param s an array of shorts to be written to.
  512. * @param off the starting position withinb to write to.
  513. * @param len the maximum number of <code>short</code>s to read.
  514. *
  515. * @exception IndexOutOfBoundsException if <code>off</code> is
  516. * negative, <code>len</code> is negative, or <code>off +
  517. * len</code> is greater than <code>s.length</code>.
  518. * @exception NullPointerException if <code>s</code> is
  519. * <code>null</code>.
  520. * @exception EOFException if the stream reaches the end before
  521. * reading all the bytes.
  522. * @exception IOException if an I/O error occurs.
  523. */
  524. void readFully(short[] s, int off, int len) throws IOException;
  525. /**
  526. * Reads <code>len</code> chars (unsigned 16-bit integers) from the
  527. * stream according to the current byte order, and
  528. * stores them into <code>c</code> starting at index
  529. * <code>off</code>. If the end of the stream is reached, an
  530. * <code>EOFException</code> will be thrown.
  531. *
  532. * <p> The bit offset within the stream is reset to zero before
  533. * the read occurs.
  534. *
  535. * @param c an array of chars to be written to.
  536. * @param off the starting position withinb to write to.
  537. * @param len the maximum number of <code>char</code>s to read.
  538. *
  539. * @exception IndexOutOfBoundsException if <code>off</code> is
  540. * negative, <code>len</code> is negative, or <code>off +
  541. * len</code> is greater than <code>c.length</code>.
  542. * @exception NullPointerException if <code>c</code> is
  543. * <code>null</code>.
  544. * @exception EOFException if the stream reaches the end before
  545. * reading all the bytes.
  546. * @exception IOException if an I/O error occurs.
  547. */
  548. void readFully(char[] c, int off, int len) throws IOException;
  549. /**
  550. * Reads <code>len</code> ints (signed 32-bit integers) from the
  551. * stream according to the current byte order, and
  552. * stores them into <code>i</code> starting at index
  553. * <code>off</code>. If the end of the stream is reached, an
  554. * <code>EOFException</code> will be thrown.
  555. *
  556. * <p> The bit offset within the stream is reset to zero before
  557. * the read occurs.
  558. *
  559. * @param i an array of ints to be written to.
  560. * @param off the starting position withinb to write to.
  561. * @param len the maximum number of <code>int</code>s to read.
  562. *
  563. * @exception IndexOutOfBoundsException if <code>off</code> is
  564. * negative, <code>len</code> is negative, or <code>off +
  565. * len</code> is greater than <code>i.length</code>.
  566. * @exception NullPointerException if <code>i</code> is
  567. * <code>null</code>.
  568. * @exception EOFException if the stream reaches the end before
  569. * reading all the bytes.
  570. * @exception IOException if an I/O error occurs.
  571. */
  572. void readFully(int[] i, int off, int len) throws IOException;
  573. /**
  574. * Reads <code>len</code> longs (signed 64-bit integers) from the
  575. * stream according to the current byte order, and
  576. * stores them into <code>l</code> starting at index
  577. * <code>off</code>. If the end of the stream is reached, an
  578. * <code>EOFException</code> will be thrown.
  579. *
  580. * <p> The bit offset within the stream is reset to zero before
  581. * the read occurs.
  582. *
  583. * @param l an array of longs to be written to.
  584. * @param off the starting position withinb to write to.
  585. * @param len the maximum number of <code>long</code>s to read.
  586. *
  587. * @exception IndexOutOfBoundsException if <code>off</code> is
  588. * negative, <code>len</code> is negative, or <code>off +
  589. * len</code> is greater than <code>l.length</code>.
  590. * @exception NullPointerException if <code>l</code> is
  591. * <code>null</code>.
  592. * @exception EOFException if the stream reaches the end before
  593. * reading all the bytes.
  594. * @exception IOException if an I/O error occurs.
  595. */
  596. void readFully(long[] l, int off, int len) throws IOException;
  597. /**
  598. * Reads <code>len</code> floats (32-bit IEEE single-precision
  599. * floats) from the stream according to the current byte order,
  600. * and stores them into <code>f</code> starting at
  601. * index <code>off</code>. If the end of the stream is reached,
  602. * an <code>EOFException</code> will be thrown.
  603. *
  604. * <p> The bit offset within the stream is reset to zero before
  605. * the read occurs.
  606. *
  607. * @param f an array of floats to be written to.
  608. * @param off the starting position withinb to write to.
  609. * @param len the maximum number of <code>float</code>s to read.
  610. *
  611. * @exception IndexOutOfBoundsException if <code>off</code> is
  612. * negative, <code>len</code> is negative, or <code>off +
  613. * len</code> is greater than <code>f.length</code>.
  614. * @exception NullPointerException if <code>f</code> is
  615. * <code>null</code>.
  616. * @exception EOFException if the stream reaches the end before
  617. * reading all the bytes.
  618. * @exception IOException if an I/O error occurs.
  619. */
  620. void readFully(float[] f, int off, int len) throws IOException;
  621. /**
  622. * Reads <code>len</code> doubles (64-bit IEEE double-precision
  623. * floats) from the stream according to the current byte order,
  624. * and stores them into <code>d</code> starting at
  625. * index <code>off</code>. If the end of the stream is reached,
  626. * an <code>EOFException</code> will be thrown.
  627. *
  628. * <p> The bit offset within the stream is reset to zero before
  629. * the read occurs.
  630. *
  631. * @param d an array of doubles to be written to.
  632. * @param off the starting position withinb to write to.
  633. * @param len the maximum number of <code>double</code>s to read.
  634. *
  635. * @exception IndexOutOfBoundsException if <code>off</code> is
  636. * negative, <code>len</code> is negative, or <code>off +
  637. * len</code> is greater than <code>d.length</code>.
  638. * @exception NullPointerException if <code>d</code> is
  639. * <code>null</code>.
  640. * @exception EOFException if the stream reaches the end before
  641. * reading all the bytes.
  642. * @exception IOException if an I/O error occurs.
  643. */
  644. void readFully(double[] d, int off, int len) throws IOException;
  645. /**
  646. * Returns the current byte position of the stream. The next read
  647. * will take place starting at this offset.
  648. *
  649. * @return a long containing the position of the stream.
  650. *
  651. * @exception IOException if an I/O error occurs.
  652. */
  653. long getStreamPosition() throws IOException;
  654. /**
  655. * Returns the current bit offset, as an integer between 0 and 7,
  656. * inclusive. The bit offset is updated implicitly by calls to
  657. * the <code>readBits</code> method. A value of 0 indicates the
  658. * most-significant bit, and a value of 7 indicates the least
  659. * significant bit, of the byte being read.
  660. *
  661. * <p> The bit offset is set to 0 when a stream is first
  662. * opened, and is reset to 0 by calls to <code>seek</code>,
  663. * <code>skipBytes</code>, or any <code>read</code> or
  664. * <code>readFully</code> method.
  665. *
  666. * @return an <code>int</code> containing the bit offset between
  667. * 0 and 7, inclusive.
  668. *
  669. * @exception IOException if an I/O error occurs.
  670. *
  671. * @see #setBitOffset
  672. */
  673. int getBitOffset() throws IOException;
  674. /**
  675. * Sets the bit offset to an integer between 0 and 7, inclusive.
  676. * The byte offset within the stream, as returned by
  677. * <code>getStreamPosition</code>, is left unchanged.
  678. * A value of 0 indicates the
  679. * most-significant bit, and a value of 7 indicates the least
  680. * significant bit, of the byte being read.
  681. *
  682. * @param bitOffset the desired offset, as an <code>int</code>
  683. * between 0 and 7, inclusive.
  684. *
  685. * @exception IllegalArgumentException if <code>bitOffset</code>
  686. * is not between 0 and 7, inclusive.
  687. * @exception IOException if an I/O error occurs.
  688. *
  689. * @see #getBitOffset
  690. */
  691. void setBitOffset(int bitOffset) throws IOException;
  692. /**
  693. * Reads a single bit from the stream and returns it as an
  694. * <code>int</code> with the value <code>0</code> or
  695. * <code>1</code>. The bit offset is advanced by one and reduced
  696. * modulo 8.
  697. *
  698. * @return an <code>int</code> containing the value <code>0</code>
  699. * or <code>1</code>.
  700. *
  701. * @exception EOFException if the stream reaches the end before
  702. * reading all the bits.
  703. * @exception IOException if an I/O error occurs.
  704. */
  705. int readBit() throws IOException;
  706. /**
  707. * Reads a bitstring from the stream and returns it as a
  708. * <code>long</code>, with the first bit read becoming the most
  709. * significant bit of the output. The read starts within the byte
  710. * indicated by <code>getStreamPosition</code>, at the bit given
  711. * by <code>getBitOffset</code>. The bit offset is advanced by
  712. * <code>numBits</code> and reduced modulo 8.
  713. *
  714. * <p> The byte order of the stream has no effect on this
  715. * method. The return value of this method is constructed as
  716. * though the bits were read one at a time, and shifted into
  717. * the right side of the return value, as shown by the following
  718. * pseudo-code:
  719. *
  720. * <pre>
  721. * long accum = 0L;
  722. * for (int i = 0; i < numBits; i++) {
  723. * accum <<= 1; // Shift left one bit to make room
  724. * accum |= readBit();
  725. * }
  726. * </pre>
  727. *
  728. * Note that the result of <code>readBits(32)</code> may thus not
  729. * be equal to that of <code>readInt()</code> if a reverse network
  730. * byte order is being used (i.e., <code>getByteOrder() ==
  731. * false</code>).
  732. *
  733. * <p> If the end of the stream is encountered before all the bits
  734. * have been read, an <code>EOFException</code> is thrown.
  735. *
  736. * @param numBits the number of bits to read, as an <code>int</code>
  737. * between 0 and 64, inclusive.
  738. * @return the bitstring, as a <code>long</code> with the last bit
  739. * read stored in the least significant bit.
  740. *
  741. * @exception IllegalArgumentException if <code>numBits</code>
  742. * is not between 0 and 64, inclusive.
  743. * @exception EOFException if the stream reaches the end before
  744. * reading all the bits.
  745. * @exception IOException if an I/O error occurs.
  746. */
  747. long readBits(int numBits) throws IOException;
  748. /**
  749. * Returns the total length of the stream, if known. Otherwise,
  750. * <code>-1</code> is returned.
  751. *
  752. * @return a <code>long</code> containing the length of the
  753. * stream, if known, or else <code>-1</code>.
  754. *
  755. * @exception IOException if an I/O error occurs.
  756. */
  757. long length() throws IOException;
  758. /**
  759. * Moves the stream position forward by a given number of bytes. It
  760. * is possible that this method will only be able to skip forward
  761. * by a smaller number of bytes than requested, for example if the
  762. * end of the stream is reached. In all cases, the actual number
  763. * of bytes skipped is returned. The bit offset is set to zero
  764. * prior to advancing the position.
  765. *
  766. * @param n an <code>int</code> containing the number of bytes to
  767. * be skipped.
  768. *
  769. * @return an <code>int</code> representing the number of bytes skipped.
  770. *
  771. * @exception IOException if an I/O error occurs.
  772. */
  773. int skipBytes(int n) throws IOException;
  774. /**
  775. * Moves the stream position forward by a given number of bytes.
  776. * This method is identical to <code>skipBytes(int)</code> except
  777. * that it allows for a larger skip distance.
  778. *
  779. * @param n a <code>long</code> containing the number of bytes to
  780. * be skipped.
  781. *
  782. * @return a <code>long</code> representing the number of bytes
  783. * skipped.
  784. *
  785. * @exception IOException if an I/O error occurs.
  786. */
  787. long skipBytes(long n) throws IOException;
  788. /**
  789. * Sets the current stream position to the desired location. The
  790. * next read will occur at this location. The bit offset is set
  791. * to 0.
  792. *
  793. * <p> An <code>IndexOutOfBoundsException</code> will be thrown if
  794. * <code>pos</code> is smaller than the flushed position (as
  795. * returned by <code>getflushedPosition</code>).
  796. *
  797. * <p> It is legal to seek past the end of the file; an
  798. * <code>EOFException</code> will be thrown only if a read is
  799. * performed.
  800. *
  801. * @param pos a <code>long</code> containing the desired file
  802. * pointer position.
  803. *
  804. * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
  805. * than the flushed position.
  806. * @exception IOException if any other I/O error occurs.
  807. */
  808. void seek(long pos) throws IOException;
  809. /**
  810. * Marks a position in the stream to be returned to by a
  811. * subsequent call to <code>reset</code>. Unlike a standard
  812. * <code>InputStream</code>, all <code>ImageInputStream</code>s
  813. * support marking. Additionally, calls to <code>mark</code> and
  814. * <code>reset</code> may be nested arbitrarily.
  815. *
  816. * <p> Unlike the <code>mark</code> methods declared by the
  817. * <code>Reader</code> <code>InputStream</code> interfaces, no
  818. * <code>readLimit</code> parameter is used. An arbitrary amount
  819. * of data may be read following the call to <code>mark</code>.
  820. *
  821. * <p> The bit position used by the <code>readBits</code> method
  822. * is saved and restored by each pair of calls to
  823. * <code>mark</code> and <code>reset</code>.
  824. */
  825. void mark();
  826. /**
  827. * Returns the file pointer to its previous position, including
  828. * the bit offset, at the time of the most recent unmatched call
  829. * to <code>mark</code>.
  830. *
  831. * <p> Calls to <code>reset</code> without a corresponding call
  832. * to <code>mark</code> have no effect.
  833. *
  834. * <p> An <code>IOException</code> will be thrown if the previous
  835. * marked position lies in the discarded portion of the stream.
  836. *
  837. * @exception IOException if an I/O error occurs.
  838. */
  839. void reset() throws IOException;
  840. /**
  841. * Discards the initial portion of the stream prior to the
  842. * indicated postion. Attempting to seek to an offset within the
  843. * flushed portion of the stream will result in an
  844. * <code>IndexOutOfBoundsException</code>.
  845. *
  846. * <p> Calling <code>flushBefore</code> may allow classes
  847. * implementing this interface to free up resources such as memory
  848. * or disk space that are being used to store data from the
  849. * stream.
  850. *
  851. * @param pos a <code>long</code> containing the length of the
  852. * file prefix that may be flushed.
  853. *
  854. * @exception IndexOutOfBoundsException if <code>pos</code> lies
  855. * in the flushed portion of the stream or past the current stream
  856. * position.
  857. * @exception IOException if an I/O error occurs.
  858. */
  859. void flushBefore(long pos) throws IOException;
  860. /**
  861. * Discards the initial position of the stream prior to the current
  862. * stream position. Equivalent to
  863. * <code>flushBefore(getStreamPosition())</code>.
  864. *
  865. * @exception IOException if an I/O error occurs.
  866. */
  867. void flush() throws IOException;
  868. /**
  869. * Returns the earliest position in the stream to which seeking
  870. * may be performed. The returned value will be the maximum of
  871. * all values passed into previous calls to
  872. * <code>flushBefore</code>.
  873. *
  874. * @return the earliest legal position for seeking, as a
  875. * <code>long</code>.
  876. */
  877. long getFlushedPosition();
  878. /**
  879. * Returns <code>true</code> if this <code>ImageInputStream</code>
  880. * caches data itself in order to allow seeking backwards.
  881. * Applications may consult this in order to decide how frequently,
  882. * or whether, to flush in order to conserve cache resources.
  883. *
  884. * @return <code>true</code> if this <code>ImageInputStream</code>
  885. * caches data.
  886. *
  887. * @see #isCachedMemory
  888. * @see #isCachedFile
  889. */
  890. boolean isCached();
  891. /**
  892. * Returns <code>true</code> if this <code>ImageInputStream</code>
  893. * caches data itself in order to allow seeking backwards, and
  894. * the cache is kept in main memory. Applications may consult
  895. * this in order to decide how frequently, or whether, to flush
  896. * in order to conserve cache resources.
  897. *
  898. * @return <code>true</code> if this <code>ImageInputStream</code>
  899. * caches data in main memory.
  900. *
  901. * @see #isCached
  902. * @see #isCachedFile
  903. */
  904. boolean isCachedMemory();
  905. /**
  906. * Returns <code>true</code> if this <code>ImageInputStream</code>
  907. * caches data itself in order to allow seeking backwards, and
  908. * the cache is kept in a temporary file. Applications may consult
  909. * this in order to decide how frequently, or whether, to flush
  910. * in order to conserve cache resources.
  911. *
  912. * @return <code>true</code> if this <code>ImageInputStream</code>
  913. * caches data in a temporary file.
  914. *
  915. * @see #isCached
  916. * @see #isCachedMemory
  917. */
  918. boolean isCachedFile();
  919. /**
  920. * Closes the stream. Attempts to access a stream that has been
  921. * closed may result in <code>IOException</code>s or incorrect
  922. * behavior. Calling this method may allow classes implementing
  923. * this interface to release resources associated with the stream
  924. * such as memory, disk space, or file descriptors.
  925. *
  926. * @exception IOException if an I/O error occurs.
  927. */
  928. void close() throws IOException;
  929. }