1. /*
  2. * @(#)DataInput.java 1.14 01/11/29
  3. *
  4. * Copyright 2002 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.14, 11/29/01
  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. * @exception EOFException if this stream reaches the end before reading
  117. * all the bytes.
  118. * @exception IOException if an I/O error occurs.
  119. */
  120. void readFully(byte b[], int off, int len) throws IOException;
  121. /**
  122. * Makes an attempt to skip over
  123. * <code>n</code> bytes
  124. * of data from the input
  125. * stream, discarding the skipped bytes. However,
  126. * it may skip
  127. * over some smaller number of
  128. * bytes, possibly zero. This may result from
  129. * any of a
  130. * number of conditions; reaching
  131. * end of file before <code>n</code> bytes
  132. * have been skipped is
  133. * only one possibility.
  134. * This method never throws an <code>EOFException</code>.
  135. * The actual
  136. * number of bytes skipped is returned.
  137. *
  138. * @param n the number of bytes to be skipped.
  139. * @return the number of bytes skipped, which is always <code>n</code>.
  140. * @exception EOFException if this stream reaches the end before skipping
  141. * all the bytes.
  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 if this stream reaches the end before reading all the bytes.
  384. * @exception IOException if an I/O error occurs.
  385. */
  386. String readLine() throws IOException;
  387. /**
  388. * Reads in a string that has been encoded using a modified UTF-8 format.
  389. * The general contract of <code>readUTF</code>
  390. * is that it reads a representation of a Unicode
  391. * character string encoded in Java modified
  392. * UTF-8 format; this string of characters
  393. * is then returned as a <code>String</code>.
  394. * <p>
  395. * First, two bytes are read and used to
  396. * construct an unsigned 16-bit integer in
  397. * exactly the manner of the <code>readUnsignedShort</code>
  398. * method . This integer value is called the
  399. * <i>UTF length</i> and specifies the number
  400. * of additional bytes to be read. These bytes
  401. * are then converted to characters by considering
  402. * them in groups. The length of each group
  403. * is computed from the value of the first
  404. * byte of the group. The byte following a
  405. * group, if any, is the first byte of the
  406. * next group.
  407. * <p>
  408. * If the first byte of a group
  409. * matches the bit pattern <code>0xxxxxxx</code>
  410. * (where <code>x</code> means "may be <code>0</code>
  411. * or <code>1</code>"), then the group consists
  412. * of just that byte. The byte is zero-extended
  413. * to form a character.
  414. * <p>
  415. * If the first byte
  416. * of a group matches the bit pattern <code>110xxxxx</code>,
  417. * then the group consists of that byte <code>a</code>
  418. * and a second byte <code>b</code>. If there
  419. * is no byte <code>b</code> (because byte
  420. * <code>a</code> was the last of the bytes
  421. * to be read), or if byte <code>b</code> does
  422. * not match the bit pattern <code>10xxxxxx</code>,
  423. * then a <code>UTFDataFormatException</code>
  424. * is thrown. Otherwise, the group is converted
  425. * to the character:<p>
  426. * <pre><code>(char)(((a& 0x1F) << 6) | (b & 0x3F))
  427. * </code></pre>
  428. * If the first byte of a group
  429. * matches the bit pattern <code>1110xxxx</code>,
  430. * then the group consists of that byte <code>a</code>
  431. * and two more bytes <code>b</code> and <code>c</code>.
  432. * If there is no byte <code>c</code> (because
  433. * byte <code>a</code> was one of the last
  434. * two of the bytes to be read), or either
  435. * byte <code>b</code> or byte <code>c</code>
  436. * does not match the bit pattern <code>10xxxxxx</code>,
  437. * then a <code>UTFDataFormatException</code>
  438. * is thrown. Otherwise, the group is converted
  439. * to the character:<p>
  440. * <pre><code>
  441. * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
  442. * </code></pre>
  443. * If the first byte of a group matches the
  444. * pattern <code>1111xxxx</code> or the pattern
  445. * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
  446. * is thrown.
  447. * <p>
  448. * If end of file is encountered
  449. * at any time during this entire process,
  450. * then an <code>EOFException</code> is thrown.
  451. * <p>
  452. * After every group has been converted to
  453. * a character by this process, the characters
  454. * are gathered, in the same order in which
  455. * their corresponding groups were read from
  456. * the input stream, to form a <code>String</code>,
  457. * which is returned.
  458. * <p>
  459. * The <code>writeUTF</code>
  460. * method of interface <code>DataOutput</code>
  461. * may be used to write data that is suitable
  462. * for reading by this method.
  463. * @return a Unicode string.
  464. * @exception EOFException if this stream reaches the end
  465. * before reading all the bytes.
  466. * @exception IOException if an I/O error occurs.
  467. * @exception UTFDataFormatException if the bytes do not represent a
  468. * valid UTF-8 encoding of a string.
  469. */
  470. String readUTF() throws IOException;
  471. }