OSDN Git Service

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