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