RandomAccessFile.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /* RandomAccessFile.java -- Class supporting random file I/O
  2. Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. import gnu.java.nio.FileChannelImpl;
  33. import java.nio.channels.FileChannel;
  34. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  35. * "The Java Language Specification", ISBN 0-201-63451-1
  36. * Status: Believe complete and correct to 1.1.
  37. */
  38. /**
  39. * This class allows reading and writing of files at random locations.
  40. * Most Java I/O classes are either pure sequential input or output. This
  41. * class fulfills the need to be able to read the bytes of a file in an
  42. * arbitrary order. In addition, this class implements the
  43. * <code>DataInput</code> and <code>DataOutput</code> interfaces to allow
  44. * the reading and writing of Java primitives.
  45. *
  46. * @author Aaron M. Renn (arenn@urbanophile.com)
  47. * @author Tom Tromey (tromey@cygnus.com)
  48. */
  49. public class RandomAccessFile implements DataOutput, DataInput, Closeable
  50. {
  51. // The underlying file.
  52. private FileChannelImpl ch;
  53. private FileDescriptor fd;
  54. // The corresponding input and output streams.
  55. private DataOutputStream out;
  56. private DataInputStream in;
  57. /**
  58. * This method initializes a new instance of <code>RandomAccessFile</code>
  59. * to read from the specified <code>File</code> object with the specified
  60. * access mode. The access mode is either "r" for read only access or "rw"
  61. * for read-write access.
  62. * <p>
  63. * Note that a <code>SecurityManager</code> check is made prior to
  64. * opening the file to determine whether or not this file is allowed to
  65. * be read or written.
  66. *
  67. * @param file The <code>File</code> object to read and/or write.
  68. * @param mode "r" for read only or "rw" for read-write access to the file
  69. *
  70. * @exception IllegalArgumentException If <code>mode</code> has an
  71. * illegal value
  72. * @exception SecurityException If the requested access to the file
  73. * is not allowed
  74. * @exception FileNotFoundException If the file is a directory, or
  75. * any other error occurs
  76. */
  77. public RandomAccessFile (File file, String mode)
  78. throws FileNotFoundException
  79. {
  80. int fdmode;
  81. if (mode.equals("r"))
  82. fdmode = FileChannelImpl.READ;
  83. else if (mode.equals("rw"))
  84. fdmode = FileChannelImpl.READ | FileChannelImpl.WRITE;
  85. else if (mode.equals("rws"))
  86. {
  87. fdmode = (FileChannelImpl.READ | FileChannelImpl.WRITE
  88. | FileChannelImpl.SYNC);
  89. }
  90. else if (mode.equals("rwd"))
  91. {
  92. fdmode = (FileChannelImpl.READ | FileChannelImpl.WRITE
  93. | FileChannelImpl.DSYNC);
  94. }
  95. else
  96. throw new IllegalArgumentException ("invalid mode: " + mode);
  97. final String fileName = file.getPath();
  98. // The obligatory SecurityManager stuff
  99. SecurityManager s = System.getSecurityManager();
  100. if (s != null)
  101. {
  102. s.checkRead(fileName);
  103. if ((fdmode & FileChannelImpl.WRITE) != 0)
  104. s.checkWrite(fileName);
  105. }
  106. try
  107. {
  108. ch = FileChannelImpl.create(file, fdmode);
  109. }
  110. catch (FileNotFoundException fnfe)
  111. {
  112. throw fnfe;
  113. }
  114. catch (IOException ioe)
  115. {
  116. FileNotFoundException fnfe = new FileNotFoundException(file.getPath());
  117. fnfe.initCause(ioe);
  118. throw fnfe;
  119. }
  120. fd = new FileDescriptor(ch);
  121. if ((fdmode & FileChannelImpl.WRITE) != 0)
  122. out = new DataOutputStream (new FileOutputStream (fd));
  123. else
  124. out = null;
  125. in = new DataInputStream (new FileInputStream (fd));
  126. }
  127. /**
  128. * This method initializes a new instance of <code>RandomAccessFile</code>
  129. * to read from the specified file name with the specified access mode.
  130. * The access mode is either "r" for read only access, "rw" for read
  131. * write access, "rws" for synchronized read/write access of both
  132. * content and metadata, or "rwd" for read/write access
  133. * where only content is required to be synchronous.
  134. * <p>
  135. * Note that a <code>SecurityManager</code> check is made prior to
  136. * opening the file to determine whether or not this file is allowed to
  137. * be read or written.
  138. *
  139. * @param fileName The name of the file to read and/or write
  140. * @param mode "r", "rw", "rws", or "rwd"
  141. *
  142. * @exception IllegalArgumentException If <code>mode</code> has an
  143. * illegal value
  144. * @exception SecurityException If the requested access to the file
  145. * is not allowed
  146. * @exception FileNotFoundException If the file is a directory or
  147. * any other error occurs
  148. */
  149. public RandomAccessFile (String fileName, String mode)
  150. throws FileNotFoundException
  151. {
  152. this (new File(fileName), mode);
  153. }
  154. /**
  155. * This method closes the file and frees up all file related system
  156. * resources. Since most operating systems put a limit on how many files
  157. * may be opened at any given time, it is a good idea to close all files
  158. * when no longer needed to avoid hitting this limit
  159. */
  160. public void close () throws IOException
  161. {
  162. ch.close();
  163. }
  164. /**
  165. * This method returns a <code>FileDescriptor</code> object that
  166. * represents the native file handle for this file.
  167. *
  168. * @return The <code>FileDescriptor</code> object for this file
  169. *
  170. * @exception IOException If an error occurs
  171. */
  172. public final FileDescriptor getFD () throws IOException
  173. {
  174. synchronized (this)
  175. {
  176. if (fd == null)
  177. fd = new FileDescriptor (ch);
  178. return fd;
  179. }
  180. }
  181. /**
  182. * This method returns the current offset in the file at which the next
  183. * read or write will occur
  184. *
  185. * @return The current file position
  186. *
  187. * @exception IOException If an error occurs
  188. */
  189. public long getFilePointer () throws IOException
  190. {
  191. return ch.position();
  192. }
  193. /**
  194. * This method sets the length of the file to the specified length.
  195. * If the currently length of the file is longer than the specified
  196. * length, then the file is truncated to the specified length (the
  197. * file position is set to the end of file in this case). If the
  198. * current length of the file is shorter than the specified length,
  199. * the file is extended with bytes of an undefined value (the file
  200. * position is unchanged in this case).
  201. * <p>
  202. * The file must be open for write access for this operation to succeed.
  203. *
  204. * @param newLen The new length of the file
  205. *
  206. * @exception IOException If an error occurs
  207. */
  208. public void setLength (long newLen) throws IOException
  209. {
  210. // FIXME: Extending a file should probably be done by one method call.
  211. // FileChannel.truncate() can only shrink a file.
  212. // To expand it we need to seek forward and write at least one byte.
  213. if (newLen < length())
  214. ch.truncate (newLen);
  215. else if (newLen > length())
  216. {
  217. long pos = getFilePointer();
  218. seek(newLen - 1);
  219. write(0);
  220. seek(pos);
  221. }
  222. }
  223. /**
  224. * This method returns the length of the file in bytes
  225. *
  226. * @return The length of the file
  227. *
  228. * @exception IOException If an error occurs
  229. */
  230. public long length () throws IOException
  231. {
  232. return ch.size();
  233. }
  234. /**
  235. * This method reads a single byte of data from the file and returns it
  236. * as an integer.
  237. *
  238. * @return The byte read as an int, or -1 if the end of the file was reached.
  239. *
  240. * @exception IOException If an error occurs
  241. */
  242. public int read () throws IOException
  243. {
  244. return in.read();
  245. }
  246. /**
  247. * This method reads bytes from the file into the specified array. The
  248. * bytes are stored starting at the beginning of the array and up to
  249. * <code>buf.length</code> bytes can be read.
  250. *
  251. * @param buffer The buffer to read bytes from the file into
  252. *
  253. * @return The actual number of bytes read or -1 if end of file
  254. *
  255. * @exception IOException If an error occurs
  256. */
  257. public int read (byte[] buffer) throws IOException
  258. {
  259. return in.read (buffer);
  260. }
  261. /**
  262. * This methods reads up to <code>len</code> bytes from the file into the
  263. * specified array starting at position <code>offset</code> into the array.
  264. *
  265. * @param buffer The array to read the bytes into
  266. * @param offset The index into the array to start storing bytes
  267. * @param len The requested number of bytes to read
  268. *
  269. * @return The actual number of bytes read, or -1 if end of file
  270. *
  271. * @exception IOException If an error occurs
  272. */
  273. public int read (byte[] buffer, int offset, int len) throws IOException
  274. {
  275. return in.read (buffer, offset, len);
  276. }
  277. /**
  278. * This method reads a Java boolean value from an input stream. It does
  279. * so by reading a single byte of data. If that byte is zero, then the
  280. * value returned is <code>false</code> If the byte is non-zero, then
  281. * the value returned is <code>true</code>
  282. * <p>
  283. * This method can read a <code>boolean</code> written by an object
  284. * implementing the
  285. * <code>writeBoolean()</code> method in the <code>DataOutput</code>
  286. * interface.
  287. *
  288. * @return The <code>boolean</code> value read
  289. *
  290. * @exception EOFException If end of file is reached before reading the
  291. * boolean
  292. * @exception IOException If any other error occurs
  293. */
  294. public final boolean readBoolean () throws IOException
  295. {
  296. return in.readBoolean ();
  297. }
  298. /**
  299. * This method reads a Java byte value from an input stream. The value
  300. * is in the range of -128 to 127.
  301. * <p>
  302. * This method can read a <code>byte</code> written by an object
  303. * implementing the
  304. * <code>writeByte()</code> method in the <code>DataOutput</code> interface.
  305. *
  306. * @return The <code>byte</code> value read
  307. *
  308. * @exception EOFException If end of file is reached before reading the byte
  309. * @exception IOException If any other error occurs
  310. *
  311. * @see DataOutput
  312. */
  313. public final byte readByte () throws IOException
  314. {
  315. return in.readByte ();
  316. }
  317. /**
  318. * This method reads a Java <code>char</code> value from an input stream.
  319. * It operates by reading two bytes from the stream and converting them to
  320. * a single 16-bit Java <code>char</code> The two bytes are stored most
  321. * significant byte first (i.e., "big endian") regardless of the native
  322. * host byte ordering.
  323. * <p>
  324. * As an example, if <code>byte1</code> and <code>byte2</code> represent
  325. * the first
  326. * and second byte read from the stream respectively, they will be
  327. * transformed to a <code>char</code> in the following manner:
  328. * <p>
  329. * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
  330. * <p>
  331. * This method can read a <code>char</code> written by an object
  332. * implementing the
  333. * <code>writeChar()</code> method in the <code>DataOutput</code> interface.
  334. *
  335. * @return The <code>char</code> value read
  336. *
  337. * @exception EOFException If end of file is reached before reading the char
  338. * @exception IOException If any other error occurs
  339. *
  340. * @see DataOutput
  341. */
  342. public final char readChar () throws IOException
  343. {
  344. return in.readChar();
  345. }
  346. /**
  347. * This method reads a Java double value from an input stream. It operates
  348. * by first reading a <code>logn</code> value from the stream by calling the
  349. * <code>readLong()</code> method in this interface, then
  350. * converts that <code>long</code>
  351. * to a <code>double</code> using the <code>longBitsToDouble</code>
  352. * method in the class <code>java.lang.Double</code>
  353. * <p>
  354. * This method can read a <code>double</code> written by an object
  355. * implementing the
  356. * <code>writeDouble()</code> method in the <code>DataOutput</code>
  357. * interface.
  358. *
  359. * @return The <code>double</code> value read
  360. *
  361. * @exception EOFException If end of file is reached before reading
  362. * the double
  363. * @exception IOException If any other error occurs
  364. *
  365. * @see java.lang.Double
  366. * @see DataOutput
  367. */
  368. public final double readDouble () throws IOException
  369. {
  370. return in.readDouble ();
  371. }
  372. /**
  373. * This method reads a Java float value from an input stream. It operates
  374. * by first reading an <code>int</code> value from the stream by calling the
  375. * <code>readInt()</code> method in this interface, then converts
  376. * that <code>int</code>
  377. * to a <code>float</code> using the <code>intBitsToFloat</code> method in
  378. * the class <code>java.lang.Float</code>
  379. * <p>
  380. * This method can read a <code>float</code> written by an object
  381. * implementing the
  382. * <code>writeFloat()</code> method in the <code>DataOutput</code> interface.
  383. *
  384. * @return The <code>float</code> value read
  385. *
  386. * @exception EOFException If end of file is reached before reading the float
  387. * @exception IOException If any other error occurs
  388. *
  389. * @see java.lang.Float
  390. * @see DataOutput
  391. */
  392. public final float readFloat () throws IOException
  393. {
  394. return in.readFloat();
  395. }
  396. /**
  397. * This method reads raw bytes into the passed array until the array is
  398. * full. Note that this method blocks until the data is available and
  399. * throws an exception if there is not enough data left in the stream to
  400. * fill the buffer
  401. *
  402. * @param buffer The buffer into which to read the data
  403. *
  404. * @exception EOFException If end of file is reached before filling the
  405. * buffer
  406. * @exception IOException If any other error occurs
  407. */
  408. public final void readFully (byte[] buffer) throws IOException
  409. {
  410. in.readFully(buffer);
  411. }
  412. /**
  413. * This method reads raw bytes into the passed array <code>buf</code>
  414. * starting
  415. * <code>offset</code> bytes into the buffer. The number of bytes read
  416. * will be
  417. * exactly <code>len</code> Note that this method blocks until the data is
  418. * available and throws an exception if there is not enough data left in
  419. * the stream to read <code>len</code> bytes.
  420. *
  421. * @param buffer The buffer into which to read the data
  422. * @param offset The offset into the buffer to start storing data
  423. * @param count The number of bytes to read into the buffer
  424. *
  425. * @exception EOFException If end of file is reached before filling
  426. * the buffer
  427. * @exception IOException If any other error occurs
  428. */
  429. public final void readFully (byte[] buffer, int offset, int count)
  430. throws IOException
  431. {
  432. in.readFully (buffer, offset, count);
  433. }
  434. /**
  435. * This method reads a Java <code>int</code> value from an input stream
  436. * It operates by reading four bytes from the stream and converting them to
  437. * a single Java <code>int</code> The bytes are stored most
  438. * significant byte first (i.e., "big endian") regardless of the native
  439. * host byte ordering.
  440. * <p>
  441. * As an example, if <code>byte1</code> through <code>byte4</code>
  442. * represent the first
  443. * four bytes read from the stream, they will be
  444. * transformed to an <code>int</code> in the following manner:
  445. * <p>
  446. * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
  447. * ((byte3 &amp; 0xFF) &lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
  448. * <p>
  449. * The value returned is in the range of 0 to 65535.
  450. * <p>
  451. * This method can read an <code>int</code> written by an object
  452. * implementing the
  453. * <code>writeInt()</code> method in the <code>DataOutput</code> interface.
  454. *
  455. * @return The <code>int</code> value read
  456. *
  457. * @exception EOFException If end of file is reached before reading the int
  458. * @exception IOException If any other error occurs
  459. *
  460. * @see DataOutput
  461. */
  462. public final int readInt () throws IOException
  463. {
  464. return in.readInt();
  465. }
  466. /**
  467. * This method reads the next line of text data from an input stream.
  468. * It operates by reading bytes and converting those bytes to
  469. * <code>char</code>
  470. * values by treating the byte read as the low eight bits of the
  471. * <code>char</code>
  472. * and using <code>0</code> as the high eight bits. Because of this, it does
  473. * not support the full 16-bit Unicode character set.
  474. * <p>
  475. * The reading of bytes ends when either the end of file or a line terminator
  476. * is encountered. The bytes read are then returned as a <code>String</code>
  477. * A line terminator is a byte sequence consisting of either
  478. * <code>\r</code> <code>\n</code> or <code>\r\n</code> These
  479. * termination charaters are
  480. * discarded and are not returned as part of the string.
  481. * <p>
  482. * This method can read data that was written by an object implementing the
  483. * <code>writeLine()</code> method in <code>DataOutput</code>
  484. *
  485. * @return The line read as a <code>String</code>
  486. *
  487. * @exception IOException If an error occurs
  488. *
  489. * @see DataOutput
  490. */
  491. public final String readLine () throws IOException
  492. {
  493. return in.readLine ();
  494. }
  495. /**
  496. * This method reads a Java long value from an input stream
  497. * It operates by reading eight bytes from the stream and converting them to
  498. * a single Java <code>long</code> The bytes are stored most
  499. * significant byte first (i.e., "big endian") regardless of the native
  500. * host byte ordering.
  501. * <p>
  502. * As an example, if <code>byte1</code> through <code>byte8</code>
  503. * represent the first
  504. * eight bytes read from the stream, they will be
  505. * transformed to an <code>long</code> in the following manner:
  506. * <p>
  507. * <code>
  508. * (long)((((long)byte1 &amp; 0xFF) &lt;&lt; 56) + (((long)byte2 &amp; 0xFF) &lt;&lt; 48) +
  509. * (((long)byte3 &amp; 0xFF) &lt;&lt; 40) + (((long)byte4 &amp; 0xFF) &lt;&lt; 32) +
  510. * (((long)byte5 &amp; 0xFF) &lt;&lt; 24) + (((long)byte6 &amp; 0xFF) &lt;&lt; 16) +
  511. * (((long)byte7 &amp; 0xFF) &lt;&lt; 8) + ((long)byte9 &amp; 0xFF)))</code>
  512. * <p>
  513. * The value returned is in the range of 0 to 65535.
  514. * <p>
  515. * This method can read an <code>long</code> written by an object
  516. * implementing the
  517. * <code>writeLong()</code> method in the <code>DataOutput</code> interface.
  518. *
  519. * @return The <code>long</code> value read
  520. *
  521. * @exception EOFException If end of file is reached before reading the long
  522. * @exception IOException If any other error occurs
  523. *
  524. * @see DataOutput
  525. */
  526. public final long readLong () throws IOException
  527. {
  528. return in.readLong();
  529. }
  530. /**
  531. * This method reads a signed 16-bit value into a Java in from the stream.
  532. * It operates by reading two bytes from the stream and converting them to
  533. * a single 16-bit Java <code>short</code> The two bytes are stored most
  534. * significant byte first (i.e., "big endian") regardless of the native
  535. * host byte ordering.
  536. * <p>
  537. * As an example, if <code>byte1</code> and <code>byte2</code>
  538. * represent the first
  539. * and second byte read from the stream respectively, they will be
  540. * transformed to a <code>short</code> in the following manner:
  541. * <p>
  542. * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
  543. * <p>
  544. * The value returned is in the range of -32768 to 32767.
  545. * <p>
  546. * This method can read a <code>short</code> written by an object
  547. * implementing the
  548. * <code>writeShort()</code> method in the <code>DataOutput</code> interface.
  549. *
  550. * @return The <code>short</code> value read
  551. *
  552. * @exception EOFException If end of file is reached before reading the value
  553. * @exception IOException If any other error occurs
  554. *
  555. * @see DataOutput
  556. */
  557. public final short readShort () throws IOException
  558. {
  559. return in.readShort();
  560. }
  561. /**
  562. * This method reads 8 unsigned bits into a Java <code>int</code> value
  563. * from the
  564. * stream. The value returned is in the range of 0 to 255.
  565. * <p>
  566. * This method can read an unsigned byte written by an object implementing
  567. * the <code>writeUnsignedByte()</code> method in the
  568. * <code>DataOutput</code> interface.
  569. *
  570. * @return The unsigned bytes value read as a Java <code>int</code>
  571. *
  572. * @exception EOFException If end of file is reached before reading the value
  573. * @exception IOException If any other error occurs
  574. *
  575. * @see DataOutput
  576. */
  577. public final int readUnsignedByte () throws IOException
  578. {
  579. return in.readUnsignedByte();
  580. }
  581. /**
  582. * This method reads 16 unsigned bits into a Java int value from the stream.
  583. * It operates by reading two bytes from the stream and converting them to
  584. * a single Java <code>int</code> The two bytes are stored most
  585. * significant byte first (i.e., "big endian") regardless of the native
  586. * host byte ordering.
  587. * <p>
  588. * As an example, if <code>byte1</code> and <code>byte2</code>
  589. * represent the first
  590. * and second byte read from the stream respectively, they will be
  591. * transformed to an <code>int</code> in the following manner:
  592. * <p>
  593. * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
  594. * <p>
  595. * The value returned is in the range of 0 to 65535.
  596. * <p>
  597. * This method can read an unsigned short written by an object implementing
  598. * the <code>writeUnsignedShort()</code> method in the
  599. * <code>DataOutput</code> interface.
  600. *
  601. * @return The unsigned short value read as a Java <code>int</code>
  602. *
  603. * @exception EOFException If end of file is reached before reading the value
  604. * @exception IOException If any other error occurs
  605. */
  606. public final int readUnsignedShort () throws IOException
  607. {
  608. return in.readUnsignedShort();
  609. }
  610. /**
  611. * This method reads a <code>String</code> from an input stream that
  612. * is encoded in
  613. * a modified UTF-8 format. This format has a leading two byte sequence
  614. * that contains the remaining number of bytes to read. This two byte
  615. * sequence is read using the <code>readUnsignedShort()</code> method of this
  616. * interface.
  617. * <p>
  618. * After the number of remaining bytes have been determined, these bytes
  619. * are read an transformed into <code>char</code> values.
  620. * These <code>char</code> values
  621. * are encoded in the stream using either a one, two, or three byte format.
  622. * The particular format in use can be determined by examining the first
  623. * byte read.
  624. * <p>
  625. * If the first byte has a high order bit of 0 then
  626. * that character consists on only one byte. This character value consists
  627. * of seven bits that are at positions 0 through 6 of the byte. As an
  628. * example, if <code>byte1</code> is the byte read from the stream, it would
  629. * be converted to a <code>char</code> like so:
  630. * <p>
  631. * <code>(char)byte1</code>
  632. * <p>
  633. * If the first byte has <code>110</code> as its high order bits, then the
  634. * character consists of two bytes. The bits that make up the character
  635. * value are in positions 0 through 4 of the first byte and bit positions
  636. * 0 through 5 of the second byte. (The second byte should have
  637. * 10 as its high order bits). These values are in most significant
  638. * byte first (i.e., "big endian") order.
  639. * <p>
  640. * As an example, if <code>byte1</code> and <code>byte2</code>
  641. * are the first two bytes
  642. * read respectively, and the high order bits of them match the patterns
  643. * which indicate a two byte character encoding, then they would be
  644. * converted to a Java <code>char</code> like so:
  645. * <p>
  646. * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
  647. * <p>
  648. * If the first byte has a <code>1110</code> as its high order bits, then the
  649. * character consists of three bytes. The bits that make up the character
  650. * value are in positions 0 through 3 of the first byte and bit positions
  651. * 0 through 5 of the other two bytes. (The second and third bytes should
  652. * have <code>10</code> as their high order bits). These values are in most
  653. * significant byte first (i.e., "big endian") order.
  654. * <p>
  655. * As an example, if <code>byte1</code> <code>byte2</code>
  656. * and <code>byte3</code> are the
  657. * three bytes read, and the high order bits of them match the patterns
  658. * which indicate a three byte character encoding, then they would be
  659. * converted to a Java <code>char</code> like so:
  660. * <p>
  661. * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) |
  662. * (byte3 & 0x3F))</code>
  663. * <p>
  664. * Note that all characters are encoded in the method that requires the
  665. * fewest number of bytes with the exception of the character with the
  666. * value of <code>&#92;u0000</code> which is encoded as two bytes. This is
  667. * a modification of the UTF standard used to prevent C language style
  668. * <code>NUL</code> values from appearing in the byte stream.
  669. * <p>
  670. * This method can read data that was written by an object implementing the
  671. * <code>writeUTF()</code> method in <code>DataOutput</code>
  672. *
  673. * @return The <code>String</code> read
  674. *
  675. * @exception EOFException If end of file is reached before reading the
  676. * String
  677. * @exception UTFDataFormatException If the data is not in UTF-8 format
  678. * @exception IOException If any other error occurs
  679. *
  680. * @see DataOutput
  681. */
  682. public final String readUTF () throws IOException
  683. {
  684. return in.readUTF();
  685. }
  686. /**
  687. * This method sets the current file position to the specified offset
  688. * from the beginning of the file. Note that some operating systems will
  689. * allow the file pointer to be set past the current end of the file.
  690. *
  691. * @param pos The offset from the beginning of the file at which to set
  692. * the file pointer
  693. *
  694. * @exception IOException If an error occurs
  695. */
  696. public void seek (long pos) throws IOException
  697. {
  698. ch.position(pos);
  699. }
  700. /**
  701. * This method attempts to skip and discard the specified number of bytes
  702. * in the input stream. It may actually skip fewer bytes than requested.
  703. * The actual number of bytes skipped is returned. This method will not
  704. * skip any bytes if passed a negative number of bytes to skip.
  705. *
  706. * @param numBytes The requested number of bytes to skip.
  707. *
  708. * @return The number of bytes actually skipped.
  709. *
  710. * @exception IOException If an error occurs.
  711. */
  712. public int skipBytes (int numBytes) throws IOException
  713. {
  714. if (numBytes < 0)
  715. throw new IllegalArgumentException ("Can't skip negative bytes: " +
  716. numBytes);
  717. if (numBytes == 0)
  718. return 0;
  719. long oldPos = ch.position();
  720. long newPos = oldPos + numBytes;
  721. long size = ch.size();
  722. if (newPos > size)
  723. newPos = size;
  724. ch.position(newPos);
  725. return (int) (ch.position() - oldPos);
  726. }
  727. /**
  728. * This method writes a single byte of data to the file. The file must
  729. * be open for read-write in order for this operation to succeed.
  730. *
  731. * @param oneByte The byte of data to write, passed as an int.
  732. *
  733. * @exception IOException If an error occurs
  734. */
  735. public void write (int oneByte) throws IOException
  736. {
  737. if (out == null)
  738. throw new IOException("Bad file descriptor");
  739. out.write(oneByte);
  740. }
  741. /**
  742. * This method writes all the bytes in the specified array to the file.
  743. * The file must be open read-write in order for this operation to succeed.
  744. *
  745. * @param buffer The array of bytes to write to the file
  746. */
  747. public void write (byte[] buffer) throws IOException
  748. {
  749. if (out == null)
  750. throw new IOException("Bad file descriptor");
  751. out.write(buffer);
  752. }
  753. /**
  754. * This method writes <code>len</code> bytes to the file from the specified
  755. * array starting at index <code>offset</code> into the array.
  756. *
  757. * @param buffer The array of bytes to write to the file
  758. * @param offset The index into the array to start writing file
  759. * @param len The number of bytes to write
  760. *
  761. * @exception IOException If an error occurs
  762. */
  763. public void write (byte[] buffer, int offset, int len) throws IOException
  764. {
  765. if (out == null)
  766. throw new IOException("Bad file descriptor");
  767. out.write (buffer, offset, len);
  768. }
  769. /**
  770. * This method writes a Java <code>boolean</code> to the underlying output
  771. * stream. For a value of <code>true</code>, 1 is written to the stream.
  772. * For a value of <code>false</code>, 0 is written.
  773. *
  774. * @param val The <code>boolean</code> value to write to the stream
  775. *
  776. * @exception IOException If an error occurs
  777. */
  778. public final void writeBoolean (boolean val) throws IOException
  779. {
  780. if (out == null)
  781. throw new IOException("Bad file descriptor");
  782. out.writeBoolean(val);
  783. }
  784. /**
  785. * This method writes a Java <code>byte</code> value to the underlying
  786. * output stream.
  787. *
  788. * @param val The <code>byte</code> to write to the stream, passed
  789. * as an <code>int</code>.
  790. *
  791. * @exception IOException If an error occurs
  792. */
  793. public final void writeByte (int val) throws IOException
  794. {
  795. if (out == null)
  796. throw new IOException("Bad file descriptor");
  797. out.writeByte(val);
  798. }
  799. /**
  800. * This method writes a Java <code>short</code> to the stream, high byte
  801. * first. This method requires two bytes to encode the value.
  802. *
  803. * @param val The <code>short</code> value to write to the stream,
  804. * passed as an <code>int</code>.
  805. *
  806. * @exception IOException If an error occurs
  807. */
  808. public final void writeShort (int val) throws IOException
  809. {
  810. if (out == null)
  811. throw new IOException("Bad file descriptor");
  812. out.writeShort(val);
  813. }
  814. /**
  815. * This method writes a single <code>char</code> value to the stream,
  816. * high byte first.
  817. *
  818. * @param val The <code>char</code> value to write, passed as
  819. * an <code>int</code>.
  820. *
  821. * @exception IOException If an error occurs
  822. */
  823. public final void writeChar (int val) throws IOException
  824. {
  825. if (out == null)
  826. throw new IOException("Bad file descriptor");
  827. out.writeChar(val);
  828. }
  829. /**
  830. * This method writes a Java <code>int</code> to the stream, high bytes
  831. * first. This method requires four bytes to encode the value.
  832. *
  833. * @param val The <code>int</code> value to write to the stream.
  834. *
  835. * @exception IOException If an error occurs
  836. */
  837. public final void writeInt (int val) throws IOException
  838. {
  839. if (out == null)
  840. throw new IOException("Bad file descriptor");
  841. out.writeInt(val);
  842. }
  843. /**
  844. * This method writes a Java <code>long</code> to the stream, high bytes
  845. * first. This method requires eight bytes to encode the value.
  846. *
  847. * @param val The <code>long</code> value to write to the stream.
  848. *
  849. * @exception IOException If an error occurs
  850. */
  851. public final void writeLong (long val) throws IOException
  852. {
  853. if (out == null)
  854. throw new IOException("Bad file descriptor");
  855. out.writeLong(val);
  856. }
  857. /**
  858. * This method writes a Java <code>float</code> value to the stream. This
  859. * value is written by first calling the method
  860. * <code>Float.floatToIntBits</code>
  861. * to retrieve an <code>int</code> representing the floating point number,
  862. * then writing this <code>int</code> value to the stream exactly the same
  863. * as the <code>writeInt()</code> method does.
  864. *
  865. * @param val The floating point number to write to the stream.
  866. *
  867. * @exception IOException If an error occurs
  868. *
  869. * @see #writeInt(int)
  870. */
  871. public final void writeFloat (float val) throws IOException
  872. {
  873. if (out == null)
  874. throw new IOException("Bad file descriptor");
  875. out.writeFloat(val);
  876. }
  877. /**
  878. * This method writes a Java <code>double</code> value to the stream. This
  879. * value is written by first calling the method
  880. * <code>Double.doubleToLongBits</code>
  881. * to retrieve an <code>long</code> representing the floating point number,
  882. * then writing this <code>long</code> value to the stream exactly the same
  883. * as the <code>writeLong()</code> method does.
  884. *
  885. * @param val The double precision floating point number to write to the
  886. * stream.
  887. *
  888. * @exception IOException If an error occurs
  889. *
  890. * @see #writeLong(long)
  891. */
  892. public final void writeDouble (double val) throws IOException
  893. {
  894. if (out == null)
  895. throw new IOException("Bad file descriptor");
  896. out.writeDouble(val);
  897. }
  898. /**
  899. * This method writes all the bytes in a <code>String</code> out to the
  900. * stream. One byte is written for each character in the <code>String</code>.
  901. * The high eight bits of each character are discarded.
  902. *
  903. * @param val The <code>String</code> to write to the stream
  904. *
  905. * @exception IOException If an error occurs
  906. */
  907. public final void writeBytes (String val) throws IOException
  908. {
  909. if (out == null)
  910. throw new IOException("Bad file descriptor");
  911. out.writeBytes(val);
  912. }
  913. /**
  914. * This method writes all the characters in a <code>String</code> to the
  915. * stream. There will be two bytes for each character value. The high
  916. * byte of the character will be written first.
  917. *
  918. * @param val The <code>String</code> to write to the stream.
  919. *
  920. * @exception IOException If an error occurs
  921. */
  922. public final void writeChars (String val) throws IOException
  923. {
  924. if (out == null)
  925. throw new IOException("Bad file descriptor");
  926. out.writeChars(val);
  927. }
  928. /**
  929. * This method writes a Java <code>String</code> to the stream in a modified
  930. * UTF-8 format. First, two bytes are written to the stream indicating the
  931. * number of bytes to follow. Note that this is the number of bytes in the
  932. * encoded <code>String</code> not the <code>String</code> length. Next
  933. * come the encoded characters. Each character in the <code>String</code>
  934. * is encoded as either one, two or three bytes. For characters in the
  935. * range of <code>&#92;u0001</code> to <code>&#92;u007F</code>,
  936. * one byte is used. The character
  937. * value goes into bits 0-7 and bit eight is 0. For characters in the range
  938. * of <code>&#92;u0080</code> to <code>&#92;u007FF</code>, two
  939. * bytes are used. Bits
  940. * 6-10 of the character value are encoded bits 0-4 of the first byte, with
  941. * the high bytes having a value of "110". Bits 0-5 of the character value
  942. * are stored in bits 0-5 of the second byte, with the high bits set to
  943. * "10". This type of encoding is also done for the null character
  944. * <code>&#92;u0000</code>. This eliminates any C style NUL character values
  945. * in the output. All remaining characters are stored as three bytes.
  946. * Bits 12-15 of the character value are stored in bits 0-3 of the first
  947. * byte. The high bits of the first bytes are set to "1110". Bits 6-11
  948. * of the character value are stored in bits 0-5 of the second byte. The
  949. * high bits of the second byte are set to "10". And bits 0-5 of the
  950. * character value are stored in bits 0-5 of byte three, with the high bits
  951. * of that byte set to "10".
  952. *
  953. * @param val The <code>String</code> to write to the output in UTF format
  954. *
  955. * @exception IOException If an error occurs
  956. */
  957. public final void writeUTF (String val) throws IOException
  958. {
  959. if (out == null)
  960. throw new IOException("Bad file descriptor");
  961. out.writeUTF(val);
  962. }
  963. /**
  964. * This method creates a java.nio.channels.FileChannel.
  965. * Nio does not allow one to create a file channel directly.
  966. * A file channel must be created by first creating an instance of
  967. * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
  968. */
  969. public final synchronized FileChannel getChannel ()
  970. {
  971. return ch;
  972. }
  973. }