1 // FileDescriptor.java - Open file or device
2 
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
4 
5    This file is part of libgcj.
6 
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10 
11 package java.io;
12 
13 /**
14  * @author Tom Tromey <tromey@cygnus.com>
15  * @date September 24, 1998
16  */
17 
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
21  */
22 
23 // For now we assume a POSIXy file system.  This can be changed later
24 // if need be.
25 public final class FileDescriptor
26 {
27 
28   public static final FileDescriptor in = null;
29   public static final FileDescriptor out = null;
30   public static final FileDescriptor err = null;
31 
init()32   private static native void init();
33   static
34   {
init()35     init();
36   }
37 
sync()38   public native void sync () throws SyncFailedException;
valid()39   public native boolean valid ();
40 
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;
49 
50   // These are WHENCE values for seek.
51   static final int SET = 0;
52   static final int CUR = 1;
53 
54   // This constructor is specified to create an invalid descriptor.
FileDescriptor()55   public FileDescriptor ()
56   {
57   }
58 
59   // Open a file.  MODE is a combination of the above mode flags.
FileDescriptor(String path, int mode)60   FileDescriptor (String path, int mode) throws FileNotFoundException
61   {
62     fd = open (path, mode);
63   }
64 
open(String path, int mode)65   native int open (String path, int mode) throws FileNotFoundException;
write(int b)66   native void write (int b) throws IOException;
write(byte[] b, int offset, int len)67   native void write (byte[] b, int offset, int len)
68     throws IOException, NullPointerException, IndexOutOfBoundsException;
close()69   native void close () throws IOException;
setLength(long pos)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
74   // will grow).
seek(long pos, int whence, boolean eof_trunc)75   native int seek (long pos, int whence, boolean eof_trunc) throws IOException;
getLength()76   native long getLength () throws IOException;
getFilePointer()77   native long getFilePointer () throws IOException;
read()78   native int read () throws IOException;
read(byte[] bytes, int offset, int len)79   native int read (byte[] bytes, int offset, int len) throws IOException;
available()80   native int available () throws IOException;
81 
82 
83   // When collected, close.
finalize()84   protected void finalize () throws Throwable
85   {
86     if (valid ())
87       close ();
88   }
89 
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.
FileDescriptor(int desc)95   FileDescriptor (int desc)
96   {
97     fd = desc;
98   }
99 
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.
105   private int fd = -1;
106 
107   private long position = 0;
108 }
109