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 |
|
---|
32 | private static native void init();
|
---|
33 | static
|
---|
34 | {
|
---|
35 | init();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public native void sync () throws SyncFailedException;
|
---|
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 |
|
---|
48 | // These are WHENCE values for seek.
|
---|
49 | static final int SET = 0;
|
---|
50 | static final int CUR = 1;
|
---|
51 |
|
---|
52 | // This constructor is specified to create an invalid descriptor.
|
---|
53 | public FileDescriptor ()
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 | // Open a file. MODE is a combination of the above mode flags.
|
---|
58 | FileDescriptor (String path, int mode) throws FileNotFoundException
|
---|
59 | {
|
---|
60 | fd = open (path, mode);
|
---|
61 | }
|
---|
62 |
|
---|
63 | native int open (String path, int mode) throws FileNotFoundException;
|
---|
64 | native void write (int b) throws IOException;
|
---|
65 | native void write (byte[] b, int offset, int len)
|
---|
66 | throws IOException, NullPointerException, IndexOutOfBoundsException;
|
---|
67 | native void close () throws IOException;
|
---|
68 | native void setLength (long pos) throws IOException;
|
---|
69 | // EOF_TRUNC is true if a request to seek past the end of file
|
---|
70 | // should actually stop at the end of file. If false, then a seek
|
---|
71 | // past the end is ok (and if a subsequent write occurs the file
|
---|
72 | // will grow).
|
---|
73 | native int seek (long pos, int whence, boolean eof_trunc) throws IOException;
|
---|
74 | native long length () throws IOException;
|
---|
75 | native long getFilePointer () throws IOException;
|
---|
76 | native int read () throws IOException;
|
---|
77 | native int read (byte[] bytes, int offset, int len) throws IOException;
|
---|
78 | native int available () throws IOException;
|
---|
79 |
|
---|
80 |
|
---|
81 | // When collected, close.
|
---|
82 | protected void finalize () throws IOException
|
---|
83 | {
|
---|
84 | if (valid ())
|
---|
85 | close ();
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Attach to an already-opened file. This is not private because we
|
---|
89 | // need access to it from other packages, for instance java.net.
|
---|
90 | // Ordinarily that wouldn't work, either, but in our case we know
|
---|
91 | // the access comes from C++, where "package private" is translated
|
---|
92 | // into "public". Eww.
|
---|
93 | FileDescriptor (int desc)
|
---|
94 | {
|
---|
95 | fd = desc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // System's notion of file descriptor. It might seem redundant to
|
---|
99 | // initialize this given that it is reassigned in the constructors.
|
---|
100 | // However, this is necessary because if open() throws an exception
|
---|
101 | // we want to make sure this has the value -1. This is the most
|
---|
102 | // efficient way to accomplish that.
|
---|
103 | private int fd = -1;
|
---|
104 |
|
---|
105 | private long position = 0;
|
---|
106 | }
|
---|