DataInputStream.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* DataInputStream.java -- FilteredInputStream that implements DataInput
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2008
  3. Free Software Foundation
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.io;
  33. import gnu.java.lang.CPStringBuilder;
  34. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  35. * "The Java Language Specification", ISBN 0-201-63451-1
  36. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  37. * Status: Believed complete and correct.
  38. */
  39. /**
  40. * This subclass of <code>FilteredInputStream</code> implements the
  41. * <code>DataInput</code> interface that provides method for reading primitive
  42. * Java data types from a stream.
  43. *
  44. * @see DataInput
  45. *
  46. * @author Warren Levy (warrenl@cygnus.com)
  47. * @author Aaron M. Renn (arenn@urbanophile.com)
  48. * @date October 20, 1998.
  49. */
  50. public class DataInputStream extends FilterInputStream implements DataInput
  51. {
  52. // Byte buffer, used to make primitive read calls more efficient.
  53. byte[] buf = new byte [8];
  54. /**
  55. * This constructor initializes a new <code>DataInputStream</code>
  56. * to read from the specified subordinate stream.
  57. *
  58. * @param in The subordinate <code>InputStream</code> to read from
  59. */
  60. public DataInputStream (InputStream in)
  61. {
  62. super (in);
  63. }
  64. /**
  65. * This method reads bytes from the underlying stream into the specified
  66. * byte array buffer. It will attempt to fill the buffer completely, but
  67. * may return a short count if there is insufficient data remaining to be
  68. * read to fill the buffer.
  69. *
  70. * @param b The buffer into which bytes will be read.
  71. *
  72. * @return The actual number of bytes read, or -1 if end of stream reached
  73. * before reading any bytes.
  74. *
  75. * @exception IOException If an error occurs.
  76. */
  77. public final int read (byte[] b) throws IOException
  78. {
  79. return in.read (b, 0, b.length);
  80. }
  81. /**
  82. * This method reads bytes from the underlying stream into the specified
  83. * byte array buffer. It will attempt to read <code>len</code> bytes and
  84. * will start storing them at position <code>off</code> into the buffer.
  85. * This method can return a short count if there is insufficient data
  86. * remaining to be read to complete the desired read length.
  87. *
  88. * @param b The buffer into which bytes will be read.
  89. * @param off The offset into the buffer to start storing bytes.
  90. * @param len The requested number of bytes to read.
  91. *
  92. * @return The actual number of bytes read, or -1 if end of stream reached
  93. * before reading any bytes.
  94. *
  95. * @exception IOException If an error occurs.
  96. */
  97. public final int read (byte[] b, int off, int len) throws IOException
  98. {
  99. return in.read (b, off, len);
  100. }
  101. /**
  102. * This method reads a Java boolean value from an input stream. It does
  103. * so by reading a single byte of data. If that byte is zero, then the
  104. * value returned is <code>false</code>. If the byte is non-zero, then
  105. * the value returned is <code>true</code>.
  106. * <p>
  107. * This method can read a <code>boolean</code> written by an object
  108. * implementing the <code>writeBoolean()</code> method in the
  109. * <code>DataOutput</code> interface.
  110. *
  111. * @return The <code>boolean</code> value read
  112. *
  113. * @exception EOFException If end of file is reached before reading
  114. * the boolean
  115. * @exception IOException If any other error occurs
  116. *
  117. * @see DataOutput#writeBoolean
  118. */
  119. public final boolean readBoolean () throws IOException
  120. {
  121. return convertToBoolean (in.read ());
  122. }
  123. /**
  124. * This method reads a Java byte value from an input stream. The value
  125. * is in the range of -128 to 127.
  126. * <p>
  127. * This method can read a <code>byte</code> written by an object
  128. * implementing the <code>writeByte()</code> method in the
  129. * <code>DataOutput</code> interface.
  130. *
  131. * @return The <code>byte</code> value read
  132. *
  133. * @exception EOFException If end of file is reached before reading the byte
  134. * @exception IOException If any other error occurs
  135. *
  136. * @see DataOutput#writeByte
  137. */
  138. public final byte readByte () throws IOException
  139. {
  140. return convertToByte (in.read ());
  141. }
  142. /**
  143. * This method reads a Java <code>char</code> value from an input stream.
  144. * It operates by reading two bytes from the stream and converting them to
  145. * a single 16-bit Java <code>char</code>. The two bytes are stored most
  146. * significant byte first (i.e., "big endian") regardless of the native
  147. * host byte ordering.
  148. * <p>
  149. * As an example, if <code>byte1</code> and <code>byte2</code>
  150. * represent the first and second byte read from the stream
  151. * respectively, they will be transformed to a <code>char</code> in
  152. * the following manner:
  153. * <p>
  154. * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
  155. * <p>
  156. * This method can read a <code>char</code> written by an object
  157. * implementing the <code>writeChar()</code> method in the
  158. * <code>DataOutput</code> interface.
  159. *
  160. * @return The <code>char</code> value read
  161. *
  162. * @exception EOFException If end of file is reached before reading the char
  163. * @exception IOException If any other error occurs
  164. *
  165. * @see DataOutput#writeChar
  166. */
  167. public final char readChar () throws IOException
  168. {
  169. readFully (buf, 0, 2);
  170. return convertToChar (buf);
  171. }
  172. /**
  173. * This method reads a Java double value from an input stream. It operates
  174. * by first reading a <code>long</code> value from the stream by calling the
  175. * <code>readLong()</code> method in this interface, then converts
  176. * that <code>long</code> to a <code>double</code> using the
  177. * <code>longBitsToDouble</code> method in the class
  178. * <code>java.lang.Double</code>
  179. * <p>
  180. * This method can read a <code>double</code> written by an object
  181. * implementing the <code>writeDouble()</code> method in the
  182. * <code>DataOutput</code> interface.
  183. *
  184. * @return The <code>double</code> value read
  185. *
  186. * @exception EOFException If end of file is reached before reading
  187. * the double
  188. * @exception IOException If any other error occurs
  189. *
  190. * @see DataOutput#writeDouble
  191. * @see java.lang.Double#longBitsToDouble
  192. */
  193. public final double readDouble () throws IOException
  194. {
  195. return Double.longBitsToDouble (readLong ());
  196. }
  197. /**
  198. * This method reads a Java float value from an input stream. It
  199. * operates by first reading an <code>int</code> value from the
  200. * stream by calling the <code>readInt()</code> method in this
  201. * interface, then converts that <code>int</code> to a
  202. * <code>float</code> using the <code>intBitsToFloat</code> method
  203. * in the class <code>java.lang.Float</code>
  204. * <p>
  205. * This method can read a <code>float</code> written by an object
  206. * implementing the <code>writeFloat()</code> method in the
  207. * <code>DataOutput</code> interface.
  208. *
  209. * @return The <code>float</code> value read
  210. *
  211. * @exception EOFException If end of file is reached before reading the float
  212. * @exception IOException If any other error occurs
  213. *
  214. * @see DataOutput#writeFloat
  215. * @see java.lang.Float#intBitsToFloat
  216. */
  217. public final float readFloat () throws IOException
  218. {
  219. return Float.intBitsToFloat (readInt ());
  220. }
  221. /**
  222. * This method reads raw bytes into the passed array until the array is
  223. * full. Note that this method blocks until the data is available and
  224. * throws an exception if there is not enough data left in the stream to
  225. * fill the buffer. Note also that zero length buffers are permitted.
  226. * In this case, the method will return immediately without reading any
  227. * bytes from the stream.
  228. *
  229. * @param b The buffer into which to read the data
  230. *
  231. * @exception EOFException If end of file is reached before filling the
  232. * buffer
  233. * @exception IOException If any other error occurs
  234. */
  235. public final void readFully (byte[] b) throws IOException
  236. {
  237. readFully (b, 0, b.length);
  238. }
  239. /**
  240. * This method reads raw bytes into the passed array <code>buf</code>
  241. * starting
  242. * <code>offset</code> bytes into the buffer. The number of bytes read
  243. * will be
  244. * exactly <code>len</code>. Note that this method blocks until the data is
  245. * available and throws an exception if there is not enough data left in
  246. * the stream to read <code>len</code> bytes. Note also that zero length
  247. * buffers are permitted. In this case, the method will return immediately
  248. * without reading any bytes from the stream.
  249. *
  250. * @param buf The buffer into which to read the data
  251. * @param offset The offset into the buffer to start storing data
  252. * @param len The number of bytes to read into the buffer
  253. *
  254. * @exception EOFException If end of file is reached before filling the
  255. * buffer
  256. * @exception IOException If any other error occurs
  257. */
  258. public final void readFully (byte[] buf, int offset, int len) throws IOException
  259. {
  260. if (len < 0)
  261. throw new IndexOutOfBoundsException("Negative length: " + len);
  262. while (len > 0)
  263. {
  264. // in.read will block until some data is available.
  265. int numread = in.read (buf, offset, len);
  266. if (numread < 0)
  267. throw new EOFException ();
  268. len -= numread;
  269. offset += numread;
  270. }
  271. }
  272. /**
  273. * This method reads a Java <code>int</code> value from an input stream
  274. * It operates by reading four bytes from the stream and converting them to
  275. * a single Java <code>int</code>. The bytes are stored most
  276. * significant byte first (i.e., "big endian") regardless of the native
  277. * host byte ordering.
  278. * <p>
  279. * As an example, if <code>byte1</code> through <code>byte4</code> represent
  280. * the first four bytes read from the stream, they will be
  281. * transformed to an <code>int</code> in the following manner:
  282. * <p>
  283. * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
  284. * ((byte3 &amp; 0xFF)&lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
  285. * <p>
  286. * The value returned is in the range of -2147483648 to 2147483647.
  287. * <p>
  288. * This method can read an <code>int</code> written by an object
  289. * implementing the <code>writeInt()</code> method in the
  290. * <code>DataOutput</code> interface.
  291. *
  292. * @return The <code>int</code> value read
  293. *
  294. * @exception EOFException If end of file is reached before reading the int
  295. * @exception IOException If any other error occurs
  296. *
  297. * @see DataOutput#writeInt
  298. */
  299. public final int readInt () throws IOException
  300. {
  301. readFully (buf, 0, 4);
  302. return convertToInt (buf);
  303. }
  304. /**
  305. * This method reads the next line of text data from an input
  306. * stream. It operates by reading bytes and converting those bytes
  307. * to <code>char</code> values by treating the byte read as the low
  308. * eight bits of the <code>char</code> and using 0 as the high eight
  309. * bits. Because of this, it does not support the full 16-bit
  310. * Unicode character set.
  311. * <p>
  312. * The reading of bytes ends when either the end of file or a line
  313. * terminator is encountered. The bytes read are then returned as a
  314. * <code>String</code> A line terminator is a byte sequence
  315. * consisting of either <code>\r</code>, <code>\n</code> or
  316. * <code>\r\n</code>. These termination charaters are discarded and
  317. * are not returned as part of the string.
  318. * <p>
  319. * This method can read data that was written by an object implementing the
  320. * <code>writeLine()</code> method in <code>DataOutput</code>.
  321. *
  322. * @return The line read as a <code>String</code>
  323. *
  324. * @exception IOException If an error occurs
  325. *
  326. * @see DataOutput
  327. *
  328. * @deprecated
  329. */
  330. public final String readLine() throws IOException
  331. {
  332. CPStringBuilder strb = new CPStringBuilder();
  333. while (true)
  334. {
  335. int c = in.read();
  336. if (c == -1) // got an EOF
  337. return strb.length() > 0 ? strb.toString() : null;
  338. if (c == '\r')
  339. {
  340. int next_c = in.read();
  341. if (next_c != '\n' && next_c != -1)
  342. {
  343. if (!(in instanceof PushbackInputStream))
  344. in = new PushbackInputStream(in);
  345. ((PushbackInputStream) in).unread(next_c);
  346. }
  347. break;
  348. }
  349. if (c == '\n')
  350. break;
  351. strb.append((char) c);
  352. }
  353. return strb.length() > 0 ? strb.toString() : "";
  354. }
  355. /**
  356. * This method reads a Java <code>long</code> value from an input stream
  357. * It operates by reading eight bytes from the stream and converting them to
  358. * a single Java <code>long</code>. The bytes are stored most
  359. * significant byte first (i.e., "big endian") regardless of the native
  360. * host byte ordering.
  361. * <p>
  362. * As an example, if <code>byte1</code> through <code>byte8</code> represent
  363. * the first eight bytes read from the stream, they will be
  364. * transformed to an <code>long</code> in the following manner:
  365. * <p>
  366. * <code>(long)(((byte1 &amp; 0xFF) &lt;&lt; 56) + ((byte2 &amp; 0xFF) &lt;&lt; 48) +
  367. * ((byte3 &amp; 0xFF) &lt;&lt; 40) + ((byte4 &amp; 0xFF) &lt;&lt; 32) +
  368. * ((byte5 &amp; 0xFF) &lt;&lt; 24) + ((byte6 &amp; 0xFF) &lt;&lt; 16) +
  369. * ((byte7 &amp; 0xFF) &lt;&lt; 8) + (byte8 &amp; 0xFF)))
  370. * </code>
  371. * <p>
  372. * The value returned is in the range of -9223372036854775808 to
  373. * 9223372036854775807.
  374. * <p>
  375. * This method can read an <code>long</code> written by an object
  376. * implementing the <code>writeLong()</code> method in the
  377. * <code>DataOutput</code> interface.
  378. *
  379. * @return The <code>long</code> value read
  380. *
  381. * @exception EOFException If end of file is reached before reading the long
  382. * @exception IOException If any other error occurs
  383. *
  384. * @see DataOutput#writeLong
  385. */
  386. public final long readLong () throws IOException
  387. {
  388. readFully (buf, 0, 8);
  389. return convertToLong (buf);
  390. }
  391. /**
  392. * This method reads a signed 16-bit value into a Java in from the
  393. * stream. It operates by reading two bytes from the stream and
  394. * converting them to a single 16-bit Java <code>short</code>. The
  395. * two bytes are stored most significant byte first (i.e., "big
  396. * endian") regardless of the native host byte ordering.
  397. * <p>
  398. * As an example, if <code>byte1</code> and <code>byte2</code>
  399. * represent the first and second byte read from the stream
  400. * respectively, they will be transformed to a <code>short</code>. in
  401. * the following manner:
  402. * <p>
  403. * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF))</code>
  404. * <p>
  405. * The value returned is in the range of -32768 to 32767.
  406. * <p>
  407. * This method can read a <code>short</code> written by an object
  408. * implementing the <code>writeShort()</code> method in the
  409. * <code>DataOutput</code> interface.
  410. *
  411. * @return The <code>short</code> value read
  412. *
  413. * @exception EOFException If end of file is reached before reading the value
  414. * @exception IOException If any other error occurs
  415. *
  416. * @see DataOutput#writeShort
  417. */
  418. public final short readShort () throws IOException
  419. {
  420. readFully (buf, 0, 2);
  421. return convertToShort (buf);
  422. }
  423. /**
  424. * This method reads 8 unsigned bits into a Java <code>int</code>
  425. * value from the stream. The value returned is in the range of 0 to
  426. * 255.
  427. * <p>
  428. * This method can read an unsigned byte written by an object
  429. * implementing the <code>writeUnsignedByte()</code> method in the
  430. * <code>DataOutput</code> interface.
  431. *
  432. * @return The unsigned bytes value read as a Java <code>int</code>.
  433. *
  434. * @exception EOFException If end of file is reached before reading the value
  435. * @exception IOException If any other error occurs
  436. *
  437. * @see DataOutput#writeByte
  438. */
  439. public final int readUnsignedByte () throws IOException
  440. {
  441. return convertToUnsignedByte (in.read ());
  442. }
  443. /**
  444. * This method reads 16 unsigned bits into a Java int value from the stream.
  445. * It operates by reading two bytes from the stream and converting them to
  446. * a single Java <code>int</code> The two bytes are stored most
  447. * significant byte first (i.e., "big endian") regardless of the native
  448. * host byte ordering.
  449. * <p>
  450. * As an example, if <code>byte1</code> and <code>byte2</code>
  451. * represent the first and second byte read from the stream
  452. * respectively, they will be transformed to an <code>int</code> in
  453. * the following manner:
  454. * <p>
  455. * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
  456. * <p>
  457. * The value returned is in the range of 0 to 65535.
  458. * <p>
  459. * This method can read an unsigned short written by an object
  460. * implementing the <code>writeUnsignedShort()</code> method in the
  461. * <code>DataOutput</code> interface.
  462. *
  463. * @return The unsigned short value read as a Java <code>int</code>
  464. *
  465. * @exception EOFException If end of file is reached before reading the value
  466. * @exception IOException If any other error occurs
  467. *
  468. * @see DataOutput#writeShort
  469. */
  470. public final int readUnsignedShort () throws IOException
  471. {
  472. readFully (buf, 0, 2);
  473. return convertToUnsignedShort (buf);
  474. }
  475. /**
  476. * This method reads a <code>String</code> from an input stream that
  477. * is encoded in a modified UTF-8 format. This format has a leading
  478. * two byte sequence that contains the remaining number of bytes to
  479. * read. This two byte sequence is read using the
  480. * <code>readUnsignedShort()</code> method of this interface.
  481. * <p>
  482. * After the number of remaining bytes have been determined, these
  483. * bytes are read an transformed into <code>char</code> values.
  484. * These <code>char</code> values are encoded in the stream using
  485. * either a one, two, or three byte format. The particular format
  486. * in use can be determined by examining the first byte read.
  487. * <p>
  488. * If the first byte has a high order bit of 0, then that character
  489. * consists on only one byte. This character value consists of
  490. * seven bits that are at positions 0 through 6 of the byte. As an
  491. * example, if <code>byte1</code> is the byte read from the stream,
  492. * it would be converted to a <code>char</code> like so:
  493. * <p>
  494. * <code>(char)byte1</code>
  495. * <p>
  496. * If the first byte has 110 as its high order bits, then the
  497. * character consists of two bytes. The bits that make up the character
  498. * value are in positions 0 through 4 of the first byte and bit positions
  499. * 0 through 5 of the second byte. (The second byte should have
  500. * 10 as its high order bits). These values are in most significant
  501. * byte first (i.e., "big endian") order.
  502. * <p>
  503. * As an example, if <code>byte1</code> and <code>byte2</code> are
  504. * the first two bytes read respectively, and the high order bits of
  505. * them match the patterns which indicate a two byte character
  506. * encoding, then they would be converted to a Java
  507. * <code>char</code> like so:
  508. * <p>
  509. * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
  510. * <p>
  511. * If the first byte has a 1110 as its high order bits, then the
  512. * character consists of three bytes. The bits that make up the character
  513. * value are in positions 0 through 3 of the first byte and bit positions
  514. * 0 through 5 of the other two bytes. (The second and third bytes should
  515. * have 10 as their high order bits). These values are in most
  516. * significant byte first (i.e., "big endian") order.
  517. * <p>
  518. * As an example, if <code>byte1</code> <code>byte2</code> and
  519. * <code>byte3</code> are the three bytes read, and the high order
  520. * bits of them match the patterns which indicate a three byte
  521. * character encoding, then they would be converted to a Java
  522. * <code>char</code> like so:
  523. * <p>
  524. * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) |
  525. * (byte3 & 0x3F))</code>
  526. * <p>
  527. * Note that all characters are encoded in the method that requires
  528. * the fewest number of bytes with the exception of the character
  529. * with the value of <code>&#92;u0000</code> which is encoded as two
  530. * bytes. This is a modification of the UTF standard used to
  531. * prevent C language style <code>NUL</code> values from appearing
  532. * in the byte stream.
  533. * <p>
  534. * This method can read data that was written by an object implementing the
  535. * <code>writeUTF()</code> method in <code>DataOutput</code>
  536. *
  537. * @return The <code>String</code> read
  538. *
  539. * @exception EOFException If end of file is reached before reading
  540. * the String
  541. * @exception UTFDataFormatException If the data is not in UTF-8 format
  542. * @exception IOException If any other error occurs
  543. *
  544. * @see DataOutput#writeUTF
  545. */
  546. public final String readUTF () throws IOException
  547. {
  548. return readUTF (this);
  549. }
  550. /**
  551. * This method reads a String encoded in UTF-8 format from the
  552. * specified <code>DataInput</code> source.
  553. *
  554. * @param in The <code>DataInput</code> source to read from
  555. *
  556. * @return The String read from the source
  557. *
  558. * @exception IOException If an error occurs
  559. *
  560. * @see DataInput#readUTF
  561. */
  562. public static final String readUTF(DataInput in) throws IOException
  563. {
  564. final int UTFlen = in.readUnsignedShort ();
  565. return readUTF(in, UTFlen);
  566. }
  567. /**
  568. * This method is similar to <code>readUTF</code>, but the
  569. * UTF-8 byte length is in 64 bits.
  570. * This method is not public. It is used by <code>ObjectInputStream</code>.
  571. *
  572. * @return The <code>String</code> read
  573. *
  574. * @exception EOFException If end of file is reached before reading
  575. * the String
  576. * @exception UTFDataFormatException If the data is not in UTF-8 format
  577. * @exception IOException If any other error occurs
  578. *
  579. * @see DataOutput#writeUTFLong
  580. */
  581. final String readUTFLong () throws IOException
  582. {
  583. long l = readLong ();
  584. if (l > Integer.MAX_VALUE)
  585. throw new IOException("The string length > Integer.MAX_VALUE");
  586. final int UTFlen = (int)l;
  587. return readUTF (this, UTFlen);
  588. }
  589. /**
  590. * This method performs the main task of <code>readUTF</code> and
  591. * <code>readUTFLong</code>.
  592. *
  593. * @param in The <code>DataInput</code> source to read from
  594. *
  595. * @param len The UTF-8 byte length of the String to be read
  596. *
  597. * @return The String read from the source
  598. *
  599. * @exception IOException If an error occurs
  600. *
  601. * @see DataInput#readUTF
  602. */
  603. private static final String readUTF(DataInput in, int len) throws IOException
  604. {
  605. byte[] buf = new byte [len];
  606. // This blocks until the entire string is available rather than
  607. // doing partial processing on the bytes that are available and then
  608. // blocking. An advantage of the latter is that Exceptions
  609. // could be thrown earlier. The former is a bit cleaner.
  610. in.readFully (buf, 0, len);
  611. return convertFromUTF (buf);
  612. }
  613. /**
  614. * This method attempts to skip and discard the specified number of bytes
  615. * in the input stream. It may actually skip fewer bytes than requested.
  616. * This method will not skip any bytes if passed a negative number of bytes
  617. * to skip.
  618. *
  619. * @param n The requested number of bytes to skip.
  620. *
  621. * @return The requested number of bytes to skip.
  622. *
  623. * @exception IOException If an error occurs.
  624. * @specnote The JDK docs claim that this returns the number of bytes
  625. * actually skipped. The JCL claims that this method can throw an
  626. * EOFException. Neither of these appear to be true in the JDK 1.3's
  627. * implementation. This tries to implement the actual JDK behaviour.
  628. */
  629. public final int skipBytes (int n) throws IOException
  630. {
  631. if (n <= 0)
  632. return 0;
  633. try
  634. {
  635. return (int) in.skip (n);
  636. }
  637. catch (EOFException x)
  638. {
  639. // do nothing.
  640. }
  641. return n;
  642. }
  643. static boolean convertToBoolean (int b) throws EOFException
  644. {
  645. if (b < 0)
  646. throw new EOFException ();
  647. return (b != 0);
  648. }
  649. static byte convertToByte (int i) throws EOFException
  650. {
  651. if (i < 0)
  652. throw new EOFException ();
  653. return (byte) i;
  654. }
  655. static int convertToUnsignedByte (int i) throws EOFException
  656. {
  657. if (i < 0)
  658. throw new EOFException ();
  659. return (i & 0xFF);
  660. }
  661. static char convertToChar (byte[] buf)
  662. {
  663. return (char) ((buf [0] << 8)
  664. | (buf [1] & 0xff));
  665. }
  666. static short convertToShort (byte[] buf)
  667. {
  668. return (short) ((buf [0] << 8)
  669. | (buf [1] & 0xff));
  670. }
  671. static int convertToUnsignedShort (byte[] buf)
  672. {
  673. return (((buf [0] & 0xff) << 8)
  674. | (buf [1] & 0xff));
  675. }
  676. static int convertToInt (byte[] buf)
  677. {
  678. return (((buf [0] & 0xff) << 24)
  679. | ((buf [1] & 0xff) << 16)
  680. | ((buf [2] & 0xff) << 8)
  681. | (buf [3] & 0xff));
  682. }
  683. static long convertToLong (byte[] buf)
  684. {
  685. return (((long)(buf [0] & 0xff) << 56) |
  686. ((long)(buf [1] & 0xff) << 48) |
  687. ((long)(buf [2] & 0xff) << 40) |
  688. ((long)(buf [3] & 0xff) << 32) |
  689. ((long)(buf [4] & 0xff) << 24) |
  690. ((long)(buf [5] & 0xff) << 16) |
  691. ((long)(buf [6] & 0xff) << 8) |
  692. ((long)(buf [7] & 0xff)));
  693. }
  694. // FIXME: This method should be re-thought. I suspect we have multiple
  695. // UTF-8 decoders floating around. We should use the standard charset
  696. // converters, maybe and adding a direct call into one of the new
  697. // NIO converters for a super-fast UTF8 decode.
  698. static String convertFromUTF (byte[] buf)
  699. throws EOFException, UTFDataFormatException
  700. {
  701. // Give StringBuffer an initial estimated size to avoid
  702. // enlarge buffer frequently
  703. CPStringBuilder strbuf = new CPStringBuilder (buf.length / 2 + 2);
  704. for (int i = 0; i < buf.length; )
  705. {
  706. if ((buf [i] & 0x80) == 0) // bit pattern 0xxxxxxx
  707. strbuf.append ((char) (buf [i++] & 0xFF));
  708. else if ((buf [i] & 0xE0) == 0xC0) // bit pattern 110xxxxx
  709. {
  710. if (i + 1 >= buf.length
  711. || (buf [i + 1] & 0xC0) != 0x80)
  712. throw new UTFDataFormatException ();
  713. strbuf.append((char) (((buf [i++] & 0x1F) << 6)
  714. | (buf [i++] & 0x3F)));
  715. }
  716. else if ((buf [i] & 0xF0) == 0xE0) // bit pattern 1110xxxx
  717. {
  718. if (i + 2 >= buf.length
  719. || (buf [i + 1] & 0xC0) != 0x80
  720. || (buf [i + 2] & 0xC0) != 0x80)
  721. throw new UTFDataFormatException ();
  722. strbuf.append ((char) (((buf [i++] & 0x0F) << 12)
  723. | ((buf [i++] & 0x3F) << 6)
  724. | (buf [i++] & 0x3F)));
  725. }
  726. else // must be ((buf [i] & 0xF0) == 0xF0 || (buf [i] & 0xC0) == 0x80)
  727. throw new UTFDataFormatException (); // bit patterns 1111xxxx or
  728. // 10xxxxxx
  729. }
  730. return strbuf.toString ();
  731. }
  732. }