OSDN Git Service

d345db29ad7ae8674a69b52b107817ab06088619
[pf3gnuchains/gcc-fork.git] / libjava / java / io / FileDescriptor.java
1 // FileDescriptor.java - Open file or device
2
3 /* Copyright (C) 1998, 1999  Cygnus Solutions
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   public static final FileDescriptor in = new FileDescriptor (0);
28   public static final FileDescriptor out = new FileDescriptor (1);
29   public static final FileDescriptor err = new FileDescriptor (2);
30
31   public native void sync () throws SyncFailedException;
32   public native boolean valid ();
33
34
35   // These are mode values for open().
36   static final int READ   = 1;
37   static final int WRITE  = 2;
38   static final int APPEND = 4;
39
40   // These are WHENCE values for seek.
41   static final int SET = 0;
42   static final int CUR = 1;
43
44   // Open a file.  MODE is a combination of the above mode flags.
45   FileDescriptor (String path, int mode) throws FileNotFoundException
46   {
47     fd = open (path, mode);
48   }
49
50   public FileDescriptor ()
51   {
52     fd = -1;
53   }
54
55   native int open (String path, int mode) throws FileNotFoundException;
56   native void write (int b) throws IOException;
57   native void write (byte[] b, int offset, int len)
58     throws IOException, NullPointerException, IndexOutOfBoundsException;
59   native void close () throws IOException;
60   native int seek (long pos, int whence) throws IOException;
61   native long length () throws IOException;
62   native long getFilePointer () throws IOException;
63   native int read () throws IOException;
64   native int read (byte[] bytes, int offset, int len) throws IOException;
65   native int available () throws IOException;
66
67
68   // When collected, close.
69   protected void finalize () throws IOException
70   {
71     if (valid ())
72       close ();
73   }
74
75   // Attach to an already-opened file.  This is not private because we
76   // need access to it from other packages, for instance java.net.
77   // Ordinarily that wouldn't work, either, but in our case we know
78   // the access comes from C++, where "package private" is translated
79   // into "public".  Eww.
80   FileDescriptor (int desc)
81   {
82     fd = desc;
83   }
84
85   // System's notion of file descriptor.
86   private int fd;
87 }