2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / io / RandomAccessFile.java
blobde00f4d141cfb5631cdf12cee6e0de8988868754
1 /* RandomAccessFile.java -- Class supporting random file I/O
2 Copyright (C) 1998, 1999, 2001, 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 java.nio.channels.FileChannel;
42 import java.nio.channels.FileChannelImpl;
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.
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.
57 * @author Aaron M. Renn <arenn@urbanophile.com>
58 * @author Tom Tromey <tromey@cygnus.com>
60 public class RandomAccessFile implements DataOutput, DataInput
63 // The underlying file.
64 private FileDescriptor fd;
65 // The corresponding input and output streams.
66 private DataOutputStream out;
67 private DataInputStream in;
69 private FileChannel ch; /* cached associated file-channel */
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.
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
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
90 public RandomAccessFile (File file, String mode)
91 throws FileNotFoundException
93 this (file.getPath(), mode);
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.
108 * @param fileName The name of the file to read and/or write
109 * @param mode "r", "rw", "rws", or "rwd"
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
117 public RandomAccessFile (String fileName, String mode)
118 throws FileNotFoundException
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"))
127 fdmode = (FileDescriptor.READ | FileDescriptor.WRITE
128 | FileDescriptor.SYNC);
130 else if (mode.equals("rwd"))
132 fdmode = (FileDescriptor.READ | FileDescriptor.WRITE
133 | FileDescriptor.DSYNC);
135 else
136 throw new IllegalArgumentException ("invalid mode: " + mode);
138 // The obligatory SecurityManager stuff
139 SecurityManager s = System.getSecurityManager();
140 if (s != null)
142 s.checkRead(fileName);
144 if ((fdmode & FileDescriptor.WRITE) != 0)
145 s.checkWrite(fileName);
148 fd = new FileDescriptor (fileName, fdmode);
149 out = new DataOutputStream (new FileOutputStream (fd));
150 in = new DataInputStream (new FileInputStream (fd));
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
159 public void close () throws IOException
161 if (fd.valid())
162 fd.close();
166 * This method returns a <code>FileDescriptor</code> object that
167 * represents the native file handle for this file.
169 * @return The <code>FileDescriptor</code> object for this file
171 * @exception IOException If an error occurs
173 public final FileDescriptor getFD () throws IOException
175 if (! fd.valid())
176 throw new IOException ();
178 return fd;
182 * This method returns the current offset in the file at which the next
183 * read or write will occur
185 * @return The current file position
187 * @exception IOException If an error occurs
189 public long getFilePointer () throws IOException
191 return fd.getFilePointer();
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.
203 * @param newlen The new length of the file
205 * @exception IOException If an error occurs
207 public void setLength (long newLen) throws IOException
209 fd.setLength (newLen);
213 * This method returns the length of the file in bytes
215 * @return The length of the file
217 * @exception IOException If an error occurs
219 public long length () throws IOException
221 return fd.getLength ();
225 * This method reads a single byte of data from the file and returns it
226 * as an integer.
228 * @return The byte read as an int, or -1 if the end of the file was reached.
230 * @exception IOException If an error occurs
232 public int read () throws IOException
234 return in.read();
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.
242 * @param buf The buffer to read bytes from the file into
244 * @return The actual number of bytes read or -1 if end of file
246 * @exception IOException If an error occurs
248 public int read (byte[] buffer) throws IOException
250 return in.read (buffer);
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.
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
261 * @return The actual number of bytes read, or -1 if end of file
263 * @exception IOException If an error occurs
265 public int read (byte[] buffer, int offset, int len) throws IOException
267 return in.read (buffer, offset, len);
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.
281 * @return The <code>boolean</code> value read
283 * @exception EOFException If end of file is reached before reading the
284 * boolean
285 * @exception IOException If any other error occurs
287 public final boolean readBoolean () throws IOException
289 return in.readBoolean ();
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.
300 * @return The <code>byte</code> value read
302 * @exception EOFException If end of file is reached before reading the byte
303 * @exception IOException If any other error occurs
305 * @see DataOutput
307 public final byte readByte () throws IOException
309 return in.readByte ();
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.
330 * @return The <code>char</code> value read
332 * @exception EOFException If end of file is reached before reading the char
333 * @exception IOException If any other error occurs
335 * @see DataOutput
337 public final char readChar () throws IOException
339 return in.readChar();
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.
355 * @return The <code>double</code> value read
357 * @exception EOFException If end of file is reached before reading
358 * the double
359 * @exception IOException If any other error occurs
361 * @see java.lang.Double
362 * @see DataOutput
364 public final double readDouble () throws IOException
366 return in.readDouble ();
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.
381 * @return The <code>float</code> value read
383 * @exception EOFException If end of file is reached before reading the float
384 * @exception IOException If any other error occurs
386 * @see java.lang.Float
387 * @see DataOutput
389 public final float readFloat () throws IOException
391 return in.readFloat();
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
400 * @param buf The buffer into which to read the data
402 * @exception EOFException If end of file is reached before filling the
403 * buffer
404 * @exception IOException If any other error occurs
406 public final void readFully (byte[] buffer) throws IOException
408 in.readFully(buffer);
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.
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
424 * @exception EOFException If end of file is reached before filling
425 * the buffer
426 * @exception IOException If any other error occurs
428 public final void readFully (byte[] buffer, int offset, int count)
429 throws IOException
431 in.readFully (buffer, offset, count);
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.
455 * @return The <code>int</code> value read
457 * @exception EOFException If end of file is reached before reading the int
458 * @exception IOException If any other error occurs
460 * @see DataOutput
462 public final int readInt () throws IOException
464 return in.readInt();
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>
486 * @return The line read as a <code>String</code>
488 * @exception IOException If an error occurs
490 * @see DataOutput
492 public final String readLine () throws IOException
494 return in.readLine ();
498 * This method reads a Java long value from an input stream
499 * It operates by reading eight bytes from the stream and converting them to
500 * a single Java <code>long</code> The bytes are stored most
501 * significant byte first (i.e., "big endian") regardless of the native
502 * host byte ordering.
503 * <p>
504 * As an example, if <code>byte1</code> through <code>byte8</code>
505 * represent the first
506 * eight bytes read from the stream, they will be
507 * transformed to an <code>long</code> in the following manner:
508 * <p>
509 * <code>
510 * (long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) +
511 * (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) +
512 * (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) +
513 * (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code>
514 * <p>
515 * The value returned is in the range of 0 to 65535.
516 * <p>
517 * This method can read an <code>long</code> written by an object
518 * implementing the
519 * <code>writeLong()</code> method in the <code>DataOutput</code> interface.
521 * @return The <code>long</code> value read
523 * @exception EOFException If end of file is reached before reading the long
524 * @exception IOException If any other error occurs
526 * @see DataOutput
528 public final long readLong () throws IOException
530 return in.readLong();
534 * This method reads a signed 16-bit value into a Java in from the stream.
535 * It operates by reading two bytes from the stream and converting them to
536 * a single 16-bit Java <code>short</code> The two bytes are stored most
537 * significant byte first (i.e., "big endian") regardless of the native
538 * host byte ordering.
539 * <p>
540 * As an example, if <code>byte1</code> and code{byte2</code>
541 * represent the first
542 * and second byte read from the stream respectively, they will be
543 * transformed to a <code>short</code> in the following manner:
544 * <p>
545 * <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
546 * <p>
547 * The value returned is in the range of -32768 to 32767.
548 * <p>
549 * This method can read a <code>short</code> written by an object
550 * implementing the
551 * <code>writeShort()</code> method in the <code>DataOutput</code> interface.
553 * @return The <code>short</code> value read
555 * @exception EOFException If end of file is reached before reading the value
556 * @exception IOException If any other error occurs
558 * @see DataOutput
560 public final short readShort () throws IOException
562 return in.readShort();
566 * This method reads 8 unsigned bits into a Java <code>int</code> value
567 * from the
568 * stream. The value returned is in the range of 0 to 255.
569 * <p>
570 * This method can read an unsigned byte written by an object implementing
571 * the <code>writeUnsignedByte()</code> method in the
572 * <code>DataOutput</code> interface.
574 * @return The unsigned bytes value read as a Java <code>int</code>
576 * @exception EOFException If end of file is reached before reading the value
577 * @exception IOException If any other error occurs
579 * @see DataOutput
581 public final int readUnsignedByte () throws IOException
583 return in.readUnsignedByte();
587 * This method reads 16 unsigned bits into a Java int value from the stream.
588 * It operates by reading two bytes from the stream and converting them to
589 * a single Java <code>int</code> The two bytes are stored most
590 * significant byte first (i.e., "big endian") regardless of the native
591 * host byte ordering.
592 * <p>
593 * As an example, if <code>byte1</code> and <code>byte2</code>
594 * represent the first
595 * and second byte read from the stream respectively, they will be
596 * transformed to an <code>int</code> in the following manner:
597 * <p>
598 * <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code>
599 * <p>
600 * The value returned is in the range of 0 to 65535.
601 * <p>
602 * This method can read an unsigned short written by an object implementing
603 * the <code>writeUnsignedShort()</code> method in the
604 * <code>DataOutput</code> interface.
606 * @return The unsigned short value read as a Java <code>int</code>
608 * @exception EOFException If end of file is reached before reading the value
609 * @exception IOException If any other error occurs
611 public final int readUnsignedShort () throws IOException
613 return in.readUnsignedShort();
617 * This method reads a <code>String</code> from an input stream that
618 * is encoded in
619 * a modified UTF-8 format. This format has a leading two byte sequence
620 * that contains the remaining number of bytes to read. This two byte
621 * sequence is read using the <code>readUnsignedShort()</code> method of this
622 * interface.
623 * <p>
624 * After the number of remaining bytes have been determined, these bytes
625 * are read an transformed into <code>char</code> values.
626 * These <code>char</code> values
627 * are encoded in the stream using either a one, two, or three byte format.
628 * The particular format in use can be determined by examining the first
629 * byte read.
630 * <p>
631 * If the first byte has a high order bit of 0 then
632 * that character consists on only one byte. This character value consists
633 * of seven bits that are at positions 0 through 6 of the byte. As an
634 * example, if <code>byte1</code> is the byte read from the stream, it would
635 * be converted to a <code>char</code> like so:
636 * <p>
637 * <code>(char)byte1</code>
638 * <p>
639 * If the first byte has <code>110</code> as its high order bits, then the
640 * character consists of two bytes. The bits that make up the character
641 * value are in positions 0 through 4 of the first byte and bit positions
642 * 0 through 5 of the second byte. (The second byte should have
643 * 10 as its high order bits). These values are in most significant
644 * byte first (i.e., "big endian") order.
645 * <p>
646 * As an example, if <code>byte1</code> and <code>byte2</code>
647 * are the first two bytes
648 * read respectively, and the high order bits of them match the patterns
649 * which indicate a two byte character encoding, then they would be
650 * converted to a Java <code>char</code> like so:
651 * <p>
652 * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
653 * <p>
654 * If the first byte has a <code>1110</code> as its high order bits, then the
655 * character consists of three bytes. The bits that make up the character
656 * value are in positions 0 through 3 of the first byte and bit positions
657 * 0 through 5 of the other two bytes. (The second and third bytes should
658 * have <code>10</code> as their high order bits). These values are in most
659 * significant byte first (i.e., "big endian") order.
660 * <p>
661 * As an example, if <code>byte1</code> <code>byte2</code>
662 * and <code>byte3</code> are the
663 * three bytes read, and the high order bits of them match the patterns
664 * which indicate a three byte character encoding, then they would be
665 * converted to a Java <code>char</code> like so:
666 * <p>
667 * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) |
668 * (byte3 & 0x3F))</code>
669 * <p>
670 * Note that all characters are encoded in the method that requires the
671 * fewest number of bytes with the exception of the character with the
672 * value of <code>&#92;u0000</code> which is encoded as two bytes. This is
673 * a modification of the UTF standard used to prevent C language style
674 * <code>NUL</code> values from appearing in the byte stream.
675 * <p>
676 * This method can read data that was written by an object implementing the
677 * <code>writeUTF()</code> method in <code>DataOutput</code>
679 * @return The <code>String</code> read
681 * @exception EOFException If end of file is reached before reading the
682 * String
683 * @exception UTFDataFormatException If the data is not in UTF-8 format
684 * @exception IOException If any other error occurs
686 * @see DataOutput
688 public final String readUTF () throws IOException
690 return in.readUTF();
694 * This method sets the current file position to the specified offset
695 * from the beginning of the file. Note that some operating systems will
696 * allow the file pointer to be set past the current end of the file.
698 * @param pos The offset from the beginning of the file at which to set
699 * the file pointer
701 * @exception IOException If an error occurs
703 public void seek (long pos) throws IOException
705 fd.seek (pos, FileDescriptor.SET, false);
709 * This method attempts to skip and discard the specified number of bytes
710 * in the input stream. It may actually skip fewer bytes than requested.
711 * The actual number of bytes skipped is returned. This method will not
712 * skip any bytes if passed a negative number of bytes to skip.
714 * @param numBytes The requested number of bytes to skip.
716 * @return The number of bytes actually skipped.
718 * @exception IOException If an error occurs.
720 public int skipBytes (int numBytes) throws IOException
722 if (numBytes < 0)
723 throw new IllegalArgumentException ("Can't skip negative bytes: " +
724 numBytes);
726 if (numBytes == 0)
727 return 0;
729 long curPos = fd.getFilePointer ();
730 long newPos = fd.seek (numBytes, FileDescriptor.CUR, true);
732 return (int) (newPos - curPos);
736 * This method writes a single byte of data to the file. The file must
737 * be open for read-write in order for this operation to succeed.
739 * @param The byte of data to write, passed as an int.
741 * @exception IOException If an error occurs
743 public void write (int oneByte) throws IOException
745 out.write(oneByte);
749 * This method writes all the bytes in the specified array to the file.
750 * The file must be open read-write in order for this operation to succeed.
752 * @param buf The array of bytes to write to the file
754 public void write (byte[] buffer) throws IOException
756 out.write(buffer);
760 * This method writes <code>len</code> bytes to the file from the specified
761 * array starting at index <code>offset</code> into the array.
763 * @param buf The array of bytes to write to the file
764 * @param offset The index into the array to start writing file
765 * @param len The number of bytes to write
767 * @exception IOException If an error occurs
769 public void write (byte[] buffer, int offset, int len) throws IOException
771 out.write (buffer, offset, len);
775 * This method writes a Java <code>boolean</code> to the underlying output
776 * stream. For a value of <code>true</code>, 1 is written to the stream.
777 * For a value of <code>false</code>, 0 is written.
779 * @param b The <code>boolean</code> value to write to the stream
781 * @exception IOException If an error occurs
783 public final void writeBoolean (boolean val) throws IOException
785 out.writeBoolean(val);
789 * This method writes a Java <code>byte</code> value to the underlying
790 * output stream.
792 * @param b The <code>byte</code> to write to the stream, passed
793 * as an <code>int</code>.
795 * @exception IOException If an error occurs
797 public final void writeByte (int v) throws IOException
799 out.writeByte(v);
803 * This method writes a Java <code>short</code> to the stream, high byte
804 * first. This method requires two bytes to encode the value.
806 * @param s The <code>short</code> value to write to the stream,
807 * passed as an <code>int</code>.
809 * @exception IOException If an error occurs
811 public final void writeShort (int v) throws IOException
813 out.writeShort(v);
817 * This method writes a single <code>char</code> value to the stream,
818 * high byte first.
820 * @param v The <code>char</code> value to write, passed as
821 * an <code>int</code>.
823 * @exception IOException If an error occurs
825 public final void writeChar (int v) throws IOException
827 out.writeChar(v);
831 * This method writes a Java <code>int</code> to the stream, high bytes
832 * first. This method requires four bytes to encode the value.
834 * @param v The <code>int</code> value to write to the stream.
836 * @exception IOException If an error occurs
838 public final void writeInt (int v) throws IOException
840 out.writeInt(v);
844 * This method writes a Java <code>long</code> to the stream, high bytes
845 * first. This method requires eight bytes to encode the value.
847 * @param v The <code>long</code> value to write to the stream.
849 * @exception IOException If an error occurs
851 public final void writeLong (long v) throws IOException
853 out.writeLong(v);
857 * This method writes a Java <code>float</code> value to the stream. This
858 * value is written by first calling the method
859 * <code>Float.floatToIntBits</code>
860 * to retrieve an <code>int</code> representing the floating point number,
861 * then writing this <code>int</code> value to the stream exactly the same
862 * as the <code>writeInt()</code> method does.
864 * @param v The floating point number to write to the stream.
866 * @exception IOException If an error occurs
868 * @see #writeInt(int)
870 public final void writeFloat (float v) throws IOException
872 out.writeFloat(v);
876 * This method writes a Java <code>double</code> value to the stream. This
877 * value is written by first calling the method
878 * <code>Double.doubleToLongBits</code>
879 * to retrieve an <code>long</code> representing the floating point number,
880 * then writing this <code>long</code> value to the stream exactly the same
881 * as the <code>writeLong()</code> method does.
883 * @param v The double precision floating point number to write to the
884 * stream.
886 * @exception IOException If an error occurs
888 * @see #writeLong(long)
890 public final void writeDouble (double v) throws IOException
892 out.writeDouble(v);
896 * This method writes all the bytes in a <code>String</code> out to the
897 * stream. One byte is written for each character in the <code>String</code>.
898 * The high eight bits of each character are discarded.
900 * @param s The <code>String</code> to write to the stream
902 * @exception IOException If an error occurs
904 public final void writeBytes (String s) throws IOException
906 out.writeBytes(s);
910 * This method writes all the characters in a <code>String</code> to the
911 * stream. There will be two bytes for each character value. The high
912 * byte of the character will be written first.
914 * @param s The <code>String</code> to write to the stream.
916 * @exception IOException If an error occurs
918 public final void writeChars (String s) throws IOException
920 out.writeChars(s);
924 * This method writes a Java <code>String</code> to the stream in a modified
925 * UTF-8 format. First, two bytes are written to the stream indicating the
926 * number of bytes to follow. Note that this is the number of bytes in the
927 * encoded <code>String</code> not the <code>String</code> length. Next
928 * come the encoded characters. Each character in the <code>String</code>
929 * is encoded as either one, two or three bytes. For characters in the
930 * range of <code>&#92;u0001</code> to <code>&#92;u007F</code>,
931 * one byte is used. The character
932 * value goes into bits 0-7 and bit eight is 0. For characters in the range
933 * of <code>&#92;u0080</code> to <code>&#92;u007FF</code>, two
934 * bytes are used. Bits
935 * 6-10 of the character value are encoded bits 0-4 of the first byte, with
936 * the high bytes having a value of "110". Bits 0-5 of the character value
937 * are stored in bits 0-5 of the second byte, with the high bits set to
938 * "10". This type of encoding is also done for the null character
939 * <code>&#92;u0000</code>. This eliminates any C style NUL character values
940 * in the output. All remaining characters are stored as three bytes.
941 * Bits 12-15 of the character value are stored in bits 0-3 of the first
942 * byte. The high bits of the first bytes are set to "1110". Bits 6-11
943 * of the character value are stored in bits 0-5 of the second byte. The
944 * high bits of the second byte are set to "10". And bits 0-5 of the
945 * character value are stored in bits 0-5 of byte three, with the high bits
946 * of that byte set to "10".
948 * @param s The <code>String</code> to write to the output in UTF format
950 * @exception IOException If an error occurs
952 public final void writeUTF (String s) throws IOException
954 out.writeUTF(s);
958 * This method creates a java.nio.channels.FileChannel.
959 * Nio does not allow one to create a file channel directly.
960 * A file channel must be created by first creating an instance of
961 * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
963 public final synchronized FileChannel getChannel ()
965 if (ch == null)
966 ch = new FileChannelImpl (fd, true, this);
968 return ch;
971 } // class RandomAccessFile