DataInput.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* DataInput.java -- Interface for reading data from a stream
  2. Copyright (C) 1998, 1999, 2001, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  33. * "The Java Language Specification", ISBN 0-201-63451-1
  34. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  35. * Status: Believed complete and correct. */
  36. /**
  37. * This interface is implemented by classes that can data from streams
  38. * into Java primitive types.
  39. *
  40. * @author Aaron M. Renn (arenn@urbanophile.com)
  41. * @author Warren Levy (warrenl@cygnus.com)
  42. */
  43. public interface DataInput
  44. {
  45. /**
  46. * This method reads a Java boolean value from an input stream. It does
  47. * so by reading a single byte of data. If that byte is zero, then the
  48. * value returned is <code>false</code>. If the byte is non-zero, then
  49. * the value returned is <code>true</code>.
  50. * <p>
  51. * This method can read a <code>boolean</code> written by an object
  52. * implementing the <code>writeBoolean()</code> method in the
  53. * <code>DataOutput</code> interface.
  54. *
  55. * @return The <code>boolean</code> value read
  56. *
  57. * @exception EOFException If end of file is reached before
  58. * reading the boolean
  59. * @exception IOException If any other error occurs
  60. *
  61. * @see DataOutput#writeBoolean
  62. */
  63. boolean readBoolean() throws EOFException, IOException;
  64. /**
  65. * This method reads a Java byte value from an input stream. The value
  66. * is in the range of -128 to 127.
  67. * <p>
  68. * This method can read a <code>byte</code> written by an object
  69. * implementing the
  70. * <code>writeByte()</code> method in the <code>DataOutput</code> interface.
  71. * <p>
  72. * @return The <code>byte</code> value read
  73. *
  74. * @exception EOFException If end of file is reached before reading the byte
  75. * @exception IOException If any other error occurs
  76. *
  77. * @see DataOutput#writeByte
  78. */
  79. byte readByte() throws EOFException, IOException;
  80. /**
  81. * This method reads 8 unsigned bits into a Java <code>int</code> value from
  82. * the stream. The value returned is in the range of 0 to 255.
  83. * <p>
  84. * This method can read an unsigned byte written by an object
  85. * implementing the
  86. * <code>writeByte()</code> method in the <code>DataOutput</code>
  87. * interface.
  88. *
  89. * @return The unsigned bytes value read as a Java <code>int</code>.
  90. *
  91. * @exception EOFException If end of file is reached before reading the value
  92. * @exception IOException If any other error occurs
  93. *
  94. * @see DataOutput#writeByte
  95. */
  96. int readUnsignedByte() throws EOFException, IOException;
  97. /**
  98. * This method reads a Java <code>char</code> value from an input stream.
  99. * It operates by reading two bytes from the stream and converting them to
  100. * a single 16-bit Java <code>char</code>. The two bytes are stored most
  101. * significant byte first (i.e., "big endian") regardless of the native
  102. * host byte ordering.
  103. * <p>
  104. * As an example, if <code>byte1</code> and <code>byte2</code> represent the
  105. * first and second byte read from the stream respectively, they will be
  106. * transformed to a <code>char</code> in the following manner:
  107. * <p>
  108. * <code>(char)((byte1 << 8) + byte2)</code>
  109. * <p>
  110. * This method can read a <code>char</code> written by an object implementing
  111. * the
  112. * <code>writeChar()</code> method in the <code>DataOutput</code> interface.
  113. *
  114. * @return The <code>char</code> value read
  115. *
  116. * @exception EOFException If end of file is reached before reading the char
  117. * @exception IOException If any other error occurs
  118. *
  119. * @see DataOutput#writeChar
  120. */
  121. char readChar() throws EOFException, IOException;
  122. /**
  123. * This method reads a signed 16-bit value into a Java in from the stream.
  124. * It operates by reading two bytes from the stream and converting them to
  125. * a single 16-bit Java <code>short</code>. The two bytes are stored most
  126. * significant byte first (i.e., "big endian") regardless of the native
  127. * host byte ordering.
  128. * <p>
  129. * As an example, if <code>byte1</code> and <code>byte2</code> represent the
  130. * first and second byte read from the stream respectively, they will be
  131. * transformed to a <code>short</code> in the following manner:
  132. * <p>
  133. * <code>(short)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code>
  134. * <p>
  135. * The value returned is in the range of -32768 to 32767.
  136. * <p>
  137. * This method can read a <code>short</code> written by an object
  138. * implementing
  139. * the <code>writeShort()</code> method in the <code>DataOutput</code>
  140. * interface.
  141. *
  142. * @return The <code>short</code> value read
  143. *
  144. * @exception EOFException If end of file is reached before reading the value
  145. * @exception IOException If any other error occurs
  146. *
  147. * @see DataOutput#writeShort
  148. */
  149. short readShort() throws EOFException, IOException;
  150. /**
  151. * This method reads 16 unsigned bits into a Java int value from the stream.
  152. * It operates by reading two bytes from the stream and converting them to
  153. * a single Java <code>int</code>. The two bytes are stored most
  154. * significant byte first (i.e., "big endian") regardless of the native
  155. * host byte ordering.
  156. * <p>
  157. * As an example, if <code>byte1</code> and <code>byte2</code> represent the
  158. * first and second byte read from the stream respectively, they will be
  159. * transformed to an <code>int</code> in the following manner:
  160. * <p>
  161. * <code>(int)(((byte1 0xFF) << 8) + (byte2 & 0xFF))</code>
  162. * <p>
  163. * The value returned is in the range of 0 to 65535.
  164. * <p>
  165. * This method can read an unsigned short written by an object implementing
  166. * the <code>writeShort()</code> method in the
  167. * <code>DataOutput</code>
  168. * interface.
  169. *
  170. * @return The unsigned short value read as a Java <code>int</code>.
  171. *
  172. * @exception EOFException If end of file is reached before reading
  173. * the value
  174. * @exception IOException If any other error occurs
  175. *
  176. * @see DataOutput#writeShort
  177. */
  178. int readUnsignedShort() throws EOFException, IOException;
  179. /**
  180. * This method reads a Java <code>int</code> value from an input stream
  181. * It operates by reading four bytes from the stream and converting them to
  182. * a single Java <code>int</code>. The bytes are stored most
  183. * significant byte first (i.e., "big endian") regardless of the native
  184. * host byte ordering.
  185. * <p>
  186. * As an example, if <code>byte1</code> through <code>byte4</code> represent
  187. * the first four bytes read from the stream, they will be
  188. * transformed to an <code>int</code> in the following manner:
  189. * <p>
  190. * <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) +
  191. * ((byte3 & 0xFF)<< 8) + (byte4 & 0xFF)))</code>
  192. * <p>
  193. * The value returned is in the range of -2147483648 to 2147483647.
  194. * <p>
  195. * This method can read an <code>int</code> written by an object
  196. * implementing the <code>writeInt()</code> method in the
  197. * <code>DataOutput</code> interface.
  198. *
  199. * @return The <code>int</code> value read
  200. *
  201. * @exception EOFException If end of file is reached before reading the int
  202. * @exception IOException If any other error occurs
  203. *
  204. * @see DataOutput#writeInt
  205. */
  206. int readInt() throws EOFException, IOException;
  207. /**
  208. * This method reads a Java <code>long</code> value from an input stream
  209. * It operates by reading eight bytes from the stream and converting them to
  210. * a single Java <code>long</code>. The bytes are stored most
  211. * significant byte first (i.e., "big endian") regardless of the native
  212. * host byte ordering.
  213. * <p>
  214. * As an example, if <code>byte1</code> through <code>byte8</code> represent
  215. * the first eight bytes read from the stream, they will be
  216. * transformed to an <code>long</code> in the following manner:
  217. * <p>
  218. * <code>(long)(((byte1 & 0xFF) << 56) + ((byte2 & 0xFF) << 48) +
  219. * ((byte3 & 0xFF) << 40) + ((byte4 & 0xFF) << 32) +
  220. * ((byte5 & 0xFF) << 24) + ((byte6 & 0xFF) << 16) +
  221. * ((byte7 & 0xFF) << 8) + (byte8 & 0xFF)))
  222. * </code>
  223. * <p>
  224. * The value returned is in the range of -9223372036854775808 to
  225. * 9223372036854775807.
  226. * <p>
  227. * This method can read an <code>long</code> written by an object
  228. * implementing the <code>writeLong()</code> method in the
  229. * <code>DataOutput</code> interface.
  230. *
  231. * @return The <code>long</code> value read
  232. *
  233. * @exception EOFException If end of file is reached before reading the long
  234. * @exception IOException If any other error occurs
  235. *
  236. * @see DataOutput#writeLong
  237. */
  238. long readLong() throws EOFException, IOException;
  239. /**
  240. * This method reads a Java float value from an input stream. It operates
  241. * by first reading an <code>int</code> value from the stream by calling the
  242. * <code>readInt()</code> method in this interface, then converts that
  243. * <code>int</code> to a <code>float</code> using the
  244. * <code>intBitsToFloat</code> method in the class
  245. * <code>java.lang.Float</code>.
  246. * <p>
  247. * This method can read a <code>float</code> written by an object
  248. * implementing
  249. * the <code>writeFloat()</code> method in the <code>DataOutput</code>
  250. * interface.
  251. *
  252. * @return The <code>float</code> value read
  253. *
  254. * @exception EOFException If end of file is reached before reading the
  255. * float
  256. * @exception IOException If any other error occurs
  257. *
  258. * @see DataOutput#writeFloat
  259. * @see java.lang.Float#intBitsToFloat
  260. */
  261. float readFloat() throws EOFException, IOException;
  262. /**
  263. * This method reads a Java double value from an input stream. It operates
  264. * by first reading a <code>long</code> value from the stream by calling the
  265. * <code>readLong()</code> method in this interface, then converts that
  266. * <code>long</code> to a <code>double</code> using the
  267. * <code>longBitsToDouble</code> method in the class
  268. * <code>java.lang.Double</code>.
  269. * <p>
  270. * This method can read a <code>double</code> written by an object
  271. * implementing the <code>writeDouble()</code> method in the
  272. * <code>DataOutput</code> interface.
  273. *
  274. * @return The <code>double</code> value read
  275. *
  276. * @exception EOFException If end of file is reached before reading the
  277. * double
  278. * @exception IOException If any other error occurs
  279. *
  280. * @see DataOutput#writeDouble
  281. * @see java.lang.Double#longBitsToDouble
  282. */
  283. double readDouble() throws EOFException, IOException;
  284. /**
  285. * This method reads the next line of text data from an input stream.
  286. * It operates by reading bytes and converting those bytes to
  287. * <code>char</code>
  288. * values by treating the byte read as the low eight bits of the
  289. * <code>char</code> and using 0 as the high eight bits. Because of this,
  290. * it does not support the full 16-bit Unicode character set.
  291. * <P>
  292. * The reading of bytes ends when either the end of file or a line terminator
  293. * is encountered. The bytes read are then returned as a
  294. * <code>String</code>.
  295. * A line terminator is a byte sequence consisting of either
  296. * <code>\r</code>, <code>\n</code> or <code>\r\n</code>. These termination
  297. * charaters are discarded and are not returned as part of the string.
  298. * A line is also terminated by an end of file condition.
  299. * <p>
  300. *
  301. * @return The line read as a <code>String</code>
  302. *
  303. * @exception IOException If an error occurs
  304. */
  305. String readLine() throws IOException;
  306. /**
  307. * This method reads a <code>String</code> from an input stream that is
  308. * encoded in a modified UTF-8 format. This format has a leading two byte
  309. * sequence that contains the remaining number of bytes to read.
  310. * This two byte
  311. * sequence is read using the <code>readUnsignedShort()</code> method of this
  312. * interface.
  313. *
  314. * After the number of remaining bytes have been determined, these bytes
  315. * are read an transformed into <code>char</code> values. These
  316. * <code>char</code> values are encoded in the stream using either a one,
  317. * two, or three byte format.
  318. * The particular format in use can be determined by examining the first
  319. * byte read.
  320. * <p>
  321. * If the first byte has a high order bit of 0, then
  322. * that character consists on only one byte. This character value consists
  323. * of seven bits that are at positions 0 through 6 of the byte. As an
  324. * example, if <code>byte1</code> is the byte read from the stream, it would
  325. * be converted to a <code>char</code> like so:
  326. * <p>
  327. * <code>(char)byte1</code>
  328. * <p>
  329. * If the first byte has 110 as its high order bits, then the
  330. * character consists of two bytes. The bits that make up the character
  331. * value are in positions 0 through 4 of the first byte and bit positions
  332. * 0 through 5 of the second byte. (The second byte should have
  333. * 10 as its high order bits). These values are in most significant
  334. * byte first (i.e., "big endian") order.
  335. * <p>
  336. * As an example, if <code>byte1</code> and <code>byte2</code> are the first
  337. * two bytes read respectively, and the high order bits of them match the
  338. * patterns which indicate a two byte character encoding, then they would be
  339. * converted to a Java <code>char</code> like so:
  340. * <p>
  341. * <code>(char)(((byte1 &amp; 0x1F) &lt;&lt; 6) + (byte2 &amp; 0x3F))</code>
  342. * <p>
  343. * If the first byte has a 1110 as its high order bits, then the
  344. * character consists of three bytes. The bits that make up the character
  345. * value are in positions 0 through 3 of the first byte and bit positions
  346. * 0 through 5 of the other two bytes. (The second and third bytes should
  347. * have 10 as their high order bits). These values are in most
  348. * significant byte first (i.e., "big endian") order.
  349. * <p>
  350. * As an example, if <code>byte1</code>, <code>byte2</code>, and
  351. * <code>byte3</code> are the three bytes read, and the high order bits of
  352. * them match the patterns which indicate a three byte character encoding,
  353. * then they would be converted to a Java <code>char</code> like so:
  354. *
  355. * <code>
  356. * (char)(((byte1 &amp; 0x0F) &lt;&lt; 12) + ((byte2 &amp; 0x3F) + (byte3 &amp; 0x3F))
  357. * </code>
  358. *
  359. * Note that all characters are encoded in the method that requires the
  360. * fewest number of bytes with the exception of the character with the
  361. * value of <code>\&lt;llll&gt;u0000</code> which is encoded as two bytes.
  362. * This is a modification of the UTF standard used to prevent C language
  363. * style <code>NUL</code> values from appearing in the byte stream.
  364. * <p>
  365. * This method can read data that was written by an object implementing the
  366. * <code>writeUTF()</code> method in <code>DataOutput</code>.
  367. *
  368. * @return The <code>String</code> read
  369. *
  370. * @exception EOFException If end of file is reached before reading the
  371. * String
  372. * @exception UTFDataFormatException If the data is not in UTF-8 format
  373. * @exception IOException If any other error occurs
  374. *
  375. * @see DataOutput#writeUTF
  376. */
  377. String readUTF() throws EOFException, UTFDataFormatException, IOException;
  378. /**
  379. * This method reads raw bytes into the passed array until the array is
  380. * full. Note that this method blocks until the data is available and
  381. * throws an exception if there is not enough data left in the stream to
  382. * fill the buffer. Note also that zero length buffers are permitted.
  383. * In this case, the method will return immediately without reading any
  384. * bytes from the stream.
  385. *
  386. * @param buf The buffer into which to read the data
  387. *
  388. * @exception EOFException If end of file is reached before filling the
  389. * buffer
  390. * @exception IOException If any other error occurs
  391. */
  392. void readFully(byte[] buf) throws EOFException, IOException;
  393. /**
  394. * This method reads raw bytes into the passed array <code>buf</code>
  395. * starting
  396. * <code>offset</code> bytes into the buffer. The number of bytes read
  397. * will be
  398. * exactly <code>len</code>. Note that this method blocks until the data is
  399. * available and throws an exception if there is not enough data left in
  400. * the stream to read <code>len</code> bytes. Note also that zero length
  401. * buffers are permitted. In this case, the method will return immediately
  402. * without reading any bytes from the stream.
  403. *
  404. * @param buf The buffer into which to read the data
  405. * @param offset The offset into the buffer to start storing data
  406. * @param len The number of bytes to read into the buffer
  407. *
  408. * @exception EOFException If end of file is reached before filling the
  409. * buffer
  410. * @exception IOException If any other error occurs
  411. */
  412. void readFully(byte[] buf, int offset, int len)
  413. throws EOFException, IOException;
  414. /**
  415. * This method skips and discards the specified number of bytes in an
  416. * input stream. Note that this method may skip less than the requested
  417. * number of bytes. The actual number of bytes skipped is returned.
  418. * No bytes are skipped if a negative number is passed to this method.
  419. *
  420. * @param numBytes The number of bytes to skip
  421. *
  422. * @return The number of bytes actually skipped, which will always be
  423. * <code>numBytes</code>
  424. *
  425. * @exception EOFException If end of file is reached before all bytes can be
  426. * skipped
  427. * @exception IOException If any other error occurs
  428. */
  429. int skipBytes(int numBytes) throws EOFException, IOException;
  430. } // interface DataInput