1. /*
  2. * @(#)DataInput.java 1.23 04/06/03
  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. * The <code>DataInput</code> interface provides
  10. * for reading bytes from a binary stream and
  11. * reconstructing from them data in any of
  12. * the Java primitive types. There is also
  13. * a
  14. * facility for reconstructing a <code>String</code>
  15. * from data in
  16. * <a href="#modified-utf-8">modified UTF-8</a>
  17. * format.
  18. * <p>
  19. * It is generally true of all the reading
  20. * routines in this interface that if end of
  21. * file is reached before the desired number
  22. * of bytes has been read, an <code>EOFException</code>
  23. * (which is a kind of <code>IOException</code>)
  24. * is thrown. If any byte cannot be read for
  25. * any reason other than end of file, an <code>IOException</code>
  26. * other than <code>EOFException</code> is
  27. * thrown. In particular, an <code>IOException</code>
  28. * may be thrown if the input stream has been
  29. * closed.
  30. *
  31. * <h4><a name="modified-utf-8">Modified UTF-8</a></h4>
  32. * <p>
  33. * Implementations of the DataInput and DataOutput interfaces represent
  34. * Unicode strings in a format that is a slight modification of UTF-8.
  35. * (For information regarding the standard UTF-8 format, see section
  36. * <i>3.9 Unicode Encoding Forms</i> of <i>The Unicode Standard, Version
  37. * 4.0</i>).
  38. * Note that in the following tables, the most significant bit appears in the
  39. * far left-hand column.
  40. * <p>
  41. * All characters in the range <code>'\u0001'</code> to
  42. * <code>'\u007F'</code> are represented by a single byte:
  43. *
  44. * <blockquote>
  45. * <table border="1" cellspacing="0" cellpadding="8" width="50%"
  46. * summary="Bit values and bytes">
  47. * <tr>
  48. * <td></td>
  49. * <th id="bit">Bit Values</th>
  50. * </tr>
  51. * <tr>
  52. * <th id="byte1">Byte 1</th>
  53. * <td>
  54. * <table border="1" cellspacing="0" width="100%">
  55. * <tr>
  56. * <td width="12%"><center>0</center>
  57. * <td colspan="7"><center>bits 6-0</center>
  58. * </tr>
  59. * </table>
  60. * </td>
  61. * </tr>
  62. * </table>
  63. * </blockquote>
  64. *
  65. * <p>
  66. * The null character <code>'\u0000'</code> and characters in the
  67. * range <code>'\u0080'</code> to <code>'\u07FF'</code> are
  68. * represented by a pair of bytes:
  69. *
  70. * <blockquote>
  71. * <table border="1" cellspacing="0" cellpadding="8" width="50%"
  72. * summary="Bit values and bytes">
  73. * <tr>
  74. * <td></td>
  75. * <th id="bit">Bit Values</th>
  76. * </tr>
  77. * <tr>
  78. * <th id="byte1">Byte 1</th>
  79. * <td>
  80. * <table border="1" cellspacing="0" width="100%">
  81. * <tr>
  82. * <td width="12%"><center>1</center>
  83. * <td width="13%"><center>1</center>
  84. * <td width="12%"><center>0</center>
  85. * <td colspan="5"><center>bits 10-6</center>
  86. * </tr>
  87. * </table>
  88. * </td>
  89. * </tr>
  90. * <tr>
  91. * <th id="byte2">Byte 2</th>
  92. * <td>
  93. * <table border="1" cellspacing="0" width="100%">
  94. * <tr>
  95. * <td width="12%"><center>1</center>
  96. * <td width="13%"><center>0</center>
  97. * <td colspan="6"><center>bits 5-0</center>
  98. * </tr>
  99. * </table>
  100. * </td>
  101. * </tr>
  102. * </table>
  103. * </blockquote>
  104. *
  105. * <br>
  106. * <code>char</code> values in the range <code>'\u0800'</code> to
  107. * <code>'\uFFFF'</code> are represented by three bytes:
  108. *
  109. * <blockquote>
  110. * <table border="1" cellspacing="0" cellpadding="8" width="50%"
  111. * summary="Bit values and bytes">
  112. * <tr>
  113. * <td></td>
  114. * <th id="bit">Bit Values</th>
  115. * </tr>
  116. * <tr>
  117. * <th id="byte1">Byte 1</th>
  118. * <td>
  119. * <table border="1" cellspacing="0" width="100%">
  120. * <tr>
  121. * <td width="12%"><center>1</center>
  122. * <td width="13%"><center>1</center>
  123. * <td width="12%"><center>1</center>
  124. * <td width="13%"><center>0</center>
  125. * <td colspan="4"><center>bits 15-12</center>
  126. * </tr>
  127. * </table>
  128. * </td>
  129. * </tr>
  130. * <tr>
  131. * <th id="byte2">Byte 2</th>
  132. * <td>
  133. * <table border="1" cellspacing="0" width="100%">
  134. * <tr>
  135. * <td width="12%"><center>1</center>
  136. * <td width="13%"><center>0</center>
  137. * <td colspan="6"><center>bits 11-6</center>
  138. * </tr>
  139. * </table>
  140. * </td>
  141. * </tr>
  142. * <tr>
  143. * <th id="byte3">Byte 3</th>
  144. * <td>
  145. * <table border="1" cellspacing="0" width="100%">
  146. * <tr>
  147. * <td width="12%"><center>1</center>
  148. * <td width="13%"><center>0</center>
  149. * <td colspan="6"><center>bits 5-0</center>
  150. * </tr>
  151. * </table>
  152. * </td>
  153. * </tr>
  154. * </table>
  155. * </blockquote>
  156. *
  157. * <p>
  158. * The differences between this format and the
  159. * standard UTF-8 format are the following:
  160. * <ul>
  161. * <li>The null byte <code>'\u0000'</code> is encoded in 2-byte format
  162. * rather than 1-byte, so that the encoded strings never have
  163. * embedded nulls.
  164. * <li>Only the 1-byte, 2-byte, and 3-byte formats are used.
  165. * <li><a href="../lang/Character.html#unicode">Supplementary characters</a>
  166. * are represented in the form of surrogate pairs.
  167. * </ul>
  168. * @author Frank Yellin
  169. * @version 1.23, 06/03/04
  170. * @see java.io.DataInputStream
  171. * @see java.io.DataOutput
  172. * @since JDK1.0
  173. */
  174. public
  175. interface DataInput {
  176. /**
  177. * Reads some bytes from an input
  178. * stream and stores them into the buffer
  179. * array <code>b</code>. The number of bytes
  180. * read is equal
  181. * to the length of <code>b</code>.
  182. * <p>
  183. * This method blocks until one of the
  184. * following conditions occurs:<p>
  185. * <ul>
  186. * <li><code>b.length</code>
  187. * bytes of input data are available, in which
  188. * case a normal return is made.
  189. *
  190. * <li>End of
  191. * file is detected, in which case an <code>EOFException</code>
  192. * is thrown.
  193. *
  194. * <li>An I/O error occurs, in
  195. * which case an <code>IOException</code> other
  196. * than <code>EOFException</code> is thrown.
  197. * </ul>
  198. * <p>
  199. * If <code>b</code> is <code>null</code>,
  200. * a <code>NullPointerException</code> is thrown.
  201. * If <code>b.length</code> is zero, then
  202. * no bytes are read. Otherwise, the first
  203. * byte read is stored into element <code>b[0]</code>,
  204. * the next one into <code>b[1]</code>, and
  205. * so on.
  206. * If an exception is thrown from
  207. * this method, then it may be that some but
  208. * not all bytes of <code>b</code> have been
  209. * updated with data from the input stream.
  210. *
  211. * @param b the buffer into which the data is read.
  212. * @exception EOFException if this stream reaches the end before reading
  213. * all the bytes.
  214. * @exception IOException if an I/O error occurs.
  215. */
  216. void readFully(byte b[]) throws IOException;
  217. /**
  218. *
  219. * Reads <code>len</code>
  220. * bytes from
  221. * an input stream.
  222. * <p>
  223. * This method
  224. * blocks until one of the following conditions
  225. * occurs:<p>
  226. * <ul>
  227. * <li><code>len</code> bytes
  228. * of input data are available, in which case
  229. * a normal return is made.
  230. *
  231. * <li>End of file
  232. * is detected, in which case an <code>EOFException</code>
  233. * is thrown.
  234. *
  235. * <li>An I/O error occurs, in
  236. * which case an <code>IOException</code> other
  237. * than <code>EOFException</code> is thrown.
  238. * </ul>
  239. * <p>
  240. * If <code>b</code> is <code>null</code>,
  241. * a <code>NullPointerException</code> is thrown.
  242. * If <code>off</code> is negative, or <code>len</code>
  243. * is negative, or <code>off+len</code> is
  244. * greater than the length of the array <code>b</code>,
  245. * then an <code>IndexOutOfBoundsException</code>
  246. * is thrown.
  247. * If <code>len</code> is zero,
  248. * then no bytes are read. Otherwise, the first
  249. * byte read is stored into element <code>b[off]</code>,
  250. * the next one into <code>b[off+1]</code>,
  251. * and so on. The number of bytes read is,
  252. * at most, equal to <code>len</code>.
  253. *
  254. * @param b the buffer into which the data is read.
  255. * @param off an int specifying the offset into the data.
  256. * @param len an int specifying the number of bytes to read.
  257. * @exception EOFException if this stream reaches the end before reading
  258. * all the bytes.
  259. * @exception IOException if an I/O error occurs.
  260. */
  261. void readFully(byte b[], int off, int len) throws IOException;
  262. /**
  263. * Makes an attempt to skip over
  264. * <code>n</code> bytes
  265. * of data from the input
  266. * stream, discarding the skipped bytes. However,
  267. * it may skip
  268. * over some smaller number of
  269. * bytes, possibly zero. This may result from
  270. * any of a
  271. * number of conditions; reaching
  272. * end of file before <code>n</code> bytes
  273. * have been skipped is
  274. * only one possibility.
  275. * This method never throws an <code>EOFException</code>.
  276. * The actual
  277. * number of bytes skipped is returned.
  278. *
  279. * @param n the number of bytes to be skipped.
  280. * @return the number of bytes actually skipped.
  281. * @exception IOException if an I/O error occurs.
  282. */
  283. int skipBytes(int n) throws IOException;
  284. /**
  285. * Reads one input byte and returns
  286. * <code>true</code> if that byte is nonzero,
  287. * <code>false</code> if that byte is zero.
  288. * This method is suitable for reading
  289. * the byte written by the <code>writeBoolean</code>
  290. * method of interface <code>DataOutput</code>.
  291. *
  292. * @return the <code>boolean</code> value read.
  293. * @exception EOFException if this stream reaches the end before reading
  294. * all the bytes.
  295. * @exception IOException if an I/O error occurs.
  296. */
  297. boolean readBoolean() throws IOException;
  298. /**
  299. * Reads and returns one input byte.
  300. * The byte is treated as a signed value in
  301. * the range <code>-128</code> through <code>127</code>,
  302. * inclusive.
  303. * This method is suitable for
  304. * reading the byte written by the <code>writeByte</code>
  305. * method of interface <code>DataOutput</code>.
  306. *
  307. * @return the 8-bit value read.
  308. * @exception EOFException if this stream reaches the end before reading
  309. * all the bytes.
  310. * @exception IOException if an I/O error occurs.
  311. */
  312. byte readByte() throws IOException;
  313. /**
  314. * Reads one input byte, zero-extends
  315. * it to type <code>int</code>, and returns
  316. * the result, which is therefore in the range
  317. * <code>0</code>
  318. * through <code>255</code>.
  319. * This method is suitable for reading
  320. * the byte written by the <code>writeByte</code>
  321. * method of interface <code>DataOutput</code>
  322. * if the argument to <code>writeByte</code>
  323. * was intended to be a value in the range
  324. * <code>0</code> through <code>255</code>.
  325. *
  326. * @return the unsigned 8-bit value read.
  327. * @exception EOFException if this stream reaches the end before reading
  328. * all the bytes.
  329. * @exception IOException if an I/O error occurs.
  330. */
  331. int readUnsignedByte() throws IOException;
  332. /**
  333. * Reads two input bytes and returns
  334. * a <code>short</code> value. Let <code>a</code>
  335. * be the first byte read and <code>b</code>
  336. * be the second byte. The value
  337. * returned
  338. * is:
  339. * <p><pre><code>(short)((a << 8) | (b & 0xff))
  340. * </code></pre>
  341. * This method
  342. * is suitable for reading the bytes written
  343. * by the <code>writeShort</code> method of
  344. * interface <code>DataOutput</code>.
  345. *
  346. * @return the 16-bit value read.
  347. * @exception EOFException if this stream reaches the end before reading
  348. * all the bytes.
  349. * @exception IOException if an I/O error occurs.
  350. */
  351. short readShort() throws IOException;
  352. /**
  353. * Reads two input bytes and returns
  354. * an <code>int</code> value in the range <code>0</code>
  355. * through <code>65535</code>. Let <code>a</code>
  356. * be the first byte read and
  357. * <code>b</code>
  358. * be the second byte. The value returned is:
  359. * <p><pre><code>(((a & 0xff) << 8) | (b & 0xff))
  360. * </code></pre>
  361. * This method is suitable for reading the bytes
  362. * written by the <code>writeShort</code> method
  363. * of interface <code>DataOutput</code> if
  364. * the argument to <code>writeShort</code>
  365. * was intended to be a value in the range
  366. * <code>0</code> through <code>65535</code>.
  367. *
  368. * @return the unsigned 16-bit value read.
  369. * @exception EOFException if this stream reaches the end before reading
  370. * all the bytes.
  371. * @exception IOException if an I/O error occurs.
  372. */
  373. int readUnsignedShort() throws IOException;
  374. /**
  375. * Reads an input <code>char</code> and returns the <code>char</code> value.
  376. * A Unicode <code>char</code> is made up of two bytes.
  377. * Let <code>a</code>
  378. * be the first byte read and <code>b</code>
  379. * be the second byte. The value
  380. * returned is:
  381. * <p><pre><code>(char)((a << 8) | (b & 0xff))
  382. * </code></pre>
  383. * This method
  384. * is suitable for reading bytes written by
  385. * the <code>writeChar</code> method of interface
  386. * <code>DataOutput</code>.
  387. *
  388. * @return the Unicode <code>char</code> read.
  389. * @exception EOFException if this stream reaches the end before reading
  390. * all the bytes.
  391. * @exception IOException if an I/O error occurs.
  392. */
  393. char readChar() throws IOException;
  394. /**
  395. * Reads four input bytes and returns an
  396. * <code>int</code> value. Let <code>a</code>
  397. * be the first byte read, <code>b</code> be
  398. * the second byte, <code>c</code> be the third
  399. * byte,
  400. * and <code>d</code> be the fourth
  401. * byte. The value returned is:
  402. * <p><pre>
  403. * <code>
  404. * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
  405. * ((c & 0xff) << 8) | (d & 0xff))
  406. * </code></pre>
  407. * This method is suitable
  408. * for reading bytes written by the <code>writeInt</code>
  409. * method of interface <code>DataOutput</code>.
  410. *
  411. * @return the <code>int</code> value read.
  412. * @exception EOFException if this stream reaches the end before reading
  413. * all the bytes.
  414. * @exception IOException if an I/O error occurs.
  415. */
  416. int readInt() throws IOException;
  417. /**
  418. * Reads eight input bytes and returns
  419. * a <code>long</code> value. Let <code>a</code>
  420. * be the first byte read, <code>b</code> be
  421. * the second byte, <code>c</code> be the third
  422. * byte, <code>d</code>
  423. * be the fourth byte,
  424. * <code>e</code> be the fifth byte, <code>f</code>
  425. * be the sixth byte, <code>g</code> be the
  426. * seventh byte,
  427. * and <code>h</code> be the
  428. * eighth byte. The value returned is:
  429. * <p><pre> <code>
  430. * (((long)(a & 0xff) << 56) |
  431. * ((long)(b & 0xff) << 48) |
  432. * ((long)(c & 0xff) << 40) |
  433. * ((long)(d & 0xff) << 32) |
  434. * ((long)(e & 0xff) << 24) |
  435. * ((long)(f & 0xff) << 16) |
  436. * ((long)(g & 0xff) << 8) |
  437. * ((long)(h & 0xff)))
  438. * </code></pre>
  439. * <p>
  440. * This method is suitable
  441. * for reading bytes written by the <code>writeLong</code>
  442. * method of interface <code>DataOutput</code>.
  443. *
  444. * @return the <code>long</code> value read.
  445. * @exception EOFException if this stream reaches the end before reading
  446. * all the bytes.
  447. * @exception IOException if an I/O error occurs.
  448. */
  449. long readLong() throws IOException;
  450. /**
  451. * Reads four input bytes and returns
  452. * a <code>float</code> value. It does this
  453. * by first constructing an <code>int</code>
  454. * value in exactly the manner
  455. * of the <code>readInt</code>
  456. * method, then converting this <code>int</code>
  457. * value to a <code>float</code> in
  458. * exactly the manner of the method <code>Float.intBitsToFloat</code>.
  459. * This method is suitable for reading
  460. * bytes written by the <code>writeFloat</code>
  461. * method of interface <code>DataOutput</code>.
  462. *
  463. * @return the <code>float</code> value read.
  464. * @exception EOFException if this stream reaches the end before reading
  465. * all the bytes.
  466. * @exception IOException if an I/O error occurs.
  467. */
  468. float readFloat() throws IOException;
  469. /**
  470. * Reads eight input bytes and returns
  471. * a <code>double</code> value. It does this
  472. * by first constructing a <code>long</code>
  473. * value in exactly the manner
  474. * of the <code>readlong</code>
  475. * method, then converting this <code>long</code>
  476. * value to a <code>double</code> in exactly
  477. * the manner of the method <code>Double.longBitsToDouble</code>.
  478. * This method is suitable for reading
  479. * bytes written by the <code>writeDouble</code>
  480. * method of interface <code>DataOutput</code>.
  481. *
  482. * @return the <code>double</code> value read.
  483. * @exception EOFException if this stream reaches the end before reading
  484. * all the bytes.
  485. * @exception IOException if an I/O error occurs.
  486. */
  487. double readDouble() throws IOException;
  488. /**
  489. * Reads the next line of text from the input stream.
  490. * It reads successive bytes, converting
  491. * each byte separately into a character,
  492. * until it encounters a line terminator or
  493. * end of
  494. * file; the characters read are then
  495. * returned as a <code>String</code>. Note
  496. * that because this
  497. * method processes bytes,
  498. * it does not support input of the full Unicode
  499. * character set.
  500. * <p>
  501. * If end of file is encountered
  502. * before even one byte can be read, then <code>null</code>
  503. * is returned. Otherwise, each byte that is
  504. * read is converted to type <code>char</code>
  505. * by zero-extension. If the character <code>'\n'</code>
  506. * is encountered, it is discarded and reading
  507. * ceases. If the character <code>'\r'</code>
  508. * is encountered, it is discarded and, if
  509. * the following byte converts to the
  510. * character <code>'\n'</code>, then that is
  511. * discarded also; reading then ceases. If
  512. * end of file is encountered before either
  513. * of the characters <code>'\n'</code> and
  514. * <code>'\r'</code> is encountered, reading
  515. * ceases. Once reading has ceased, a <code>String</code>
  516. * is returned that contains all the characters
  517. * read and not discarded, taken in order.
  518. * Note that every character in this string
  519. * will have a value less than <code>\u0100</code>,
  520. * that is, <code>(char)256</code>.
  521. *
  522. * @return the next line of text from the input stream,
  523. * or <CODE>null</CODE> if the end of file is
  524. * encountered before a byte can be read.
  525. * @exception IOException if an I/O error occurs.
  526. */
  527. String readLine() throws IOException;
  528. /**
  529. * Reads in a string that has been encoded using a
  530. * <a href="#modified-utf-8">modified UTF-8</a>
  531. * format.
  532. * The general contract of <code>readUTF</code>
  533. * is that it reads a representation of a Unicode
  534. * character string encoded in modified
  535. * UTF-8 format; this string of characters
  536. * is then returned as a <code>String</code>.
  537. * <p>
  538. * First, two bytes are read and used to
  539. * construct an unsigned 16-bit integer in
  540. * exactly the manner of the <code>readUnsignedShort</code>
  541. * method . This integer value is called the
  542. * <i>UTF length</i> and specifies the number
  543. * of additional bytes to be read. These bytes
  544. * are then converted to characters by considering
  545. * them in groups. The length of each group
  546. * is computed from the value of the first
  547. * byte of the group. The byte following a
  548. * group, if any, is the first byte of the
  549. * next group.
  550. * <p>
  551. * If the first byte of a group
  552. * matches the bit pattern <code>0xxxxxxx</code>
  553. * (where <code>x</code> means "may be <code>0</code>
  554. * or <code>1</code>"), then the group consists
  555. * of just that byte. The byte is zero-extended
  556. * to form a character.
  557. * <p>
  558. * If the first byte
  559. * of a group matches the bit pattern <code>110xxxxx</code>,
  560. * then the group consists of that byte <code>a</code>
  561. * and a second byte <code>b</code>. If there
  562. * is no byte <code>b</code> (because byte
  563. * <code>a</code> was the last of the bytes
  564. * to be read), or if byte <code>b</code> does
  565. * not match the bit pattern <code>10xxxxxx</code>,
  566. * then a <code>UTFDataFormatException</code>
  567. * is thrown. Otherwise, the group is converted
  568. * to the character:<p>
  569. * <pre><code>(char)(((a& 0x1F) << 6) | (b & 0x3F))
  570. * </code></pre>
  571. * If the first byte of a group
  572. * matches the bit pattern <code>1110xxxx</code>,
  573. * then the group consists of that byte <code>a</code>
  574. * and two more bytes <code>b</code> and <code>c</code>.
  575. * If there is no byte <code>c</code> (because
  576. * byte <code>a</code> was one of the last
  577. * two of the bytes to be read), or either
  578. * byte <code>b</code> or byte <code>c</code>
  579. * does not match the bit pattern <code>10xxxxxx</code>,
  580. * then a <code>UTFDataFormatException</code>
  581. * is thrown. Otherwise, the group is converted
  582. * to the character:<p>
  583. * <pre><code>
  584. * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
  585. * </code></pre>
  586. * If the first byte of a group matches the
  587. * pattern <code>1111xxxx</code> or the pattern
  588. * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
  589. * is thrown.
  590. * <p>
  591. * If end of file is encountered
  592. * at any time during this entire process,
  593. * then an <code>EOFException</code> is thrown.
  594. * <p>
  595. * After every group has been converted to
  596. * a character by this process, the characters
  597. * are gathered, in the same order in which
  598. * their corresponding groups were read from
  599. * the input stream, to form a <code>String</code>,
  600. * which is returned.
  601. * <p>
  602. * The <code>writeUTF</code>
  603. * method of interface <code>DataOutput</code>
  604. * may be used to write data that is suitable
  605. * for reading by this method.
  606. * @return a Unicode string.
  607. * @exception EOFException if this stream reaches the end
  608. * before reading all the bytes.
  609. * @exception IOException if an I/O error occurs.
  610. * @exception UTFDataFormatException if the bytes do not represent a
  611. * valid modified UTF-8 encoding of a string.
  612. */
  613. String readUTF() throws IOException;
  614. }