1 // FileDescriptor.java - Open file or device
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date September 24, 1998
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19 * "The Java Language Specification", ISBN 0-201-63451-1
20 * Status: Complete to 1.1
23 // For now we assume a POSIXy file system. This can be changed later
25 public final class FileDescriptor
28 public static final FileDescriptor in
= null;
29 public static final FileDescriptor out
= null;
30 public static final FileDescriptor err
= null;
32 private static native void init();
38 public native void sync () throws SyncFailedException
;
39 public native boolean valid ();
41 // These are mode values for open().
42 static final int READ
= 1;
43 static final int WRITE
= 2;
44 static final int APPEND
= 4;
45 // EXCL is used only when making a temp file.
46 static final int EXCL
= 8;
47 static final int SYNC
= 16;
48 static final int DSYNC
= 32;
50 // These are WHENCE values for seek.
51 static final int SET
= 0;
52 static final int CUR
= 1;
54 // This constructor is specified to create an invalid descriptor.
55 public FileDescriptor ()
59 // Open a file. MODE is a combination of the above mode flags.
60 FileDescriptor (String path
, int mode
) throws FileNotFoundException
62 fd
= open (path
, mode
);
65 native int open (String path
, int mode
) throws FileNotFoundException
;
66 native void write (int b
) throws IOException
;
67 native void write (byte[] b
, int offset
, int len
)
68 throws IOException
, NullPointerException
, IndexOutOfBoundsException
;
69 native void close () throws IOException
;
70 native void setLength (long pos
) throws IOException
;
71 // EOF_TRUNC is true if a request to seek past the end of file
72 // should actually stop at the end of file. If false, then a seek
73 // past the end is ok (and if a subsequent write occurs the file
75 native int seek (long pos
, int whence
, boolean eof_trunc
) throws IOException
;
76 native long getLength () throws IOException
;
77 native long getFilePointer () throws IOException
;
78 native int read () throws IOException
;
79 native int read (byte[] bytes
, int offset
, int len
) throws IOException
;
80 native int available () throws IOException
;
83 // When collected, close.
84 protected void finalize () throws Throwable
90 // Attach to an already-opened file. This is not private because we
91 // need access to it from other packages, for instance java.net.
92 // Ordinarily that wouldn't work, either, but in our case we know
93 // the access comes from C++, where "package private" is translated
94 // into "public". Eww.
95 FileDescriptor (int desc
)
100 // System's notion of file descriptor. It might seem redundant to
101 // initialize this given that it is reassigned in the constructors.
102 // However, this is necessary because if open() throws an exception
103 // we want to make sure this has the value -1. This is the most
104 // efficient way to accomplish that.
107 private long position
= 0;