1 /* RandomAccessFile.java -- Class supporting random file I/O
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
41 import gnu
.java
.nio
.channels
.FileChannelImpl
;
43 import java
.nio
.channels
.FileChannel
;
45 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
46 * "The Java Language Specification", ISBN 0-201-63451-1
47 * Status: Believe complete and correct to 1.1.
51 * This class allows reading and writing of files at random locations.
52 * Most Java I/O classes are either pure sequential input or output. This
53 * class fulfills the need to be able to read the bytes of a file in an
54 * arbitrary order. In addition, this class implements the
55 * <code>DataInput</code> and <code>DataOutput</code> interfaces to allow
56 * the reading and writing of Java primitives.
58 * @author Aaron M. Renn (arenn@urbanophile.com)
59 * @author Tom Tromey (tromey@cygnus.com)
61 public class RandomAccessFile
implements DataOutput
, DataInput
, Closeable
64 // The underlying file.
65 private FileChannelImpl ch
;
66 private FileDescriptor fd
;
67 // The corresponding input and output streams.
68 private DataOutputStream out
;
69 private DataInputStream in
;
73 * This method initializes a new instance of <code>RandomAccessFile</code>
74 * to read from the specified <code>File</code> object with the specified
75 * access mode. The access mode is either "r" for read only access or "rw"
76 * for read-write access.
78 * Note that a <code>SecurityManager</code> check is made prior to
79 * opening the file to determine whether or not this file is allowed to
82 * @param file The <code>File</code> object to read and/or write.
83 * @param mode "r" for read only or "rw" for read-write access to the file
85 * @exception IllegalArgumentException If <code>mode</code> has an
87 * @exception SecurityException If the requested access to the file
89 * @exception FileNotFoundException If the file is a directory, or
90 * any other error occurs
92 public RandomAccessFile (File file
, String mode
)
93 throws FileNotFoundException
97 fdmode
= FileChannelImpl
.READ
;
98 else if (mode
.equals("rw"))
99 fdmode
= FileChannelImpl
.READ
| FileChannelImpl
.WRITE
;
100 else if (mode
.equals("rws"))
102 fdmode
= (FileChannelImpl
.READ
| FileChannelImpl
.WRITE
103 | FileChannelImpl
.SYNC
);
105 else if (mode
.equals("rwd"))
107 fdmode
= (FileChannelImpl
.READ
| FileChannelImpl
.WRITE
108 | FileChannelImpl
.DSYNC
);
111 throw new IllegalArgumentException ("invalid mode: " + mode
);
113 final String fileName
= file
.getPath();
115 // The obligatory SecurityManager stuff
116 SecurityManager s
= System
.getSecurityManager();
119 s
.checkRead(fileName
);
121 if ((fdmode
& FileChannelImpl
.WRITE
) != 0)
122 s
.checkWrite(fileName
);
125 ch
= FileChannelImpl
.create(file
, fdmode
);
126 fd
= new FileDescriptor(ch
);
127 if ((fdmode
& FileChannelImpl
.WRITE
) != 0)
128 out
= new DataOutputStream (new FileOutputStream (fd
));
131 in
= new DataInputStream (new FileInputStream (fd
));
135 * This method initializes a new instance of <code>RandomAccessFile</code>
136 * to read from the specified file name with the specified access mode.
137 * The access mode is either "r" for read only access, "rw" for read
138 * write access, "rws" for synchronized read/write access of both
139 * content and metadata, or "rwd" for read/write access
140 * where only content is required to be synchronous.
142 * Note that a <code>SecurityManager</code> check is made prior to
143 * opening the file to determine whether or not this file is allowed to
144 * be read or written.
146 * @param fileName The name of the file to read and/or write
147 * @param mode "r", "rw", "rws", or "rwd"
149 * @exception IllegalArgumentException If <code>mode</code> has an
151 * @exception SecurityException If the requested access to the file
153 * @exception FileNotFoundException If the file is a directory or
154 * any other error occurs
156 public RandomAccessFile (String fileName
, String mode
)
157 throws FileNotFoundException
159 this (new File(fileName
), mode
);
163 * This method closes the file and frees up all file related system
164 * resources. Since most operating systems put a limit on how many files
165 * may be opened at any given time, it is a good idea to close all files
166 * when no longer needed to avoid hitting this limit
168 public void close () throws IOException
174 * This method returns a <code>FileDescriptor</code> object that
175 * represents the native file handle for this file.
177 * @return The <code>FileDescriptor</code> object for this file
179 * @exception IOException If an error occurs
181 public final FileDescriptor
getFD () throws IOException
186 fd
= new FileDescriptor (ch
);
192 * This method returns the current offset in the file at which the next
193 * read or write will occur
195 * @return The current file position
197 * @exception IOException If an error occurs
199 public long getFilePointer () throws IOException
201 return ch
.position();
205 * This method sets the length of the file to the specified length.
206 * If the currently length of the file is longer than the specified
207 * length, then the file is truncated to the specified length (the
208 * file position is set to the end of file in this case). If the
209 * current length of the file is shorter than the specified length,
210 * the file is extended with bytes of an undefined value (the file
211 * position is unchanged in this case).
213 * The file must be open for write access for this operation to succeed.
215 * @param newLen The new length of the file
217 * @exception IOException If an error occurs
219 public void setLength (long newLen
) throws IOException
221 // FIXME: Extending a file should probably be done by one method call.
223 // FileChannel.truncate() can only shrink a file.
224 // To expand it we need to seek forward and write at least one byte.
225 if (newLen
< length())
226 ch
.truncate (newLen
);
227 else if (newLen
> length())
229 long pos
= getFilePointer();
237 * This method returns the length of the file in bytes
239 * @return The length of the file
241 * @exception IOException If an error occurs
243 public long length () throws IOException
249 * This method reads a single byte of data from the file and returns it
252 * @return The byte read as an int, or -1 if the end of the file was reached.
254 * @exception IOException If an error occurs
256 public int read () throws IOException
262 * This method reads bytes from the file into the specified array. The
263 * bytes are stored starting at the beginning of the array and up to
264 * <code>buf.length</code> bytes can be read.
266 * @param buffer The buffer to read bytes from the file into
268 * @return The actual number of bytes read or -1 if end of file
270 * @exception IOException If an error occurs
272 public int read (byte[] buffer
) throws IOException
274 return in
.read (buffer
);
278 * This methods reads up to <code>len</code> bytes from the file into the
279 * specified array starting at position <code>offset</code> into the array.
281 * @param buffer The array to read the bytes into
282 * @param offset The index into the array to start storing bytes
283 * @param len The requested number of bytes to read
285 * @return The actual number of bytes read, or -1 if end of file
287 * @exception IOException If an error occurs
289 public int read (byte[] buffer
, int offset
, int len
) throws IOException
291 return in
.read (buffer
, offset
, len
);
295 * This method reads a Java boolean value from an input stream. It does
296 * so by reading a single byte of data. If that byte is zero, then the
297 * value returned is <code>false</code> If the byte is non-zero, then
298 * the value returned is <code>true</code>
300 * This method can read a <code>boolean</code> written by an object
302 * <code>writeBoolean()</code> method in the <code>DataOutput</code>
305 * @return The <code>boolean</code> value read
307 * @exception EOFException If end of file is reached before reading the
309 * @exception IOException If any other error occurs
311 public final boolean readBoolean () throws IOException
313 return in
.readBoolean ();
317 * This method reads a Java byte value from an input stream. The value
318 * is in the range of -128 to 127.
320 * This method can read a <code>byte</code> written by an object
322 * <code>writeByte()</code> method in the <code>DataOutput</code> interface.
324 * @return The <code>byte</code> value read
326 * @exception EOFException If end of file is reached before reading the byte
327 * @exception IOException If any other error occurs
331 public final byte readByte () throws IOException
333 return in
.readByte ();
337 * This method reads a Java <code>char</code> value from an input stream.
338 * It operates by reading two bytes from the stream and converting them to
339 * a single 16-bit Java <code>char</code> The two bytes are stored most
340 * significant byte first (i.e., "big endian") regardless of the native
341 * host byte ordering.
343 * As an example, if <code>byte1</code> and <code>byte2</code> represent
345 * and second byte read from the stream respectively, they will be
346 * transformed to a <code>char</code> in the following manner:
348 * <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
350 * This method can read a <code>char</code> written by an object
352 * <code>writeChar()</code> method in the <code>DataOutput</code> interface.
354 * @return The <code>char</code> value read
356 * @exception EOFException If end of file is reached before reading the char
357 * @exception IOException If any other error occurs
361 public final char readChar () throws IOException
363 return in
.readChar();
367 * This method reads a Java double value from an input stream. It operates
368 * by first reading a <code>logn</code> value from the stream by calling the
369 * <code>readLong()</code> method in this interface, then
370 * converts that <code>long</code>
371 * to a <code>double</code> using the <code>longBitsToDouble</code>
372 * method in the class <code>java.lang.Double</code>
374 * This method can read a <code>double</code> written by an object
376 * <code>writeDouble()</code> method in the <code>DataOutput</code>
379 * @return The <code>double</code> value read
381 * @exception EOFException If end of file is reached before reading
383 * @exception IOException If any other error occurs
385 * @see java.lang.Double
388 public final double readDouble () throws IOException
390 return in
.readDouble ();
394 * This method reads a Java float value from an input stream. It operates
395 * by first reading an <code>int</code> value from the stream by calling the
396 * <code>readInt()</code> method in this interface, then converts
397 * that <code>int</code>
398 * to a <code>float</code> using the <code>intBitsToFloat</code> method in
399 * the class <code>java.lang.Float</code>
401 * This method can read a <code>float</code> written by an object
403 * <code>writeFloat()</code> method in the <code>DataOutput</code> interface.
405 * @return The <code>float</code> value read
407 * @exception EOFException If end of file is reached before reading the float
408 * @exception IOException If any other error occurs
410 * @see java.lang.Float
413 public final float readFloat () throws IOException
415 return in
.readFloat();
419 * This method reads raw bytes into the passed array until the array is
420 * full. Note that this method blocks until the data is available and
421 * throws an exception if there is not enough data left in the stream to
424 * @param buffer The buffer into which to read the data
426 * @exception EOFException If end of file is reached before filling the
428 * @exception IOException If any other error occurs
430 public final void readFully (byte[] buffer
) throws IOException
432 in
.readFully(buffer
);
436 * This method reads raw bytes into the passed array <code>buf</code>
438 * <code>offset</code> bytes into the buffer. The number of bytes read
440 * exactly <code>len</code> Note that this method blocks until the data is
441 * available and throws an exception if there is not enough data left in
442 * the stream to read <code>len</code> bytes.
444 * @param buffer The buffer into which to read the data
445 * @param offset The offset into the buffer to start storing data
446 * @param count The number of bytes to read into the buffer
448 * @exception EOFException If end of file is reached before filling
450 * @exception IOException If any other error occurs
452 public final void readFully (byte[] buffer
, int offset
, int count
)
455 in
.readFully (buffer
, offset
, count
);
459 * This method reads a Java <code>int</code> value from an input stream
460 * It operates by reading four bytes from the stream and converting them to
461 * a single Java <code>int</code> The bytes are stored most
462 * significant byte first (i.e., "big endian") regardless of the native
463 * host byte ordering.
465 * As an example, if <code>byte1</code> through <code>byte4</code>
466 * represent the first
467 * four bytes read from the stream, they will be
468 * transformed to an <code>int</code> in the following manner:
470 * <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) +
471 * ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code>
473 * The value returned is in the range of 0 to 65535.
475 * This method can read an <code>int</code> written by an object
477 * <code>writeInt()</code> method in the <code>DataOutput</code> interface.
479 * @return The <code>int</code> value read
481 * @exception EOFException If end of file is reached before reading the int
482 * @exception IOException If any other error occurs
486 public final int readInt () throws IOException
492 * This method reads the next line of text data from an input stream.
493 * It operates by reading bytes and converting those bytes to
495 * values by treating the byte read as the low eight bits of the
497 * and using <code>0</code> as the high eight bits. Because of this, it does
498 * not support the full 16-bit Unicode character set.
500 * The reading of bytes ends when either the end of file or a line terminator
501 * is encountered. The bytes read are then returned as a <code>String</code>
502 * A line terminator is a byte sequence consisting of either
503 * <code>\r</code> <code>\n</code> or <code>\r\n</code> These
504 * termination charaters are
505 * discarded and are not returned as part of the string.
507 * This method can read data that was written by an object implementing the
508 * <code>writeLine()</code> method in <code>DataOutput</code>
510 * @return The line read as a <code>String</code>
512 * @exception IOException If an error occurs
516 public final String
readLine () throws IOException
518 return in
.readLine ();
522 * This method reads a Java long value from an input stream
523 * It operates by reading eight bytes from the stream and converting them to
524 * a single Java <code>long</code> The bytes are stored most
525 * significant byte first (i.e., "big endian") regardless of the native
526 * host byte ordering.
528 * As an example, if <code>byte1</code> through <code>byte8</code>
529 * represent the first
530 * eight bytes read from the stream, they will be
531 * transformed to an <code>long</code> in the following manner:
534 * (long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) +
535 * (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) +
536 * (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) +
537 * (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code>
539 * The value returned is in the range of 0 to 65535.
541 * This method can read an <code>long</code> written by an object
543 * <code>writeLong()</code> method in the <code>DataOutput</code> interface.
545 * @return The <code>long</code> value read
547 * @exception EOFException If end of file is reached before reading the long
548 * @exception IOException If any other error occurs
552 public final long readLong () throws IOException
554 return in
.readLong();
558 * This method reads a signed 16-bit value into a Java in from the stream.
559 * It operates by reading two bytes from the stream and converting them to
560 * a single 16-bit Java <code>short</code> The two bytes are stored most
561 * significant byte first (i.e., "big endian") regardless of the native
562 * host byte ordering.
564 * As an example, if <code>byte1</code> and <code>byte2</code>
565 * represent the first
566 * and second byte read from the stream respectively, they will be
567 * transformed to a <code>short</code> in the following manner:
569 * <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
571 * The value returned is in the range of -32768 to 32767.
573 * This method can read a <code>short</code> written by an object
575 * <code>writeShort()</code> method in the <code>DataOutput</code> interface.
577 * @return The <code>short</code> value read
579 * @exception EOFException If end of file is reached before reading the value
580 * @exception IOException If any other error occurs
584 public final short readShort () throws IOException
586 return in
.readShort();
590 * This method reads 8 unsigned bits into a Java <code>int</code> value
592 * stream. The value returned is in the range of 0 to 255.
594 * This method can read an unsigned byte written by an object implementing
595 * the <code>writeUnsignedByte()</code> method in the
596 * <code>DataOutput</code> interface.
598 * @return The unsigned bytes value read as a Java <code>int</code>
600 * @exception EOFException If end of file is reached before reading the value
601 * @exception IOException If any other error occurs
605 public final int readUnsignedByte () throws IOException
607 return in
.readUnsignedByte();
611 * This method reads 16 unsigned bits into a Java int value from the stream.
612 * It operates by reading two bytes from the stream and converting them to
613 * a single Java <code>int</code> The two bytes are stored most
614 * significant byte first (i.e., "big endian") regardless of the native
615 * host byte ordering.
617 * As an example, if <code>byte1</code> and <code>byte2</code>
618 * represent the first
619 * and second byte read from the stream respectively, they will be
620 * transformed to an <code>int</code> in the following manner:
622 * <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code>
624 * The value returned is in the range of 0 to 65535.
626 * This method can read an unsigned short written by an object implementing
627 * the <code>writeUnsignedShort()</code> method in the
628 * <code>DataOutput</code> interface.
630 * @return The unsigned short value read as a Java <code>int</code>
632 * @exception EOFException If end of file is reached before reading the value
633 * @exception IOException If any other error occurs
635 public final int readUnsignedShort () throws IOException
637 return in
.readUnsignedShort();
641 * This method reads a <code>String</code> from an input stream that
643 * a modified UTF-8 format. This format has a leading two byte sequence
644 * that contains the remaining number of bytes to read. This two byte
645 * sequence is read using the <code>readUnsignedShort()</code> method of this
648 * After the number of remaining bytes have been determined, these bytes
649 * are read an transformed into <code>char</code> values.
650 * These <code>char</code> values
651 * are encoded in the stream using either a one, two, or three byte format.
652 * The particular format in use can be determined by examining the first
655 * If the first byte has a high order bit of 0 then
656 * that character consists on only one byte. This character value consists
657 * of seven bits that are at positions 0 through 6 of the byte. As an
658 * example, if <code>byte1</code> is the byte read from the stream, it would
659 * be converted to a <code>char</code> like so:
661 * <code>(char)byte1</code>
663 * If the first byte has <code>110</code> as its high order bits, then the
664 * character consists of two bytes. The bits that make up the character
665 * value are in positions 0 through 4 of the first byte and bit positions
666 * 0 through 5 of the second byte. (The second byte should have
667 * 10 as its high order bits). These values are in most significant
668 * byte first (i.e., "big endian") order.
670 * As an example, if <code>byte1</code> and <code>byte2</code>
671 * are the first two bytes
672 * read respectively, and the high order bits of them match the patterns
673 * which indicate a two byte character encoding, then they would be
674 * converted to a Java <code>char</code> like so:
676 * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
678 * If the first byte has a <code>1110</code> as its high order bits, then the
679 * character consists of three bytes. The bits that make up the character
680 * value are in positions 0 through 3 of the first byte and bit positions
681 * 0 through 5 of the other two bytes. (The second and third bytes should
682 * have <code>10</code> as their high order bits). These values are in most
683 * significant byte first (i.e., "big endian") order.
685 * As an example, if <code>byte1</code> <code>byte2</code>
686 * and <code>byte3</code> are the
687 * three bytes read, and the high order bits of them match the patterns
688 * which indicate a three byte character encoding, then they would be
689 * converted to a Java <code>char</code> like so:
691 * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) |
692 * (byte3 & 0x3F))</code>
694 * Note that all characters are encoded in the method that requires the
695 * fewest number of bytes with the exception of the character with the
696 * value of <code>\u0000</code> which is encoded as two bytes. This is
697 * a modification of the UTF standard used to prevent C language style
698 * <code>NUL</code> values from appearing in the byte stream.
700 * This method can read data that was written by an object implementing the
701 * <code>writeUTF()</code> method in <code>DataOutput</code>
703 * @return The <code>String</code> read
705 * @exception EOFException If end of file is reached before reading the
707 * @exception UTFDataFormatException If the data is not in UTF-8 format
708 * @exception IOException If any other error occurs
712 public final String
readUTF () throws IOException
718 * This method sets the current file position to the specified offset
719 * from the beginning of the file. Note that some operating systems will
720 * allow the file pointer to be set past the current end of the file.
722 * @param pos The offset from the beginning of the file at which to set
725 * @exception IOException If an error occurs
727 public void seek (long pos
) throws IOException
733 * This method attempts to skip and discard the specified number of bytes
734 * in the input stream. It may actually skip fewer bytes than requested.
735 * The actual number of bytes skipped is returned. This method will not
736 * skip any bytes if passed a negative number of bytes to skip.
738 * @param numBytes The requested number of bytes to skip.
740 * @return The number of bytes actually skipped.
742 * @exception IOException If an error occurs.
744 public int skipBytes (int numBytes
) throws IOException
747 throw new IllegalArgumentException ("Can't skip negative bytes: " +
753 long oldPos
= ch
.position();
754 long newPos
= oldPos
+ numBytes
;
755 long size
= ch
.size();
759 return (int) (ch
.position() - oldPos
);
763 * This method writes a single byte of data to the file. The file must
764 * be open for read-write in order for this operation to succeed.
766 * @param oneByte The byte of data to write, passed as an int.
768 * @exception IOException If an error occurs
770 public void write (int oneByte
) throws IOException
773 throw new IOException("Bad file descriptor");
779 * This method writes all the bytes in the specified array to the file.
780 * The file must be open read-write in order for this operation to succeed.
782 * @param buffer The array of bytes to write to the file
784 public void write (byte[] buffer
) throws IOException
787 throw new IOException("Bad file descriptor");
793 * This method writes <code>len</code> bytes to the file from the specified
794 * array starting at index <code>offset</code> into the array.
796 * @param buffer The array of bytes to write to the file
797 * @param offset The index into the array to start writing file
798 * @param len The number of bytes to write
800 * @exception IOException If an error occurs
802 public void write (byte[] buffer
, int offset
, int len
) throws IOException
805 throw new IOException("Bad file descriptor");
807 out
.write (buffer
, offset
, len
);
811 * This method writes a Java <code>boolean</code> to the underlying output
812 * stream. For a value of <code>true</code>, 1 is written to the stream.
813 * For a value of <code>false</code>, 0 is written.
815 * @param val The <code>boolean</code> value to write to the stream
817 * @exception IOException If an error occurs
819 public final void writeBoolean (boolean val
) throws IOException
822 throw new IOException("Bad file descriptor");
824 out
.writeBoolean(val
);
828 * This method writes a Java <code>byte</code> value to the underlying
831 * @param val The <code>byte</code> to write to the stream, passed
832 * as an <code>int</code>.
834 * @exception IOException If an error occurs
836 public final void writeByte (int val
) throws IOException
839 throw new IOException("Bad file descriptor");
845 * This method writes a Java <code>short</code> to the stream, high byte
846 * first. This method requires two bytes to encode the value.
848 * @param val The <code>short</code> value to write to the stream,
849 * passed as an <code>int</code>.
851 * @exception IOException If an error occurs
853 public final void writeShort (int val
) throws IOException
856 throw new IOException("Bad file descriptor");
862 * This method writes a single <code>char</code> value to the stream,
865 * @param val The <code>char</code> value to write, passed as
866 * an <code>int</code>.
868 * @exception IOException If an error occurs
870 public final void writeChar (int val
) throws IOException
873 throw new IOException("Bad file descriptor");
879 * This method writes a Java <code>int</code> to the stream, high bytes
880 * first. This method requires four bytes to encode the value.
882 * @param val The <code>int</code> value to write to the stream.
884 * @exception IOException If an error occurs
886 public final void writeInt (int val
) throws IOException
889 throw new IOException("Bad file descriptor");
895 * This method writes a Java <code>long</code> to the stream, high bytes
896 * first. This method requires eight bytes to encode the value.
898 * @param val The <code>long</code> value to write to the stream.
900 * @exception IOException If an error occurs
902 public final void writeLong (long val
) throws IOException
905 throw new IOException("Bad file descriptor");
911 * This method writes a Java <code>float</code> value to the stream. This
912 * value is written by first calling the method
913 * <code>Float.floatToIntBits</code>
914 * to retrieve an <code>int</code> representing the floating point number,
915 * then writing this <code>int</code> value to the stream exactly the same
916 * as the <code>writeInt()</code> method does.
918 * @param val The floating point number to write to the stream.
920 * @exception IOException If an error occurs
922 * @see #writeInt(int)
924 public final void writeFloat (float val
) throws IOException
927 throw new IOException("Bad file descriptor");
933 * This method writes a Java <code>double</code> value to the stream. This
934 * value is written by first calling the method
935 * <code>Double.doubleToLongBits</code>
936 * to retrieve an <code>long</code> representing the floating point number,
937 * then writing this <code>long</code> value to the stream exactly the same
938 * as the <code>writeLong()</code> method does.
940 * @param val The double precision floating point number to write to the
943 * @exception IOException If an error occurs
945 * @see #writeLong(long)
947 public final void writeDouble (double val
) throws IOException
950 throw new IOException("Bad file descriptor");
952 out
.writeDouble(val
);
956 * This method writes all the bytes in a <code>String</code> out to the
957 * stream. One byte is written for each character in the <code>String</code>.
958 * The high eight bits of each character are discarded.
960 * @param val The <code>String</code> to write to the stream
962 * @exception IOException If an error occurs
964 public final void writeBytes (String val
) throws IOException
967 throw new IOException("Bad file descriptor");
973 * This method writes all the characters in a <code>String</code> to the
974 * stream. There will be two bytes for each character value. The high
975 * byte of the character will be written first.
977 * @param val The <code>String</code> to write to the stream.
979 * @exception IOException If an error occurs
981 public final void writeChars (String val
) throws IOException
984 throw new IOException("Bad file descriptor");
990 * This method writes a Java <code>String</code> to the stream in a modified
991 * UTF-8 format. First, two bytes are written to the stream indicating the
992 * number of bytes to follow. Note that this is the number of bytes in the
993 * encoded <code>String</code> not the <code>String</code> length. Next
994 * come the encoded characters. Each character in the <code>String</code>
995 * is encoded as either one, two or three bytes. For characters in the
996 * range of <code>\u0001</code> to <code>\u007F</code>,
997 * one byte is used. The character
998 * value goes into bits 0-7 and bit eight is 0. For characters in the range
999 * of <code>\u0080</code> to <code>\u007FF</code>, two
1000 * bytes are used. Bits
1001 * 6-10 of the character value are encoded bits 0-4 of the first byte, with
1002 * the high bytes having a value of "110". Bits 0-5 of the character value
1003 * are stored in bits 0-5 of the second byte, with the high bits set to
1004 * "10". This type of encoding is also done for the null character
1005 * <code>\u0000</code>. This eliminates any C style NUL character values
1006 * in the output. All remaining characters are stored as three bytes.
1007 * Bits 12-15 of the character value are stored in bits 0-3 of the first
1008 * byte. The high bits of the first bytes are set to "1110". Bits 6-11
1009 * of the character value are stored in bits 0-5 of the second byte. The
1010 * high bits of the second byte are set to "10". And bits 0-5 of the
1011 * character value are stored in bits 0-5 of byte three, with the high bits
1012 * of that byte set to "10".
1014 * @param val The <code>String</code> to write to the output in UTF format
1016 * @exception IOException If an error occurs
1018 public final void writeUTF (String val
) throws IOException
1021 throw new IOException("Bad file descriptor");
1027 * This method creates a java.nio.channels.FileChannel.
1028 * Nio does not allow one to create a file channel directly.
1029 * A file channel must be created by first creating an instance of
1030 * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
1032 public final synchronized FileChannel
getChannel ()