1. /*
  2. * @(#)DataInput.java 1.20 03/01/23
  3. *
  4. * Copyright 2003 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 Java modified UTF-8 format.
  16. * <p>
  17. * It is generally true of all the reading
  18. * routines in this interface that if end of
  19. * file is reached before the desired number
  20. * of bytes has been read, an <code>EOFException</code>
  21. * (which is a kind of <code>IOException</code>)
  22. * is thrown. If any byte cannot be read for
  23. * any reason other than end of file, an <code>IOException</code>
  24. * other than <code>EOFException</code> is
  25. * thrown. In particular, an <code>IOException</code>
  26. * may be thrown if the input stream has been
  27. * closed.
  28. *
  29. * @author Frank Yellin
  30. * @version 1.20, 01/23/03
  31. * @see java.io.DataInputStream
  32. * @see java.io.DataOutput
  33. * @since JDK1.0
  34. */
  35. public
  36. interface DataInput {
  37. /**
  38. * Reads some bytes from an input
  39. * stream and stores them into the buffer
  40. * array <code>b</code>. The number of bytes
  41. * read is equal
  42. * to the length of <code>b</code>.
  43. * <p>
  44. * This method blocks until one of the
  45. * following conditions occurs:<p>
  46. * <ul>
  47. * <li><code>b.length</code>
  48. * bytes of input data are available, in which
  49. * case a normal return is made.
  50. *
  51. * <li>End of
  52. * file is detected, in which case an <code>EOFException</code>
  53. * is thrown.
  54. *
  55. * <li>An I/O error occurs, in
  56. * which case an <code>IOException</code> other
  57. * than <code>EOFException</code> is thrown.
  58. * </ul>
  59. * <p>
  60. * If <code>b</code> is <code>null</code>,
  61. * a <code>NullPointerException</code> is thrown.
  62. * If <code>b.length</code> is zero, then
  63. * no bytes are read. Otherwise, the first
  64. * byte read is stored into element <code>b[0]</code>,
  65. * the next one into <code>b[1]</code>, and
  66. * so on.
  67. * If an exception is thrown from
  68. * this method, then it may be that some but
  69. * not all bytes of <code>b</code> have been
  70. * updated with data from the input stream.
  71. *
  72. * @param b the buffer into which the data is read.
  73. * @exception EOFException if this stream reaches the end before reading
  74. * all the bytes.
  75. * @exception IOException if an I/O error occurs.
  76. */
  77. void readFully(byte b[]) throws IOException;
  78. /**
  79. *
  80. * Reads <code>len</code>
  81. * bytes from
  82. * an input stream.
  83. * <p>
  84. * This method
  85. * blocks until one of the following conditions
  86. * occurs:<p>
  87. * <ul>
  88. * <li><code>len</code> bytes
  89. * of input data are available, in which case
  90. * a normal return is made.
  91. *
  92. * <li>End of file
  93. * is detected, in which case an <code>EOFException</code>
  94. * is thrown.
  95. *
  96. * <li>An I/O error occurs, in
  97. * which case an <code>IOException</code> other
  98. * than <code>EOFException</code> is thrown.
  99. * </ul>
  100. * <p>
  101. * If <code>b</code> is <code>null</code>,
  102. * a <code>NullPointerException</code> is thrown.
  103. * If <code>off</code> is negative, or <code>len</code>
  104. * is negative, or <code>off+len</code> is
  105. * greater than the length of the array <code>b</code>,
  106. * then an <code>IndexOutOfBoundsException</code>
  107. * is thrown.
  108. * If <code>len</code> is zero,
  109. * then no bytes are read. Otherwise, the first
  110. * byte read is stored into element <code>b[off]</code>,
  111. * the next one into <code>b[off+1]</code>,
  112. * and so on. The number of bytes read is,
  113. * at most, equal to <code>len</code>.
  114. *
  115. * @param b the buffer into which the data is read.
  116. * @param off an int specifying the offset into the data.
  117. * @param len an int specifying the number of bytes to read.
  118. * @exception EOFException if this stream reaches the end before reading
  119. * all the bytes.
  120. * @exception IOException if an I/O error occurs.
  121. */
  122. void readFully(byte b[], int off, int len) throws IOException;
  123. /**
  124. * Makes an attempt to skip over
  125. * <code>n</code> bytes
  126. * of data from the input
  127. * stream, discarding the skipped bytes. However,
  128. * it may skip
  129. * over some smaller number of
  130. * bytes, possibly zero. This may result from
  131. * any of a
  132. * number of conditions; reaching
  133. * end of file before <code>n</code> bytes
  134. * have been skipped is
  135. * only one possibility.
  136. * This method never throws an <code>EOFException</code>.
  137. * The actual
  138. * number of bytes skipped is returned.
  139. *
  140. * @param n the number of bytes to be skipped.
  141. * @return the number of bytes actually skipped.
  142. * @exception IOException if an I/O error occurs.
  143. */
  144. int skipBytes(int n) throws IOException;
  145. /**
  146. * Reads one input byte and returns
  147. * <code>true</code> if that byte is nonzero,
  148. * <code>false</code> if that byte is zero.
  149. * This method is suitable for reading
  150. * the byte written by the <code>writeBoolean</code>
  151. * method of interface <code>DataOutput</code>.
  152. *
  153. * @return the <code>boolean</code> value read.
  154. * @exception EOFException if this stream reaches the end before reading
  155. * all the bytes.
  156. * @exception IOException if an I/O error occurs.
  157. */
  158. boolean readBoolean() throws IOException;
  159. /**
  160. * Reads and returns one input byte.
  161. * The byte is treated as a signed value in
  162. * the range <code>-128</code> through <code>127</code>,
  163. * inclusive.
  164. * This method is suitable for
  165. * reading the byte written by the <code>writeByte</code>
  166. * method of interface <code>DataOutput</code>.
  167. *
  168. * @return the 8-bit value read.
  169. * @exception EOFException if this stream reaches the end before reading
  170. * all the bytes.
  171. * @exception IOException if an I/O error occurs.
  172. */
  173. byte readByte() throws IOException;
  174. /**
  175. * Reads one input byte, zero-extends
  176. * it to type <code>int</code>, and returns
  177. * the result, which is therefore in the range
  178. * <code>0</code>
  179. * through <code>255</code>.
  180. * This method is suitable for reading
  181. * the byte written by the <code>writeByte</code>
  182. * method of interface <code>DataOutput</code>
  183. * if the argument to <code>writeByte</code>
  184. * was intended to be a value in the range
  185. * <code>0</code> through <code>255</code>.
  186. *
  187. * @return the unsigned 8-bit value read.
  188. * @exception EOFException if this stream reaches the end before reading
  189. * all the bytes.
  190. * @exception IOException if an I/O error occurs.
  191. */
  192. int readUnsignedByte() throws IOException;
  193. /**
  194. * Reads two input bytes and returns
  195. * a <code>short</code> value. Let <code>a</code>
  196. * be the first byte read and <code>b</code>
  197. * be the second byte. The value
  198. * returned
  199. * is:
  200. * <p><pre><code>(short)((a << 8) * | (b & 0xff))
  201. * </code></pre>
  202. * This method
  203. * is suitable for reading the bytes written
  204. * by the <code>writeShort</code> method of
  205. * interface <code>DataOutput</code>.
  206. *
  207. * @return the 16-bit value read.
  208. * @exception EOFException if this stream reaches the end before reading
  209. * all the bytes.
  210. * @exception IOException if an I/O error occurs.
  211. */
  212. short readShort() throws IOException;
  213. /**
  214. * Reads two input bytes and returns
  215. * an <code>int</code> value in the range <code>0</code>
  216. * through <code>65535</code>. Let <code>a</code>
  217. * be the first byte read and
  218. * <code>b</code>
  219. * be the second byte. The value returned is:
  220. * <p><pre><code>(((a & 0xff) << 8) | (b & 0xff))
  221. * </code></pre>
  222. * This method is suitable for reading the bytes
  223. * written by the <code>writeShort</code> method
  224. * of interface <code>DataOutput</code> if
  225. * the argument to <code>writeShort</code>
  226. * was intended to be a value in the range
  227. * <code>0</code> through <code>65535</code>.
  228. *
  229. * @return the unsigned 16-bit value read.
  230. * @exception EOFException if this stream reaches the end before reading
  231. * all the bytes.
  232. * @exception IOException if an I/O error occurs.
  233. */
  234. int readUnsignedShort() throws IOException;
  235. /**
  236. * Reads an input <code>char</code> and returns the <code>char</code> value.
  237. * A Unicode <code>char</code> is made up of two bytes.
  238. * Let <code>a</code>
  239. * be the first byte read and <code>b</code>
  240. * be the second byte. The value
  241. * returned is:
  242. * <p><pre><code>(char)((a << 8) | (b & 0xff))
  243. * </code></pre>
  244. * This method
  245. * is suitable for reading bytes written by
  246. * the <code>writeChar</code> method of interface
  247. * <code>DataOutput</code>.
  248. *
  249. * @return the Unicode <code>char</code> read.
  250. * @exception EOFException if this stream reaches the end before reading
  251. * all the bytes.
  252. * @exception IOException if an I/O error occurs.
  253. */
  254. char readChar() throws IOException;
  255. /**
  256. * Reads four input bytes and returns an
  257. * <code>int</code> value. Let <code>a</code>
  258. * be the first byte read, <code>b</code> be
  259. * the second byte, <code>c</code> be the third
  260. * byte,
  261. * and <code>d</code> be the fourth
  262. * byte. The value returned is:
  263. * <p><pre>
  264. * <code>
  265. * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
  266. * ((c & 0xff) << 8) | (d & 0xff))
  267. * </code></pre>
  268. * This method is suitable
  269. * for reading bytes written by the <code>writeInt</code>
  270. * method of interface <code>DataOutput</code>.
  271. *
  272. * @return the <code>int</code> value read.
  273. * @exception EOFException if this stream reaches the end before reading
  274. * all the bytes.
  275. * @exception IOException if an I/O error occurs.
  276. */
  277. int readInt() throws IOException;
  278. /**
  279. * Reads eight input bytes and returns
  280. * a <code>long</code> value. Let <code>a</code>
  281. * be the first byte read, <code>b</code> be
  282. * the second byte, <code>c</code> be the third
  283. * byte, <code>d</code>
  284. * be the fourth byte,
  285. * <code>e</code> be the fifth byte, <code>f</code>
  286. * be the sixth byte, <code>g</code> be the
  287. * seventh byte,
  288. * and <code>h</code> be the
  289. * eighth byte. The value returned is:
  290. * <p><pre> <code>
  291. * (((long)(a & 0xff) << 56) |
  292. * ((long)(b & 0xff) << 48) |
  293. * ((long)(c & 0xff) << 40) |
  294. * ((long)(d & 0xff) << 32) |
  295. * ((long)(e & 0xff) << 24) |
  296. * ((long)(f & 0xff) << 16) |
  297. * ((long)(g & 0xff) << 8) |
  298. * ((long)(h & 0xff)))
  299. * </code></pre>
  300. * <p>
  301. * This method is suitable
  302. * for reading bytes written by the <code>writeLong</code>
  303. * method of interface <code>DataOutput</code>.
  304. *
  305. * @return the <code>long</code> value read.
  306. * @exception EOFException if this stream reaches the end before reading
  307. * all the bytes.
  308. * @exception IOException if an I/O error occurs.
  309. */
  310. long readLong() throws IOException;
  311. /**
  312. * Reads four input bytes and returns
  313. * a <code>float</code> value. It does this
  314. * by first constructing an <code>int</code>
  315. * value in exactly the manner
  316. * of the <code>readInt</code>
  317. * method, then converting this <code>int</code>
  318. * value to a <code>float</code> in
  319. * exactly the manner of the method <code>Float.intBitsToFloat</code>.
  320. * This method is suitable for reading
  321. * bytes written by the <code>writeFloat</code>
  322. * method of interface <code>DataOutput</code>.
  323. *
  324. * @return the <code>float</code> value read.
  325. * @exception EOFException if this stream reaches the end before reading
  326. * all the bytes.
  327. * @exception IOException if an I/O error occurs.
  328. */
  329. float readFloat() throws IOException;
  330. /**
  331. * Reads eight input bytes and returns
  332. * a <code>double</code> value. It does this
  333. * by first constructing a <code>long</code>
  334. * value in exactly the manner
  335. * of the <code>readlong</code>
  336. * method, then converting this <code>long</code>
  337. * value to a <code>double</code> in exactly
  338. * the manner of the method <code>Double.longBitsToDouble</code>.
  339. * This method is suitable for reading
  340. * bytes written by the <code>writeDouble</code>
  341. * method of interface <code>DataOutput</code>.
  342. *
  343. * @return the <code>double</code> value read.
  344. * @exception EOFException if this stream reaches the end before reading
  345. * all the bytes.
  346. * @exception IOException if an I/O error occurs.
  347. */
  348. double readDouble() throws IOException;
  349. /**
  350. * Reads the next line of text from the input stream.
  351. * It reads successive bytes, converting
  352. * each byte separately into a character,
  353. * until it encounters a line terminator or
  354. * end of
  355. * file; the characters read are then
  356. * returned as a <code>String</code>. Note
  357. * that because this
  358. * method processes bytes,
  359. * it does not support input of the full Unicode
  360. * character set.
  361. * <p>
  362. * If end of file is encountered
  363. * before even one byte can be read, then <code>null</code>
  364. * is returned. Otherwise, each byte that is
  365. * read is converted to type <code>char</code>
  366. * by zero-extension. If the character <code>'\n'</code>
  367. * is encountered, it is discarded and reading
  368. * ceases. If the character <code>'\r'</code>
  369. * is encountered, it is discarded and, if
  370. * the following byte converts to the
  371. * character <code>'\n'</code>, then that is
  372. * discarded also; reading then ceases. If
  373. * end of file is encountered before either
  374. * of the characters <code>'\n'</code> and
  375. * <code>'\r'</code> is encountered, reading
  376. * ceases. Once reading has ceased, a <code>String</code>
  377. * is returned that contains all the characters
  378. * read and not discarded, taken in order.
  379. * Note that every character in this string
  380. * will have a value less than <code>\u0100</code>,
  381. * that is, <code>(char)256</code>.
  382. *
  383. * @return the next line of text from the input stream,
  384. * or <CODE>null</CODE> if the end of file is
  385. * encountered before a byte can be read.
  386. * @exception IOException if an I/O error occurs.
  387. */
  388. String readLine() throws IOException;
  389. /**
  390. * Reads in a string that has been encoded using a modified UTF-8 format.
  391. * The general contract of <code>readUTF</code>
  392. * is that it reads a representation of a Unicode
  393. * character string encoded in Java modified
  394. * UTF-8 format; this string of characters
  395. * is then returned as a <code>String</code>.
  396. * <p>
  397. * First, two bytes are read and used to
  398. * construct an unsigned 16-bit integer in
  399. * exactly the manner of the <code>readUnsignedShort</code>
  400. * method . This integer value is called the
  401. * <i>UTF length</i> and specifies the number
  402. * of additional bytes to be read. These bytes
  403. * are then converted to characters by considering
  404. * them in groups. The length of each group
  405. * is computed from the value of the first
  406. * byte of the group. The byte following a
  407. * group, if any, is the first byte of the
  408. * next group.
  409. * <p>
  410. * If the first byte of a group
  411. * matches the bit pattern <code>0xxxxxxx</code>
  412. * (where <code>x</code> means "may be <code>0</code>
  413. * or <code>1</code>"), then the group consists
  414. * of just that byte. The byte is zero-extended
  415. * to form a character.
  416. * <p>
  417. * If the first byte
  418. * of a group matches the bit pattern <code>110xxxxx</code>,
  419. * then the group consists of that byte <code>a</code>
  420. * and a second byte <code>b</code>. If there
  421. * is no byte <code>b</code> (because byte
  422. * <code>a</code> was the last of the bytes
  423. * to be read), or if byte <code>b</code> does
  424. * not match the bit pattern <code>10xxxxxx</code>,
  425. * then a <code>UTFDataFormatException</code>
  426. * is thrown. Otherwise, the group is converted
  427. * to the character:<p>
  428. * <pre><code>(char)(((a& 0x1F) << 6) | (b & 0x3F))
  429. * </code></pre>
  430. * If the first byte of a group
  431. * matches the bit pattern <code>1110xxxx</code>,
  432. * then the group consists of that byte <code>a</code>
  433. * and two more bytes <code>b</code> and <code>c</code>.
  434. * If there is no byte <code>c</code> (because
  435. * byte <code>a</code> was one of the last
  436. * two of the bytes to be read), or either
  437. * byte <code>b</code> or byte <code>c</code>
  438. * does not match the bit pattern <code>10xxxxxx</code>,
  439. * then a <code>UTFDataFormatException</code>
  440. * is thrown. Otherwise, the group is converted
  441. * to the character:<p>
  442. * <pre><code>
  443. * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
  444. * </code></pre>
  445. * If the first byte of a group matches the
  446. * pattern <code>1111xxxx</code> or the pattern
  447. * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
  448. * is thrown.
  449. * <p>
  450. * If end of file is encountered
  451. * at any time during this entire process,
  452. * then an <code>EOFException</code> is thrown.
  453. * <p>
  454. * After every group has been converted to
  455. * a character by this process, the characters
  456. * are gathered, in the same order in which
  457. * their corresponding groups were read from
  458. * the input stream, to form a <code>String</code>,
  459. * which is returned.
  460. * <p>
  461. * The <code>writeUTF</code>
  462. * method of interface <code>DataOutput</code>
  463. * may be used to write data that is suitable
  464. * for reading by this method.
  465. * @return a Unicode string.
  466. * @exception EOFException if this stream reaches the end
  467. * before reading all the bytes.
  468. * @exception IOException if an I/O error occurs.
  469. * @exception UTFDataFormatException if the bytes do not represent a
  470. * valid UTF-8 encoding of a string.
  471. */
  472. String readUTF() throws IOException;
  473. }