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