RandomAccessFile.java 36 KB

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