Fix a bug that broke -freorder-functions
[official-gcc.git] / libjava / classpath / java / io / RandomAccessFile.java
blobda0c81272ec178b8164aba25699f3eae48fa53a5
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)
9 any later version.
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
19 02110-1301 USA.
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.
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. */
39 package java.io;
41 import gnu.java.nio.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.
50 /**
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;
72 /**
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.
77 * <p>
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
80 * be read or written.
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
86 * illegal value
87 * @exception SecurityException If the requested access to the file
88 * is not allowed
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
95 int fdmode;
96 if (mode.equals("r"))
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);
110 else
111 throw new IllegalArgumentException ("invalid mode: " + mode);
113 final String fileName = file.getPath();
115 // The obligatory SecurityManager stuff
116 SecurityManager s = System.getSecurityManager();
117 if (s != null)
119 s.checkRead(fileName);
121 if ((fdmode & FileChannelImpl.WRITE) != 0)
122 s.checkWrite(fileName);
127 ch = FileChannelImpl.create(file, fdmode);
129 catch (FileNotFoundException fnfe)
131 throw fnfe;
133 catch (IOException ioe)
135 FileNotFoundException fnfe = new FileNotFoundException(file.getPath());
136 fnfe.initCause(ioe);
137 throw fnfe;
139 fd = new FileDescriptor(ch);
140 if ((fdmode & FileChannelImpl.WRITE) != 0)
141 out = new DataOutputStream (new FileOutputStream (fd));
142 else
143 out = null;
144 in = new DataInputStream (new FileInputStream (fd));
148 * This method initializes a new instance of <code>RandomAccessFile</code>
149 * to read from the specified file name with the specified access mode.
150 * The access mode is either "r" for read only access, "rw" for read
151 * write access, "rws" for synchronized read/write access of both
152 * content and metadata, or "rwd" for read/write access
153 * where only content is required to be synchronous.
154 * <p>
155 * Note that a <code>SecurityManager</code> check is made prior to
156 * opening the file to determine whether or not this file is allowed to
157 * be read or written.
159 * @param fileName The name of the file to read and/or write
160 * @param mode "r", "rw", "rws", or "rwd"
162 * @exception IllegalArgumentException If <code>mode</code> has an
163 * illegal value
164 * @exception SecurityException If the requested access to the file
165 * is not allowed
166 * @exception FileNotFoundException If the file is a directory or
167 * any other error occurs
169 public RandomAccessFile (String fileName, String mode)
170 throws FileNotFoundException
172 this (new File(fileName), mode);
176 * This method closes the file and frees up all file related system
177 * resources. Since most operating systems put a limit on how many files
178 * may be opened at any given time, it is a good idea to close all files
179 * when no longer needed to avoid hitting this limit
181 public void close () throws IOException
183 ch.close();
187 * This method returns a <code>FileDescriptor</code> object that
188 * represents the native file handle for this file.
190 * @return The <code>FileDescriptor</code> object for this file
192 * @exception IOException If an error occurs
194 public final FileDescriptor getFD () throws IOException
196 synchronized (this)
198 if (fd == null)
199 fd = new FileDescriptor (ch);
200 return fd;
205 * This method returns the current offset in the file at which the next
206 * read or write will occur
208 * @return The current file position
210 * @exception IOException If an error occurs
212 public long getFilePointer () throws IOException
214 return ch.position();
218 * This method sets the length of the file to the specified length.
219 * If the currently length of the file is longer than the specified
220 * length, then the file is truncated to the specified length (the
221 * file position is set to the end of file in this case). If the
222 * current length of the file is shorter than the specified length,
223 * the file is extended with bytes of an undefined value (the file
224 * position is unchanged in this case).
225 * <p>
226 * The file must be open for write access for this operation to succeed.
228 * @param newLen The new length of the file
230 * @exception IOException If an error occurs
232 public void setLength (long newLen) throws IOException
234 // FIXME: Extending a file should probably be done by one method call.
236 // FileChannel.truncate() can only shrink a file.
237 // To expand it we need to seek forward and write at least one byte.
238 if (newLen < length())
239 ch.truncate (newLen);
240 else if (newLen > length())
242 long pos = getFilePointer();
243 seek(newLen - 1);
244 write(0);
245 seek(pos);
250 * This method returns the length of the file in bytes
252 * @return The length of the file
254 * @exception IOException If an error occurs
256 public long length () throws IOException
258 return ch.size();
262 * This method reads a single byte of data from the file and returns it
263 * as an integer.
265 * @return The byte read as an int, or -1 if the end of the file was reached.
267 * @exception IOException If an error occurs
269 public int read () throws IOException
271 return in.read();
275 * This method reads bytes from the file into the specified array. The
276 * bytes are stored starting at the beginning of the array and up to
277 * <code>buf.length</code> bytes can be read.
279 * @param buffer The buffer to read bytes from the file into
281 * @return The actual number of bytes read or -1 if end of file
283 * @exception IOException If an error occurs
285 public int read (byte[] buffer) throws IOException
287 return in.read (buffer);
291 * This methods reads up to <code>len</code> bytes from the file into the
292 * specified array starting at position <code>offset</code> into the array.
294 * @param buffer The array to read the bytes into
295 * @param offset The index into the array to start storing bytes
296 * @param len The requested number of bytes to read
298 * @return The actual number of bytes read, or -1 if end of file
300 * @exception IOException If an error occurs
302 public int read (byte[] buffer, int offset, int len) throws IOException
304 return in.read (buffer, offset, len);
308 * This method reads a Java boolean value from an input stream. It does
309 * so by reading a single byte of data. If that byte is zero, then the
310 * value returned is <code>false</code> If the byte is non-zero, then
311 * the value returned is <code>true</code>
312 * <p>
313 * This method can read a <code>boolean</code> written by an object
314 * implementing the
315 * <code>writeBoolean()</code> method in the <code>DataOutput</code>
316 * interface.
318 * @return The <code>boolean</code> value read
320 * @exception EOFException If end of file is reached before reading the
321 * boolean
322 * @exception IOException If any other error occurs
324 public final boolean readBoolean () throws IOException
326 return in.readBoolean ();
330 * This method reads a Java byte value from an input stream. The value
331 * is in the range of -128 to 127.
332 * <p>
333 * This method can read a <code>byte</code> written by an object
334 * implementing the
335 * <code>writeByte()</code> method in the <code>DataOutput</code> interface.
337 * @return The <code>byte</code> value read
339 * @exception EOFException If end of file is reached before reading the byte
340 * @exception IOException If any other error occurs
342 * @see DataOutput
344 public final byte readByte () throws IOException
346 return in.readByte ();
350 * This method reads a Java <code>char</code> value from an input stream.
351 * It operates by reading two bytes from the stream and converting them to
352 * a single 16-bit Java <code>char</code> The two bytes are stored most
353 * significant byte first (i.e., "big endian") regardless of the native
354 * host byte ordering.
355 * <p>
356 * As an example, if <code>byte1</code> and <code>byte2</code> represent
357 * the first
358 * and second byte read from the stream respectively, they will be
359 * transformed to a <code>char</code> in the following manner:
360 * <p>
361 * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
362 * <p>
363 * This method can read a <code>char</code> written by an object
364 * implementing the
365 * <code>writeChar()</code> method in the <code>DataOutput</code> interface.
367 * @return The <code>char</code> value read
369 * @exception EOFException If end of file is reached before reading the char
370 * @exception IOException If any other error occurs
372 * @see DataOutput
374 public final char readChar () throws IOException
376 return in.readChar();
380 * This method reads a Java double value from an input stream. It operates
381 * by first reading a <code>logn</code> value from the stream by calling the
382 * <code>readLong()</code> method in this interface, then
383 * converts that <code>long</code>
384 * to a <code>double</code> using the <code>longBitsToDouble</code>
385 * method in the class <code>java.lang.Double</code>
386 * <p>
387 * This method can read a <code>double</code> written by an object
388 * implementing the
389 * <code>writeDouble()</code> method in the <code>DataOutput</code>
390 * interface.
392 * @return The <code>double</code> value read
394 * @exception EOFException If end of file is reached before reading
395 * the double
396 * @exception IOException If any other error occurs
398 * @see java.lang.Double
399 * @see DataOutput
401 public final double readDouble () throws IOException
403 return in.readDouble ();
407 * This method reads a Java float value from an input stream. It operates
408 * by first reading an <code>int</code> value from the stream by calling the
409 * <code>readInt()</code> method in this interface, then converts
410 * that <code>int</code>
411 * to a <code>float</code> using the <code>intBitsToFloat</code> method in
412 * the class <code>java.lang.Float</code>
413 * <p>
414 * This method can read a <code>float</code> written by an object
415 * implementing the
416 * <code>writeFloat()</code> method in the <code>DataOutput</code> interface.
418 * @return The <code>float</code> value read
420 * @exception EOFException If end of file is reached before reading the float
421 * @exception IOException If any other error occurs
423 * @see java.lang.Float
424 * @see DataOutput
426 public final float readFloat () throws IOException
428 return in.readFloat();
432 * This method reads raw bytes into the passed array until the array is
433 * full. Note that this method blocks until the data is available and
434 * throws an exception if there is not enough data left in the stream to
435 * fill the buffer
437 * @param buffer The buffer into which to read the data
439 * @exception EOFException If end of file is reached before filling the
440 * buffer
441 * @exception IOException If any other error occurs
443 public final void readFully (byte[] buffer) throws IOException
445 in.readFully(buffer);
449 * This method reads raw bytes into the passed array <code>buf</code>
450 * starting
451 * <code>offset</code> bytes into the buffer. The number of bytes read
452 * will be
453 * exactly <code>len</code> Note that this method blocks until the data is
454 * available and throws an exception if there is not enough data left in
455 * the stream to read <code>len</code> bytes.
457 * @param buffer The buffer into which to read the data
458 * @param offset The offset into the buffer to start storing data
459 * @param count The number of bytes to read into the buffer
461 * @exception EOFException If end of file is reached before filling
462 * the buffer
463 * @exception IOException If any other error occurs
465 public final void readFully (byte[] buffer, int offset, int count)
466 throws IOException
468 in.readFully (buffer, offset, count);
472 * This method reads a Java <code>int</code> value from an input stream
473 * It operates by reading four bytes from the stream and converting them to
474 * a single Java <code>int</code> The bytes are stored most
475 * significant byte first (i.e., "big endian") regardless of the native
476 * host byte ordering.
477 * <p>
478 * As an example, if <code>byte1</code> through <code>byte4</code>
479 * represent the first
480 * four bytes read from the stream, they will be
481 * transformed to an <code>int</code> in the following manner:
482 * <p>
483 * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
484 * ((byte3 &amp; 0xFF) &lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
485 * <p>
486 * The value returned is in the range of 0 to 65535.
487 * <p>
488 * This method can read an <code>int</code> written by an object
489 * implementing the
490 * <code>writeInt()</code> method in the <code>DataOutput</code> interface.
492 * @return The <code>int</code> value read
494 * @exception EOFException If end of file is reached before reading the int
495 * @exception IOException If any other error occurs
497 * @see DataOutput
499 public final int readInt () throws IOException
501 return in.readInt();
505 * This method reads the next line of text data from an input stream.
506 * It operates by reading bytes and converting those bytes to
507 * <code>char</code>
508 * values by treating the byte read as the low eight bits of the
509 * <code>char</code>
510 * and using <code>0</code> as the high eight bits. Because of this, it does
511 * not support the full 16-bit Unicode character set.
512 * <p>
513 * The reading of bytes ends when either the end of file or a line terminator
514 * is encountered. The bytes read are then returned as a <code>String</code>
515 * A line terminator is a byte sequence consisting of either
516 * <code>\r</code> <code>\n</code> or <code>\r\n</code> These
517 * termination charaters are
518 * discarded and are not returned as part of the string.
519 * <p>
520 * This method can read data that was written by an object implementing the
521 * <code>writeLine()</code> method in <code>DataOutput</code>
523 * @return The line read as a <code>String</code>
525 * @exception IOException If an error occurs
527 * @see DataOutput
529 public final String readLine () throws IOException
531 return in.readLine ();
535 * This method reads a Java long value from an input stream
536 * It operates by reading eight bytes from the stream and converting them to
537 * a single Java <code>long</code> The bytes are stored most
538 * significant byte first (i.e., "big endian") regardless of the native
539 * host byte ordering.
540 * <p>
541 * As an example, if <code>byte1</code> through <code>byte8</code>
542 * represent the first
543 * eight bytes read from the stream, they will be
544 * transformed to an <code>long</code> in the following manner:
545 * <p>
546 * <code>
547 * (long)((((long)byte1 &amp; 0xFF) &lt;&lt; 56) + (((long)byte2 &amp; 0xFF) &lt;&lt; 48) +
548 * (((long)byte3 &amp; 0xFF) &lt;&lt; 40) + (((long)byte4 &amp; 0xFF) &lt;&lt; 32) +
549 * (((long)byte5 &amp; 0xFF) &lt;&lt; 24) + (((long)byte6 &amp; 0xFF) &lt;&lt; 16) +
550 * (((long)byte7 &amp; 0xFF) &lt;&lt; 8) + ((long)byte9 &amp; 0xFF)))</code>
551 * <p>
552 * The value returned is in the range of 0 to 65535.
553 * <p>
554 * This method can read an <code>long</code> written by an object
555 * implementing the
556 * <code>writeLong()</code> method in the <code>DataOutput</code> interface.
558 * @return The <code>long</code> value read
560 * @exception EOFException If end of file is reached before reading the long
561 * @exception IOException If any other error occurs
563 * @see DataOutput
565 public final long readLong () throws IOException
567 return in.readLong();
571 * This method reads a signed 16-bit value into a Java in from the stream.
572 * It operates by reading two bytes from the stream and converting them to
573 * a single 16-bit Java <code>short</code> The two bytes are stored most
574 * significant byte first (i.e., "big endian") regardless of the native
575 * host byte ordering.
576 * <p>
577 * As an example, if <code>byte1</code> and <code>byte2</code>
578 * represent the first
579 * and second byte read from the stream respectively, they will be
580 * transformed to a <code>short</code> in the following manner:
581 * <p>
582 * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
583 * <p>
584 * The value returned is in the range of -32768 to 32767.
585 * <p>
586 * This method can read a <code>short</code> written by an object
587 * implementing the
588 * <code>writeShort()</code> method in the <code>DataOutput</code> interface.
590 * @return The <code>short</code> value read
592 * @exception EOFException If end of file is reached before reading the value
593 * @exception IOException If any other error occurs
595 * @see DataOutput
597 public final short readShort () throws IOException
599 return in.readShort();
603 * This method reads 8 unsigned bits into a Java <code>int</code> value
604 * from the
605 * stream. The value returned is in the range of 0 to 255.
606 * <p>
607 * This method can read an unsigned byte written by an object implementing
608 * the <code>writeUnsignedByte()</code> method in the
609 * <code>DataOutput</code> interface.
611 * @return The unsigned bytes value read as a Java <code>int</code>
613 * @exception EOFException If end of file is reached before reading the value
614 * @exception IOException If any other error occurs
616 * @see DataOutput
618 public final int readUnsignedByte () throws IOException
620 return in.readUnsignedByte();
624 * This method reads 16 unsigned bits into a Java int value from the stream.
625 * It operates by reading two bytes from the stream and converting them to
626 * a single Java <code>int</code> The two bytes are stored most
627 * significant byte first (i.e., "big endian") regardless of the native
628 * host byte ordering.
629 * <p>
630 * As an example, if <code>byte1</code> and <code>byte2</code>
631 * represent the first
632 * and second byte read from the stream respectively, they will be
633 * transformed to an <code>int</code> in the following manner:
634 * <p>
635 * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
636 * <p>
637 * The value returned is in the range of 0 to 65535.
638 * <p>
639 * This method can read an unsigned short written by an object implementing
640 * the <code>writeUnsignedShort()</code> method in the
641 * <code>DataOutput</code> interface.
643 * @return The unsigned short value read as a Java <code>int</code>
645 * @exception EOFException If end of file is reached before reading the value
646 * @exception IOException If any other error occurs
648 public final int readUnsignedShort () throws IOException
650 return in.readUnsignedShort();
654 * This method reads a <code>String</code> from an input stream that
655 * is encoded in
656 * a modified UTF-8 format. This format has a leading two byte sequence
657 * that contains the remaining number of bytes to read. This two byte
658 * sequence is read using the <code>readUnsignedShort()</code> method of this
659 * interface.
660 * <p>
661 * After the number of remaining bytes have been determined, these bytes
662 * are read an transformed into <code>char</code> values.
663 * These <code>char</code> values
664 * are encoded in the stream using either a one, two, or three byte format.
665 * The particular format in use can be determined by examining the first
666 * byte read.
667 * <p>
668 * If the first byte has a high order bit of 0 then
669 * that character consists on only one byte. This character value consists
670 * of seven bits that are at positions 0 through 6 of the byte. As an
671 * example, if <code>byte1</code> is the byte read from the stream, it would
672 * be converted to a <code>char</code> like so:
673 * <p>
674 * <code>(char)byte1</code>
675 * <p>
676 * If the first byte has <code>110</code> as its high order bits, then the
677 * character consists of two bytes. The bits that make up the character
678 * value are in positions 0 through 4 of the first byte and bit positions
679 * 0 through 5 of the second byte. (The second byte should have
680 * 10 as its high order bits). These values are in most significant
681 * byte first (i.e., "big endian") order.
682 * <p>
683 * As an example, if <code>byte1</code> and <code>byte2</code>
684 * are the first two bytes
685 * read respectively, and the high order bits of them match the patterns
686 * which indicate a two byte character encoding, then they would be
687 * converted to a Java <code>char</code> like so:
688 * <p>
689 * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
690 * <p>
691 * If the first byte has a <code>1110</code> as its high order bits, then the
692 * character consists of three bytes. The bits that make up the character
693 * value are in positions 0 through 3 of the first byte and bit positions
694 * 0 through 5 of the other two bytes. (The second and third bytes should
695 * have <code>10</code> as their high order bits). These values are in most
696 * significant byte first (i.e., "big endian") order.
697 * <p>
698 * As an example, if <code>byte1</code> <code>byte2</code>
699 * and <code>byte3</code> are the
700 * three bytes read, and the high order bits of them match the patterns
701 * which indicate a three byte character encoding, then they would be
702 * converted to a Java <code>char</code> like so:
703 * <p>
704 * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) |
705 * (byte3 & 0x3F))</code>
706 * <p>
707 * Note that all characters are encoded in the method that requires the
708 * fewest number of bytes with the exception of the character with the
709 * value of <code>&#92;u0000</code> which is encoded as two bytes. This is
710 * a modification of the UTF standard used to prevent C language style
711 * <code>NUL</code> values from appearing in the byte stream.
712 * <p>
713 * This method can read data that was written by an object implementing the
714 * <code>writeUTF()</code> method in <code>DataOutput</code>
716 * @return The <code>String</code> read
718 * @exception EOFException If end of file is reached before reading the
719 * String
720 * @exception UTFDataFormatException If the data is not in UTF-8 format
721 * @exception IOException If any other error occurs
723 * @see DataOutput
725 public final String readUTF () throws IOException
727 return in.readUTF();
731 * This method sets the current file position to the specified offset
732 * from the beginning of the file. Note that some operating systems will
733 * allow the file pointer to be set past the current end of the file.
735 * @param pos The offset from the beginning of the file at which to set
736 * the file pointer
738 * @exception IOException If an error occurs
740 public void seek (long pos) throws IOException
742 ch.position(pos);
746 * This method attempts to skip and discard the specified number of bytes
747 * in the input stream. It may actually skip fewer bytes than requested.
748 * The actual number of bytes skipped is returned. This method will not
749 * skip any bytes if passed a negative number of bytes to skip.
751 * @param numBytes The requested number of bytes to skip.
753 * @return The number of bytes actually skipped.
755 * @exception IOException If an error occurs.
757 public int skipBytes (int numBytes) throws IOException
759 if (numBytes < 0)
760 throw new IllegalArgumentException ("Can't skip negative bytes: " +
761 numBytes);
763 if (numBytes == 0)
764 return 0;
766 long oldPos = ch.position();
767 long newPos = oldPos + numBytes;
768 long size = ch.size();
769 if (newPos > size)
770 newPos = size;
771 ch.position(newPos);
772 return (int) (ch.position() - oldPos);
776 * This method writes a single byte of data to the file. The file must
777 * be open for read-write in order for this operation to succeed.
779 * @param oneByte The byte of data to write, passed as an int.
781 * @exception IOException If an error occurs
783 public void write (int oneByte) throws IOException
785 if (out == null)
786 throw new IOException("Bad file descriptor");
788 out.write(oneByte);
792 * This method writes all the bytes in the specified array to the file.
793 * The file must be open read-write in order for this operation to succeed.
795 * @param buffer The array of bytes to write to the file
797 public void write (byte[] buffer) throws IOException
799 if (out == null)
800 throw new IOException("Bad file descriptor");
802 out.write(buffer);
806 * This method writes <code>len</code> bytes to the file from the specified
807 * array starting at index <code>offset</code> into the array.
809 * @param buffer The array of bytes to write to the file
810 * @param offset The index into the array to start writing file
811 * @param len The number of bytes to write
813 * @exception IOException If an error occurs
815 public void write (byte[] buffer, int offset, int len) throws IOException
817 if (out == null)
818 throw new IOException("Bad file descriptor");
820 out.write (buffer, offset, len);
824 * This method writes a Java <code>boolean</code> to the underlying output
825 * stream. For a value of <code>true</code>, 1 is written to the stream.
826 * For a value of <code>false</code>, 0 is written.
828 * @param val The <code>boolean</code> value to write to the stream
830 * @exception IOException If an error occurs
832 public final void writeBoolean (boolean val) throws IOException
834 if (out == null)
835 throw new IOException("Bad file descriptor");
837 out.writeBoolean(val);
841 * This method writes a Java <code>byte</code> value to the underlying
842 * output stream.
844 * @param val The <code>byte</code> to write to the stream, passed
845 * as an <code>int</code>.
847 * @exception IOException If an error occurs
849 public final void writeByte (int val) throws IOException
851 if (out == null)
852 throw new IOException("Bad file descriptor");
854 out.writeByte(val);
858 * This method writes a Java <code>short</code> to the stream, high byte
859 * first. This method requires two bytes to encode the value.
861 * @param val The <code>short</code> value to write to the stream,
862 * passed as an <code>int</code>.
864 * @exception IOException If an error occurs
866 public final void writeShort (int val) throws IOException
868 if (out == null)
869 throw new IOException("Bad file descriptor");
871 out.writeShort(val);
875 * This method writes a single <code>char</code> value to the stream,
876 * high byte first.
878 * @param val The <code>char</code> value to write, passed as
879 * an <code>int</code>.
881 * @exception IOException If an error occurs
883 public final void writeChar (int val) throws IOException
885 if (out == null)
886 throw new IOException("Bad file descriptor");
888 out.writeChar(val);
892 * This method writes a Java <code>int</code> to the stream, high bytes
893 * first. This method requires four bytes to encode the value.
895 * @param val The <code>int</code> value to write to the stream.
897 * @exception IOException If an error occurs
899 public final void writeInt (int val) throws IOException
901 if (out == null)
902 throw new IOException("Bad file descriptor");
904 out.writeInt(val);
908 * This method writes a Java <code>long</code> to the stream, high bytes
909 * first. This method requires eight bytes to encode the value.
911 * @param val The <code>long</code> value to write to the stream.
913 * @exception IOException If an error occurs
915 public final void writeLong (long val) throws IOException
917 if (out == null)
918 throw new IOException("Bad file descriptor");
920 out.writeLong(val);
924 * This method writes a Java <code>float</code> value to the stream. This
925 * value is written by first calling the method
926 * <code>Float.floatToIntBits</code>
927 * to retrieve an <code>int</code> representing the floating point number,
928 * then writing this <code>int</code> value to the stream exactly the same
929 * as the <code>writeInt()</code> method does.
931 * @param val The floating point number to write to the stream.
933 * @exception IOException If an error occurs
935 * @see #writeInt(int)
937 public final void writeFloat (float val) throws IOException
939 if (out == null)
940 throw new IOException("Bad file descriptor");
942 out.writeFloat(val);
946 * This method writes a Java <code>double</code> value to the stream. This
947 * value is written by first calling the method
948 * <code>Double.doubleToLongBits</code>
949 * to retrieve an <code>long</code> representing the floating point number,
950 * then writing this <code>long</code> value to the stream exactly the same
951 * as the <code>writeLong()</code> method does.
953 * @param val The double precision floating point number to write to the
954 * stream.
956 * @exception IOException If an error occurs
958 * @see #writeLong(long)
960 public final void writeDouble (double val) throws IOException
962 if (out == null)
963 throw new IOException("Bad file descriptor");
965 out.writeDouble(val);
969 * This method writes all the bytes in a <code>String</code> out to the
970 * stream. One byte is written for each character in the <code>String</code>.
971 * The high eight bits of each character are discarded.
973 * @param val The <code>String</code> to write to the stream
975 * @exception IOException If an error occurs
977 public final void writeBytes (String val) throws IOException
979 if (out == null)
980 throw new IOException("Bad file descriptor");
982 out.writeBytes(val);
986 * This method writes all the characters in a <code>String</code> to the
987 * stream. There will be two bytes for each character value. The high
988 * byte of the character will be written first.
990 * @param val The <code>String</code> to write to the stream.
992 * @exception IOException If an error occurs
994 public final void writeChars (String val) throws IOException
996 if (out == null)
997 throw new IOException("Bad file descriptor");
999 out.writeChars(val);
1003 * This method writes a Java <code>String</code> to the stream in a modified
1004 * UTF-8 format. First, two bytes are written to the stream indicating the
1005 * number of bytes to follow. Note that this is the number of bytes in the
1006 * encoded <code>String</code> not the <code>String</code> length. Next
1007 * come the encoded characters. Each character in the <code>String</code>
1008 * is encoded as either one, two or three bytes. For characters in the
1009 * range of <code>&#92;u0001</code> to <code>&#92;u007F</code>,
1010 * one byte is used. The character
1011 * value goes into bits 0-7 and bit eight is 0. For characters in the range
1012 * of <code>&#92;u0080</code> to <code>&#92;u007FF</code>, two
1013 * bytes are used. Bits
1014 * 6-10 of the character value are encoded bits 0-4 of the first byte, with
1015 * the high bytes having a value of "110". Bits 0-5 of the character value
1016 * are stored in bits 0-5 of the second byte, with the high bits set to
1017 * "10". This type of encoding is also done for the null character
1018 * <code>&#92;u0000</code>. This eliminates any C style NUL character values
1019 * in the output. All remaining characters are stored as three bytes.
1020 * Bits 12-15 of the character value are stored in bits 0-3 of the first
1021 * byte. The high bits of the first bytes are set to "1110". Bits 6-11
1022 * of the character value are stored in bits 0-5 of the second byte. The
1023 * high bits of the second byte are set to "10". And bits 0-5 of the
1024 * character value are stored in bits 0-5 of byte three, with the high bits
1025 * of that byte set to "10".
1027 * @param val The <code>String</code> to write to the output in UTF format
1029 * @exception IOException If an error occurs
1031 public final void writeUTF (String val) throws IOException
1033 if (out == null)
1034 throw new IOException("Bad file descriptor");
1036 out.writeUTF(val);
1040 * This method creates a java.nio.channels.FileChannel.
1041 * Nio does not allow one to create a file channel directly.
1042 * A file channel must be created by first creating an instance of
1043 * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
1045 public final synchronized FileChannel getChannel ()
1047 return ch;