AbstractDataInput.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* AbstractDataInput.java --
  2. Copyright (C) 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 gnu.CORBA.CDR;
  32. import java.io.IOException;
  33. /**
  34. * Some data input stream that can be either Big or
  35. * Little Endian.
  36. *
  37. * This class reuses code from GNU Classpath DataInputStream.
  38. *
  39. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  40. * @author Warren Levy (warrenl@cygnus.com)
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. */
  43. public interface AbstractDataInput
  44. {
  45. /**
  46. * This method reads bytes from the underlying stream into the specified
  47. * byte array buffer. It will attempt to fill the buffer completely, but
  48. * may return a short count if there is insufficient data remaining to be
  49. * read to fill the buffer.
  50. *
  51. * @param b The buffer into which bytes will be read.
  52. *
  53. * @return The actual number of bytes read, or -1 if end of stream reached
  54. * before reading any bytes.
  55. *
  56. * @exception IOException If an error occurs.
  57. */
  58. int read(byte[] b)
  59. throws IOException;
  60. /**
  61. * This method reads bytes from the underlying stream into the specified
  62. * byte array buffer. It will attempt to read <code>len</code> bytes and
  63. * will start storing them at position <code>off</code> into the buffer.
  64. * This method can return a short count if there is insufficient data
  65. * remaining to be read to complete the desired read length.
  66. *
  67. * @param b The buffer into which bytes will be read.
  68. * @param off The offset into the buffer to start storing bytes.
  69. * @param len The requested number of bytes to read.
  70. *
  71. * @return The actual number of bytes read, or -1 if end of stream reached
  72. * before reading any bytes.
  73. *
  74. * @exception IOException If an error occurs.
  75. */
  76. int read(byte[] b, int off, int len)
  77. throws IOException;
  78. /**
  79. * This method reads a Java boolean value from an input stream. It does
  80. * so by reading a single byte of data. If that byte is zero, then the
  81. * value returned is <code>false</code>. If the byte is non-zero, then
  82. * the value returned is <code>true</code>.
  83. * <p>
  84. * This method can read a <code>boolean</code> written by an object
  85. * implementing the <code>writeBoolean()</code> method in the
  86. * <code>DataOutput</code> interface.
  87. *
  88. * @return The <code>boolean</code> value read
  89. *
  90. * @exception EOFException If end of file is reached before reading
  91. * the boolean
  92. * @exception IOException If any other error occurs
  93. *
  94. * @see DataOutput#writeBoolean
  95. */
  96. boolean readBoolean()
  97. throws IOException;
  98. /**
  99. * This method reads a Java byte value from an input stream. The value
  100. * is in the range of -128 to 127.
  101. * <p>
  102. * This method can read a <code>byte</code> written by an object
  103. * implementing the <code>writeByte()</code> method in the
  104. * <code>DataOutput</code> interface.
  105. *
  106. * @return The <code>byte</code> value read
  107. *
  108. * @exception EOFException If end of file is reached before reading the byte
  109. * @exception IOException If any other error occurs
  110. *
  111. * @see DataOutput#writeByte
  112. */
  113. byte readByte()
  114. throws IOException;
  115. /**
  116. * This method reads a Java <code>char</code> value from an input stream.
  117. * It operates by reading two bytes from the stream and converting them to
  118. * a single 16-bit Java <code>char</code>. The two bytes are stored most
  119. * significant byte first (i.e., "big endian") regardless of the native
  120. * host byte ordering.
  121. * <p>
  122. * As an example, if <code>byte1</code> and <code>byte2</code>
  123. * represent the first and second byte read from the stream
  124. * respectively, they will be transformed to a <code>char</code> in
  125. * the following manner:
  126. * <p>
  127. * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
  128. * <p>
  129. * This method can read a <code>char</code> written by an object
  130. * implementing the <code>writeChar()</code> method in the
  131. * <code>DataOutput</code> interface.
  132. *
  133. * @return The <code>char</code> value read
  134. *
  135. * @exception EOFException If end of file is reached before reading the char
  136. * @exception IOException If any other error occurs
  137. *
  138. * @see DataOutput#writeChar
  139. */
  140. char readChar()
  141. throws IOException;
  142. /**
  143. * This method reads a Java double value from an input stream. It operates
  144. * by first reading a <code>long</code> value from the stream by calling the
  145. * <code>readLong()</code> method in this interface, then converts
  146. * that <code>long</code> to a <code>double</code> using the
  147. * <code>longBitsToDouble</code> method in the class
  148. * <code>java.lang.Double</code>
  149. * <p>
  150. * This method can read a <code>double</code> written by an object
  151. * implementing the <code>writeDouble()</code> method in the
  152. * <code>DataOutput</code> interface.
  153. *
  154. * @return The <code>double</code> value read
  155. *
  156. * @exception EOFException If end of file is reached before reading
  157. * the double
  158. * @exception IOException If any other error occurs
  159. *
  160. * @see DataOutput#writeDouble
  161. * @see java.lang.Double#longBitsToDouble
  162. */
  163. double readDouble()
  164. throws IOException;
  165. /**
  166. * This method reads a Java float value from an input stream. It
  167. * operates by first reading an <code>int</code> value from the
  168. * stream by calling the <code>readInt()</code> method in this
  169. * interface, then converts that <code>int</code> to a
  170. * <code>float</code> using the <code>intBitsToFloat</code> method
  171. * in the class <code>java.lang.Float</code>
  172. * <p>
  173. * This method can read a <code>float</code> written by an object
  174. * implementing the <code>writeFloat()</code> method in the
  175. * <code>DataOutput</code> interface.
  176. *
  177. * @return The <code>float</code> value read
  178. *
  179. * @exception EOFException If end of file is reached before reading the float
  180. * @exception IOException If any other error occurs
  181. *
  182. * @see DataOutput#writeFloat
  183. * @see java.lang.Float#intBitsToFloat
  184. */
  185. float readFloat()
  186. throws IOException;
  187. /**
  188. * This method reads raw bytes into the passed array until the array is
  189. * full. Note that this method blocks until the data is available and
  190. * throws an exception if there is not enough data left in the stream to
  191. * fill the buffer. Note also that zero length buffers are permitted.
  192. * In this case, the method will return immediately without reading any
  193. * bytes from the stream.
  194. *
  195. * @param b The buffer into which to read the data
  196. *
  197. * @exception EOFException If end of file is reached before filling the
  198. * buffer
  199. * @exception IOException If any other error occurs
  200. */
  201. void readFully(byte[] b)
  202. throws IOException;
  203. /**
  204. * This method reads a Java <code>int</code> value from an input stream
  205. * It operates by reading four bytes from the stream and converting them to
  206. * a single Java <code>int</code>. The bytes are stored most
  207. * significant byte first (i.e., "big endian") regardless of the native
  208. * host byte ordering.
  209. * <p>
  210. * As an example, if <code>byte1</code> through <code>byte4</code> represent
  211. * the first four bytes read from the stream, they will be
  212. * transformed to an <code>int</code> in the following manner:
  213. * <p>
  214. * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
  215. * ((byte3 &amp; 0xFF)&lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
  216. * <p>
  217. * The value returned is in the range of -2147483648 to 2147483647.
  218. * <p>
  219. * This method can read an <code>int</code> written by an object
  220. * implementing the <code>writeInt()</code> method in the
  221. * <code>DataOutput</code> interface.
  222. *
  223. * @return The <code>int</code> value read
  224. *
  225. * @exception EOFException If end of file is reached before reading the int
  226. * @exception IOException If any other error occurs
  227. *
  228. * @see DataOutput#writeInt
  229. */
  230. int readInt()
  231. throws IOException;
  232. /**
  233. * This method reads a Java <code>long</code> value from an input stream
  234. * It operates by reading eight bytes from the stream and converting them to
  235. * a single Java <code>long</code>. The bytes are stored most
  236. * significant byte first (i.e., "big endian") regardless of the native
  237. * host byte ordering.
  238. * <p>
  239. * As an example, if <code>byte1</code> through <code>byte8</code> represent
  240. * the first eight bytes read from the stream, they will be
  241. * transformed to an <code>long</code> in the following manner:
  242. * <p>
  243. * <code>(long)(((byte1 &amp; 0xFF) &lt;&lt; 56) + ((byte2 &amp; 0xFF) &lt;&lt; 48) +
  244. * ((byte3 &amp; 0xFF) &lt;&lt; 40) + ((byte4 &amp; 0xFF) &lt;&lt; 32) +
  245. * ((byte5 &amp; 0xFF) &lt;&lt; 24) + ((byte6 &amp; 0xFF) &lt;&lt; 16) +
  246. * ((byte7 &amp; 0xFF) &lt;&lt; 8) + (byte8 &amp; 0xFF)))
  247. * </code>
  248. * <p>
  249. * The value returned is in the range of -9223372036854775808 to
  250. * 9223372036854775807.
  251. * <p>
  252. * This method can read an <code>long</code> written by an object
  253. * implementing the <code>writeLong()</code> method in the
  254. * <code>DataOutput</code> interface.
  255. *
  256. * @return The <code>long</code> value read
  257. *
  258. * @exception EOFException If end of file is reached before reading the long
  259. * @exception IOException If any other error occurs
  260. *
  261. * @see DataOutput#writeLong
  262. */
  263. long readLong()
  264. throws IOException;
  265. /**
  266. * This method reads a signed 16-bit value into a Java in from the
  267. * stream. It operates by reading two bytes from the stream and
  268. * converting them to a single 16-bit Java <code>short</code>. The
  269. * two bytes are stored most significant byte first (i.e., "big
  270. * endian") regardless of the native host byte ordering.
  271. * <p>
  272. * As an example, if <code>byte1</code> and <code>byte2</code>
  273. * represent the first and second byte read from the stream
  274. * respectively, they will be transformed to a <code>short</code>. in
  275. * the following manner:
  276. * <p>
  277. * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF))</code>
  278. * <p>
  279. * The value returned is in the range of -32768 to 32767.
  280. * <p>
  281. * This method can read a <code>short</code> written by an object
  282. * implementing the <code>writeShort()</code> method in the
  283. * <code>DataOutput</code> interface.
  284. *
  285. * @return The <code>short</code> value read
  286. *
  287. * @exception EOFException If end of file is reached before reading the value
  288. * @exception IOException If any other error occurs
  289. *
  290. * @see DataOutput#writeShort
  291. */
  292. short readShort()
  293. throws IOException;
  294. /**
  295. * This method reads 8 unsigned bits into a Java <code>int</code>
  296. * value from the stream. The value returned is in the range of 0 to
  297. * 255.
  298. * <p>
  299. * This method can read an unsigned byte written by an object
  300. * implementing the <code>writeUnsignedByte()</code> method in the
  301. * <code>DataOutput</code> interface.
  302. *
  303. * @return The unsigned bytes value read as a Java <code>int</code>.
  304. *
  305. * @exception EOFException If end of file is reached before reading the value
  306. * @exception IOException If any other error occurs
  307. *
  308. * @see DataOutput#writeByte
  309. */
  310. int readUnsignedByte()
  311. throws IOException;
  312. /**
  313. * This method reads 16 unsigned bits into a Java int value from the stream.
  314. * It operates by reading two bytes from the stream and converting them to
  315. * a single Java <code>int</code> The two bytes are stored most
  316. * significant byte first (i.e., "big endian") regardless of the native
  317. * host byte ordering.
  318. * <p>
  319. * As an example, if <code>byte1</code> and <code>byte2</code>
  320. * represent the first and second byte read from the stream
  321. * respectively, they will be transformed to an <code>int</code> in
  322. * the following manner:
  323. * <p>
  324. * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
  325. * <p>
  326. * The value returned is in the range of 0 to 65535.
  327. * <p>
  328. * This method can read an unsigned short written by an object
  329. * implementing the <code>writeUnsignedShort()</code> method in the
  330. * <code>DataOutput</code> interface.
  331. *
  332. * @return The unsigned short value read as a Java <code>int</code>
  333. *
  334. * @exception EOFException If end of file is reached before reading the value
  335. * @exception IOException If any other error occurs
  336. *
  337. * @see DataOutput#writeShort
  338. */
  339. int readUnsignedShort()
  340. throws IOException;
  341. /**
  342. * Read a single byte.
  343. *
  344. * @return a byte, extracted from the stream or -1 if
  345. * EOF has been reached.
  346. * @throws IOException
  347. */
  348. public int read()
  349. throws IOException;
  350. /**
  351. * This method attempts to skip and discard the specified number of bytes
  352. * in the input stream. It may actually skip fewer bytes than requested.
  353. * This method will not skip any bytes if passed a negative number of bytes
  354. * to skip.
  355. *
  356. * @param n The requested number of bytes to skip.
  357. *
  358. * @return The requested number of bytes to skip.
  359. *
  360. * @exception IOException If an error occurs.
  361. * @specnote The JDK docs claim that this returns the number of bytes
  362. * actually skipped. The JCL claims that this method can throw an
  363. * EOFException. Neither of these appear to be true in the JDK 1.3's
  364. * implementation. This tries to implement the actual JDK behaviour.
  365. */
  366. int skipBytes(int n)
  367. throws IOException;
  368. }