1. /*
  2. * @(#)RandomAccessFile.java 1.56 00/02/02
  3. *
  4. * Copyright 1994-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. * Instances of this class support both reading and writing to a
  13. * random access file. A random access file behaves like a large
  14. * array of bytes stored in the file system. There is a kind of cursor,
  15. * or index into the implied array, called the <em>file pointer</em>
  16. * input operations read bytes starting at the file pointer and advance
  17. * the file pointer past the bytes read. If the random access file is
  18. * created in read/write mode, then output operations are also available;
  19. * output operations write bytes starting at the file pointer and advance
  20. * the file pointer past the bytes written. Output operations that write
  21. * past the current end of the implied array cause the array to be
  22. * extended. The file pointer can be read by the
  23. * <code>getFilePointer</code> method and set by the <code>seek</code>
  24. * method.
  25. * <p>
  26. * It is generally true of all the reading routines in this class that
  27. * if end-of-file is reached before the desired number of bytes has been
  28. * read, an <code>EOFException</code> (which is a kind of
  29. * <code>IOException</code>) is thrown. If any byte cannot be read for
  30. * any reason other than end-of-file, an <code>IOException</code> other
  31. * than <code>EOFException</code> is thrown. In particular, an
  32. * <code>IOException</code> may be thrown if the stream has been closed.
  33. *
  34. * @author unascribed
  35. * @version 1.56, 02/02/00
  36. * @since JDK1.0
  37. */
  38. public class RandomAccessFile implements DataOutput, DataInput {
  39. private FileDescriptor fd;
  40. /**
  41. * Creates a random access file stream to read from, and optionally
  42. * to write to, a file with the specified name. A new
  43. * {@link FileDescriptor} object is created to represent the
  44. * connection to the file.
  45. * <p>
  46. * The mode argument must either be equal to <code>"r"</code> or
  47. * <code>"rw"</code>, indicating that the file is to be opened for
  48. * input only or for both input and output, respectively. The
  49. * write methods on this object will always throw an
  50. * <code>IOException</code> if the file is opened with a mode of
  51. * <code>"r"</code>. If the mode is <code>"rw"</code> and the
  52. * file does not exist, then an attempt is made to create it.
  53. * An <code>IOException</code> is thrown if the name argument
  54. * refers to a directory.
  55. * <p>
  56. * If there is a security manager, its <code>checkRead</code> method
  57. * is called with the <code>name</code> argument
  58. * as its argument to see if read access to the file is allowed.
  59. * If the mode is "rw", the security manager's
  60. * <code>checkWrite</code> method
  61. * is also called with the <code>name</code> argument
  62. * as its argument to see if write access to the file is allowed.
  63. *
  64. * @param name the system-dependent filename.
  65. * @param mode the access mode.
  66. * @exception IllegalArgumentException if the mode argument is not equal
  67. * to <code>"r"</code> or to <code>"rw"</code>.
  68. * @exception FileNotFoundException if the file exists but is a directory
  69. * rather than a regular file, or cannot be opened or
  70. * created for any other reason
  71. * @exception SecurityException if a security manager exists and its
  72. * <code>checkRead</code> method denies read access to the file
  73. * or the mode is "rw" and the security manager's
  74. * <code>checkWrite</code> method denies write access to the file.
  75. * @see java.lang.SecurityException
  76. * @see java.lang.SecurityManager#checkRead(java.lang.String)
  77. * @see java.lang.SecurityManager#checkWrite(java.lang.String)
  78. */
  79. public RandomAccessFile(String name, String mode)
  80. throws FileNotFoundException
  81. {
  82. boolean rw = mode.equals("rw");
  83. if (!rw && !mode.equals("r"))
  84. throw new IllegalArgumentException("mode must be r or rw");
  85. SecurityManager security = System.getSecurityManager();
  86. if (security != null) {
  87. security.checkRead(name);
  88. if (rw) {
  89. security.checkWrite(name);
  90. }
  91. }
  92. fd = new FileDescriptor();
  93. open(name, rw);
  94. }
  95. /**
  96. * Creates a random access file stream to read from, and optionally
  97. * to write to, the file specified by the <code>File</code> argument.
  98. * A new {@link FileDescriptor} object is created to represent
  99. * this file connection.
  100. * <p>
  101. * The mode argument must either be equal to <code>"r"</code> or
  102. * <code>"rw"</code>, indicating that the file is to be opened for
  103. * input only or for both input and output, respectively. The
  104. * write methods on this object will always throw an
  105. * <code>IOException</code> if the file is opened with a mode of
  106. * <code>"r"</code>. If the mode is <code>"rw"</code> and the
  107. * file does not exist, then an attempt is made to create it.
  108. * An <code>IOException</code> is thrown if the file argument
  109. * refers to a directory.
  110. * <p>
  111. * If there is a security manager, its <code>checkRead</code> method
  112. * is called with the pathname of the <code>file</code>
  113. * argument as its argument to see if read access to the file is allowed.
  114. * If the mode is "rw", the security manager's
  115. * <code>checkWrite</code> method
  116. * is also called with the path argument
  117. * to see if write access to the file is allowed.
  118. *
  119. * @param file the file object.
  120. * @param mode the access mode.
  121. * @exception IllegalArgumentException if the mode argument is not equal
  122. * to <code>"r"</code> or to <code>"rw"</code>.
  123. * @exception FileNotFoundException if the file exists but is a directory
  124. * rather than a regular file, or cannot be opened or
  125. * created for any other reason
  126. * @exception SecurityException if a security manager exists and its
  127. * <code>checkRead</code> method denies read access to the file
  128. * or the mode is "rw" and the security manager's
  129. * <code>checkWrite</code> method denies write access to the file.
  130. * @see java.io.File#getPath()
  131. * @see java.lang.SecurityManager#checkRead(java.lang.String)
  132. * @see java.lang.SecurityManager#checkWrite(java.lang.String)
  133. */
  134. public RandomAccessFile(File file, String mode)
  135. throws FileNotFoundException
  136. {
  137. this(file.getPath(), mode);
  138. }
  139. /**
  140. * Returns the opaque file descriptor object associated with this stream.
  141. *
  142. * @return the file descriptor object associated with this stream.
  143. * @exception IOException if an I/O error occurs.
  144. * @see java.io.FileDescriptor
  145. */
  146. public final FileDescriptor getFD() throws IOException {
  147. if (fd != null) return fd;
  148. throw new IOException();
  149. }
  150. /**
  151. * Opens a file and returns the file descriptor. The file is
  152. * opened in read-write mode if writeable is true, else
  153. * the file is opened as read-only.
  154. * If the <code>name</code> refers to a directory, an IOException
  155. * is thrown.
  156. *
  157. * @param name the name of the file
  158. * @param writeable the boolean indicating whether file is
  159. * writeable or not.
  160. */
  161. private native void open(String name, boolean writeable)
  162. throws FileNotFoundException;
  163. // 'Read' primitives
  164. /**
  165. * Reads a byte of data from this file. The byte is returned as an
  166. * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This
  167. * method blocks if no input is yet available.
  168. * <p>
  169. * Although <code>RandomAccessFile</code> is not a subclass of
  170. * <code>InputStream</code>, this method behaves in exactly the same
  171. * way as the {@link InputStream#read()} method of
  172. * <code>InputStream</code>.
  173. *
  174. * @return the next byte of data, or <code>-1</code> if the end of the
  175. * file has been reached.
  176. * @exception IOException if an I/O error occurs. Not thrown if
  177. * end-of-file has been reached.
  178. */
  179. public native int read() throws IOException;
  180. /**
  181. * Reads a sub array as a sequence of bytes.
  182. * @param b the data to be written
  183. * @param off the start offset in the data
  184. * @param len the number of bytes that are written
  185. * @exception IOException If an I/O error has occurred.
  186. */
  187. private native int readBytes(byte b[], int off, int len) throws IOException;
  188. /**
  189. * Reads up to <code>len</code> bytes of data from this file into an
  190. * array of bytes. This method blocks until at least one byte of input
  191. * is available.
  192. * <p>
  193. * Although <code>RandomAccessFile</code> is not a subclass of
  194. * <code>InputStream</code>, this method behaves in the exactly the
  195. * same way as the {@link InputStream#read(byte[], int, int)} method of
  196. * <code>InputStream</code>.
  197. *
  198. * @param b the buffer into which the data is read.
  199. * @param off the start offset of the data.
  200. * @param len the maximum number of bytes read.
  201. * @return the total number of bytes read into the buffer, or
  202. * <code>-1</code> if there is no more data because the end of
  203. * the file has been reached.
  204. * @exception IOException if an I/O error occurs.
  205. */
  206. public int read(byte b[], int off, int len) throws IOException {
  207. return readBytes(b, off, len);
  208. }
  209. /**
  210. * Reads up to <code>b.length</code> bytes of data from this file
  211. * into an array of bytes. This method blocks until at least one byte
  212. * of input is available.
  213. * <p>
  214. * Although <code>RandomAccessFile</code> is not a subclass of
  215. * <code>InputStream</code>, this method behaves in the exactly the
  216. * same way as the {@link InputStream#read(byte[])} method of
  217. * <code>InputStream</code>.
  218. *
  219. * @param b the buffer into which the data is read.
  220. * @return the total number of bytes read into the buffer, or
  221. * <code>-1</code> if there is no more data because the end of
  222. * this file has been reached.
  223. * @exception IOException if an I/O error occurs.
  224. */
  225. public int read(byte b[]) throws IOException {
  226. return readBytes(b, 0, b.length);
  227. }
  228. /**
  229. * Reads <code>b.length</code> bytes from this file into the byte
  230. * array, starting at the current file pointer. This method reads
  231. * repeatedly from the file until the requested number of bytes are
  232. * read. This method blocks until the requested number of bytes are
  233. * read, the end of the stream is detected, or an exception is thrown.
  234. *
  235. * @param b the buffer into which the data is read.
  236. * @exception EOFException if this file reaches the end before reading
  237. * all the bytes.
  238. * @exception IOException if an I/O error occurs.
  239. */
  240. public final void readFully(byte b[]) throws IOException {
  241. readFully(b, 0, b.length);
  242. }
  243. /**
  244. * Reads exactly <code>len</code> bytes from this file into the byte
  245. * array, starting at the current file pointer. This method reads
  246. * repeatedly from the file until the requested number of bytes are
  247. * read. This method blocks until the requested number of bytes are
  248. * read, the end of the stream is detected, or an exception is thrown.
  249. *
  250. * @param b the buffer into which the data is read.
  251. * @param off the start offset of the data.
  252. * @param len the number of bytes to read.
  253. * @exception EOFException if this file reaches the end before reading
  254. * all the bytes.
  255. * @exception IOException if an I/O error occurs.
  256. */
  257. public final void readFully(byte b[], int off, int len) throws IOException {
  258. int n = 0;
  259. do {
  260. int count = this.read(b, off + n, len - n);
  261. if (count < 0)
  262. throw new EOFException();
  263. n += count;
  264. } while (n < len);
  265. }
  266. /**
  267. * Attempts to skip over <code>n</code> bytes of input discarding the
  268. * skipped bytes.
  269. * <p>
  270. *
  271. * This method may skip over some smaller number of bytes, possibly zero.
  272. * This may result from any of a number of conditions; reaching end of
  273. * file before <code>n</code> bytes have been skipped is only one
  274. * possibility. This method never throws an <code>EOFException</code>.
  275. * The actual number of bytes skipped is returned. If <code>n</code>
  276. * is negative, no bytes are skipped.
  277. *
  278. * @param n the number of bytes to be skipped.
  279. * @return the actual number of bytes skipped.
  280. * @exception IOException if an I/O error occurs.
  281. */
  282. public int skipBytes(int n) throws IOException {
  283. long pos;
  284. long len;
  285. long newpos;
  286. if (n <= 0) {
  287. return 0;
  288. }
  289. pos = getFilePointer();
  290. len = length();
  291. newpos = pos + n;
  292. if (newpos > len) {
  293. newpos = len;
  294. }
  295. seek(newpos);
  296. /* return the actual number of bytes skipped */
  297. return (int) (newpos - pos);
  298. }
  299. // 'Write' primitives
  300. /**
  301. * Writes the specified byte to this file. The write starts at
  302. * the current file pointer.
  303. *
  304. * @param b the <code>byte</code> to be written.
  305. * @exception IOException if an I/O error occurs.
  306. */
  307. public native void write(int b) throws IOException;
  308. /**
  309. * Writes a sub array as a sequence of bytes.
  310. * @param b the data to be written
  311. * @param off the start offset in the data
  312. * @param len the number of bytes that are written
  313. * @exception IOException If an I/O error has occurred.
  314. */
  315. private native void writeBytes(byte b[], int off, int len) throws IOException;
  316. /**
  317. * Writes <code>b.length</code> bytes from the specified byte array
  318. * to this file, starting at the current file pointer.
  319. *
  320. * @param b the data.
  321. * @exception IOException if an I/O error occurs.
  322. */
  323. public void write(byte b[]) throws IOException {
  324. writeBytes(b, 0, b.length);
  325. }
  326. /**
  327. * Writes <code>len</code> bytes from the specified byte array
  328. * starting at offset <code>off</code> to this file.
  329. *
  330. * @param b the data.
  331. * @param off the start offset in the data.
  332. * @param len the number of bytes to write.
  333. * @exception IOException if an I/O error occurs.
  334. */
  335. public void write(byte b[], int off, int len) throws IOException {
  336. writeBytes(b, off, len);
  337. }
  338. // 'Random access' stuff
  339. /**
  340. * Returns the current offset in this file.
  341. *
  342. * @return the offset from the beginning of the file, in bytes,
  343. * at which the next read or write occurs.
  344. * @exception IOException if an I/O error occurs.
  345. */
  346. public native long getFilePointer() throws IOException;
  347. /**
  348. * Sets the file-pointer offset, measured from the beginning of this
  349. * file, at which the next read or write occurs. The offset may be
  350. * set beyond the end of the file. Setting the offset beyond the end
  351. * of the file does not change the file length. The file length will
  352. * change only by writing after the offset has been set beyond the end
  353. * of the file.
  354. *
  355. * @param pos the offset position, measured in bytes from the
  356. * beginning of the file, at which to set the file
  357. * pointer.
  358. * @exception IOException if <code>pos</code> is less than
  359. * <code>0</code> or if an I/O error occurs.
  360. */
  361. public native void seek(long pos) throws IOException;
  362. /**
  363. * Returns the length of this file.
  364. *
  365. * @return the length of this file, measured in bytes.
  366. * @exception IOException if an I/O error occurs.
  367. */
  368. public native long length() throws IOException;
  369. /**
  370. * Sets the length of this file.
  371. *
  372. * <p> If the present length of the file as returned by the
  373. * <code>length</code> method is greater than the <code>newLength</code>
  374. * argument then the file will be truncated. In this case, if the file
  375. * offset as returned by the <code>getFilePointer</code> method is greater
  376. * then <code>newLength</code> then after this method returns the offset
  377. * will be equal to <code>newLength</code>.
  378. *
  379. * <p> If the present length of the file as returned by the
  380. * <code>length</code> method is smaller than the <code>newLength</code>
  381. * argument then the file will be extended. In this case, the contents of
  382. * the extended portion of the file are not defined.
  383. *
  384. * @param newLength The desired length of the file
  385. * @exception IOException If an I/O error occurs
  386. * @since 1.2
  387. */
  388. public native void setLength(long newLength) throws IOException;
  389. /**
  390. * Closes this random access file stream and releases any system
  391. * resources associated with the stream. A closed random access
  392. * file cannot perform input or output operations and cannot be
  393. * reopened.
  394. *
  395. * @exception IOException if an I/O error occurs.
  396. */
  397. public native void close() throws IOException;
  398. //
  399. // Some "reading/writing Java data types" methods stolen from
  400. // DataInputStream and DataOutputStream.
  401. //
  402. /**
  403. * Reads a <code>boolean</code> from this file. This method reads a
  404. * single byte from the file, starting at the current file pointer.
  405. * A value of <code>0</code> represents
  406. * <code>false</code>. Any other value represents <code>true</code>.
  407. * This method blocks until the byte is read, the end of the stream
  408. * is detected, or an exception is thrown.
  409. *
  410. * @return the <code>boolean</code> value read.
  411. * @exception EOFException if this file has reached the end.
  412. * @exception IOException if an I/O error occurs.
  413. */
  414. public final boolean readBoolean() throws IOException {
  415. int ch = this.read();
  416. if (ch < 0)
  417. throw new EOFException();
  418. return (ch != 0);
  419. }
  420. /**
  421. * Reads a signed eight-bit value from this file. This method reads a
  422. * byte from the file, starting from the current file pointer.
  423. * If the byte read is <code>b</code>, where
  424. * <code>0 <= b <= 255</code>,
  425. * then the result is:
  426. * <blockquote><pre>
  427. * (byte)(b)
  428. * </pre></blockquote>
  429. * <p>
  430. * This method blocks until the byte is read, the end of the stream
  431. * is detected, or an exception is thrown.
  432. *
  433. * @return the next byte of this file as a signed eight-bit
  434. * <code>byte</code>.
  435. * @exception EOFException if this file has reached the end.
  436. * @exception IOException if an I/O error occurs.
  437. */
  438. public final byte readByte() throws IOException {
  439. int ch = this.read();
  440. if (ch < 0)
  441. throw new EOFException();
  442. return (byte)(ch);
  443. }
  444. /**
  445. * Reads an unsigned eight-bit number from this file. This method reads
  446. * a byte from this file, starting at the current file pointer,
  447. * and returns that byte.
  448. * <p>
  449. * This method blocks until the byte is read, the end of the stream
  450. * is detected, or an exception is thrown.
  451. *
  452. * @return the next byte of this file, interpreted as an unsigned
  453. * eight-bit number.
  454. * @exception EOFException if this file has reached the end.
  455. * @exception IOException if an I/O error occurs.
  456. */
  457. public final int readUnsignedByte() throws IOException {
  458. int ch = this.read();
  459. if (ch < 0)
  460. throw new EOFException();
  461. return ch;
  462. }
  463. /**
  464. * Reads a signed 16-bit number from this file. The method reads two
  465. * bytes from this file, starting at the current file pointer.
  466. * If the two bytes read, in order, are
  467. * <code>b1</code> and <code>b2</code>, where each of the two values is
  468. * between <code>0</code> and <code>255</code>, inclusive, then the
  469. * result is equal to:
  470. * <blockquote><pre>
  471. * (short)((b1 << 8) | b2)
  472. * </pre></blockquote>
  473. * <p>
  474. * This method blocks until the two bytes are read, the end of the
  475. * stream is detected, or an exception is thrown.
  476. *
  477. * @return the next two bytes of this file, interpreted as a signed
  478. * 16-bit number.
  479. * @exception EOFException if this file reaches the end before reading
  480. * two bytes.
  481. * @exception IOException if an I/O error occurs.
  482. */
  483. public final short readShort() throws IOException {
  484. int ch1 = this.read();
  485. int ch2 = this.read();
  486. if ((ch1 | ch2) < 0)
  487. throw new EOFException();
  488. return (short)((ch1 << 8) + (ch2 << 0));
  489. }
  490. /**
  491. * Reads an unsigned 16-bit number from this file. This method reads
  492. * two bytes from the file, starting at the current file pointer.
  493. * If the bytes read, in order, are
  494. * <code>b1</code> and <code>b2</code>, where
  495. * <code>0 <= b1, b2 <= 255</code>,
  496. * then the result is equal to:
  497. * <blockquote><pre>
  498. * (b1 << 8) | b2
  499. * </pre></blockquote>
  500. * <p>
  501. * This method blocks until the two bytes are read, the end of the
  502. * stream is detected, or an exception is thrown.
  503. *
  504. * @return the next two bytes of this file, interpreted as an unsigned
  505. * 16-bit integer.
  506. * @exception EOFException if this file reaches the end before reading
  507. * two bytes.
  508. * @exception IOException if an I/O error occurs.
  509. */
  510. public final int readUnsignedShort() throws IOException {
  511. int ch1 = this.read();
  512. int ch2 = this.read();
  513. if ((ch1 | ch2) < 0)
  514. throw new EOFException();
  515. return (ch1 << 8) + (ch2 << 0);
  516. }
  517. /**
  518. * Reads a Unicode character from this file. This method reads two
  519. * bytes from the file, starting at the current file pointer.
  520. * If the bytes read, in order, are
  521. * <code>b1</code> and <code>b2</code>, where
  522. * <code>0 <= b1, b2 <= 255</code>,
  523. * then the result is equal to:
  524. * <blockquote><pre>
  525. * (char)((b1 << 8) | b2)
  526. * </pre></blockquote>
  527. * <p>
  528. * This method blocks until the two bytes are read, the end of the
  529. * stream is detected, or an exception is thrown.
  530. *
  531. * @return the next two bytes of this file as a Unicode character.
  532. * @exception EOFException if this file reaches the end before reading
  533. * two bytes.
  534. * @exception IOException if an I/O error occurs.
  535. */
  536. public final char readChar() throws IOException {
  537. int ch1 = this.read();
  538. int ch2 = this.read();
  539. if ((ch1 | ch2) < 0)
  540. throw new EOFException();
  541. return (char)((ch1 << 8) + (ch2 << 0));
  542. }
  543. /**
  544. * Reads a signed 32-bit integer from this file. This method reads 4
  545. * bytes from the file, starting at the current file pointer.
  546. * If the bytes read, in order, are <code>b1</code>,
  547. * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
  548. * <code>0 <= b1, b2, b3, b4 <= 255</code>,
  549. * then the result is equal to:
  550. * <blockquote><pre>
  551. * (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
  552. * </pre></blockquote>
  553. * <p>
  554. * This method blocks until the four bytes are read, the end of the
  555. * stream is detected, or an exception is thrown.
  556. *
  557. * @return the next four bytes of this file, interpreted as an
  558. * <code>int</code>.
  559. * @exception EOFException if this file reaches the end before reading
  560. * four bytes.
  561. * @exception IOException if an I/O error occurs.
  562. */
  563. public final int readInt() throws IOException {
  564. int ch1 = this.read();
  565. int ch2 = this.read();
  566. int ch3 = this.read();
  567. int ch4 = this.read();
  568. if ((ch1 | ch2 | ch3 | ch4) < 0)
  569. throw new EOFException();
  570. return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  571. }
  572. /**
  573. * Reads a signed 64-bit integer from this file. This method reads eight
  574. * bytes from the file, starting at the current file pointer.
  575. * If the bytes read, in order, are
  576. * <code>b1</code>, <code>b2</code>, <code>b3</code>,
  577. * <code>b4</code>, <code>b5</code>, <code>b6</code>,
  578. * <code>b7</code>, and <code>b8,</code> where:
  579. * <blockquote><pre>
  580. * 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
  581. * </pre></blockquote>
  582. * <p>
  583. * then the result is equal to:
  584. * <p><blockquote><pre>
  585. * ((long)b1 << 56) + ((long)b2 << 48)
  586. * + ((long)b3 << 40) + ((long)b4 << 32)
  587. * + ((long)b5 << 24) + ((long)b6 << 16)
  588. * + ((long)b7 << 8) + b8
  589. * </pre></blockquote>
  590. * <p>
  591. * This method blocks until the eight bytes are read, the end of the
  592. * stream is detected, or an exception is thrown.
  593. *
  594. * @return the next eight bytes of this file, interpreted as a
  595. * <code>long</code>.
  596. * @exception EOFException if this file reaches the end before reading
  597. * eight bytes.
  598. * @exception IOException if an I/O error occurs.
  599. */
  600. public final long readLong() throws IOException {
  601. return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  602. }
  603. /**
  604. * Reads a <code>float</code> from this file. This method reads an
  605. * <code>int</code> value, starting at the current file pointer,
  606. * as if by the <code>readInt</code> method
  607. * and then converts that <code>int</code> to a <code>float</code>
  608. * using the <code>intBitsToFloat</code> method in class
  609. * <code>Float</code>.
  610. * <p>
  611. * This method blocks until the four bytes are read, the end of the
  612. * stream is detected, or an exception is thrown.
  613. *
  614. * @return the next four bytes of this file, interpreted as a
  615. * <code>float</code>.
  616. * @exception EOFException if this file reaches the end before reading
  617. * four bytes.
  618. * @exception IOException if an I/O error occurs.
  619. * @see java.io.RandomAccessFile#readInt()
  620. * @see java.lang.Float#intBitsToFloat(int)
  621. */
  622. public final float readFloat() throws IOException {
  623. return Float.intBitsToFloat(readInt());
  624. }
  625. /**
  626. * Reads a <code>double</code> from this file. This method reads a
  627. * <code>long</code> value, starting at the current file pointer,
  628. * as if by the <code>readLong</code> method
  629. * and then converts that <code>long</code> to a <code>double</code>
  630. * using the <code>longBitsToDouble</code> method in
  631. * class <code>Double</code>.
  632. * <p>
  633. * This method blocks until the eight bytes are read, the end of the
  634. * stream is detected, or an exception is thrown.
  635. *
  636. * @return the next eight bytes of this file, interpreted as a
  637. * <code>double</code>.
  638. * @exception EOFException if this file reaches the end before reading
  639. * eight bytes.
  640. * @exception IOException if an I/O error occurs.
  641. * @see java.io.RandomAccessFile#readLong()
  642. * @see java.lang.Double#longBitsToDouble(long)
  643. */
  644. public final double readDouble() throws IOException {
  645. return Double.longBitsToDouble(readLong());
  646. }
  647. /**
  648. * Reads the next line of text from this file. This method successively
  649. * reads bytes from the file, starting at the current file pointer,
  650. * until it reaches a line terminator or the end
  651. * of the file. Each byte is converted into a character by taking the
  652. * byte's value for the lower eight bits of the character and setting the
  653. * high eight bits of the character to zero. This method does not,
  654. * therefore, support the full Unicode character set.
  655. *
  656. * <p> A line of text is terminated by a carriage-return character
  657. * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a
  658. * carriage-return character immediately followed by a newline character,
  659. * or the end of the file. Line-terminating characters are discarded and
  660. * are not included as part of the string returned.
  661. *
  662. * <p> This method blocks until a newline character is read, a carriage
  663. * return and the byte following it are read (to see if it is a newline),
  664. * the end of the file is reached, or an exception is thrown.
  665. *
  666. * @return the next line of text from this file, or null if end
  667. * of file is encountered before even one byte is read.
  668. * @exception IOException if an I/O error occurs.
  669. */
  670. public final String readLine() throws IOException {
  671. StringBuffer input = new StringBuffer();
  672. int c = -1;
  673. boolean eol = false;
  674. while (!eol) {
  675. switch (c = read()) {
  676. case -1:
  677. case '\n':
  678. eol = true;
  679. break;
  680. case '\r':
  681. eol = true;
  682. long cur = getFilePointer();
  683. if ((read()) != '\n') {
  684. seek(cur);
  685. }
  686. break;
  687. default:
  688. input.append((char)c);
  689. break;
  690. }
  691. }
  692. if ((c == -1) && (input.length() == 0)) {
  693. return null;
  694. }
  695. return input.toString();
  696. }
  697. /**
  698. * Reads in a string from this file. The string has been encoded
  699. * using a modified UTF-8 format.
  700. * <p>
  701. * The first two bytes are read, starting from the current file
  702. * pointer, as if by
  703. * <code>readUnsignedShort</code>. This value gives the number of
  704. * following bytes that are in the encoded string, not
  705. * the length of the resulting string. The following bytes are then
  706. * interpreted as bytes encoding characters in the UTF-8 format
  707. * and are converted into characters.
  708. * <p>
  709. * This method blocks until all the bytes are read, the end of the
  710. * stream is detected, or an exception is thrown.
  711. *
  712. * @return a Unicode string.
  713. * @exception EOFException if this file reaches the end before
  714. * reading all the bytes.
  715. * @exception IOException if an I/O error occurs.
  716. * @exception UTFDataFormatException if the bytes do not represent
  717. * valid UTF-8 encoding of a Unicode string.
  718. * @see java.io.RandomAccessFile#readUnsignedShort()
  719. */
  720. public final String readUTF() throws IOException {
  721. return DataInputStream.readUTF(this);
  722. }
  723. /**
  724. * Writes a <code>boolean</code> to the file as a one-byte value. The
  725. * value <code>true</code> is written out as the value
  726. * <code>(byte)1</code> the value <code>false</code> is written out
  727. * as the value <code>(byte)0</code>. The write starts at
  728. * the current position of the file pointer.
  729. *
  730. * @param v a <code>boolean</code> value to be written.
  731. * @exception IOException if an I/O error occurs.
  732. */
  733. public final void writeBoolean(boolean v) throws IOException {
  734. write(v ? 1 : 0);
  735. //written++;
  736. }
  737. /**
  738. * Writes a <code>byte</code> to the file as a one-byte value. The
  739. * write starts at the current position of the file pointer.
  740. *
  741. * @param v a <code>byte</code> value to be written.
  742. * @exception IOException if an I/O error occurs.
  743. */
  744. public final void writeByte(int v) throws IOException {
  745. write(v);
  746. //written++;
  747. }
  748. /**
  749. * Writes a <code>short</code> to the file as two bytes, high byte first.
  750. * The write starts at the current position of the file pointer.
  751. *
  752. * @param v a <code>short</code> to be written.
  753. * @exception IOException if an I/O error occurs.
  754. */
  755. public final void writeShort(int v) throws IOException {
  756. write((v >>> 8) & 0xFF);
  757. write((v >>> 0) & 0xFF);
  758. //written += 2;
  759. }
  760. /**
  761. * Writes a <code>char</code> to the file as a two-byte value, high
  762. * byte first. The write starts at the current position of the
  763. * file pointer.
  764. *
  765. * @param v a <code>char</code> value to be written.
  766. * @exception IOException if an I/O error occurs.
  767. */
  768. public final void writeChar(int v) throws IOException {
  769. write((v >>> 8) & 0xFF);
  770. write((v >>> 0) & 0xFF);
  771. //written += 2;
  772. }
  773. /**
  774. * Writes an <code>int</code> to the file as four bytes, high byte first.
  775. * The write starts at the current position of the file pointer.
  776. *
  777. * @param v an <code>int</code> to be written.
  778. * @exception IOException if an I/O error occurs.
  779. */
  780. public final void writeInt(int v) throws IOException {
  781. write((v >>> 24) & 0xFF);
  782. write((v >>> 16) & 0xFF);
  783. write((v >>> 8) & 0xFF);
  784. write((v >>> 0) & 0xFF);
  785. //written += 4;
  786. }
  787. /**
  788. * Writes a <code>long</code> to the file as eight bytes, high byte first.
  789. * The write starts at the current position of the file pointer.
  790. *
  791. * @param v a <code>long</code> to be written.
  792. * @exception IOException if an I/O error occurs.
  793. */
  794. public final void writeLong(long v) throws IOException {
  795. write((int)(v >>> 56) & 0xFF);
  796. write((int)(v >>> 48) & 0xFF);
  797. write((int)(v >>> 40) & 0xFF);
  798. write((int)(v >>> 32) & 0xFF);
  799. write((int)(v >>> 24) & 0xFF);
  800. write((int)(v >>> 16) & 0xFF);
  801. write((int)(v >>> 8) & 0xFF);
  802. write((int)(v >>> 0) & 0xFF);
  803. //written += 8;
  804. }
  805. /**
  806. * Converts the float argument to an <code>int</code> using the
  807. * <code>floatToIntBits</code> method in class <code>Float</code>,
  808. * and then writes that <code>int</code> value to the file as a
  809. * four-byte quantity, high byte first. The write starts at the
  810. * current position of the file pointer.
  811. *
  812. * @param v a <code>float</code> value to be written.
  813. * @exception IOException if an I/O error occurs.
  814. * @see java.lang.Float#floatToIntBits(float)
  815. */
  816. public final void writeFloat(float v) throws IOException {
  817. writeInt(Float.floatToIntBits(v));
  818. }
  819. /**
  820. * Converts the double argument to a <code>long</code> using the
  821. * <code>doubleToLongBits</code> method in class <code>Double</code>,
  822. * and then writes that <code>long</code> value to the file as an
  823. * eight-byte quantity, high byte first. The write starts at the current
  824. * position of the file pointer.
  825. *
  826. * @param v a <code>double</code> value to be written.
  827. * @exception IOException if an I/O error occurs.
  828. * @see java.lang.Double#doubleToLongBits(double)
  829. */
  830. public final void writeDouble(double v) throws IOException {
  831. writeLong(Double.doubleToLongBits(v));
  832. }
  833. /**
  834. * Writes the string to the file as a sequence of bytes. Each
  835. * character in the string is written out, in sequence, by discarding
  836. * its high eight bits. The write starts at the current position of
  837. * the file pointer.
  838. *
  839. * @param s a string of bytes to be written.
  840. * @exception IOException if an I/O error occurs.
  841. */
  842. public final void writeBytes(String s) throws IOException {
  843. int len = s.length();
  844. byte[] b = new byte[len];
  845. s.getBytes(0, len, b, 0);
  846. writeBytes(b, 0, len);
  847. }
  848. /**
  849. * Writes a string to the file as a sequence of characters. Each
  850. * character is written to the data output stream as if by the
  851. * <code>writeChar</code> method. The write starts at the current
  852. * position of the file pointer.
  853. *
  854. * @param s a <code>String</code> value to be written.
  855. * @exception IOException if an I/O error occurs.
  856. * @see java.io.RandomAccessFile#writeChar(int)
  857. */
  858. public final void writeChars(String s) throws IOException {
  859. int clen = s.length();
  860. int blen = 2*clen;
  861. byte[] b = new byte[blen];
  862. char[] c = new char[clen];
  863. s.getChars(0, clen, c, 0);
  864. for (int i = 0, j = 0; i < clen; i++) {
  865. b[j++] = (byte)(c[i] >>> 8);
  866. b[j++] = (byte)(c[i] >>> 0);
  867. }
  868. writeBytes(b, 0, blen);
  869. }
  870. /**
  871. * Writes a string to the file using UTF-8 encoding in a
  872. * machine-independent manner.
  873. * <p>
  874. * First, two bytes are written to the file, starting at the
  875. * current file pointer, as if by the
  876. * <code>writeShort</code> method giving the number of bytes to
  877. * follow. This value is the number of bytes actually written out,
  878. * not the length of the string. Following the length, each character
  879. * of the string is output, in sequence, using the UTF-8 encoding
  880. * for each character.
  881. *
  882. * @param str a string to be written.
  883. * @exception IOException if an I/O error occurs.
  884. */
  885. public final void writeUTF(String str) throws IOException {
  886. DataOutputStream.writeUTF(str, this);
  887. }
  888. private static native void initIDs();
  889. static {
  890. initIDs();
  891. }
  892. }